PageRenderTime 59ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/exult/tags/Release0_92Alpha3/audio/xmidi.cc

#
C++ | 1486 lines | 1127 code | 243 blank | 116 comment | 250 complexity | 8772a530bb47a7adc16207327438b8fe MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU General Public License
  5. * as published by the Free Software Foundation; either version 2
  6. * of the License, or (at your option) any later version.
  7. *
  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. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. *
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #ifndef ALPHA_LINUX_CXX
  22. # include <unistd.h>
  23. # include <cstdio>
  24. # include <cmath>
  25. # include <iostream>
  26. # include <cmath>
  27. #endif
  28. #include "utils.h"
  29. #include "xmidi.h"
  30. #include "Configuration.h"
  31. extern Configuration *config;
  32. using std::atof;
  33. using std::atoi;
  34. using std::cerr;
  35. using std::endl;
  36. using std::memcmp;
  37. using std::memcpy;
  38. using std::snprintf;
  39. using std::string;
  40. #include "gamma.h"
  41. // This is used to correct incorrect patch, vol and pan changes in midi files
  42. // The bias is just a value to used to work out if a vol and pan belong with a
  43. // patch change. This is to ensure that the default state of a midi file is with
  44. // the tracks centred, unless the first patch change says otherwise.
  45. #define PATCH_VOL_PAN_BIAS 5
  46. // This is a default set of patches to convert from MT32 to GM
  47. // The index is the MT32 Patch nubmer and the value is the GM Patch
  48. // This is only suitable for music that doesn'tdo timbre changes
  49. // XMIDIs that contain Timbre changes will not convert properly
  50. const char XMIDI::mt32asgm[128] = {
  51. 0, // 0 Piano 1
  52. 1, // 1 Piano 2
  53. 2, // 2 Piano 3 (synth)
  54. 4, // 3 EPiano 1
  55. 4, // 4 EPiano 2
  56. 5, // 5 EPiano 3
  57. 5, // 6 EPiano 4
  58. 3, // 7 Honkytonk
  59. 16, // 8 Organ 1
  60. 17, // 9 Organ 2
  61. 18, // 10 Organ 3
  62. 16, // 11 Organ 4
  63. 19, // 12 Pipe Organ 1
  64. 19, // 13 Pipe Organ 2
  65. 19, // 14 Pipe Organ 3
  66. 21, // 15 Accordion
  67. 6, // 16 Harpsichord 1
  68. 6, // 17 Harpsichord 2
  69. 6, // 18 Harpsichord 3
  70. 7, // 19 Clavinet 1
  71. 7, // 20 Clavinet 2
  72. 7, // 21 Clavinet 3
  73. 8, // 22 Celesta 1
  74. 8, // 23 Celesta 2
  75. 62, // 24 Synthbrass 1 (62)
  76. 63, // 25 Synthbrass 2 (63)
  77. 62, // 26 Synthbrass 3 Bank 8
  78. 63, // 27 Synthbrass 4 Bank 8
  79. 38, // 28 Synthbass 1
  80. 39, // 29 Synthbass 2
  81. 38, // 30 Synthbass 3 Bank 8
  82. 39, // 31 Synthbass 4 Bank 8
  83. 88, // 32 Fantasy
  84. 90, // 33 Harmonic Pan - No equiv closest is polysynth(90) :(
  85. 52, // 34 Choral ?? Currently set to SynthVox(54). Should it be ChoirAhhs(52)???
  86. 92, // 35 Glass
  87. 97, // 36 Soundtrack
  88. 99, // 37 Atmosphere
  89. 14, // 38 Warmbell, sounds kind of like crystal(98) perhaps Tubular Bells(14) would be better. It is!
  90. 54, // 39 FunnyVox, sounds alot like Bagpipe(109) and Shania(111)
  91. 98, // 40 EchoBell, no real equiv, sounds like Crystal(98)
  92. 96, // 41 IceRain
  93. 68, // 42 Oboe 2001, no equiv, just patching it to normal oboe(68)
  94. 95, // 43 EchoPans, no equiv, setting to SweepPad
  95. 81, // 44 DoctorSolo Bank 8
  96. 87, // 45 SchoolDaze, no real equiv
  97. 112, // 46 Bell Singer
  98. 80, // 47 SquareWave
  99. 48, // 48 Strings 1
  100. 48, // 49 Strings 2 - should be 49
  101. 44, // 50 Strings 3 (Synth) - Experimental set to Tremollo Strings - should be 50
  102. 45, // 51 Pizzicato Strings
  103. 40, // 52 Violin 1
  104. 40, // 53 Violin 2 ? Viola
  105. 42, // 54 Cello 1
  106. 42, // 55 Cello 2
  107. 43, // 56 Contrabass
  108. 46, // 57 Harp 1
  109. 46, // 58 Harp 2
  110. 24, // 59 Guitar 1 (Nylon)
  111. 25, // 60 Guitar 2 (Steel)
  112. 26, // 61 Elec Guitar 1
  113. 27, // 62 Elec Guitar 2
  114. 104, // 63 Sitar
  115. 32, // 64 Acou Bass 1
  116. 32, // 65 Acou Bass 2
  117. 33, // 66 Elec Bass 1
  118. 34, // 67 Elec Bass 2
  119. 36, // 68 Slap Bass 1
  120. 37, // 69 Slap Bass 2
  121. 35, // 70 Fretless Bass 1
  122. 35, // 71 Fretless Bass 2
  123. 73, // 72 Flute 1
  124. 73, // 73 Flute 2
  125. 72, // 74 Piccolo 1
  126. 72, // 75 Piccolo 2
  127. 74, // 76 Recorder
  128. 75, // 77 Pan Pipes
  129. 64, // 78 Sax 1
  130. 65, // 79 Sax 2
  131. 66, // 80 Sax 3
  132. 67, // 81 Sax 4
  133. 71, // 82 Clarinet 1
  134. 71, // 83 Clarinet 2
  135. 68, // 84 Oboe
  136. 69, // 85 English Horn (Cor Anglais)
  137. 70, // 86 Bassoon
  138. 22, // 87 Harmonica
  139. 56, // 88 Trumpet 1
  140. 56, // 89 Trumpet 2
  141. 57, // 90 Trombone 1
  142. 57, // 91 Trombone 2
  143. 60, // 92 French Horn 1
  144. 60, // 93 French Horn 2
  145. 58, // 94 Tuba
  146. 61, // 95 Brass Section 1
  147. 61, // 96 Brass Section 2
  148. 11, // 97 Vibes 1
  149. 11, // 98 Vibes 2
  150. 99, // 99 Syn Mallet Bank 1
  151. 112, // 100 WindBell no real equiv Set to TinkleBell(112)
  152. 9, // 101 Glockenspiel
  153. 14, // 102 Tubular Bells
  154. 13, // 103 Xylophone
  155. 12, // 104 Marimba
  156. 107, // 105 Koto
  157. 111, // 106 Sho?? set to Shanai(111)
  158. 77, // 107 Shakauhachi
  159. 78, // 108 Whistle 1
  160. 78, // 109 Whistle 2
  161. 76, // 110 Bottle Blow
  162. 76, // 111 Breathpipe no real equiv set to bottle blow(76)
  163. 47, // 112 Timpani
  164. 117, // 113 Melodic Tom
  165. 116, // 114 Deap Snare no equiv, set to Taiko(116)
  166. 118, // 115 Electric Perc 1
  167. 118, // 116 Electric Perc 2
  168. 116, // 117 Taiko
  169. 115, // 118 Taiko Rim, no real equiv, set to Woodblock(115)
  170. 119, // 119 Cymbal, no real equiv, set to reverse cymbal(119)
  171. 115, // 120 Castanets, no real equiv, in GM set to Woodblock(115)
  172. 112, // 121 Triangle, no real equiv, set to TinkleBell(112)
  173. 55, // 122 Orchestral Hit
  174. 124, // 123 Telephone
  175. 123, // 124 BirdTweet
  176. 94, // 125 Big Notes Pad no equiv, set to halo pad (94)
  177. 98, // 126 Water Bell set to Crystal Pad(98)
  178. 121 // 127 Jungle Tune set to Breath Noise
  179. };
  180. // Same as above, except include patch changes
  181. // so GS instruments can be used
  182. const char XMIDI::mt32asgs[256] = {
  183. 0, 0, // 0 Piano 1
  184. 1, 0, // 1 Piano 2
  185. 2, 0, // 2 Piano 3 (synth)
  186. 4, 0, // 3 EPiano 1
  187. 4, 0, // 4 EPiano 2
  188. 5, 0, // 5 EPiano 3
  189. 5, 0, // 6 EPiano 4
  190. 3, 0, // 7 Honkytonk
  191. 16, 0, // 8 Organ 1
  192. 17, 0, // 9 Organ 2
  193. 18, 0, // 10 Organ 3
  194. 16, 0, // 11 Organ 4
  195. 19, 0, // 12 Pipe Organ 1
  196. 19, 0, // 13 Pipe Organ 2
  197. 19, 0, // 14 Pipe Organ 3
  198. 21, 0, // 15 Accordion
  199. 6, 0, // 16 Harpsichord 1
  200. 6, 0, // 17 Harpsichord 2
  201. 6, 0, // 18 Harpsichord 3
  202. 7, 0, // 19 Clavinet 1
  203. 7, 0, // 20 Clavinet 2
  204. 7, 0, // 21 Clavinet 3
  205. 8, 0, // 22 Celesta 1
  206. 8, 0, // 23 Celesta 2
  207. 62, 0, // 24 Synthbrass 1 (62)
  208. 63, 0, // 25 Synthbrass 2 (63)
  209. 62, 0, // 26 Synthbrass 3 Bank 8
  210. 63, 0, // 27 Synthbrass 4 Bank 8
  211. 38, 0, // 28 Synthbass 1
  212. 39, 0, // 29 Synthbass 2
  213. 38, 0, // 30 Synthbass 3 Bank 8
  214. 39, 0, // 31 Synthbass 4 Bank 8
  215. 88, 0, // 32 Fantasy
  216. 90, 0, // 33 Harmonic Pan - No equiv closest is polysynth(90) :(
  217. 52, 0, // 34 Choral ?? Currently set to SynthVox(54). Should it be ChoirAhhs(52)???
  218. 92, 0, // 35 Glass
  219. 97, 0, // 36 Soundtrack
  220. 99, 0, // 37 Atmosphere
  221. 14, 0, // 38 Warmbell, sounds kind of like crystal(98) perhaps Tubular Bells(14) would be better. It is!
  222. 54, 0, // 39 FunnyVox, sounds alot like Bagpipe(109) and Shania(111)
  223. 98, 0, // 40 EchoBell, no real equiv, sounds like Crystal(98)
  224. 96, 0, // 41 IceRain
  225. 68, 0, // 42 Oboe 2001, no equiv, just patching it to normal oboe(68)
  226. 95, 0, // 43 EchoPans, no equiv, setting to SweepPad
  227. 81, 0, // 44 DoctorSolo Bank 8
  228. 87, 0, // 45 SchoolDaze, no real equiv
  229. 112, 0, // 46 Bell Singer
  230. 80, 0, // 47 SquareWave
  231. 48, 0, // 48 Strings 1
  232. 48, 0, // 49 Strings 2 - should be 49
  233. 44, 0, // 50 Strings 3 (Synth) - Experimental set to Tremollo Strings - should be 50
  234. 45, 0, // 51 Pizzicato Strings
  235. 40, 0, // 52 Violin 1
  236. 40, 0, // 53 Violin 2 ? Viola
  237. 42, 0, // 54 Cello 1
  238. 42, 0, // 55 Cello 2
  239. 43, 0, // 56 Contrabass
  240. 46, 0, // 57 Harp 1
  241. 46, 0, // 58 Harp 2
  242. 24, 0, // 59 Guitar 1 (Nylon)
  243. 25, 0, // 60 Guitar 2 (Steel)
  244. 26, 0, // 61 Elec Guitar 1
  245. 27, 0, // 62 Elec Guitar 2
  246. 104, 0, // 63 Sitar
  247. 32, 0, // 64 Acou Bass 1
  248. 32, 0, // 65 Acou Bass 2
  249. 33, 0, // 66 Elec Bass 1
  250. 34, 0, // 67 Elec Bass 2
  251. 36, 0, // 68 Slap Bass 1
  252. 37, 0, // 69 Slap Bass 2
  253. 35, 0, // 70 Fretless Bass 1
  254. 35, 0, // 71 Fretless Bass 2
  255. 73, 0, // 72 Flute 1
  256. 73, 0, // 73 Flute 2
  257. 72, 0, // 74 Piccolo 1
  258. 72, 0, // 75 Piccolo 2
  259. 74, 0, // 76 Recorder
  260. 75, 0, // 77 Pan Pipes
  261. 64, 0, // 78 Sax 1
  262. 65, 0, // 79 Sax 2
  263. 66, 0, // 80 Sax 3
  264. 67, 0, // 81 Sax 4
  265. 71, 0, // 82 Clarinet 1
  266. 71, 0, // 83 Clarinet 2
  267. 68, 0, // 84 Oboe
  268. 69, 0, // 85 English Horn (Cor Anglais)
  269. 70, 0, // 86 Bassoon
  270. 22, 0, // 87 Harmonica
  271. 56, 0, // 88 Trumpet 1
  272. 56, 0, // 89 Trumpet 2
  273. 57, 0, // 90 Trombone 1
  274. 57, 0, // 91 Trombone 2
  275. 60, 0, // 92 French Horn 1
  276. 60, 0, // 93 French Horn 2
  277. 58, 0, // 94 Tuba
  278. 61, 0, // 95 Brass Section 1
  279. 61, 0, // 96 Brass Section 2
  280. 11, 0, // 97 Vibes 1
  281. 11, 0, // 98 Vibes 2
  282. 99, 0, // 99 Syn Mallet Bank 1
  283. 112, 0, // 100 WindBell no real equiv Set to TinkleBell(112)
  284. 9, 0, // 101 Glockenspiel
  285. 14, 0, // 102 Tubular Bells
  286. 13, 0, // 103 Xylophone
  287. 12, 0, // 104 Marimba
  288. 107, 0, // 105 Koto
  289. 111, 0, // 106 Sho?? set to Shanai(111)
  290. 77, 0, // 107 Shakauhachi
  291. 78, 0, // 108 Whistle 1
  292. 78, 0, // 109 Whistle 2
  293. 76, 0, // 110 Bottle Blow
  294. 76, 0, // 111 Breathpipe no real equiv set to bottle blow(76)
  295. 47, 0, // 112 Timpani
  296. 117, 0, // 113 Melodic Tom
  297. 116, 0, // 114 Deap Snare no equiv, set to Taiko(116)
  298. 118, 0, // 115 Electric Perc 1
  299. 118, 0, // 116 Electric Perc 2
  300. 116, 0, // 117 Taiko
  301. 115, 0, // 118 Taiko Rim, no real equiv, set to Woodblock(115)
  302. 119, 0, // 119 Cymbal, no real equiv, set to reverse cymbal(119)
  303. 115, 0, // 120 Castanets, no real equiv, in GM set to Woodblock(115)
  304. 112, 0, // 121 Triangle, no real equiv, set to TinkleBell(112)
  305. 55, 0, // 122 Orchestral Hit
  306. 124, 0, // 123 Telephone
  307. 123, 0, // 124 BirdTweet
  308. 94, 0, // 125 Big Notes Pad no equiv, set to halo pad (94)
  309. 98, 0, // 126 Water Bell set to Crystal Pad(98)
  310. 121, 0 // 127 Jungle Tune set to Breath Noise
  311. };
  312. GammaTable<unsigned char> XMIDI::VolumeCurve(128);
  313. // Constructor
  314. XMIDI::XMIDI(DataSource *source, int pconvert) : events(NULL),timing(NULL),
  315. convert_type(pconvert),fixed(NULL),
  316. do_reverb(false), do_chorus(false)
  317. {
  318. int i = 16;
  319. while (i--) bank127[i] = 0;
  320. ExtractTracks (source);
  321. }
  322. XMIDI::~XMIDI()
  323. {
  324. if (events)
  325. {
  326. for (int i=0; i < info.tracks; i++)
  327. DeleteEventList (events[i]);
  328. delete [] events;
  329. }
  330. if (timing) delete [] timing;
  331. if (fixed) delete [] fixed;
  332. }
  333. int XMIDI::retrieve (uint32 track, DataSource *dest)
  334. {
  335. int len = 0;
  336. if (!events)
  337. {
  338. cerr << "No midi data in loaded." << endl;
  339. return 0;
  340. }
  341. // Convert type 1 midi's to type 0
  342. if (info.type == 1)
  343. {
  344. DuplicateAndMerge(-1);
  345. for (int i=0; i < info.tracks; i++)
  346. DeleteEventList (events[i]);
  347. delete [] events;
  348. events = new midi_event *[1];
  349. events[0] = list;
  350. info.tracks = 1;
  351. info.type = 0;
  352. }
  353. if (track >= info.tracks)
  354. {
  355. cerr << "Can't retrieve MIDI data, track out of range" << endl;
  356. return 0;
  357. }
  358. // And fix the midis if they are broken
  359. if (!fixed[track])
  360. {
  361. list = events[track];
  362. MovePatchVolAndPan ();
  363. fixed[track] = true;
  364. events[track] = list;
  365. }
  366. // This is so if using buffer datasource, the caller can know how big to make the buffer
  367. if (!dest)
  368. {
  369. // Header is 14 bytes long and add the rest as well
  370. len = ConvertListToMTrk (NULL, events[track]);
  371. return 14 + len;
  372. }
  373. dest->write1 ('M');
  374. dest->write1 ('T');
  375. dest->write1 ('h');
  376. dest->write1 ('d');
  377. dest->write4high (6);
  378. dest->write2high (0);
  379. dest->write2high (1);
  380. dest->write2high (timing[track]);
  381. len = ConvertListToMTrk (dest, events[track]);
  382. return len + 14;
  383. }
  384. int XMIDI::retrieve (uint32 track, midi_event **dest, int &ppqn)
  385. {
  386. if (!events)
  387. {
  388. cerr << "No midi data in loaded." << endl;
  389. return 0;
  390. }
  391. if ((info.type == 1 && track != 0) || (track >= info.tracks))
  392. {
  393. cerr << "Can't retrieve MIDI data, track out of range" << endl;
  394. return 0;
  395. }
  396. DuplicateAndMerge(track);
  397. MovePatchVolAndPan ();
  398. *dest = list;
  399. ppqn = timing[track];
  400. return 1;
  401. }
  402. void XMIDI::DeleteEventList (midi_event *mlist)
  403. {
  404. midi_event *event;
  405. midi_event *next;
  406. next = mlist;
  407. event = mlist;
  408. while ((event = next))
  409. {
  410. next = event->next;
  411. if (event->buffer) delete [] event->buffer;
  412. delete event;
  413. }
  414. }
  415. // Sets current to the new event and updates list
  416. void XMIDI::CreateNewEvent (int time)
  417. {
  418. if (!list)
  419. {
  420. list = current = new midi_event;
  421. current->next = NULL;
  422. if (time < 0)
  423. current->time = 0;
  424. else
  425. current->time = time;
  426. current->buffer = NULL;
  427. current->len = 0;
  428. return;
  429. }
  430. if (time < 0)
  431. {
  432. midi_event *event = new midi_event;
  433. event->next = list;
  434. list = current = event;
  435. current->time = 0;
  436. current->buffer = NULL;
  437. current->len = 0;
  438. return;
  439. }
  440. if (current->time > time)
  441. current = list;
  442. while (current->next)
  443. {
  444. if (current->next->time > time)
  445. {
  446. midi_event *event = new midi_event;
  447. event->next = current->next;
  448. current->next = event;
  449. current = event;
  450. current->time = time;
  451. current->buffer = NULL;
  452. current->len = 0;
  453. return;
  454. }
  455. current = current->next;
  456. }
  457. current->next = new midi_event;
  458. current = current->next;
  459. current->next = NULL;
  460. current->time = time;
  461. current->buffer = NULL;
  462. current->len = 0;
  463. }
  464. // Conventional Variable Length Quantity
  465. int XMIDI::GetVLQ (DataSource *source, uint32 &quant)
  466. {
  467. int i;
  468. quant = 0;
  469. unsigned int data;
  470. for (i = 0; i < 4; i++)
  471. {
  472. data = source->read1();
  473. quant <<= 7;
  474. quant |= data & 0x7F;
  475. if (!(data & 0x80))
  476. {
  477. i++;
  478. break;
  479. }
  480. }
  481. return i;
  482. }
  483. // XMIDI Delta Variable Length Quantity
  484. int XMIDI::GetVLQ2 (DataSource *source, uint32 &quant)
  485. {
  486. int i;
  487. quant = 0;
  488. int data = 0;
  489. for (i = 0; i < 4; i++)
  490. {
  491. data = source->read1();
  492. if (data & 0x80)
  493. {
  494. source->skip(-1);
  495. break;
  496. }
  497. quant += data;
  498. }
  499. return i;
  500. }
  501. int XMIDI::PutVLQ(DataSource *dest, uint32 value)
  502. {
  503. int buffer;
  504. int i = 1;
  505. buffer = value & 0x7F;
  506. while (value >>= 7)
  507. {
  508. buffer <<= 8;
  509. buffer |= ((value & 0x7F) | 0x80);
  510. i++;
  511. }
  512. if (!dest) return i;
  513. for (int j = 0; j < i; j++)
  514. {
  515. dest->write1(buffer & 0xFF);
  516. buffer >>= 8;
  517. }
  518. return i;
  519. }
  520. // MovePatchVolAndPan
  521. //
  522. // This little function attempts to correct errors in midi files
  523. // that relate to patch, volume and pan changing
  524. void XMIDI::MovePatchVolAndPan (int channel)
  525. {
  526. if (channel == -1)
  527. {
  528. for (int i = 0; i < 16; i++)
  529. MovePatchVolAndPan (i);
  530. return;
  531. }
  532. midi_event *patch = NULL;
  533. midi_event *vol = NULL;
  534. midi_event *pan = NULL;
  535. midi_event *bank = NULL;
  536. midi_event *reverb = NULL;
  537. midi_event *chorus = NULL;
  538. midi_event *temp;
  539. for (current = list; current; )
  540. {
  541. if (!patch && (current->status >> 4) == 0xC && (current->status & 0xF) == channel)
  542. patch = current;
  543. else if (!vol && (current->status >> 4) == 0xB && current->data[0] == 7 && (current->status & 0xF) == channel)
  544. vol = current;
  545. else if (!pan && (current->status >> 4) == 0xB && current->data[0] == 10 && (current->status & 0xF) == channel)
  546. pan = current;
  547. else if (!bank && (current->status >> 4) == 0xB && current->data[0] == 0 && (current->status & 0xF) == channel)
  548. bank = current;
  549. if (pan && vol && patch) break;
  550. if (current) current = current->next;
  551. else current = list;
  552. }
  553. // Got no patch change, return and don't try fixing it
  554. if (!patch) return;
  555. // Copy Patch Change Event
  556. temp = patch;
  557. patch = new midi_event;
  558. patch->time = temp->time;
  559. patch->status = channel|(MIDI_STATUS_PROG_CHANGE << 4);
  560. patch->len = 0;
  561. patch->buffer = NULL;
  562. patch->data[0] = temp->data[0];
  563. // Copy Volume
  564. if (vol && (vol->time > patch->time+PATCH_VOL_PAN_BIAS || vol->time < patch->time-PATCH_VOL_PAN_BIAS))
  565. vol = NULL;
  566. temp = vol;
  567. vol = new midi_event;
  568. vol->status = channel|(MIDI_STATUS_CONTROLLER << 4);
  569. vol->data[0] = 7;
  570. vol->len = 0;
  571. vol->buffer = NULL;
  572. if (!temp)
  573. vol->data[1] = 64;
  574. else
  575. vol->data[1] = temp->data[1];
  576. // Copy Bank
  577. if (bank && (bank->time > patch->time+PATCH_VOL_PAN_BIAS || bank->time < patch->time-PATCH_VOL_PAN_BIAS))
  578. bank = NULL;
  579. temp = bank;
  580. bank = new midi_event;
  581. bank->status = channel|(MIDI_STATUS_CONTROLLER << 4);
  582. bank->data[0] = 0;
  583. bank->len = 0;
  584. bank->buffer = NULL;
  585. if (!temp)
  586. bank->data[1] = 0;
  587. else
  588. bank->data[1] = temp->data[1];
  589. // Copy Pan
  590. if (pan && (pan->time > patch->time+PATCH_VOL_PAN_BIAS || pan->time < patch->time-PATCH_VOL_PAN_BIAS))
  591. pan = NULL;
  592. temp = pan;
  593. pan = new midi_event;
  594. pan->status = channel|(MIDI_STATUS_CONTROLLER << 4);
  595. pan->data[0] = 10;
  596. pan->len = 0;
  597. pan->buffer = NULL;
  598. if (!temp)
  599. pan->data[1] = 64;
  600. else
  601. pan->data[1] = temp->data[1];
  602. if (do_reverb)
  603. {
  604. reverb = new midi_event;
  605. reverb->time = 0;
  606. reverb->status = channel|(MIDI_STATUS_CONTROLLER << 4);
  607. reverb->len = 0;
  608. reverb->buffer = NULL;
  609. reverb->data[0] = 91;
  610. reverb->data[1] = reverb_value;
  611. }
  612. if (do_chorus)
  613. {
  614. chorus = new midi_event;
  615. chorus->time = 0;
  616. chorus->status = channel|(MIDI_STATUS_CONTROLLER << 4);
  617. chorus->len = 0;
  618. chorus->buffer = NULL;
  619. chorus->data[0] = 93;
  620. chorus->data[1] = chorus_value;
  621. }
  622. vol->time = 0;
  623. pan->time = 0;
  624. patch->time = 0;
  625. bank->time = 0;
  626. if (do_reverb && do_chorus) reverb->next = chorus;
  627. else if (do_reverb) reverb->next = bank;
  628. if (do_chorus) chorus->next = bank;
  629. bank->next = vol;
  630. vol->next = pan;
  631. pan->next = patch;
  632. patch->next = list;
  633. if (do_reverb) list = reverb;
  634. else if (do_chorus) list = chorus;
  635. else list = bank;
  636. }
  637. // DuplicateAndMerge
  638. void XMIDI::DuplicateAndMerge (int num)
  639. {
  640. int i;
  641. midi_event **track;
  642. int time = 0;
  643. int start = 0;
  644. int end = 1;
  645. if (info.type == 1)
  646. {
  647. start = 0;
  648. end = info.tracks;
  649. }
  650. else if (num >= 0 && num < info.tracks)
  651. {
  652. start += num;
  653. end += num;
  654. }
  655. track = new midi_event *[info.tracks];
  656. for (i = 0; i < info.tracks; i++) track[i] = events[i];
  657. current = list = NULL;
  658. while (1)
  659. {
  660. int lowest = 1 << 30;
  661. int selected = -1;
  662. int num_na = end-start;
  663. // Firstly we find the track with the lowest time
  664. // for it's current event
  665. for (i = start; i < end; i++)
  666. {
  667. if (!track[i])
  668. {
  669. num_na--;
  670. continue;
  671. }
  672. if (track[i]->time < lowest)
  673. {
  674. selected = i;
  675. lowest = track[i]->time;
  676. }
  677. }
  678. // This is just so I don't have to type [selected] all the time
  679. i = selected;
  680. // None left to convert
  681. if (!num_na) break;
  682. // Only need 1 end of track
  683. // So take the last one and ignore the rest;
  684. if ((num_na != 1) && (track[i]->status == 0xff) && (track[i]->data[0] == 0x2f))
  685. {
  686. track[i] = NULL;
  687. continue;
  688. }
  689. if (current)
  690. {
  691. current->next = new midi_event;
  692. current = current->next;
  693. }
  694. else
  695. list = current = new midi_event;
  696. current->next = NULL;
  697. time = track[i]->time;
  698. current->time = time;
  699. current->status = track[i]->status;
  700. current->data[0] = track[i]->data[0];
  701. current->data[1] = track[i]->data[1];
  702. current->len = track[i]->len;
  703. if (current->len)
  704. {
  705. current->buffer = new unsigned char[current->len];
  706. memcpy (current->buffer, track[i]->buffer, current->len);
  707. }
  708. else
  709. current->buffer = NULL;
  710. track[i] = track[i]->next;
  711. }
  712. }
  713. // Converts Events
  714. //
  715. // Source is at the first data byte
  716. // size 1 is single data byte
  717. // size 2 is dual data byte
  718. // size 3 is XMI Note on
  719. // Returns bytes converted
  720. int XMIDI::ConvertEvent (const int time, const unsigned char status, DataSource *source, const int size)
  721. {
  722. uint32 delta = 0;
  723. int data;
  724. data = source->read1();
  725. // Bank changes are handled here
  726. if ((status >> 4) == 0xB && data == 0)
  727. {
  728. data = source->read1();
  729. bank127[status&0xF] = false;
  730. if (convert_type == XMIDI_CONVERT_MT32_TO_GM || convert_type == XMIDI_CONVERT_MT32_TO_GS
  731. || convert_type == XMIDI_CONVERT_MT32_TO_GS127)
  732. return 2;
  733. CreateNewEvent (time);
  734. current->status = status;
  735. current->data[0] = 0;
  736. current->data[1] = data;
  737. if (convert_type == XMIDI_CONVERT_GS127_TO_GS && data == 127)
  738. bank127[status&0xF] = true;
  739. return 2;
  740. }
  741. // Handling for patch change mt32 conversion, probably should go elsewhere
  742. if ((status >> 4) == 0xC && (status&0xF) != 9 && convert_type != XMIDI_CONVERT_NOCONVERSION)
  743. {
  744. if (convert_type == XMIDI_CONVERT_MT32_TO_GM)
  745. {
  746. data = mt32asgm[data];
  747. }
  748. else if ((convert_type == XMIDI_CONVERT_GS127_TO_GS && bank127[status&0xF]) ||
  749. convert_type == XMIDI_CONVERT_MT32_TO_GS)
  750. {
  751. CreateNewEvent (time);
  752. current->status = 0xB0 | (status&0xF);
  753. current->data[0] = 0;
  754. current->data[1] = mt32asgs[data*2+1];
  755. data = mt32asgs[data*2];
  756. }
  757. else if (convert_type == XMIDI_CONVERT_MT32_TO_GS127)
  758. {
  759. CreateNewEvent (time);
  760. current->status = 0xB0 | (status&0xF);
  761. current->data[0] = 0;
  762. current->data[1] = 127;
  763. }
  764. }
  765. CreateNewEvent (time);
  766. current->status = status;
  767. current->data[0] = data;
  768. if (size == 1)
  769. return 1;
  770. current->data[1] = source->read1();
  771. // Volume modify the note on's, only if converting
  772. if (convert_type && (current->status >> 4) == MIDI_STATUS_NOTE_ON && current->data[1])
  773. current->data[1] = VolumeCurve[current->data[1]];
  774. if (size == 2)
  775. return 2;
  776. // XMI Note On handling
  777. int i = GetVLQ (source, delta);
  778. CreateNewEvent (time+delta*3);
  779. current->status = status;
  780. current->data[0] = data;
  781. current->data[1] = 0;
  782. return i + 2;
  783. }
  784. // Simple routine to convert system messages
  785. int XMIDI::ConvertSystemMessage (const int time, const unsigned char status, DataSource *source)
  786. {
  787. int i=0;
  788. CreateNewEvent (time);
  789. current->status = status;
  790. // Handling of Meta events
  791. if (status == 0xFF)
  792. {
  793. current->data[0] = source->read1();
  794. i++;
  795. }
  796. i += GetVLQ (source, current->len);
  797. if (!current->len) return i;
  798. current->buffer = new unsigned char[current->len];
  799. source->read ((char *) current->buffer, current->len);
  800. return i+current->len;
  801. }
  802. // XMIDI and Midi to List
  803. // Returns XMIDI PPQN
  804. int XMIDI::ConvertFiletoList (DataSource *source, const bool is_xmi)
  805. {
  806. int time = 0;
  807. uint32 data;
  808. int end = 0;
  809. int tempo = 500000;
  810. int tempo_set = 0;
  811. uint32 status = 0;
  812. int play_size = 2;
  813. int file_size = source->getSize();
  814. if (is_xmi) play_size = 3;
  815. // Set Drum track to correct setting if required
  816. if (convert_type == XMIDI_CONVERT_MT32_TO_GS127)
  817. {
  818. CreateNewEvent (0);
  819. current->status = 0xB9;
  820. current->data[0] = 0;
  821. current->data[1] = 127;
  822. }
  823. while (!end && source->getPos() < file_size)
  824. {
  825. if (!is_xmi)
  826. {
  827. GetVLQ (source, data);
  828. time += data;
  829. data = source->read1();
  830. if (data >= 0x80)
  831. {
  832. status = data;
  833. }
  834. else
  835. source->skip (-1);
  836. }
  837. else
  838. {
  839. GetVLQ2 (source, data);
  840. time += data*3;
  841. status = source->read1();
  842. }
  843. switch (status >> 4)
  844. {
  845. case MIDI_STATUS_NOTE_ON:
  846. ConvertEvent (time, status, source, play_size);
  847. break;
  848. // 2 byte data
  849. case MIDI_STATUS_NOTE_OFF:
  850. case MIDI_STATUS_AFTERTOUCH:
  851. case MIDI_STATUS_CONTROLLER:
  852. case MIDI_STATUS_PITCH_WHEEL:
  853. ConvertEvent (time, status, source, 2);
  854. break;
  855. // 1 byte data
  856. case MIDI_STATUS_PROG_CHANGE:
  857. case MIDI_STATUS_PRESSURE:
  858. ConvertEvent (time, status, source, 1);
  859. break;
  860. case MIDI_STATUS_SYSEX:
  861. if (status == 0xFF)
  862. {
  863. int pos = source->getPos();
  864. uint32 data = source->read1();
  865. if (data == 0x2F) // End
  866. end = 1;
  867. else if (data == 0x51 && !tempo_set) // Tempo. Need it for PPQN
  868. {
  869. source->skip(1);
  870. tempo = source->read1() << 16;
  871. tempo += source->read1() << 8;
  872. tempo += source->read1();
  873. tempo *= 3;
  874. tempo_set = 1;
  875. }
  876. else if (data == 0x51 && tempo_set && is_xmi) // Skip any other tempo changes
  877. {
  878. GetVLQ (source, data);
  879. source->skip(data);
  880. break;
  881. }
  882. source->seek (pos);
  883. }
  884. ConvertSystemMessage (time, status, source);
  885. break;
  886. default:
  887. break;
  888. }
  889. }
  890. return (tempo*3)/25000;
  891. }
  892. // Converts and event list to a MTrk
  893. // Returns bytes of the array
  894. // buf can be NULL
  895. uint32 XMIDI::ConvertListToMTrk (DataSource *dest, midi_event *mlist)
  896. {
  897. int time = 0;
  898. midi_event *event;
  899. uint32 delta;
  900. unsigned char last_status = 0;
  901. uint32 i = 8;
  902. uint32 j;
  903. uint32 size_pos=0;
  904. bool end = false;
  905. if (dest)
  906. {
  907. dest->write1('M');
  908. dest->write1('T');
  909. dest->write1('r');
  910. dest->write1('k');
  911. size_pos = dest->getPos();
  912. dest->skip(4);
  913. }
  914. for (event = mlist; event && !end; event=event->next)
  915. {
  916. delta = (event->time - time);
  917. time = event->time;
  918. i += PutVLQ (dest, delta);
  919. if ((event->status != last_status) || (event->status >= 0xF0))
  920. {
  921. if (dest) dest->write1(event->status);
  922. i++;
  923. }
  924. last_status = event->status;
  925. switch (event->status >> 4)
  926. {
  927. // 2 bytes data
  928. // Note off, Note on, Aftertouch, Controller and Pitch Wheel
  929. case 0x8: case 0x9: case 0xA: case 0xB: case 0xE:
  930. if (dest)
  931. {
  932. dest->write1(event->data[0]);
  933. dest->write1(event->data[1]);
  934. }
  935. i += 2;
  936. break;
  937. // 1 bytes data
  938. // Program Change and Channel Pressure
  939. case 0xC: case 0xD:
  940. if (dest) dest->write1(event->data[0]);
  941. i++;
  942. break;
  943. // Variable length
  944. // SysEx
  945. case 0xF:
  946. if (event->status == 0xFF)
  947. {
  948. if (event->data[0] == 0x2f) end = true;
  949. if (dest) dest->write1(event->data[0]);
  950. i++;
  951. }
  952. i += PutVLQ (dest, event->len);
  953. if (event->len)
  954. {
  955. for (j = 0; j < event->len; j++)
  956. {
  957. if (dest) dest->write1(event->buffer[j]);
  958. i++;
  959. }
  960. }
  961. break;
  962. // Never occur
  963. default:
  964. cerr << "Not supposed to see this" << endl;
  965. break;
  966. }
  967. }
  968. if (dest)
  969. {
  970. int cur_pos = dest->getPos();
  971. dest->seek (size_pos);
  972. dest->write4high (i-8);
  973. dest->seek (cur_pos);
  974. }
  975. return i;
  976. }
  977. // Assumes correct xmidi
  978. int XMIDI::ExtractTracksFromXmi (DataSource *source)
  979. {
  980. int num = 0;
  981. signed short ppqn;
  982. uint32 len = 0;
  983. char buf[32];
  984. while (source->getPos() < source->getSize() && num != info.tracks)
  985. {
  986. // Read first 4 bytes of name
  987. source->read (buf, 4);
  988. len = source->read4high();
  989. // Skip the FORM entries
  990. if (!memcmp(buf,"FORM",4))
  991. {
  992. source->skip (4);
  993. source->read (buf, 4);
  994. len = source->read4high();
  995. }
  996. if (memcmp(buf,"EVNT",4))
  997. {
  998. source->skip ((len+1)&~1);
  999. continue;
  1000. }
  1001. list = NULL;
  1002. int begin = source->getPos ();
  1003. // Convert it
  1004. if (!(ppqn = ConvertFiletoList (source, true)))
  1005. {
  1006. cerr << "Unable to convert data" << endl;
  1007. break;
  1008. }
  1009. timing[num] = ppqn;
  1010. events[num] = list;
  1011. // Increment Counter
  1012. num++;
  1013. // go to start of next track
  1014. source->seek (begin + ((len+1)&~1));
  1015. }
  1016. // Return how many were converted
  1017. return num;
  1018. }
  1019. int XMIDI::ExtractTracksFromMid (DataSource *source)
  1020. {
  1021. int num = 0;
  1022. uint32 len = 0;
  1023. char buf[32];
  1024. while (source->getPos() < source->getSize() && num != info.tracks)
  1025. {
  1026. // Read first 4 bytes of name
  1027. source->read (buf, 4);
  1028. len = source->read4high();
  1029. if (memcmp(buf,"MTrk",4))
  1030. {
  1031. source->skip (len);
  1032. continue;
  1033. }
  1034. list = NULL;
  1035. int begin = source->getPos ();
  1036. // Convert it
  1037. if (!ConvertFiletoList (source, false))
  1038. {
  1039. cerr << "Unable to convert data" << endl;
  1040. break;
  1041. }
  1042. events[num] = list;
  1043. // Increment Counter
  1044. num++;
  1045. source->seek (begin+len);
  1046. }
  1047. // Return how many were converted
  1048. return num;
  1049. }
  1050. int XMIDI::ExtractTracks (DataSource *source)
  1051. {
  1052. uint32 i = 0;
  1053. int start;
  1054. uint32 len;
  1055. uint32 chunk_len;
  1056. int count;
  1057. char buf[32];
  1058. string s;
  1059. config->value("config/audio/midi/reverb/enabled",s,"no");
  1060. if (s == "yes") do_reverb = true;
  1061. config->set("config/audio/midi/reverb/enabled",s,true);
  1062. config->value("config/audio/midi/reverb/level",s,"---");
  1063. if (s == "---") config->value("config/audio/midi/reverb",s,"64");
  1064. reverb_value = atoi(s.c_str());
  1065. if (reverb_value > 127) reverb_value = 127;
  1066. else if (reverb_value < 0) reverb_value = 0;
  1067. config->set("config/audio/midi/reverb/level",reverb_value,true);
  1068. config->value("config/audio/midi/chorus/enabled",s,"no");
  1069. if (s == "yes") do_chorus = true;
  1070. config->set("config/audio/midi/chorus/enabled",s,true);
  1071. config->value("config/audio/midi/chorus/level",s,"---");
  1072. if (s == "---") config->value("config/audio/midi/chorus",s,"16");
  1073. chorus_value = atoi(s.c_str());
  1074. if (chorus_value > 127) chorus_value = 127;
  1075. else if (chorus_value < 0) chorus_value = 0;
  1076. config->set("config/audio/midi/chorus/level",chorus_value,true);
  1077. config->value("config/audio/midi/volume_curve",s,"---");
  1078. if (s == "---") config->value("config/audio/midi/gamma",s,"1");
  1079. VolumeCurve.set_gamma (atof(s.c_str()));
  1080. snprintf (buf, 32, "%f", VolumeCurve.get_gamma ());
  1081. config->set("config/audio/midi/volume_curve",buf,true);
  1082. // Read first 4 bytes of header
  1083. source->read (buf, 4);
  1084. // Could be XMIDI
  1085. if (!memcmp (buf, "FORM", 4))
  1086. {
  1087. // Read length of
  1088. len = source->read4high();
  1089. start = source->getPos();
  1090. // Read 4 bytes of type
  1091. source->read (buf, 4);
  1092. // XDIRless XMIDI, we can handle them here.
  1093. if (!memcmp (buf, "XMID", 4))
  1094. {
  1095. cerr << "Warning: XMIDI doesn't have XDIR" << endl;
  1096. info.tracks = 1;
  1097. } // Not an XMIDI that we recognise
  1098. else if (memcmp (buf, "XDIR", 4))
  1099. {
  1100. cerr << "Not a recognised XMID" << endl;
  1101. return 0;
  1102. } // Seems Valid
  1103. else
  1104. {
  1105. info.tracks = 0;
  1106. for (i = 4; i < len; i++)
  1107. {
  1108. // Read 4 bytes of type
  1109. source->read (buf, 4);
  1110. // Read length of chunk
  1111. chunk_len = source->read4high();
  1112. // Add eight bytes
  1113. i+=8;
  1114. if (memcmp (buf, "INFO", 4))
  1115. {
  1116. // Must allign
  1117. source->skip((chunk_len+1)&~1);
  1118. i+= (chunk_len+1)&~1;
  1119. continue;
  1120. }
  1121. // Must be at least 2 bytes long
  1122. if (chunk_len < 2)
  1123. break;
  1124. info.tracks = source->read2();
  1125. break;
  1126. }
  1127. // Didn't get to fill the header
  1128. if (info.tracks == 0)
  1129. {
  1130. cerr << "Not a valid XMID" << endl;
  1131. return 0;
  1132. }
  1133. // Ok now to start part 2
  1134. // Goto the right place
  1135. source->seek (start+((len+1)&~1));
  1136. // Read 4 bytes of type
  1137. source->read (buf, 4);
  1138. // Not an XMID
  1139. if (memcmp (buf, "CAT ", 4))
  1140. {
  1141. cerr << "Not a recognised XMID (" << buf[0] << buf[1] << buf[2] << buf[3] << ") should be (CAT )" << endl;
  1142. return 0;
  1143. }
  1144. // Now read length of this track
  1145. len = source->read4high();
  1146. // Read 4 bytes of type
  1147. source->read (buf, 4);
  1148. // Not an XMID
  1149. if (memcmp (buf, "XMID", 4))
  1150. {
  1151. cerr << "Not a recognised XMID (" << buf[0] << buf[1] << buf[2] << buf[3] << ") should be (XMID)" << endl;
  1152. return 0;
  1153. }
  1154. }
  1155. // Ok it's an XMID, so pass it to the ExtractCode
  1156. events = new midi_event *[info.tracks];
  1157. timing = new short[info.tracks];
  1158. fixed = new bool[info.tracks];
  1159. info.type = 0;
  1160. for (i = 0; i < info.tracks; i++)
  1161. {
  1162. events[i] = NULL;
  1163. fixed[i] = false;
  1164. }
  1165. count = ExtractTracksFromXmi (source);
  1166. if (count != info.tracks)
  1167. {
  1168. cerr << "Error: unable to extract all (" << info.tracks << ") tracks specified from XMIDI. Only ("<< count << ")" << endl;
  1169. int i = 0;
  1170. for (i = 0; i < info.tracks; i++)
  1171. DeleteEventList (events[i]);
  1172. delete [] events;
  1173. delete [] timing;
  1174. return 0;
  1175. }
  1176. return 1;
  1177. }// Definately a Midi
  1178. else if (!memcmp (buf, "MThd", 4))
  1179. {
  1180. // Simple read length of header
  1181. len = source->read4high();
  1182. if (len < 6)
  1183. {
  1184. cerr << "Not a valid MIDI" << endl;
  1185. return 0;
  1186. }
  1187. info.type = source->read2high();
  1188. info.tracks = source->read2high();
  1189. events = new midi_event *[info.tracks];
  1190. timing = new short[info.tracks];
  1191. fixed = new bool[info.tracks];
  1192. timing[0] = source->read2high();
  1193. for (i = 0; i < info.tracks; i++)
  1194. {
  1195. timing[i] = timing[0];
  1196. events[i] = NULL;
  1197. fixed[i] = false;
  1198. }
  1199. count = ExtractTracksFromMid (source);
  1200. if (count != info.tracks)
  1201. {
  1202. cerr << "Error: unable to extract all (" << info.tracks << ") tracks specified from MIDI. Only ("<< count << ")" << endl;
  1203. for (i = 0; i < info.tracks; i++)
  1204. DeleteEventList (events[i]);
  1205. delete [] events;
  1206. delete [] timing;
  1207. return 0;
  1208. }
  1209. return 1;
  1210. }// A RIFF Midi, just pass the source back to this function at the start of the midi file
  1211. else if (!memcmp (buf, "RIFF", 4))
  1212. {
  1213. // Read len
  1214. len = source->read4();
  1215. // Read 4 bytes of type
  1216. source->read (buf, 4);
  1217. // Not an RMID
  1218. if (memcmp (buf, "RMID", 4))
  1219. {
  1220. cerr << "Invalid RMID" << endl;
  1221. return 0;
  1222. }
  1223. // Is a RMID
  1224. for (i = 4; i < len; i++)
  1225. {
  1226. // Read 4 bytes of type
  1227. source->read (buf, 4);
  1228. chunk_len = source->read4();
  1229. i+=8;
  1230. if (memcmp (buf, "data", 4))
  1231. {
  1232. // Must allign
  1233. source->skip ((chunk_len+1)&~1);
  1234. i+= (chunk_len+1)&~1;
  1235. continue;
  1236. }
  1237. return ExtractTracks (source);
  1238. }
  1239. cerr << "Failed to find midi data in RIFF Midi" << endl;
  1240. return 0;
  1241. }
  1242. return 0;
  1243. }