PageRenderTime 60ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/exult/tags/Release0_94Alpha5/audio/xmidi.cc

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