Jump to content

Transmitting C variables as Midi CC's?


sneakthief
 Share

Recommended Posts

Hi C experts!

I know very little about C, so please be patient ;)

Question 1: Does a variable have to be in hex to be transmitted as a midi CC value?

I'm working with the BPM variable from the Clockbox application and I want to also transmit this variable as a midi CC

(Clockbox code is here: http://www.midibox.org/forum/index.php?topic=5691.0 )

Question 2: Does this syntax look OK?

// Step 1. Make new variable called bpmCC for holding CC to transmit based on current BPM:

    unsigned char bpmCC; // holds the current BPM-based CC to transmit

// Step 2. Convert current BPM to a CC value and offset bpm to transmit by 80 in order to get CC value within valid range (0-127)

    bpmCC = bpm - 80;

// Step 3. Transmit bpmCC (does this have to be in HEX? if so, what's the best way to convert decimal to hex?)

    bpmCC = (unsigned char)value;

    MIOS_MIDI_TxBufferPut(0xbf);          // CC at MIDI Channel #16

    MIOS_MIDI_TxBufferPut(0x77);          // CC# 119 (reserved)

    MIOS_MIDI_TxBufferPut((unsigned char)value);

Background:

When using the Clockbox, I would like to transmit a midi CC whenever the BPM is changed.

Why?

This function can provide tempo-sync'd midi CC functions to midi devices that don't provide this feature.

Transmitted Data:

BPM's between 80 and 207 would transmit corresponding values of 0-127 on CC #119 (or other desired midi CC)

Example:

1. When the BPM is set to 80, Clockbox would transmit a value 0 on CC #119

2. When the BPM is set to 122, Clockbox would transmit a value 41 on CC #119

3. When the BPM is set to 160, Clockbox would transmit a value 80 on CC #119

Thanks!

michel

Link to comment
Share on other sites

Hi Michel,

in principle the code looks ok, but you don't need the bpmCC variable. It's even not required to convert this value into a different number format, because hexadecimal and decimal are just different forms to display/input values, but both will be translated into binary numbers by the compiler/assembler.

One thing which should be considered is, that you have to take care for underruns. The clockbox provides BPM rates from 48 to 255. this means, as soon as the BPM are less than 80, you will get a negative result. For an "unsigned char" this means, that the result will be >=128 (since there is no sign), and such a value should not be sent as 3rd byte of a CC, since it violates the MIDI protocol (only values between 0 and 127 are allowed).

So, you could add an if condition which ensure that values <80 and >(80+128) are not sent

Best Regards, Thorsten.

Link to comment
Share on other sites

you don't need the bpmCC variable.

But I need to perform this simple arithmatic operation before I transmit it:

bpmCC = bpm - 80;

I'm making a new variable because I don't want to mess with the original BPM value.

One thing which should be considered is, that you have to take care for underruns.

So, you could add an if condition which ensure that values <80 and >(80+128) are not sent

That's a good point. Even though I'm only going to be working in the 80-207 bpm range, I don't want to cause any errors.

One last question: I only want to transmit this midi CC when the BPM is changed, so is this an OK place to put this code (in mclock.c)?

/////////////////////////////////////////////////////////////////////////////
// These functions are used to set/query the BPM
/////////////////////////////////////////////////////////////////////////////
void MCLOCK_BPMSet(unsigned char _bpm)
{
  // re-init timer depending on new BPM value
  bpm = _bpm;
  MIOS_TIMER_ReInit(3, MCLOCK_GetTimerValue(bpm));


              /////////////////////////////////////////////////////////////////////////////
              // New BPM to midi CC subrouting
              /////////////////////////////////////////////////////////////////////////////

               bpmCC = (unsigned char)value;
               bpmCC = bpm - 80;

               MIOS_MIDI_TxBufferPut(0xbf);           // CC at MIDI Channel #16
               MIOS_MIDI_TxBufferPut(0x77);           // CC# 119 (reserved)
               MIOS_MIDI_TxBufferPut((unsigned char)value);

}

unsigned char MCLOCK_BPMGet(void)
{
  return bpm;
}

Thanks so much for your help!

cheers,

michel

Link to comment
Share on other sites

Yes, the MCLOCK_BPMSet() function is the right one, because it's always called when the BPM rate is initialized or modified.

With "bpmCC = (unsigned char)value" you are trying to access a variable (name: "value") which doesn't exist. But without loosing too much words: it's really not required.

Just write MIOS_MIDI_TxBufferPut(bpm - 80) instead of MIOS_MIDI_TxBufferPut((unsigned char)value);

Best Regards, Thorsten.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...