PageRenderTime 88ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/exult/tags/Release0_91Alpha2/audio/xmidi.cc

#
C++ | 1475 lines | 1118 code | 241 blank | 116 comment | 242 complexity | bc2bf9cb30acd601fd8e9b5769f13e25 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::MidiGamma(128);
  313. // Constructor
  314. XMIDI::XMIDI(DataSource *source, int pconvert) : events(NULL),timing(NULL),
  315. convert_type(pconvert),fixed(NULL)
  316. {
  317. int i = 16;
  318. while (i--) bank127[i] = 0;
  319. ExtractTracks (source);
  320. }
  321. XMIDI::~XMIDI()
  322. {
  323. if (events)
  324. {
  325. for (int i=0; i < info.tracks; i++)
  326. DeleteEventList (events[i]);
  327. delete [] events;
  328. }
  329. if (timing) delete [] timing;
  330. if (fixed) delete [] fixed;
  331. }
  332. int XMIDI::retrieve (uint32 track, DataSource *dest)
  333. {
  334. int len = 0;
  335. if (!events)
  336. {
  337. cerr << "No midi data in loaded." << endl;
  338. return 0;
  339. }
  340. // Convert type 1 midi's to type 0
  341. if (info.type == 1)
  342. {
  343. DuplicateAndMerge(-1);
  344. for (int i=0; i < info.tracks; i++)
  345. DeleteEventList (events[i]);
  346. delete [] events;
  347. events = new midi_event *[1];
  348. events[0] = list;
  349. info.tracks = 1;
  350. info.type = 0;
  351. }
  352. if (track >= info.tracks)
  353. {
  354. cerr << "Can't retrieve MIDI data, track out of range" << endl;
  355. return 0;
  356. }
  357. // And fix the midis if they are broken
  358. if (!fixed[track])
  359. {
  360. list = events[track];
  361. MovePatchVolAndPan ();
  362. fixed[track] = true;
  363. events[track] = list;
  364. }
  365. // This is so if using buffer datasource, the caller can know how big to make the buffer
  366. if (!dest)
  367. {
  368. // Header is 14 bytes long and add the rest as well
  369. len = ConvertListToMTrk (NULL, events[track]);
  370. return 14 + len;
  371. }
  372. dest->write1 ('M');
  373. dest->write1 ('T');
  374. dest->write1 ('h');
  375. dest->write1 ('d');
  376. dest->write4high (6);
  377. dest->write2high (0);
  378. dest->write2high (1);
  379. dest->write2high (timing[track]);
  380. len = ConvertListToMTrk (dest, events[track]);
  381. return len + 14;
  382. }
  383. int XMIDI::retrieve (uint32 track, midi_event **dest, int &ppqn)
  384. {
  385. if (!events)
  386. {
  387. cerr << "No midi data in loaded." << endl;
  388. return 0;
  389. }
  390. if ((info.type == 1 && track != 0) || (track >= info.tracks))
  391. {
  392. cerr << "Can't retrieve MIDI data, track out of range" << endl;
  393. return 0;
  394. }
  395. DuplicateAndMerge(track);
  396. MovePatchVolAndPan ();
  397. *dest = list;
  398. ppqn = timing[track];
  399. return 1;
  400. }
  401. void XMIDI::DeleteEventList (midi_event *mlist)
  402. {
  403. midi_event *event;
  404. midi_event *next;
  405. next = mlist;
  406. event = mlist;
  407. while ((event = next))
  408. {
  409. next = event->next;
  410. if (event->buffer) delete [] event->buffer;
  411. delete event;
  412. }
  413. }
  414. // Sets current to the new event and updates list
  415. void XMIDI::CreateNewEvent (int time)
  416. {
  417. if (!list)
  418. {
  419. list = current = new midi_event;
  420. current->next = NULL;
  421. if (time < 0)
  422. current->time = 0;
  423. else
  424. current->time = time;
  425. current->buffer = NULL;
  426. current->len = 0;
  427. return;
  428. }
  429. if (time < 0)
  430. {
  431. midi_event *event = new midi_event;
  432. event->next = list;
  433. list = current = event;
  434. current->time = 0;
  435. current->buffer = NULL;
  436. current->len = 0;
  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 = new midi_event;
  446. event->next = current->next;
  447. current->next = event;
  448. current = event;
  449. current->time = time;
  450. current->buffer = NULL;
  451. current->len = 0;
  452. return;
  453. }
  454. current = current->next;
  455. }
  456. current->next = new midi_event;
  457. current = current->next;
  458. current->next = NULL;
  459. current->time = time;
  460. current->buffer = NULL;
  461. current->len = 0;
  462. }
  463. // Conventional Variable Length Quantity
  464. int XMIDI::GetVLQ (DataSource *source, uint32 &quant)
  465. {
  466. int i;
  467. quant = 0;
  468. unsigned int data;
  469. for (i = 0; i < 4; i++)
  470. {
  471. data = source->read1();
  472. quant <<= 7;
  473. quant |= data & 0x7F;
  474. if (!(data & 0x80))
  475. {
  476. i++;
  477. break;
  478. }
  479. }
  480. return i;
  481. }
  482. // XMIDI Delta Variable Length Quantity
  483. int XMIDI::GetVLQ2 (DataSource *source, uint32 &quant)
  484. {
  485. int i;
  486. quant = 0;
  487. int data = 0;
  488. for (i = 0; i < 4; i++)
  489. {
  490. data = source->read1();
  491. if (data & 0x80)
  492. {
  493. source->skip(-1);
  494. break;
  495. }
  496. quant += data;
  497. }
  498. return i;
  499. }
  500. int XMIDI::PutVLQ(DataSource *dest, uint32 value)
  501. {
  502. int buffer;
  503. int i = 1;
  504. buffer = value & 0x7F;
  505. while (value >>= 7)
  506. {
  507. buffer <<= 8;
  508. buffer |= ((value & 0x7F) | 0x80);
  509. i++;
  510. }
  511. if (!dest) return i;
  512. for (int j = 0; j < i; j++)
  513. {
  514. dest->write1(buffer & 0xFF);
  515. buffer >>= 8;
  516. }
  517. return i;
  518. }
  519. // MovePatchVolAndPan
  520. //
  521. // This little function attempts to correct errors in midi files
  522. // that relate to patch, volume and pan changing
  523. void XMIDI::MovePatchVolAndPan (int channel)
  524. {
  525. if (channel == -1)
  526. {
  527. for (int i = 0; i < 16; i++)
  528. MovePatchVolAndPan (i);
  529. return;
  530. }
  531. midi_event *patch = NULL;
  532. midi_event *vol = NULL;
  533. midi_event *pan = NULL;
  534. midi_event *bank = NULL;
  535. midi_event *reverb = NULL;
  536. midi_event *chorus = NULL;
  537. midi_event *temp;
  538. for (current = list; current; )
  539. {
  540. if (!patch && (current->status >> 4) == 0xC && (current->status & 0xF) == channel)
  541. patch = current;
  542. else if (!vol && (current->status >> 4) == 0xB && current->data[0] == 7 && (current->status & 0xF) == channel)
  543. vol = current;
  544. else if (!pan && (current->status >> 4) == 0xB && current->data[0] == 10 && (current->status & 0xF) == channel)
  545. pan = current;
  546. else if (!bank && (current->status >> 4) == 0xB && current->data[0] == 0 && (current->status & 0xF) == channel)
  547. bank = current;
  548. if (pan && vol && patch) break;
  549. if (current) current = current->next;
  550. else current = list;
  551. }
  552. // Got no patch change, return and don't try fixing it
  553. if (!patch) return;
  554. // Copy Patch Change Event
  555. temp = patch;
  556. patch = new midi_event;
  557. patch->time = temp->time;
  558. patch->status = channel|(MIDI_STATUS_PROG_CHANGE << 4);
  559. patch->len = 0;
  560. patch->buffer = NULL;
  561. patch->data[0] = temp->data[0];
  562. // Copy Volume
  563. if (vol && (vol->time > patch->time+PATCH_VOL_PAN_BIAS || vol->time < patch->time-PATCH_VOL_PAN_BIAS))
  564. vol = NULL;
  565. temp = vol;
  566. vol = new midi_event;
  567. vol->status = channel|(MIDI_STATUS_CONTROLLER << 4);
  568. vol->data[0] = 7;
  569. vol->len = 0;
  570. vol->buffer = NULL;
  571. if (!temp)
  572. vol->data[1] = 64;
  573. else
  574. vol->data[1] = temp->data[1];
  575. // Copy Bank
  576. if (bank && (bank->time > patch->time+PATCH_VOL_PAN_BIAS || bank->time < patch->time-PATCH_VOL_PAN_BIAS))
  577. bank = NULL;
  578. temp = bank;
  579. bank = new midi_event;
  580. bank->status = channel|(MIDI_STATUS_CONTROLLER << 4);
  581. bank->data[0] = 0;
  582. bank->len = 0;
  583. bank->buffer = NULL;
  584. if (!temp)
  585. bank->data[1] = 0;
  586. else
  587. bank->data[1] = temp->data[1];
  588. // Copy Pan
  589. if (pan && (pan->time > patch->time+PATCH_VOL_PAN_BIAS || pan->time < patch->time-PATCH_VOL_PAN_BIAS))
  590. pan = NULL;
  591. temp = pan;
  592. pan = new midi_event;
  593. pan->status = channel|(MIDI_STATUS_CONTROLLER << 4);
  594. pan->data[0] = 10;
  595. pan->len = 0;
  596. pan->buffer = NULL;
  597. if (!temp)
  598. pan->data[1] = 64;
  599. else
  600. pan->data[1] = temp->data[1];
  601. reverb = new midi_event;
  602. reverb->time = 0;
  603. reverb->status = channel|(MIDI_STATUS_CONTROLLER << 4);
  604. reverb->len = 0;
  605. reverb->buffer = NULL;
  606. reverb->data[0] = 91;
  607. reverb->data[1] = reverb_value;
  608. chorus = new midi_event;
  609. chorus->time = 0;
  610. chorus->status = channel|(MIDI_STATUS_CONTROLLER << 4);
  611. chorus->len = 0;
  612. chorus->buffer = NULL;
  613. chorus->data[0] = 93;
  614. chorus->data[1] = chorus_value;
  615. vol->time = 0;
  616. pan->time = 0;
  617. patch->time = 0;
  618. bank->time = 0;
  619. reverb->next = chorus;
  620. chorus->next = bank;
  621. bank->next = vol;
  622. vol->next = pan;
  623. pan->next = patch;
  624. patch->next = list;
  625. list = reverb;
  626. }
  627. // DuplicateAndMerge
  628. void XMIDI::DuplicateAndMerge (int num)
  629. {
  630. int i;
  631. midi_event **track;
  632. int time = 0;
  633. int start = 0;
  634. int end = 1;
  635. if (info.type == 1)
  636. {
  637. start = 0;
  638. end = info.tracks;
  639. }
  640. else if (num >= 0 && num < info.tracks)
  641. {
  642. start += num;
  643. end += num;
  644. }
  645. track = new midi_event *[info.tracks];
  646. for (i = 0; i < info.tracks; i++) track[i] = events[i];
  647. current = list = NULL;
  648. while (1)
  649. {
  650. int lowest = 1 << 30;
  651. int selected = -1;
  652. int num_na = end-start;
  653. // Firstly we find the track with the lowest time
  654. // for it's current event
  655. for (i = start; i < end; i++)
  656. {
  657. if (!track[i])
  658. {
  659. num_na--;
  660. continue;
  661. }
  662. if (track[i]->time < lowest)
  663. {
  664. selected = i;
  665. lowest = track[i]->time;
  666. }
  667. }
  668. // This is just so I don't have to type [selected] all the time
  669. i = selected;
  670. // None left to convert
  671. if (!num_na) break;
  672. // Only need 1 end of track
  673. // So take the last one and ignore the rest;
  674. if ((num_na != 1) && (track[i]->status == 0xff) && (track[i]->data[0] == 0x2f))
  675. {
  676. track[i] = NULL;
  677. continue;
  678. }
  679. if (current)
  680. {
  681. current->next = new midi_event;
  682. current = current->next;
  683. }
  684. else
  685. list = current = new midi_event;
  686. current->next = NULL;
  687. time = track[i]->time;
  688. current->time = time;
  689. current->status = track[i]->status;
  690. current->data[0] = track[i]->data[0];
  691. current->data[1] = track[i]->data[1];
  692. current->len = track[i]->len;
  693. if (current->len)
  694. {
  695. current->buffer = new unsigned char[current->len];
  696. memcpy (current->buffer, track[i]->buffer, current->len);
  697. }
  698. else
  699. current->buffer = NULL;
  700. track[i] = track[i]->next;
  701. }
  702. }
  703. // Converts Events
  704. //
  705. // Source is at the first data byte
  706. // size 1 is single data byte
  707. // size 2 is dual data byte
  708. // size 3 is XMI Note on
  709. // Returns bytes converted
  710. int XMIDI::ConvertEvent (const int time, const unsigned char status, DataSource *source, const int size)
  711. {
  712. uint32 delta = 0;
  713. int data;
  714. data = source->read1();
  715. // Bank changes are handled here
  716. if ((status >> 4) == 0xB && data == 0)
  717. {
  718. data = source->read1();
  719. bank127[status&0xF] = false;
  720. if (convert_type == XMIDI_CONVERT_MT32_TO_GM || convert_type == XMIDI_CONVERT_MT32_TO_GS
  721. || convert_type == XMIDI_CONVERT_MT32_TO_GS127 ||
  722. (convert_type == XMIDI_CONVERT_MT32_TO_GS127DRUM && (status&0xF) == 9))
  723. return 2;
  724. CreateNewEvent (time);
  725. current->status = status;
  726. current->data[0] = 0;
  727. current->data[1] = data;
  728. if (convert_type == XMIDI_CONVERT_GS127_TO_GS && data == 127)
  729. bank127[status&0xF] = true;
  730. return 2;
  731. }
  732. // Handling for patch change mt32 conversion, probably should go elsewhere
  733. if ((status >> 4) == 0xC && (status&0xF) != 9 && convert_type != XMIDI_CONVERT_NOCONVERSION)
  734. {
  735. if (convert_type == XMIDI_CONVERT_MT32_TO_GM)
  736. {
  737. data = mt32asgm[data];
  738. }
  739. else if ((convert_type == XMIDI_CONVERT_GS127_TO_GS && bank127[status&0xF]) ||
  740. convert_type == XMIDI_CONVERT_MT32_TO_GS ||
  741. convert_type == XMIDI_CONVERT_MT32_TO_GS127DRUM)
  742. {
  743. CreateNewEvent (time);
  744. current->status = 0xB0 | (status&0xF);
  745. current->data[0] = 0;
  746. current->data[1] = mt32asgs[data*2+1];
  747. data = mt32asgs[data*2];
  748. }
  749. else if (convert_type == XMIDI_CONVERT_MT32_TO_GS127)
  750. {
  751. CreateNewEvent (time);
  752. current->status = 0xB0 | (status&0xF);
  753. current->data[0] = 0;
  754. current->data[1] = 127;
  755. }
  756. } // Drum track handling
  757. else if ((status >> 4) == 0xC && (status&0xF) == 9 &&
  758. (convert_type == XMIDI_CONVERT_MT32_TO_GS127DRUM || convert_type == XMIDI_CONVERT_MT32_TO_GS127))
  759. {
  760. CreateNewEvent (time);
  761. current->status = 0xB9;
  762. current->data[0] = 0;
  763. current->data[1] = 127;
  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] = MidiGamma[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",s,"64");
  1060. reverb_value = atoi(s.c_str());
  1061. if (reverb_value > 127) reverb_value = 127;
  1062. else if (reverb_value < 0) reverb_value = 0;
  1063. config->set("config/audio/midi/reverb",reverb_value,true);
  1064. config->value("config/audio/midi/chorus",s,"16");
  1065. chorus_value = atoi(s.c_str());
  1066. if (chorus_value > 127) reverb_value = 127;
  1067. else if (chorus_value < 0) reverb_value = 0;
  1068. config->set("config/audio/midi/chorus",chorus_value,true);
  1069. config->value("config/audio/midi/gamma",s,"1");
  1070. MidiGamma.set_gamma (atof(s.c_str()));
  1071. snprintf (buf, 32, "%f", MidiGamma.get_gamma ());
  1072. config->set("config/audio/midi/gamma",buf,true);
  1073. // Read first 4 bytes of header
  1074. source->read (buf, 4);
  1075. // Could be XMIDI
  1076. if (!memcmp (buf, "FORM", 4))
  1077. {
  1078. // Read length of
  1079. len = source->read4high();
  1080. start = source->getPos();
  1081. // Read 4 bytes of type
  1082. source->read (buf, 4);
  1083. // XDIRless XMIDI, we can handle them here.
  1084. if (!memcmp (buf, "XMID", 4))
  1085. {
  1086. cerr << "Warning: XMIDI doesn't have XDIR" << endl;
  1087. info.tracks = 1;
  1088. } // Not an XMIDI that we recognise
  1089. else if (memcmp (buf, "XDIR", 4))
  1090. {
  1091. cerr << "Not a recognised XMID" << endl;
  1092. return 0;
  1093. } // Seems Valid
  1094. else
  1095. {
  1096. info.tracks = 0;
  1097. for (i = 4; i < len; i++)
  1098. {
  1099. // Read 4 bytes of type
  1100. source->read (buf, 4);
  1101. // Read length of chunk
  1102. chunk_len = source->read4high();
  1103. // Add eight bytes
  1104. i+=8;
  1105. if (memcmp (buf, "INFO", 4))
  1106. {
  1107. // Must allign
  1108. source->skip((chunk_len+1)&~1);
  1109. i+= (chunk_len+1)&~1;
  1110. continue;
  1111. }
  1112. // Must be at least 2 bytes long
  1113. if (chunk_len < 2)
  1114. break;
  1115. info.tracks = source->read2();
  1116. break;
  1117. }
  1118. // Didn't get to fill the header
  1119. if (info.tracks == 0)
  1120. {
  1121. cerr << "Not a valid XMID" << endl;
  1122. return 0;
  1123. }
  1124. // Ok now to start part 2
  1125. // Goto the right place
  1126. source->seek (start+((len+1)&~1));
  1127. // Read 4 bytes of type
  1128. source->read (buf, 4);
  1129. // Not an XMID
  1130. if (memcmp (buf, "CAT ", 4))
  1131. {
  1132. cerr << "Not a recognised XMID (" << buf[0] << buf[1] << buf[2] << buf[3] << ") should be (CAT )" << endl;
  1133. return 0;
  1134. }
  1135. // Now read length of this track
  1136. len = source->read4high();
  1137. // Read 4 bytes of type
  1138. source->read (buf, 4);
  1139. // Not an XMID
  1140. if (memcmp (buf, "XMID", 4))
  1141. {
  1142. cerr << "Not a recognised XMID (" << buf[0] << buf[1] << buf[2] << buf[3] << ") should be (XMID)" << endl;
  1143. return 0;
  1144. }
  1145. }
  1146. // Ok it's an XMID, so pass it to the ExtractCode
  1147. events = new midi_event *[info.tracks];
  1148. timing = new short[info.tracks];
  1149. fixed = new bool[info.tracks];
  1150. info.type = 0;
  1151. for (i = 0; i < info.tracks; i++)
  1152. {
  1153. events[i] = NULL;
  1154. fixed[i] = false;
  1155. }
  1156. count = ExtractTracksFromXmi (source);
  1157. if (count != info.tracks)
  1158. {
  1159. cerr << "Error: unable to extract all (" << info.tracks << ") tracks specified from XMIDI. Only ("<< count << ")" << endl;
  1160. int i = 0;
  1161. for (i = 0; i < info.tracks; i++)
  1162. DeleteEventList (events[i]);
  1163. delete [] 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 = 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. delete [] timing;
  1198. return 0;
  1199. }
  1200. return 1;
  1201. }// A RIFF Midi, just pass the source back to this function at the start of the midi file
  1202. else if (!memcmp (buf, "RIFF", 4))
  1203. {
  1204. // Read len
  1205. len = source->read4();
  1206. // Read 4 bytes of type
  1207. source->read (buf, 4);
  1208. // Not an RMID
  1209. if (memcmp (buf, "RMID", 4))
  1210. {
  1211. cerr << "Invalid RMID" << endl;
  1212. return 0;
  1213. }
  1214. // Is a RMID
  1215. for (i = 4; i < len; i++)
  1216. {
  1217. // Read 4 bytes of type
  1218. source->read (buf, 4);
  1219. chunk_len = source->read4();
  1220. i+=8;
  1221. if (memcmp (buf, "data", 4))
  1222. {
  1223. // Must allign
  1224. source->skip ((chunk_len+1)&~1);
  1225. i+= (chunk_len+1)&~1;
  1226. continue;
  1227. }
  1228. return ExtractTracks (source);
  1229. }
  1230. cerr << "Failed to find midi data in RIFF Midi" << endl;
  1231. return 0;
  1232. }
  1233. return 0;
  1234. }