|
 |
|
 |
05-26-2004, 07:45 AM
|
#1 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,489
|
Splitting Bytes in C
I am working on midi events.
There is one byte in the event which I need to take apart.
B0 (hex value)
B is refers to the event being a control change, and 0 refers to the midi channel.
in C, how can i put each of these in their own variable?
__________________
Mike
|
|
|
05-26-2004, 09:30 AM
|
#2 (permalink)
|
|
Registered User
Join Date: May 2004
Posts: 4
|
i think it's similar to the operation that the decimal does.
for instance
how will you split the 21 into two variables?
21%10=1
21/10=2
just a referrence
|
|
|
05-26-2004, 11:21 AM
|
#3 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
How are the two bytes stored and to what (in what datatype) do you wish to store the single byte.
In case you were looking for what Blood Night meant, then here is a play thingy:
Code:
#include <iostream.h>
const unsigned short currentbyte = 0xB3;
int main()
{
//unsigned short = max 2 bytes; e.g. 0xFF;
unsigned short value, remain;
//A bit of toying: 0x10 = 16 Decimal!
value = currentbyte / 0x10;
//Convert to hex
cout<<hex<<value<<endl; //b
cout<<value<<endl; //b
//convert to decimal
cout<<dec<<value<<endl; //11
cout<<value<<endl; //11
remain = currentbyte % 0x10;
cout<<hex<<remain<<endl; //3
cout<<remain<<endl; //3
cout<<dec<<remain<<endl; //3
cout<<remain<<endl; //3
return 0;
}
__________________
|
|
|
05-26-2004, 01:44 PM
|
#4 (permalink)
|
|
LOAD "*",8,1
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
|
Code:
unsigned char v, control, channel;
v = 0xB0;
control = (v >> 4);
channel = (v & 0xf);
|
|
|
05-26-2004, 02:33 PM
|
#5 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,489
|
the original type is DWORD.
when i do this, i have problems casting DWORD to unsigned char: after a little bit of fussing around with it, this works out good.
Code:
unsigned char cc,channel,value;
cc = (dwParam1>>8);
channel = (dwParam1>>0)%16;
value = (dwParam1>>16);
i am suprised that i'm not getting some sort of type casting errors that second way.
thanks again!
__________________
Mike
|
|
|
05-26-2004, 03:09 PM
|
#6 (permalink)
|
|
LOAD "*",8,1
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
|
alright, let's see if we understand this correctly.
what is the dword's value? is it 0x000000b0 ?
and you want the 'b' and the last '0' (ie, two nibbles from the last byte)?
|
|
|
05-26-2004, 03:17 PM
|
#7 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,489
|
the DWORD is representing a short MIDI Event message.
Normal MIDI Events can be up to 4 bytes: status byte, data1, data2, data3
this is how i understand it anyway.
the control change and channel information is in the first byte (status byte), the value of that is in the second byte(data1). in this message, the third and forth bytes are not used.
http://harmony-central.com/MIDI/Doc/table1.html
That is sorta what is going on here. It's not a great example, but you might get a more clear picture of the MIDI event.
__________________
Mike
|
|
|
05-26-2004, 03:24 PM
|
#8 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,489
|
when i open a midi port, i can designate a callback function. this function is triggered whenever a midi event is sent to the midi input.
if you really want to understand what i'm doing, here is some of the callback function:
Code:
void CALLBACK midiCallback(HMIDIIN handle, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2)
{
LPMIDIHDR lpMIDIHeader;
unsigned char *ptr;
TCHAR buffer[80];
unsigned char bytes;
switch (uMsg)
{
// Received some regular MIDI message
case MIM_DATA:
{
// ignore active sensing
if( dwParam1 != 0xF8 ){
unsigned char cc,channel,value;
cc = (dwParam1>>8);
channel = (dwParam1>>0)%16;
value = (dwParam1>>16);
sprintf(&buffer[0], "cc: %d ch: %d v: %d \r\n", cc,
channel,
value);
_cputs(&buffer[0]);
}
break;
}
}
}
__________________
Mike
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 11:31 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|