readmidi.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /*
  2. TiMidity -- Experimental MIDI to WAVE converter
  3. Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include "../../neo/idlib/precompiled.h"
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <errno.h>
  20. #include <string.h>
  21. #include "config.h"
  22. #include "common.h"
  23. #include "instrum.h"
  24. #include "playmidi.h"
  25. #include "readmidi.h"
  26. #include "output.h"
  27. #include "controls.h"
  28. void Real_Tim_Free( void *pt );
  29. int32_t quietchannels=0;
  30. /* to avoid some unnecessary parameter passing */
  31. static MidiEventList *evlist;
  32. static int32_t event_count;
  33. static idFile * fp = NULL;
  34. static int32_t at;
  35. static unsigned char* local_buffer = 0;
  36. static size_t local_buffer_length = 0;
  37. static size_t local_buffer_cur = 0;
  38. /* These would both fit into 32 bits, but they are often added in
  39. large multiples, so it's simpler to have two roomy ints */
  40. static int32_t sample_increment, sample_correction; /*samples per MIDI delta-t*/
  41. static int32_t read_local(void* buffer, size_t len, size_t count)
  42. {
  43. if (fp && len > 0) {
  44. return (int32_t)fp->Read(buffer, len * count ) / len;
  45. } else if( local_buffer != NULL ) {
  46. if (len * count + local_buffer_cur > local_buffer_length) {
  47. memcpy(buffer, &local_buffer[local_buffer_cur], local_buffer_length - local_buffer_cur);
  48. return(int32_t)(local_buffer_length - local_buffer_cur)/len;
  49. } else {
  50. memcpy(buffer, &local_buffer[local_buffer_cur], len * count);
  51. local_buffer_cur += len * count;
  52. return(count);
  53. }
  54. }
  55. return 0;
  56. }
  57. static void skip_local(size_t len)
  58. {
  59. if (fp) {
  60. skip(fp, len);
  61. } else {
  62. if (len + local_buffer_cur > local_buffer_length) {
  63. ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "skip_local failed on memory buffer");
  64. } else {
  65. local_buffer_cur += len;
  66. }
  67. }
  68. }
  69. /* Computes how many (fractional) samples one MIDI delta-time unit contains */
  70. static void compute_sample_increment( int32_t tempo, int32_t divisions)
  71. {
  72. double a;
  73. a = (double) (tempo) * (double) (play_mode->rate) * (65536.0/1000000.0) /
  74. (double)(divisions);
  75. sample_correction = (int32_t)(a) & 0xFFFF;
  76. sample_increment = (int32_t)(a) >> 16;
  77. ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Samples per delta-t: %d (correction %d)",
  78. sample_increment, sample_correction);
  79. }
  80. /* Read variable-length number (7 bits per byte, MSB first) */
  81. static int32_t getvl(void)
  82. {
  83. int32_t l=0;
  84. uint8_t c;
  85. for (;;)
  86. {
  87. read_local(&c,1,1);
  88. l += (c & 0x7f);
  89. if (!(c & 0x80)) return l;
  90. l<<=7;
  91. }
  92. }
  93. /* Print a string from the file, followed by a newline. Any non-ASCII
  94. or unprintable characters will be converted to periods. */
  95. static int dumpstring( int32_t len, char *label)
  96. {
  97. signed char *s=(signed char *)safe_malloc(len+1);
  98. if (len != (int32_t)read_local(s, 1, len))
  99. {
  100. Real_Tim_Free(s);
  101. return -1;
  102. }
  103. s[len]='\0';
  104. while (len--)
  105. {
  106. if (s[len]<32)
  107. s[len]='.';
  108. }
  109. ctl->cmsg(CMSG_TEXT, VERB_VERBOSE, "%s%s", label, s);
  110. Real_Tim_Free(s);
  111. return 0;
  112. }
  113. #define MIDIEVENT(at,t,ch,pa,pb) \
  114. newEventList=(MidiEventList*)safe_malloc(sizeof(MidiEventList)); \
  115. newEventList->event.time=at; newEventList->event.type=t; newEventList->event.channel=ch; \
  116. newEventList->event.a=pa; newEventList->event.b=pb; newEventList->next=0;\
  117. return newEventList;
  118. #define MAGIC_EOT ((MidiEventList *)(-1))
  119. /* Read a MIDI event, returning a freshly allocated element that can
  120. be linked to the event list */
  121. static MidiEventList *read_midi_event(void)
  122. {
  123. static uint8_t laststatus, lastchan;
  124. static uint8_t nrpn=0, rpn_msb[16], rpn_lsb[16]; /* one per channel */
  125. uint8_t me, type, a,b,c;
  126. int32_t len;
  127. MidiEventList *newEventList;
  128. for (;;)
  129. {
  130. at+=getvl();
  131. if (read_local(&me,1,1)!=1)
  132. {
  133. ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: read_midi_event: %s",
  134. current_filename, strerror(errno));
  135. return 0;
  136. }
  137. if(me==0xF0 || me == 0xF7) /* SysEx event */
  138. {
  139. len=getvl();
  140. skip_local(len);
  141. }
  142. else if(me==0xFF) /* Meta event */
  143. {
  144. read_local(&type,1,1);
  145. len=getvl();
  146. if (type>0 && type<16)
  147. {
  148. static char *label[]={
  149. "Text event: ", "Text: ", "Copyright: ", "Track name: ",
  150. "Instrument: ", "Lyric: ", "Marker: ", "Cue point: "};
  151. dumpstring(len, label[(type>7) ? 0 : type]);
  152. }
  153. else
  154. switch(type)
  155. {
  156. case 0x2F: /* End of Track */
  157. return MAGIC_EOT;
  158. case 0x51: /* Tempo */
  159. read_local(&a,1,1); read_local(&b,1,1); read_local(&c,1,1);
  160. MIDIEVENT(at, ME_TEMPO, c, a, b);
  161. default:
  162. ctl->cmsg(CMSG_INFO, VERB_DEBUG,
  163. "(Meta event type 0x%02x, length %ld)", type, len);
  164. skip_local(len);
  165. break;
  166. }
  167. }
  168. else
  169. {
  170. a=me;
  171. if (a & 0x80) /* status byte */
  172. {
  173. lastchan=a & 0x0F;
  174. laststatus=(a>>4) & 0x07;
  175. read_local(&a, 1,1);
  176. a &= 0x7F;
  177. }
  178. switch(laststatus)
  179. {
  180. case 0: /* Note off */
  181. read_local(&b, 1,1);
  182. b &= 0x7F;
  183. MIDIEVENT(at, ME_NOTEOFF, lastchan, a,b);
  184. case 1: /* Note on */
  185. read_local(&b, 1,1);
  186. b &= 0x7F;
  187. MIDIEVENT(at, ME_NOTEON, lastchan, a,b);
  188. case 2: /* Key Pressure */
  189. read_local(&b, 1,1);
  190. b &= 0x7F;
  191. MIDIEVENT(at, ME_KEYPRESSURE, lastchan, a, b);
  192. case 3: /* Control change */
  193. read_local(&b, 1,1);
  194. b &= 0x7F;
  195. {
  196. int control=255;
  197. switch(a)
  198. {
  199. case 7: control=ME_MAINVOLUME; break;
  200. case 10: control=ME_PAN; break;
  201. case 11: control=ME_EXPRESSION; break;
  202. case 64: control=ME_SUSTAIN; break;
  203. case 120: control=ME_ALL_SOUNDS_OFF; break;
  204. case 121: control=ME_RESET_CONTROLLERS; break;
  205. case 123: control=ME_ALL_NOTES_OFF; break;
  206. /* These should be the SCC-1 tone bank switch
  207. commands. I don't know why there are two, or
  208. why the latter only allows switching to bank 0.
  209. Also, some MIDI files use 0 as some sort of
  210. continuous controller. This will cause lots of
  211. warnings about undefined tone banks. */
  212. case 0: control=ME_TONE_BANK; break;
  213. case 32:
  214. if (b!=0)
  215. ctl->cmsg(CMSG_INFO, VERB_DEBUG,
  216. "(Strange: tone bank change 0x20%02x)", b);
  217. else
  218. control=ME_TONE_BANK;
  219. break;
  220. case 100: nrpn=0; rpn_msb[lastchan]=b; break;
  221. case 101: nrpn=0; rpn_lsb[lastchan]=b; break;
  222. case 99: nrpn=1; rpn_msb[lastchan]=b; break;
  223. case 98: nrpn=1; rpn_lsb[lastchan]=b; break;
  224. case 6:
  225. if (nrpn)
  226. {
  227. ctl->cmsg(CMSG_INFO, VERB_DEBUG,
  228. "(Data entry (MSB) for NRPN %02x,%02x: %ld)",
  229. rpn_msb[lastchan], rpn_lsb[lastchan],
  230. b);
  231. break;
  232. }
  233. switch((rpn_msb[lastchan]<<8) | rpn_lsb[lastchan])
  234. {
  235. case 0x0000: /* Pitch bend sensitivity */
  236. control=ME_PITCH_SENS;
  237. break;
  238. case 0x7F7F: /* RPN reset */
  239. /* reset pitch bend sensitivity to 2 */
  240. MIDIEVENT(at, ME_PITCH_SENS, lastchan, 2, 0);
  241. default:
  242. ctl->cmsg(CMSG_INFO, VERB_DEBUG,
  243. "(Data entry (MSB) for RPN %02x,%02x: %ld)",
  244. rpn_msb[lastchan], rpn_lsb[lastchan],
  245. b);
  246. break;
  247. }
  248. break;
  249. default:
  250. ctl->cmsg(CMSG_INFO, VERB_DEBUG,
  251. "(Control %d: %d)", a, b);
  252. break;
  253. }
  254. if (control != 255)
  255. {
  256. MIDIEVENT(at, control, lastchan, b, 0);
  257. }
  258. }
  259. break;
  260. case 4: /* Program change */
  261. a &= 0x7f;
  262. MIDIEVENT(at, ME_PROGRAM, lastchan, a, 0);
  263. case 5: /* Channel pressure - NOT IMPLEMENTED */
  264. break;
  265. case 6: /* Pitch wheel */
  266. read_local(&b, 1,1);
  267. b &= 0x7F;
  268. MIDIEVENT(at, ME_PITCHWHEEL, lastchan, a, b);
  269. default:
  270. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  271. "*** Can't happen: status 0x%02X, channel 0x%02X",
  272. laststatus, lastchan);
  273. break;
  274. }
  275. }
  276. }
  277. return newEventList;
  278. }
  279. #undef MIDIEVENT
  280. /* Read a midi track into the linked list, either merging with any previous
  281. tracks or appending to them. */
  282. static int read_track(int append)
  283. {
  284. MidiEventList *meep;
  285. MidiEventList *next, *newEventList;
  286. int32_t len;
  287. char tmp[4];
  288. meep=evlist;
  289. if (append && meep)
  290. {
  291. /* find the last event in the list */
  292. for (; meep->next; meep=(MidiEventList *)meep->next)
  293. ;
  294. at=meep->event.time;
  295. }
  296. else
  297. at=0;
  298. /* Check the formalities */
  299. if ((read_local(tmp,1,4) != 4) || (read_local(&len,4,1) != 1))
  300. {
  301. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  302. "%s: Can't read track header.", current_filename);
  303. return -1;
  304. }
  305. len=BE_LONG(len);
  306. if (memcmp(tmp, "MTrk", 4))
  307. {
  308. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  309. "%s: Corrupt MIDI file.", current_filename);
  310. return -2;
  311. }
  312. for (;;)
  313. {
  314. if (!(newEventList=read_midi_event())) /* Some kind of error */
  315. return -2;
  316. if (newEventList==MAGIC_EOT) /* End-of-track Hack. */
  317. {
  318. return 0;
  319. }
  320. next=(MidiEventList *)meep->next;
  321. while (next && (next->event.time < newEventList->event.time))
  322. {
  323. meep=next;
  324. next=(MidiEventList *)meep->next;
  325. }
  326. newEventList->next=next;
  327. meep->next=newEventList;
  328. event_count++; /* Count the event. (About one?) */
  329. meep=newEventList;
  330. }
  331. }
  332. /* Free the linked event list from memory. */
  333. static void free_midi_list(void)
  334. {
  335. MidiEventList *meep, *next;
  336. if (!(meep=evlist)) return;
  337. while (meep)
  338. {
  339. next=(MidiEventList *)meep->next;
  340. Real_Tim_Free(meep);
  341. meep=next;
  342. }
  343. evlist=0;
  344. }
  345. /* Allocate an array of MidiEvents and fill it from the linked list of
  346. events, marking used instruments for loading. Convert event times to
  347. samples: handle tempo changes. Strip unnecessary events from the list.
  348. Free the linked list. */
  349. static MidiEvent *groom_list( int32_t divisions, int32_t *eventsp, int32_t *samplesp)
  350. {
  351. MidiEvent *groomed_list, *lp;
  352. MidiEventList *meep;
  353. int32_t i, our_event_count, tempo, skip_local_this_event, new_value;
  354. int32_t sample_cum, samples_to_do, at, st, dt, counting_time;
  355. int current_bank[16], current_set[16], current_program[16];
  356. /* Or should each bank have its own current program? */
  357. for (i=0; i<16; i++)
  358. {
  359. current_bank[i]=0;
  360. current_set[i]=0;
  361. current_program[i]=default_program;
  362. }
  363. tempo=500000;
  364. compute_sample_increment(tempo, divisions);
  365. /* This may allocate a bit more than we need */
  366. groomed_list=lp=(MidiEvent*)safe_malloc(sizeof(MidiEvent) * (event_count+1));
  367. meep=evlist;
  368. our_event_count=0;
  369. st=at=sample_cum=0;
  370. counting_time=2; /* We strip any silence before the first NOTE ON. */
  371. for (i=0; i<event_count; i++)
  372. {
  373. skip_local_this_event=0;
  374. ctl->cmsg(CMSG_INFO, VERB_DEBUG_SILLY,
  375. "%6d: ch %2d: event %d (%d,%d)",
  376. meep->event.time, meep->event.channel + 1,
  377. meep->event.type, meep->event.a, meep->event.b);
  378. if (meep->event.type==ME_TEMPO)
  379. {
  380. tempo=
  381. meep->event.channel + meep->event.b * 256 + meep->event.a * 65536;
  382. compute_sample_increment(tempo, divisions);
  383. skip_local_this_event=1;
  384. }
  385. else if ((quietchannels & (1<<meep->event.channel)))
  386. skip_local_this_event=1;
  387. else switch (meep->event.type)
  388. {
  389. case ME_PROGRAM:
  390. if (ISDRUMCHANNEL(meep->event.channel))
  391. {
  392. if (drumset[meep->event.a]) /* Is this a defined drumset? */
  393. new_value=meep->event.a;
  394. else
  395. {
  396. ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
  397. "Drum set %d is undefined", meep->event.a);
  398. new_value=meep->event.a=0;
  399. }
  400. if (current_set[meep->event.channel] != new_value)
  401. current_set[meep->event.channel]=new_value;
  402. else
  403. skip_local_this_event=1;
  404. }
  405. else
  406. {
  407. new_value=meep->event.a;
  408. if ((current_program[meep->event.channel] != SPECIAL_PROGRAM)
  409. && (current_program[meep->event.channel] != new_value))
  410. current_program[meep->event.channel] = new_value;
  411. else
  412. skip_local_this_event=1;
  413. }
  414. break;
  415. case ME_NOTEON:
  416. if (counting_time)
  417. counting_time=1;
  418. if (ISDRUMCHANNEL(meep->event.channel))
  419. {
  420. /* Mark this instrument to be loaded */
  421. if (!(drumset[current_set[meep->event.channel]]
  422. ->tone[meep->event.a].instrument))
  423. drumset[current_set[meep->event.channel]]
  424. ->tone[meep->event.a].instrument=
  425. MAGIC_LOAD_INSTRUMENT;
  426. }
  427. else
  428. {
  429. if (current_program[meep->event.channel]==SPECIAL_PROGRAM)
  430. break;
  431. /* Mark this instrument to be loaded */
  432. if (!(tonebank[current_bank[meep->event.channel]]
  433. ->tone[current_program[meep->event.channel]].instrument))
  434. tonebank[current_bank[meep->event.channel]]
  435. ->tone[current_program[meep->event.channel]].instrument=
  436. MAGIC_LOAD_INSTRUMENT;
  437. }
  438. break;
  439. case ME_TONE_BANK:
  440. if (ISDRUMCHANNEL(meep->event.channel))
  441. {
  442. skip_local_this_event=1;
  443. break;
  444. }
  445. if (tonebank[meep->event.a]) /* Is this a defined tone bank? */
  446. new_value=meep->event.a;
  447. else
  448. {
  449. ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
  450. "Tone bank %d is undefined", meep->event.a);
  451. new_value=meep->event.a=0;
  452. }
  453. if (current_bank[meep->event.channel]!=new_value)
  454. current_bank[meep->event.channel]=new_value;
  455. else
  456. skip_local_this_event=1;
  457. break;
  458. }
  459. /* Recompute time in samples*/
  460. if ((dt=meep->event.time - at) && !counting_time)
  461. {
  462. samples_to_do=sample_increment * dt;
  463. sample_cum += sample_correction * dt;
  464. if (sample_cum & 0xFFFF0000)
  465. {
  466. samples_to_do += ((sample_cum >> 16) & 0xFFFF);
  467. sample_cum &= 0x0000FFFF;
  468. }
  469. st += samples_to_do;
  470. }
  471. else if (counting_time==1) counting_time=0;
  472. if (!skip_local_this_event)
  473. {
  474. /* Add the event to the list */
  475. *lp=meep->event;
  476. lp->time=st;
  477. lp++;
  478. our_event_count++;
  479. }
  480. at=meep->event.time;
  481. meep=(MidiEventList *)meep->next;
  482. }
  483. /* Add an End-of-Track event */
  484. lp->time=st;
  485. lp->type=ME_EOT;
  486. our_event_count++;
  487. free_midi_list();
  488. *eventsp=our_event_count;
  489. *samplesp=st;
  490. return groomed_list;
  491. }
  492. MidiEvent *read_midi_file(idFile * mfp, int32_t *count, int32_t *sp)
  493. {
  494. int32_t len, divisions;
  495. int16_t format, tracks, divisions_tmp;
  496. int i;
  497. char tmp[4];
  498. fp = mfp;
  499. local_buffer = 0;
  500. local_buffer_length = 0;
  501. local_buffer_cur = 0;
  502. event_count=0;
  503. at=0;
  504. evlist=0;
  505. int first = (read_local(tmp,1,4));
  506. int second = (read_local(&len,4,1));
  507. if ( first != 4 || second != 1)
  508. {
  509. if (0)
  510. {
  511. ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s", current_filename,
  512. strerror(errno));
  513. }
  514. else
  515. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  516. "%s: Not a MIDI file!", current_filename);
  517. return 0;
  518. }
  519. len=BE_LONG(len);
  520. if (memcmp(tmp, "MThd", 4) || len < 6)
  521. {
  522. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  523. "%s: Not a MIDI file!", current_filename);
  524. return 0;
  525. }
  526. read_local(&format, 2, 1);
  527. read_local(&tracks, 2, 1);
  528. read_local(&divisions_tmp, 2, 1);
  529. format=BE_SHORT(format);
  530. tracks=BE_SHORT(tracks);
  531. divisions_tmp=BE_SHORT(divisions_tmp);
  532. if (divisions_tmp<0)
  533. {
  534. /* SMPTE time -- totally untested. Got a MIDI file that uses this? */
  535. divisions=
  536. (int32_t)(-(divisions_tmp/256)) * (int32_t)(divisions_tmp & 0xFF);
  537. }
  538. else divisions=(int32_t)(divisions_tmp);
  539. if (len > 6)
  540. {
  541. ctl->cmsg(CMSG_WARNING, VERB_NORMAL,
  542. "%s: MIDI file header size %ld bytes",
  543. current_filename, len);
  544. skip_local(len-6); /* skip_local the excess */
  545. }
  546. if (format<0 || format >2)
  547. {
  548. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  549. "%s: Unknown MIDI file format %d", current_filename, format);
  550. return 0;
  551. }
  552. //ctl->cmsg(CMSG_INFO, VERB_VERBOSE,
  553. // "Format: %d Tracks: %d Divisions: %d", format, tracks, divisions);
  554. /* Put a do-nothing event first in the list for easier processing */
  555. evlist=(MidiEventList *)safe_malloc(sizeof(MidiEventList));
  556. evlist->event.time=0;
  557. evlist->event.type=ME_NONE;
  558. evlist->next=0;
  559. event_count++;
  560. switch(format)
  561. {
  562. case 0:
  563. if (read_track(0))
  564. {
  565. free_midi_list();
  566. return 0;
  567. }
  568. break;
  569. case 1:
  570. for (i=0; i<tracks; i++)
  571. if (read_track(0))
  572. {
  573. free_midi_list();
  574. return 0;
  575. }
  576. break;
  577. case 2: /* We simply play the tracks sequentially */
  578. for (i=0; i<tracks; i++)
  579. if (read_track(1))
  580. {
  581. free_midi_list();
  582. return 0;
  583. }
  584. break;
  585. }
  586. return groom_list(divisions, count, sp);
  587. }
  588. MidiEvent *read_midi_buffer(unsigned char* buffer, size_t length, int32_t *count, int32_t *sp)
  589. {
  590. int32_t len, divisions;
  591. int16_t format, tracks, divisions_tmp;
  592. int i;
  593. char tmp[4];
  594. fp = 0;
  595. local_buffer = buffer;
  596. local_buffer_length = length;
  597. local_buffer_cur = 0;
  598. event_count=0;
  599. at=0;
  600. evlist=0;
  601. if ((read_local(tmp,1,4) != 4) || (read_local(&len,4,1) != 1))
  602. {
  603. ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Not a MIDI file!");
  604. return 0;
  605. }
  606. len=BE_LONG(len);
  607. if (memcmp(tmp, "MThd", 4) || len < 6)
  608. {
  609. ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: Not a MIDI file!", current_filename);
  610. return 0;
  611. }
  612. read_local(&format, 2, 1);
  613. read_local(&tracks, 2, 1);
  614. read_local(&divisions_tmp, 2, 1);
  615. format=BE_SHORT(format);
  616. tracks=BE_SHORT(tracks);
  617. divisions_tmp=BE_SHORT(divisions_tmp);
  618. if (divisions_tmp<0) {
  619. /* SMPTE time -- totally untested. Got a MIDI file that uses this? */
  620. divisions= (int32_t)(-(divisions_tmp/256)) * (int32_t)(divisions_tmp & 0xFF);
  621. }
  622. else divisions=(int32_t)(divisions_tmp);
  623. if (len > 6)
  624. {
  625. ctl->cmsg(CMSG_WARNING, VERB_NORMAL,
  626. "%s: MIDI file header size %ld bytes",
  627. current_filename, len);
  628. skip_local(len-6); /* skip_local the excess */
  629. }
  630. if (format<0 || format >2)
  631. {
  632. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  633. "%s: Unknown MIDI file format %d", current_filename, format);
  634. return 0;
  635. }
  636. //ctl->cmsg(CMSG_INFO, VERB_VERBOSE,
  637. // "Format: %d Tracks: %d Divisions: %d", format, tracks, divisions);
  638. /* Put a do-nothing event first in the list for easier processing */
  639. evlist=(MidiEventList *)safe_malloc(sizeof(MidiEventList));
  640. evlist->event.time=0;
  641. evlist->event.type=ME_NONE;
  642. evlist->next=0;
  643. event_count++;
  644. switch(format)
  645. {
  646. case 0:
  647. if (read_track(0))
  648. {
  649. free_midi_list();
  650. return 0;
  651. }
  652. break;
  653. case 1:
  654. for (i=0; i<tracks; i++)
  655. if (read_track(0))
  656. {
  657. free_midi_list();
  658. return 0;
  659. }
  660. break;
  661. case 2: /* We simply play the tracks sequentially */
  662. for (i=0; i<tracks; i++)
  663. if (read_track(1))
  664. {
  665. free_midi_list();
  666. return 0;
  667. }
  668. break;
  669. }
  670. return groom_list(divisions, count, sp);
  671. }