PageRenderTime 63ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/exult/tags/Release0_93Alpha4/audio/xmidi.cc

#
C++ | 1508 lines | 1138 code | 245 blank | 125 comment | 262 complexity | d804ceb35fddff19195c093ea9356896 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::free;
  37. using std::malloc;
  38. using std::memcmp;
  39. using std::memcpy;
  40. using std::snprintf;
  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. int i = 16;
  321. while (i--) bank127[i] = 0;
  322. ExtractTracks (source);
  323. }
  324. XMIDI::~XMIDI()
  325. {
  326. if (events)
  327. {
  328. for (int i=0; i < info.tracks; i++)
  329. DeleteEventList (events[i]);
  330. //delete [] events;
  331. free (events);
  332. }
  333. if (timing) delete [] timing;
  334. if (fixed) delete [] fixed;
  335. }
  336. int XMIDI::retrieve (uint32 track, DataSource *dest)
  337. {
  338. int len = 0;
  339. if (!events)
  340. {
  341. cerr << "No midi data in loaded." << endl;
  342. return 0;
  343. }
  344. // Convert type 1 midi's to type 0
  345. if (info.type == 1)
  346. {
  347. DuplicateAndMerge(-1);
  348. for (int i=0; i < info.tracks; i++)
  349. DeleteEventList (events[i]);
  350. //delete [] events;
  351. free (events);
  352. //events = new midi_event *[1];
  353. events = (midi_event **) malloc (sizeof (midi_event *));
  354. events[0] = list;
  355. info.tracks = 1;
  356. info.type = 0;
  357. }
  358. if (track >= info.tracks)
  359. {
  360. cerr << "Can't retrieve MIDI data, track out of range" << endl;
  361. return 0;
  362. }
  363. // And fix the midis if they are broken
  364. if (!fixed[track])
  365. {
  366. list = events[track];
  367. MovePatchVolAndPan ();
  368. fixed[track] = true;
  369. events[track] = list;
  370. }
  371. // This is so if using buffer datasource, the caller can know how big to make the buffer
  372. if (!dest)
  373. {
  374. // Header is 14 bytes long and add the rest as well
  375. len = ConvertListToMTrk (NULL, events[track]);
  376. return 14 + len;
  377. }
  378. dest->write1 ('M');
  379. dest->write1 ('T');
  380. dest->write1 ('h');
  381. dest->write1 ('d');
  382. dest->write4high (6);
  383. dest->write2high (0);
  384. dest->write2high (1);
  385. dest->write2high (timing[track]);
  386. len = ConvertListToMTrk (dest, events[track]);
  387. return len + 14;
  388. }
  389. int XMIDI::retrieve (uint32 track, midi_event **dest, int &ppqn)
  390. {
  391. if (!events)
  392. {
  393. cerr << "No midi data in loaded." << endl;
  394. return 0;
  395. }
  396. if ((info.type == 1 && track != 0) || (track >= info.tracks))
  397. {
  398. cerr << "Can't retrieve MIDI data, track out of range" << endl;
  399. return 0;
  400. }
  401. DuplicateAndMerge(track);
  402. MovePatchVolAndPan ();
  403. *dest = list;
  404. ppqn = timing[track];
  405. return 1;
  406. }
  407. void XMIDI::DeleteEventList (midi_event *mlist)
  408. {
  409. midi_event *event;
  410. midi_event *next;
  411. next = mlist;
  412. event = mlist;
  413. while ((event = next))
  414. {
  415. next = event->next;
  416. if (event->buffer)
  417. //delete [] event->buffer;
  418. free (event->buffer);
  419. //delete event;
  420. free (event);
  421. }
  422. }
  423. // Sets current to the new event and updates list
  424. void XMIDI::CreateNewEvent (int time)
  425. {
  426. if (!list)
  427. {
  428. list = current = (midi_event *) malloc (sizeof (midi_event)); //new midi_event;
  429. current->next = NULL;
  430. if (time < 0)
  431. current->time = 0;
  432. else
  433. current->time = time;
  434. current->buffer = NULL;
  435. current->len = 0;
  436. return;
  437. }
  438. if (time < 0)
  439. {
  440. midi_event *event = (midi_event *) malloc (sizeof (midi_event)); //new midi_event;
  441. event->next = list;
  442. list = current = event;
  443. current->time = 0;
  444. current->buffer = NULL;
  445. current->len = 0;
  446. return;
  447. }
  448. if (current->time > time)
  449. current = list;
  450. while (current->next)
  451. {
  452. if (current->next->time > time)
  453. {
  454. midi_event *event = (midi_event *) malloc (sizeof (midi_event)); //new midi_event;
  455. event->next = current->next;
  456. current->next = event;
  457. current = event;
  458. current->time = time;
  459. current->buffer = NULL;
  460. current->len = 0;
  461. return;
  462. }
  463. current = current->next;
  464. }
  465. current->next = (midi_event *) malloc (sizeof (midi_event)); //new midi_event;
  466. current = current->next;
  467. current->next = NULL;
  468. current->time = time;
  469. current->buffer = NULL;
  470. current->len = 0;
  471. }
  472. // Conventional Variable Length Quantity
  473. int XMIDI::GetVLQ (DataSource *source, uint32 &quant)
  474. {
  475. int i;
  476. quant = 0;
  477. unsigned int data;
  478. for (i = 0; i < 4; i++)
  479. {
  480. data = source->read1();
  481. quant <<= 7;
  482. quant |= data & 0x7F;
  483. if (!(data & 0x80))
  484. {
  485. i++;
  486. break;
  487. }
  488. }
  489. return i;
  490. }
  491. // XMIDI Delta Variable Length Quantity
  492. int XMIDI::GetVLQ2 (DataSource *source, uint32 &quant)
  493. {
  494. int i;
  495. quant = 0;
  496. int data = 0;
  497. for (i = 0; i < 4; i++)
  498. {
  499. data = source->read1();
  500. if (data & 0x80)
  501. {
  502. source->skip(-1);
  503. break;
  504. }
  505. quant += data;
  506. }
  507. return i;
  508. }
  509. int XMIDI::PutVLQ(DataSource *dest, uint32 value)
  510. {
  511. int buffer;
  512. int i = 1;
  513. buffer = value & 0x7F;
  514. while (value >>= 7)
  515. {
  516. buffer <<= 8;
  517. buffer |= ((value & 0x7F) | 0x80);
  518. i++;
  519. }
  520. if (!dest) return i;
  521. for (int j = 0; j < i; j++)
  522. {
  523. dest->write1(buffer & 0xFF);
  524. buffer >>= 8;
  525. }
  526. return i;
  527. }
  528. // MovePatchVolAndPan
  529. //
  530. // This little function attempts to correct errors in midi files
  531. // that relate to patch, volume and pan changing
  532. void XMIDI::MovePatchVolAndPan (int channel)
  533. {
  534. if (channel == -1)
  535. {
  536. for (int i = 0; i < 16; i++)
  537. MovePatchVolAndPan (i);
  538. return;
  539. }
  540. midi_event *patch = NULL;
  541. midi_event *vol = NULL;
  542. midi_event *pan = NULL;
  543. midi_event *bank = NULL;
  544. midi_event *reverb = NULL;
  545. midi_event *chorus = NULL;
  546. midi_event *temp;
  547. for (current = list; current; )
  548. {
  549. if (!patch && (current->status >> 4) == 0xC && (current->status & 0xF) == channel)
  550. patch = current;
  551. else if (!vol && (current->status >> 4) == 0xB && current->data[0] == 7 && (current->status & 0xF) == channel)
  552. vol = current;
  553. else if (!pan && (current->status >> 4) == 0xB && current->data[0] == 10 && (current->status & 0xF) == channel)
  554. pan = current;
  555. else if (!bank && (current->status >> 4) == 0xB && current->data[0] == 0 && (current->status & 0xF) == channel)
  556. bank = current;
  557. if (pan && vol && patch) break;
  558. if (current) current = current->next;
  559. else current = list;
  560. }
  561. // Got no patch change, return and don't try fixing it
  562. if (!patch) return;
  563. // Copy Patch Change Event
  564. temp = patch;
  565. patch = (midi_event *) malloc (sizeof (midi_event)); //new midi_event;
  566. patch->time = temp->time;
  567. patch->status = channel|(MIDI_STATUS_PROG_CHANGE << 4);
  568. patch->len = 0;
  569. patch->buffer = NULL;
  570. patch->data[0] = temp->data[0];
  571. // Copy Volume
  572. if (vol && (vol->time > patch->time+PATCH_VOL_PAN_BIAS || vol->time < patch->time-PATCH_VOL_PAN_BIAS))
  573. vol = NULL;
  574. temp = vol;
  575. vol = (midi_event *) malloc (sizeof (midi_event)); //new midi_event;
  576. vol->status = channel|(MIDI_STATUS_CONTROLLER << 4);
  577. vol->data[0] = 7;
  578. vol->len = 0;
  579. vol->buffer = NULL;
  580. if (!temp)
  581. {
  582. if (convert_type) vol->data[1] = VolumeCurve[64];
  583. else vol->data[1] = 64;
  584. }
  585. else
  586. vol->data[1] = temp->data[1];
  587. // Copy Bank
  588. if (bank && (bank->time > patch->time+PATCH_VOL_PAN_BIAS || bank->time < patch->time-PATCH_VOL_PAN_BIAS))
  589. bank = NULL;
  590. temp = bank;
  591. bank = (midi_event *) malloc (sizeof (midi_event)); //new midi_event;
  592. bank->status = channel|(MIDI_STATUS_CONTROLLER << 4);
  593. bank->data[0] = 0;
  594. bank->len = 0;
  595. bank->buffer = NULL;
  596. if (!temp)
  597. bank->data[1] = 0;
  598. else
  599. bank->data[1] = temp->data[1];
  600. // Copy Pan
  601. if (pan && (pan->time > patch->time+PATCH_VOL_PAN_BIAS || pan->time < patch->time-PATCH_VOL_PAN_BIAS))
  602. pan = NULL;
  603. temp = pan;
  604. pan = (midi_event *) malloc (sizeof (midi_event)); //new midi_event;
  605. pan->status = channel|(MIDI_STATUS_CONTROLLER << 4);
  606. pan->data[0] = 10;
  607. pan->len = 0;
  608. pan->buffer = NULL;
  609. if (!temp)
  610. pan->data[1] = 64;
  611. else
  612. pan->data[1] = temp->data[1];
  613. if (do_reverb)
  614. {
  615. reverb = (midi_event *) malloc (sizeof (midi_event)); //new midi_event;
  616. reverb->time = 0;
  617. reverb->status = channel|(MIDI_STATUS_CONTROLLER << 4);
  618. reverb->len = 0;
  619. reverb->buffer = NULL;
  620. reverb->data[0] = 91;
  621. reverb->data[1] = reverb_value;
  622. }
  623. if (do_chorus)
  624. {
  625. chorus = (midi_event *) malloc (sizeof (midi_event)); //new midi_event;
  626. chorus->time = 0;
  627. chorus->status = channel|(MIDI_STATUS_CONTROLLER << 4);
  628. chorus->len = 0;
  629. chorus->buffer = NULL;
  630. chorus->data[0] = 93;
  631. chorus->data[1] = chorus_value;
  632. }
  633. vol->time = 0;
  634. pan->time = 0;
  635. patch->time = 0;
  636. bank->time = 0;
  637. if (do_reverb && do_chorus) reverb->next = chorus;
  638. else if (do_reverb) reverb->next = bank;
  639. if (do_chorus) chorus->next = bank;
  640. bank->next = vol;
  641. vol->next = pan;
  642. pan->next = patch;
  643. patch->next = list;
  644. if (do_reverb) list = reverb;
  645. else if (do_chorus) list = chorus;
  646. else list = bank;
  647. }
  648. // DuplicateAndMerge
  649. void XMIDI::DuplicateAndMerge (int num)
  650. {
  651. int i;
  652. midi_event **track;
  653. int time = 0;
  654. int start = 0;
  655. int end = 1;
  656. if (info.type == 1)
  657. {
  658. start = 0;
  659. end = info.tracks;
  660. }
  661. else if (num >= 0 && num < info.tracks)
  662. {
  663. start += num;
  664. end += num;
  665. }
  666. track = (midi_event **) malloc (sizeof (midi_event*)*info.tracks); //new midi_event *[info.tracks];
  667. for (i = 0; i < info.tracks; i++) track[i] = events[i];
  668. current = list = NULL;
  669. while (1)
  670. {
  671. int lowest = 1 << 30;
  672. int selected = -1;
  673. int num_na = end-start;
  674. // Firstly we find the track with the lowest time
  675. // for it's current event
  676. for (i = start; i < end; i++)
  677. {
  678. if (!track[i])
  679. {
  680. num_na--;
  681. continue;
  682. }
  683. if (track[i]->time < lowest)
  684. {
  685. selected = i;
  686. lowest = track[i]->time;
  687. }
  688. }
  689. // This is just so I don't have to type [selected] all the time
  690. i = selected;
  691. // None left to convert
  692. if (!num_na) break;
  693. // Only need 1 end of track
  694. // So take the last one and ignore the rest;
  695. if ((num_na != 1) && (track[i]->status == 0xff) && (track[i]->data[0] == 0x2f))
  696. {
  697. track[i] = NULL;
  698. continue;
  699. }
  700. if (current)
  701. {
  702. current->next = (midi_event *) malloc (sizeof (midi_event)); //new midi_event;
  703. current = current->next;
  704. }
  705. else
  706. list = current = (midi_event *) malloc (sizeof (midi_event)); //new midi_event;
  707. current->next = NULL;
  708. time = track[i]->time;
  709. current->time = time;
  710. current->status = track[i]->status;
  711. current->data[0] = track[i]->data[0];
  712. current->data[1] = track[i]->data[1];
  713. current->len = track[i]->len;
  714. if (current->len)
  715. {
  716. current->buffer = (unsigned char *) malloc(current->len);
  717. memcpy (current->buffer, track[i]->buffer, current->len);
  718. }
  719. else
  720. current->buffer = NULL;
  721. track[i] = track[i]->next;
  722. }
  723. }
  724. // Converts Events
  725. //
  726. // Source is at the first data byte
  727. // size 1 is single data byte
  728. // size 2 is dual data byte
  729. // size 3 is XMI Note on
  730. // Returns bytes converted
  731. int XMIDI::ConvertEvent (const int time, const unsigned char status, DataSource *source, const int size)
  732. {
  733. uint32 delta = 0;
  734. int data;
  735. data = source->read1();
  736. // Bank changes are handled here
  737. if ((status >> 4) == 0xB && data == 0)
  738. {
  739. data = source->read1();
  740. bank127[status&0xF] = false;
  741. if (convert_type == XMIDI_CONVERT_MT32_TO_GM || convert_type == XMIDI_CONVERT_MT32_TO_GS
  742. || convert_type == XMIDI_CONVERT_MT32_TO_GS127)
  743. return 2;
  744. CreateNewEvent (time);
  745. current->status = status;
  746. current->data[0] = 0;
  747. current->data[1] = data;
  748. if (convert_type == XMIDI_CONVERT_GS127_TO_GS && data == 127)
  749. bank127[status&0xF] = true;
  750. return 2;
  751. }
  752. // Handling for patch change mt32 conversion, probably should go elsewhere
  753. if ((status >> 4) == 0xC && (status&0xF) != 9 && convert_type != XMIDI_CONVERT_NOCONVERSION)
  754. {
  755. if (convert_type == XMIDI_CONVERT_MT32_TO_GM)
  756. {
  757. data = mt32asgm[data];
  758. }
  759. else if ((convert_type == XMIDI_CONVERT_GS127_TO_GS && bank127[status&0xF]) ||
  760. convert_type == XMIDI_CONVERT_MT32_TO_GS)
  761. {
  762. CreateNewEvent (time);
  763. current->status = 0xB0 | (status&0xF);
  764. current->data[0] = 0;
  765. current->data[1] = mt32asgs[data*2+1];
  766. data = mt32asgs[data*2];
  767. }
  768. else if (convert_type == XMIDI_CONVERT_MT32_TO_GS127)
  769. {
  770. CreateNewEvent (time);
  771. current->status = 0xB0 | (status&0xF);
  772. current->data[0] = 0;
  773. current->data[1] = 127;
  774. }
  775. }// Disable patch changes on Track 10 is doing a conversion
  776. else if ((status >> 4) == 0xC && (status&0xF) == 9 && convert_type != XMIDI_CONVERT_NOCONVERSION)
  777. {
  778. return size;
  779. }
  780. CreateNewEvent (time);
  781. current->status = status;
  782. current->data[0] = data;
  783. if (size == 1)
  784. return 1;
  785. current->data[1] = source->read1();
  786. // Volume modify the note on's, only if converting
  787. if (convert_type && (current->status >> 4) == MIDI_STATUS_NOTE_ON && current->data[1])
  788. current->data[1] = VolumeCurve[current->data[1]];
  789. // Volume modify the volume controller, only if converting
  790. if (convert_type && (current->status >> 4) == MIDI_STATUS_CONTROLLER && current->data[0] == 7)
  791. current->data[1] = VolumeCurve[current->data[1]];
  792. if (size == 2)
  793. return 2;
  794. // XMI Note On handling
  795. // This is an optimization
  796. midi_event *prev = current;
  797. int i = GetVLQ (source, delta);
  798. CreateNewEvent (time+delta*3);
  799. current->status = status;
  800. current->data[0] = data;
  801. current->data[1] = 0;
  802. // Optimization
  803. current = prev;
  804. return i + 2;
  805. }
  806. // Simple routine to convert system messages
  807. int XMIDI::ConvertSystemMessage (const int time, const unsigned char status, DataSource *source)
  808. {
  809. int i=0;
  810. CreateNewEvent (time);
  811. current->status = status;
  812. // Handling of Meta events
  813. if (status == 0xFF)
  814. {
  815. current->data[0] = source->read1();
  816. i++;
  817. }
  818. i += GetVLQ (source, current->len);
  819. if (!current->len)
  820. {
  821. current->buffer = NULL;
  822. return i;
  823. }
  824. current->buffer = (unsigned char *) malloc(current->len);
  825. source->read ((char *) current->buffer, current->len);
  826. return i+current->len;
  827. }
  828. // XMIDI and Midi to List
  829. // Returns XMIDI PPQN
  830. int XMIDI::ConvertFiletoList (DataSource *source, const bool is_xmi)
  831. {
  832. int time = 0;
  833. uint32 data;
  834. int end = 0;
  835. int tempo = 500000;
  836. int tempo_set = 0;
  837. uint32 status = 0;
  838. int play_size = 2;
  839. int file_size = source->getSize();
  840. if (is_xmi) play_size = 3;
  841. while (!end && source->getPos() < file_size)
  842. {
  843. if (!is_xmi)
  844. {
  845. GetVLQ (source, data);
  846. time += data;
  847. data = source->read1();
  848. if (data >= 0x80)
  849. {
  850. status = data;
  851. }
  852. else
  853. source->skip (-1);
  854. }
  855. else
  856. {
  857. GetVLQ2 (source, data);
  858. time += data*3;
  859. status = source->read1();
  860. }
  861. switch (status >> 4)
  862. {
  863. case MIDI_STATUS_NOTE_ON:
  864. ConvertEvent (time, status, source, play_size);
  865. break;
  866. // 2 byte data
  867. case MIDI_STATUS_NOTE_OFF:
  868. case MIDI_STATUS_AFTERTOUCH:
  869. case MIDI_STATUS_CONTROLLER:
  870. case MIDI_STATUS_PITCH_WHEEL:
  871. ConvertEvent (time, status, source, 2);
  872. break;
  873. // 1 byte data
  874. case MIDI_STATUS_PROG_CHANGE:
  875. case MIDI_STATUS_PRESSURE:
  876. ConvertEvent (time, status, source, 1);
  877. break;
  878. case MIDI_STATUS_SYSEX:
  879. if (status == 0xFF)
  880. {
  881. int pos = source->getPos();
  882. uint32 data = source->read1();
  883. if (data == 0x2F) // End
  884. end = 1;
  885. else if (data == 0x51 && !tempo_set) // Tempo. Need it for PPQN
  886. {
  887. source->skip(1);
  888. tempo = source->read1() << 16;
  889. tempo += source->read1() << 8;
  890. tempo += source->read1();
  891. tempo *= 3;
  892. tempo_set = 1;
  893. }
  894. else if (data == 0x51 && tempo_set && is_xmi) // Skip any other tempo changes
  895. {
  896. GetVLQ (source, data);
  897. source->skip(data);
  898. break;
  899. }
  900. source->seek (pos);
  901. }
  902. ConvertSystemMessage (time, status, source);
  903. break;
  904. default:
  905. break;
  906. }
  907. }
  908. return (tempo*3)/25000;
  909. }
  910. // Converts and event list to a MTrk
  911. // Returns bytes of the array
  912. // buf can be NULL
  913. uint32 XMIDI::ConvertListToMTrk (DataSource *dest, midi_event *mlist)
  914. {
  915. int time = 0;
  916. midi_event *event;
  917. uint32 delta;
  918. unsigned char last_status = 0;
  919. uint32 i = 8;
  920. uint32 j;
  921. uint32 size_pos=0;
  922. bool end = false;
  923. if (dest)
  924. {
  925. dest->write1('M');
  926. dest->write1('T');
  927. dest->write1('r');
  928. dest->write1('k');
  929. size_pos = dest->getPos();
  930. dest->skip(4);
  931. }
  932. for (event = mlist; event && !end; event=event->next)
  933. {
  934. delta = (event->time - time);
  935. time = event->time;
  936. i += PutVLQ (dest, delta);
  937. if ((event->status != last_status) || (event->status >= 0xF0))
  938. {
  939. if (dest) dest->write1(event->status);
  940. i++;
  941. }
  942. last_status = event->status;
  943. switch (event->status >> 4)
  944. {
  945. // 2 bytes data
  946. // Note off, Note on, Aftertouch, Controller and Pitch Wheel
  947. case 0x8: case 0x9: case 0xA: case 0xB: case 0xE:
  948. if (dest)
  949. {
  950. dest->write1(event->data[0]);
  951. dest->write1(event->data[1]);
  952. }
  953. i += 2;
  954. break;
  955. // 1 bytes data
  956. // Program Change and Channel Pressure
  957. case 0xC: case 0xD:
  958. if (dest) dest->write1(event->data[0]);
  959. i++;
  960. break;
  961. // Variable length
  962. // SysEx
  963. case 0xF:
  964. if (event->status == 0xFF)
  965. {
  966. if (event->data[0] == 0x2f) end = true;
  967. if (dest) dest->write1(event->data[0]);
  968. i++;
  969. }
  970. i += PutVLQ (dest, event->len);
  971. if (event->len)
  972. {
  973. for (j = 0; j < event->len; j++)
  974. {
  975. if (dest) dest->write1(event->buffer[j]);
  976. i++;
  977. }
  978. }
  979. break;
  980. // Never occur
  981. default:
  982. cerr << "Not supposed to see this" << endl;
  983. break;
  984. }
  985. }
  986. if (dest)
  987. {
  988. int cur_pos = dest->getPos();
  989. dest->seek (size_pos);
  990. dest->write4high (i-8);
  991. dest->seek (cur_pos);
  992. }
  993. return i;
  994. }
  995. // Assumes correct xmidi
  996. int XMIDI::ExtractTracksFromXmi (DataSource *source)
  997. {
  998. int num = 0;
  999. signed short ppqn;
  1000. uint32 len = 0;
  1001. char buf[32];
  1002. while (source->getPos() < source->getSize() && num != info.tracks)
  1003. {
  1004. // Read first 4 bytes of name
  1005. source->read (buf, 4);
  1006. len = source->read4high();
  1007. // Skip the FORM entries
  1008. if (!memcmp(buf,"FORM",4))
  1009. {
  1010. source->skip (4);
  1011. source->read (buf, 4);
  1012. len = source->read4high();
  1013. }
  1014. if (memcmp(buf,"EVNT",4))
  1015. {
  1016. source->skip ((len+1)&~1);
  1017. continue;
  1018. }
  1019. list = NULL;
  1020. int begin = source->getPos ();
  1021. // Convert it
  1022. if (!(ppqn = ConvertFiletoList (source, true)))
  1023. {
  1024. cerr << "Unable to convert data" << endl;
  1025. break;
  1026. }
  1027. timing[num] = ppqn;
  1028. events[num] = list;
  1029. // Increment Counter
  1030. num++;
  1031. // go to start of next track
  1032. source->seek (begin + ((len+1)&~1));
  1033. }
  1034. // Return how many were converted
  1035. return num;
  1036. }
  1037. int XMIDI::ExtractTracksFromMid (DataSource *source)
  1038. {
  1039. int num = 0;
  1040. uint32 len = 0;
  1041. char buf[32];
  1042. while (source->getPos() < source->getSize() && num != info.tracks)
  1043. {
  1044. // Read first 4 bytes of name
  1045. source->read (buf, 4);
  1046. len = source->read4high();
  1047. if (memcmp(buf,"MTrk",4))
  1048. {
  1049. source->skip (len);
  1050. continue;
  1051. }
  1052. list = NULL;
  1053. int begin = source->getPos ();
  1054. // Convert it
  1055. if (!ConvertFiletoList (source, false))
  1056. {
  1057. cerr << "Unable to convert data" << endl;
  1058. break;
  1059. }
  1060. events[num] = list;
  1061. // Increment Counter
  1062. num++;
  1063. source->seek (begin+len);
  1064. }
  1065. // Return how many were converted
  1066. return num;
  1067. }
  1068. int XMIDI::ExtractTracks (DataSource *source)
  1069. {
  1070. uint32 i = 0;
  1071. int start;
  1072. uint32 len;
  1073. uint32 chunk_len;
  1074. int count;
  1075. char buf[32];
  1076. string s;
  1077. config->value("config/audio/midi/reverb/enabled",s,"no");
  1078. if (s == "yes") do_reverb = true;
  1079. config->set("config/audio/midi/reverb/enabled",s,true);
  1080. config->value("config/audio/midi/reverb/level",s,"---");
  1081. if (s == "---") config->value("config/audio/midi/reverb",s,"64");
  1082. reverb_value = atoi(s.c_str());
  1083. if (reverb_value > 127) reverb_value = 127;
  1084. else if (reverb_value < 0) reverb_value = 0;
  1085. config->set("config/audio/midi/reverb/level",reverb_value,true);
  1086. config->value("config/audio/midi/chorus/enabled",s,"no");
  1087. if (s == "yes") do_chorus = true;
  1088. config->set("config/audio/midi/chorus/enabled",s,true);
  1089. config->value("config/audio/midi/chorus/level",s,"---");
  1090. if (s == "---") config->value("config/audio/midi/chorus",s,"16");
  1091. chorus_value = atoi(s.c_str());
  1092. if (chorus_value > 127) chorus_value = 127;
  1093. else if (chorus_value < 0) chorus_value = 0;
  1094. config->set("config/audio/midi/chorus/level",chorus_value,true);
  1095. config->value("config/audio/midi/volume_curve",s,"---");
  1096. if (s == "---") config->value("config/audio/midi/gamma",s,"1");
  1097. VolumeCurve.set_gamma (atof(s.c_str()));
  1098. snprintf (buf, 32, "%f", VolumeCurve.get_gamma ());
  1099. config->set("config/audio/midi/volume_curve",buf,true);
  1100. // Read first 4 bytes of header
  1101. source->read (buf, 4);
  1102. // Could be XMIDI
  1103. if (!memcmp (buf, "FORM", 4))
  1104. {
  1105. // Read length of
  1106. len = source->read4high();
  1107. start = source->getPos();
  1108. // Read 4 bytes of type
  1109. source->read (buf, 4);
  1110. // XDIRless XMIDI, we can handle them here.
  1111. if (!memcmp (buf, "XMID", 4))
  1112. {
  1113. cerr << "Warning: XMIDI doesn't have XDIR" << endl;
  1114. info.tracks = 1;
  1115. } // Not an XMIDI that we recognise
  1116. else if (memcmp (buf, "XDIR", 4))
  1117. {
  1118. cerr << "Not a recognised XMID" << endl;
  1119. return 0;
  1120. } // Seems Valid
  1121. else
  1122. {
  1123. info.tracks = 0;
  1124. for (i = 4; i < len; i++)
  1125. {
  1126. // Read 4 bytes of type
  1127. source->read (buf, 4);
  1128. // Read length of chunk
  1129. chunk_len = source->read4high();
  1130. // Add eight bytes
  1131. i+=8;
  1132. if (memcmp (buf, "INFO", 4))
  1133. {
  1134. // Must allign
  1135. source->skip((chunk_len+1)&~1);
  1136. i+= (chunk_len+1)&~1;
  1137. continue;
  1138. }
  1139. // Must be at least 2 bytes long
  1140. if (chunk_len < 2)
  1141. break;
  1142. info.tracks = source->read2();
  1143. break;
  1144. }
  1145. // Didn't get to fill the header
  1146. if (info.tracks == 0)
  1147. {
  1148. cerr << "Not a valid XMID" << endl;
  1149. return 0;
  1150. }
  1151. // Ok now to start part 2
  1152. // Goto the right place
  1153. source->seek (start+((len+1)&~1));
  1154. // Read 4 bytes of type
  1155. source->read (buf, 4);
  1156. // Not an XMID
  1157. if (memcmp (buf, "CAT ", 4))
  1158. {
  1159. cerr << "Not a recognised XMID (" << buf[0] << buf[1] << buf[2] << buf[3] << ") should be (CAT )" << endl;
  1160. return 0;
  1161. }
  1162. // Now read length of this track
  1163. len = source->read4high();
  1164. // Read 4 bytes of type
  1165. source->read (buf, 4);
  1166. // Not an XMID
  1167. if (memcmp (buf, "XMID", 4))
  1168. {
  1169. cerr << "Not a recognised XMID (" << buf[0] << buf[1] << buf[2] << buf[3] << ") should be (XMID)" << endl;
  1170. return 0;
  1171. }
  1172. }
  1173. // Ok it's an XMID, so pass it to the ExtractCode
  1174. events = (midi_event **) malloc (sizeof (midi_event*)*info.tracks); //new midi_event *[info.tracks];
  1175. timing = new short[info.tracks];
  1176. fixed = new bool[info.tracks];
  1177. info.type = 0;
  1178. for (i = 0; i < info.tracks; i++)
  1179. {
  1180. events[i] = NULL;
  1181. fixed[i] = false;
  1182. }
  1183. count = ExtractTracksFromXmi (source);
  1184. if (count != info.tracks)
  1185. {
  1186. cerr << "Error: unable to extract all (" << info.tracks << ") tracks specified from XMIDI. Only ("<< count << ")" << endl;
  1187. int i = 0;
  1188. for (i = 0; i < info.tracks; i++)
  1189. DeleteEventList (events[i]);
  1190. //delete [] events;
  1191. free (events);
  1192. delete [] timing;
  1193. return 0;
  1194. }
  1195. return 1;
  1196. }// Definately a Midi
  1197. else if (!memcmp (buf, "MThd", 4))
  1198. {
  1199. // Simple read length of header
  1200. len = source->read4high();
  1201. if (len < 6)
  1202. {
  1203. cerr << "Not a valid MIDI" << endl;
  1204. return 0;
  1205. }
  1206. info.type = source->read2high();
  1207. info.tracks = source->read2high();
  1208. events = (midi_event **) malloc (sizeof (midi_event*)*info.tracks); //new midi_event *[info.tracks];
  1209. timing = new short[info.tracks];
  1210. fixed = new bool[info.tracks];
  1211. timing[0] = source->read2high();
  1212. for (i = 0; i < info.tracks; i++)
  1213. {
  1214. timing[i] = timing[0];
  1215. events[i] = NULL;
  1216. fixed[i] = false;
  1217. }
  1218. count = ExtractTracksFromMid (source);
  1219. if (count != info.tracks)
  1220. {
  1221. cerr << "Error: unable to extract all (" << info.tracks << ") tracks specified from MIDI. Only ("<< count << ")" << endl;
  1222. for (i = 0; i < info.tracks; i++)
  1223. DeleteEventList (events[i]);
  1224. //delete [] events;
  1225. free (events);
  1226. delete [] timing;
  1227. return 0;
  1228. }
  1229. return 1;
  1230. }// A RIFF Midi, just pass the source back to this function at the start of the midi file
  1231. else if (!memcmp (buf, "RIFF", 4))
  1232. {
  1233. // Read len
  1234. len = source->read4();
  1235. // Read 4 bytes of type
  1236. source->read (buf, 4);
  1237. // Not an RMID
  1238. if (memcmp (buf, "RMID", 4))
  1239. {
  1240. cerr << "Invalid RMID" << endl;
  1241. return 0;
  1242. }
  1243. // Is a RMID
  1244. for (i = 4; i < len; i++)
  1245. {
  1246. // Read 4 bytes of type
  1247. source->read (buf, 4);
  1248. chunk_len = source->read4();
  1249. i+=8;
  1250. if (memcmp (buf, "data", 4))
  1251. {
  1252. // Must allign
  1253. source->skip ((chunk_len+1)&~1);
  1254. i+= (chunk_len+1)&~1;
  1255. continue;
  1256. }
  1257. return ExtractTracks (source);
  1258. }
  1259. cerr << "Failed to find midi data in RIFF Midi" << endl;
  1260. return 0;
  1261. }
  1262. return 0;
  1263. }