1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
byte read_VLV(byte **midi_stream, uint_32 *result)
{
uint_32 temp = 0;
byte curdata;
if (!consumeStream(midi_stream, &curdata)) return 0; //Read first VLV failed?
for (;;) //Process/read the VLV!
{
temp |= (curdata & 0x7F); //Add to length!
if (!(curdata & 0x80)) break; //No byte to follow?
temp <<= 7; //Make some room for the next byte!
if (!consumeStream(midi_stream, &curdata)) return 0; //Read VLV failed?
}
*result = temp; //Give the result!
return 1; //OK!
}
float calcfreq(uint_32 tempo, HEADER_CHNK *header)
{
float PPQN, speed;
byte frames;
byte subframes; //Pulses per quarter note!
word division;
division = byteswap16(header->division); //Byte swap!
if (division & 0x8000) //SMTPE?
{
frames = (float)((division >> 8)&0x7F); //Frames!
subframes = (float)(division & 0xFF); //Subframes!
speed = frames; //Default: we're the frames!
if (subframes)
{
//We don't use the tempo: our rate is fixed!
frames *= subframes; //The result in subframes/second!
}
speed = frames; //Use (sub)frames!
}
else
{
//PPQN method!
PPQN = (float)division; //Read PPQN!
speed = (float)tempo; //Convert to speed!
speed /= 1000000.0f; //Divide by 1 second!
speed /= PPQN; //Divide to get the ticks per second!
speed = 1.0f / speed; //Ammount per second!
}
//We're counting in ticks!
return speed; //ticks per second!
}
//The protective semaphore for our flags!
SDL_sem *MID_timing_pos_Lock = NULL; //Timing position lock!
SDL_sem *MID_BPM_Lock = NULL; //BPM/Active tempo lock!
uint_64 timing_pos = 0; //Current timing position!
float BPM = 0.0f; //No BPM by default!
void updateMIDTimer(HEADER_CHNK *header) //Request an update of our timer!
{
addtimer(calcfreq(activetempo, header), (Handler)&timing_pos, "MID_tempotimer", 0, 2, MID_timing_pos_Lock); //Add a counter timer!
}
//The protective semaphore for the hardware!
SDL_sem *MIDLock = NULL;
#define MIDI_ERROR {error = 1; goto abortMIDI;}
void playMIDIStream(byte *midi_stream, HEADER_CHNK *header)
{
byte *curstream = midi_stream; //Copy the stream!
byte curdata;
//Metadata event!
byte meta_type;
uint_32 length; //Our metadata variable length!
uint_64 play_pos = 0; //Current play position!
uint_32 delta_time; //Delta time!
byte last_command = 0; //Last executed command!
byte error = 0; //Default: no error!
for (;;) //Playing?
{
//First, timing information and timing itself!
if (!read_VLV(&curstream, &delta_time)) return; //Read VLV time index!
play_pos += delta_time; //Add the delta time to the playing position!
for (;;)
{
//Lock
SDL_SemWait(MID_timing_pos_Lock);
if (timing_pos >= play_pos)
{
//Unlock
SDL_SemPost(MID_timing_pos_Lock);
break; //Arrived? Play!
}
//Unlock
SDL_SemPost(MID_timing_pos_Lock);
delay(0); //Wait for our tick!
}
if (!peekStream(&curstream, &curdata))
{
return; //Failed to peek!
}
if (curdata == 0xFF) //System?
{
if (!consumeStream(&curstream, &curdata)) return; //EOS!
if (!consumeStream(&curstream, &meta_type)) return; //Meta type failed? Give error!
if (!read_VLV(&curstream, &length)) return; //Error: unexpected EOS!
switch (meta_type) //What event?
{
case 0x2F: //EOT?
timing_pos = 0; //Reset timing position for next playback!
return; //End of track reached: done!
case 0x51: //Set tempo?
//Lock
SDL_SemWait(MID_BPM_Lock);
removetimer("MID_tempotimer"); //Remove old timer!
if (!consumeStream(&curstream, &curdata)) return; //Tempo 1/3 failed?
activetempo = curdata; //Final byte!
activetempo <<= 8;
if (!consumeStream(&curstream, &curdata)) return; //Tempo 2/3 failed?
activetempo |= curdata; //Final byte!
activetempo <<= 8;
if (!consumeStream(&curstream, &curdata)) return; //Tempo 3/3 failed?
activetempo |= curdata; //Final byte!
//Tempo = us per quarter note!
updateMIDTimer(header);
//Unlock
SDL_SemPost(MID_BPM_Lock);
break;
default: //Unrecognised meta event? Skip it!
for (; length--;) //Process length bytes!
{
if (!consumeStream(&curstream, &curdata)) return; //Skip failed?
}
break;
}
}
else //Hardware?
{
//Lock
SDL_SemWait(MIDLock);
dolog("MID", "Processing command: %02X", curdata); //Log the outgoing command!
if (curdata & 0x80) //Starting a new command?
{
if (!consumeStream(&curstream, &curdata)) MIDI_ERROR //EOS!
last_command = curdata; //Save the last command!
if (last_command != 0xF7) //Escaped continue isn't sent!
{
PORT_OUT_B(0x330, last_command); //Send the command!
}
}
else
{
if (last_command != 0xF7) //Escaped continue isn't used last?
{
PORT_OUT_B(0x330, last_command); //Repeat the status bytes: we don't know what the other channels do!
}
}
//Process the data for the command!
switch ((last_command >> 4) & 0xF) //What command to send data for?
{
case 0xF: //Special?
switch (last_command & 0xF) //What subcommand are we sending?
{
case 0x0: //System exclusive?
case 0x7: //Escaped continue?
if (!read_VLV(&curstream, &length)) MIDI_ERROR //Error: unexpected EOS!
for (; length--;) //Transmit the packet!
{
if (!consumeStream(&curstream, &curdata)) MIDI_ERROR //EOS!
PORT_OUT_B(0x330, curdata); //Send the byte!
}
break;
case 0x1:
case 0x3:
//1 byte follows!
if (!consumeStream(&curstream, &curdata)) MIDI_ERROR //EOS!
PORT_OUT_B(0x330, curdata); //Passthrough to MIDI!
break;
case 0x2:
//2 bytes follow!
if (!consumeStream(&curstream, &curdata)) MIDI_ERROR //EOS!
PORT_OUT_B(0x330, curdata); //Passthrough to MIDI!
if (!consumeStream(&curstream, &curdata)) MIDI_ERROR //EOS!
PORT_OUT_B(0x330, curdata); //Passthrough to MIDI!
break;
default: //Unknown special instruction?
break; //Single byte instruction?
}
break;
case 0x8: //Note off?
case 0x9: //Note on?
case 0xA: //Aftertouch?
case 0xB: //Control change?
case 0xE: //Pitch bend?
//2 bytes follow!
if (!consumeStream(&curstream, &curdata)) MIDI_ERROR //EOS!
PORT_OUT_B(0x330, curdata); //Passthrough to MIDI!
if (!consumeStream(&curstream, &curdata)) MIDI_ERROR //EOS!
PORT_OUT_B(0x330, curdata); //Passthrough to MIDI!
break;
case 0xC: //Program change?
case 0xD: //Channel pressure/aftertouch?
//1 byte follows
if (!consumeStream(&curstream, &curdata)) MIDI_ERROR //EOS!
PORT_OUT_B(0x330, curdata); //Passthrough to MIDI!
break;
default: //Unknown data? We're sending directly to the hardware! We shouldn't be here!
if (!consumeStream(&curstream, &curdata)) MIDI_ERROR //EOS!
PORT_OUT_B(0x330, curdata); //Passthrough to MIDI!
break;
}
abortMIDI:
//Unlock
if (error)
{
dolog("MID_error", "Error during MID processing! Unexpected EOS? Last command: %02X",last_command);
return; //Abort on error!
}
SDL_SemPost(MIDLock);
}
}
}
|