gm.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. ----------------------------------------------------------
  3. GeneralMIDI
  4. ----------------------------------------------------------
  5. */
  6. (function(root) { 'use strict';
  7. root.GM = (function(arr) {
  8. var clean = function(name) {
  9. return name.replace(/[^a-z0-9 ]/gi, '').replace(/[ ]/g, '_').toLowerCase();
  10. };
  11. var res = {
  12. byName: { },
  13. byId: { },
  14. byCategory: { }
  15. };
  16. for (var key in arr) {
  17. var list = arr[key];
  18. for (var n = 0, length = list.length; n < length; n++) {
  19. var instrument = list[n];
  20. if (!instrument) continue;
  21. var num = parseInt(instrument.substr(0, instrument.indexOf(' ')), 10);
  22. instrument = instrument.replace(num + ' ', '');
  23. res.byId[--num] =
  24. res.byName[clean(instrument)] =
  25. res.byCategory[clean(key)] = {
  26. // id: clean(instrument),
  27. // instrument: instrument,
  28. id: clean('Acoustic Grand Piano'),
  29. instrument: 'Acoustic Grand Piano',
  30. number: num,
  31. category: key
  32. };
  33. }
  34. }
  35. return res;
  36. })({
  37. 'Piano': ['1 Acoustic Grand Piano', '2 Bright Acoustic Piano', '3 Electric Grand Piano', '4 Honky-tonk Piano', '5 Electric Piano 1', '6 Electric Piano 2', '7 Harpsichord', '8 Clavinet'],
  38. 'Chromatic Percussion': ['9 Celesta', '10 Glockenspiel', '11 Music Box', '12 Vibraphone', '13 Marimba', '14 Xylophone', '15 Tubular Bells', '16 Dulcimer'],
  39. 'Organ': ['17 Drawbar Organ', '18 Percussive Organ', '19 Rock Organ', '20 Church Organ', '21 Reed Organ', '22 Accordion', '23 Harmonica', '24 Tango Accordion'],
  40. 'Guitar': ['25 Acoustic Guitar (nylon)', '26 Acoustic Guitar (steel)', '27 Electric Guitar (jazz)', '28 Electric Guitar (clean)', '29 Electric Guitar (muted)', '30 Overdriven Guitar', '31 Distortion Guitar', '32 Guitar Harmonics'],
  41. 'Bass': ['33 Acoustic Bass', '34 Electric Bass (finger)', '35 Electric Bass (pick)', '36 Fretless Bass', '37 Slap Bass 1', '38 Slap Bass 2', '39 Synth Bass 1', '40 Synth Bass 2'],
  42. 'Strings': ['41 Violin', '42 Viola', '43 Cello', '44 Contrabass', '45 Tremolo Strings', '46 Pizzicato Strings', '47 Orchestral Harp', '48 Timpani'],
  43. 'Ensemble': ['49 String Ensemble 1', '50 String Ensemble 2', '51 Synth Strings 1', '52 Synth Strings 2', '53 Choir Aahs', '54 Voice Oohs', '55 Synth Choir', '56 Orchestra Hit'],
  44. 'Brass': ['57 Trumpet', '58 Trombone', '59 Tuba', '60 Muted Trumpet', '61 French Horn', '62 Brass Section', '63 Synth Brass 1', '64 Synth Brass 2'],
  45. 'Reed': ['65 Soprano Sax', '66 Alto Sax', '67 Tenor Sax', '68 Baritone Sax', '69 Oboe', '70 English Horn', '71 Bassoon', '72 Clarinet'],
  46. 'Pipe': ['73 Piccolo', '74 Flute', '75 Recorder', '76 Pan Flute', '77 Blown Bottle', '78 Shakuhachi', '79 Whistle', '80 Ocarina'],
  47. 'Synth Lead': ['81 Lead 1 (square)', '82 Lead 2 (sawtooth)', '83 Lead 3 (calliope)', '84 Lead 4 (chiff)', '85 Lead 5 (charang)', '86 Lead 6 (voice)', '87 Lead 7 (fifths)', '88 Lead 8 (bass + lead)'],
  48. 'Synth Pad': ['89 Pad 1 (new age)', '90 Pad 2 (warm)', '91 Pad 3 (polysynth)', '92 Pad 4 (choir)', '93 Pad 5 (bowed)', '94 Pad 6 (metallic)', '95 Pad 7 (halo)', '96 Pad 8 (sweep)'],
  49. 'Synth Effects': ['97 FX 1 (rain)', '98 FX 2 (soundtrack)', '99 FX 3 (crystal)', '100 FX 4 (atmosphere)', '101 FX 5 (brightness)', '102 FX 6 (goblins)', '103 FX 7 (echoes)', '104 FX 8 (sci-fi)'],
  50. 'Ethnic': ['105 Sitar', '106 Banjo', '107 Shamisen', '108 Koto', '109 Kalimba', '110 Bagpipe', '111 Fiddle', '112 Shanai'],
  51. 'Percussive': ['113 Tinkle Bell', '114 Agogo', '115 Steel Drums', '116 Woodblock', '117 Taiko Drum', '118 Melodic Tom', '119 Synth Drum'],
  52. 'Sound effects': ['120 Reverse Cymbal', '121 Guitar Fret Noise', '122 Breath Noise', '123 Seashore', '124 Bird Tweet', '125 Telephone Ring', '126 Helicopter', '127 Applause', '128 Gunshot']
  53. // 'Piano': ['1 Acoustic Grand Piano', '2 Acoustic Grand Piano', '3 Acoustic Grand Piano', '4 Acoustic Grand Piano', '5 Acoustic Grand Piano', '6 Acoustic Grand Piano', '7 Acoustic Grand Piano', '8 Acoustic Grand Piano'],
  54. // 'Chromatic Percussion': ['9 Acoustic Grand Piano', '10 Acoustic Grand Piano', '11 Acoustic Grand Piano', '12 Acoustic Grand Piano', '13 Acoustic Grand Piano', '14 Acoustic Grand Piano', '15 Acoustic Grand Piano', '16 Acoustic Grand Piano'],
  55. // 'Organ': ['17 Acoustic Grand Piano', '18 Acoustic Grand Piano', '19 Acoustic Grand Piano', '20 Acoustic Grand Piano', '21 Acoustic Grand Piano', '22 Acoustic Grand Piano', '23 Acoustic Grand Piano', '24 Acoustic Grand Piano'],
  56. // 'Guitar': ['25 Acoustic Grand Piano', '26 Acoustic Grand Piano', '27 Acoustic Grand Piano', '28 Acoustic Grand Piano', '29 Acoustic Grand Piano', '30 Acoustic Grand Piano', '31 Acoustic Grand Piano', '32 Acoustic Grand Piano'],
  57. // 'Bass': ['33 Acoustic Grand Piano', '34 Acoustic Grand Piano', '35 Acoustic Grand Piano', '36 Acoustic Grand Piano', '37 Acoustic Grand Piano', '38 Acoustic Grand Piano', '39 Acoustic Grand Piano', '40 Acoustic Grand Piano'],
  58. // 'Strings': ['41 Acoustic Grand Piano', '42 Acoustic Grand Piano', '43 Acoustic Grand Piano', '44 Acoustic Grand Piano', '45 Acoustic Grand Piano', '46 Acoustic Grand Piano', '47 Acoustic Grand Piano', '48 Acoustic Grand Piano'],
  59. // 'Ensemble': ['49 Acoustic Grand Piano', '50 Acoustic Grand Piano', '51 Acoustic Grand Piano', '52 Acoustic Grand Piano', '53 Acoustic Grand Piano', '54 Acoustic Grand Piano', '55 Acoustic Grand Piano', '56 Acoustic Grand Piano'],
  60. // 'Brass': ['57 Acoustic Grand Piano', '58 Acoustic Grand Piano', '59 Acoustic Grand Piano', '60 Acoustic Grand Piano', '61 Acoustic Grand Piano', '62 Acoustic Grand Piano', '63 Acoustic Grand Piano', '64 Acoustic Grand Piano'],
  61. // 'Reed': ['65 Acoustic Grand Piano', '66 Acoustic Grand Piano', '67 Acoustic Grand Piano', '68 Acoustic Grand Piano', '69 Acoustic Grand Piano', '70 Acoustic Grand Piano', '71 Acoustic Grand Piano', '72 Acoustic Grand Piano'],
  62. // 'Pipe': ['73 Acoustic Grand Piano', '74 Acoustic Grand Piano', '75 Acoustic Grand Piano', '76 Acoustic Grand Piano', '77 Acoustic Grand Piano', '78 Acoustic Grand Piano', '79 Acoustic Grand Piano', '80 Acoustic Grand Piano'],
  63. // 'Synth Lead': ['81 Acoustic Grand Piano', '82 Acoustic Grand Piano', '83 Acoustic Grand Piano', '84 Acoustic Grand Piano', '85 Acoustic Grand Piano', '86 Acoustic Grand Piano', '87 Acoustic Grand Piano', '88 Acoustic Grand Piano'],
  64. // 'Synth Pad': ['89 Acoustic Grand Piano', '90 Acoustic Grand Piano', '91 Acoustic Grand Piano', '92 Acoustic Grand Piano', '93 Acoustic Grand Piano', '94 Acoustic Grand Piano', '95 Acoustic Grand Piano', '96 Acoustic Grand Piano'],
  65. // 'Synth Effects': ['97 Acoustic Grand Piano', '98 Acoustic Grand Piano', '99 Acoustic Grand Piano', '100 Acoustic Grand Piano', '101 Acoustic Grand Piano', '102 Acoustic Grand Piano', '103 Acoustic Grand Piano', '104 Acoustic Grand Piano'],
  66. // 'Ethnic': ['105 Acoustic Grand Piano', '106 Acoustic Grand Piano', '107 Acoustic Grand Piano', '108 Acoustic Grand Piano', '109 Acoustic Grand Piano', '110 Acoustic Grand Piano', '111 Acoustic Grand Piano', '112 Acoustic Grand Piano'],
  67. // 'Percussive': ['113 Acoustic Grand Piano', '114 Acoustic Grand Piano', '115 Acoustic Grand Piano', '116 Acoustic Grand Piano', '117 Acoustic Grand Piano', '118 Acoustic Grand Piano', '119 Acoustic Grand Piano'],
  68. // 'Sound effects': ['120 Acoustic Grand Piano', '121 Acoustic Grand Piano', '122 Acoustic Grand Piano', '123 Acoustic Grand Piano', '124 Acoustic Grand Piano', '125 Acoustic Grand Piano', '126 Acoustic Grand Piano', '127 Acoustic Grand Piano', '128 Acoustic Grand Piano']
  69. });
  70. /* get/setInstrument
  71. --------------------------------------------------- */
  72. root.getInstrument = function(channelId) {
  73. var channel = root.channels[channelId];
  74. return channel && channel.instrument;
  75. };
  76. root.setInstrument = function(channelId, program, delay) {
  77. var channel = root.channels[channelId];
  78. if (delay) {
  79. return setTimeout(function() {
  80. channel.instrument = program;
  81. }, delay);
  82. } else {
  83. channel.instrument = program;
  84. }
  85. };
  86. /* get/setMono
  87. --------------------------------------------------- */
  88. root.getMono = function(channelId) {
  89. var channel = root.channels[channelId];
  90. return channel && channel.mono;
  91. };
  92. root.setMono = function(channelId, truthy, delay) {
  93. var channel = root.channels[channelId];
  94. if (delay) {
  95. return setTimeout(function() {
  96. channel.mono = truthy;
  97. }, delay);
  98. } else {
  99. channel.mono = truthy;
  100. }
  101. };
  102. /* get/setOmni
  103. --------------------------------------------------- */
  104. root.getOmni = function(channelId) {
  105. var channel = root.channels[channelId];
  106. return channel && channel.omni;
  107. };
  108. root.setOmni = function(channelId, truthy) {
  109. var channel = root.channels[channelId];
  110. if (delay) {
  111. return setTimeout(function() {
  112. channel.omni = truthy;
  113. }, delay);
  114. } else {
  115. channel.omni = truthy;
  116. }
  117. };
  118. /* get/setSolo
  119. --------------------------------------------------- */
  120. root.getSolo = function(channelId) {
  121. var channel = root.channels[channelId];
  122. return channel && channel.solo;
  123. };
  124. root.setSolo = function(channelId, truthy) {
  125. var channel = root.channels[channelId];
  126. if (delay) {
  127. return setTimeout(function() {
  128. channel.solo = truthy;
  129. }, delay);
  130. } else {
  131. channel.solo = truthy;
  132. }
  133. };
  134. /* channels
  135. --------------------------------------------------- */
  136. root.channels = (function() { // 0 - 15 channels
  137. var channels = {};
  138. for (var i = 0; i < 16; i++) {
  139. channels[i] = { // default values
  140. instrument: i,
  141. pitchBend: 0,
  142. mute: false,
  143. mono: false,
  144. omni: false,
  145. solo: false
  146. };
  147. }
  148. return channels;
  149. })();
  150. /* note conversions
  151. --------------------------------------------------- */
  152. root.keyToNote = {}; // C8 == 108
  153. root.noteToKey = {}; // 108 == C8
  154. (function() {
  155. var A0 = 0x15; // first note
  156. var C8 = 0x6C; // last note
  157. var number2key = ['C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'B'];
  158. for (var n = A0; n <= C8; n++) {
  159. var octave = (n - 12) / 12 >> 0;
  160. var name = number2key[n % 12] + octave;
  161. root.keyToNote[name] = n;
  162. root.noteToKey[n] = name;
  163. }
  164. })();
  165. })(MIDI);