KORG microX utility.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

552 lines
11 KiB

  1. /*
  2. * asysex - A SysEx Utility
  3. * Copyright (C) 2012 Damien Goutte-Gattat
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include <midi.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <assert.h>
  25. #include <xmem.h>
  26. #ifdef HAVE_ALSA
  27. #include <alsa/asoundlib.h>
  28. #endif
  29. #ifdef HAVE_OSS
  30. #include <fcntl.h>
  31. #include <string.h>
  32. #include <dirent.h>
  33. #endif
  34. typedef int (*m_close) (midi_io_t *);
  35. typedef ssize_t (*m_write) (midi_io_t *, unsigned char *, size_t);
  36. typedef ssize_t (*m_read) (midi_io_t *, unsigned char *, size_t);
  37. typedef const char * (*m_error) (midi_io_t *);
  38. #define MIDI_IO_BUFFER_SIZE 256
  39. struct midi_io
  40. {
  41. /* Backend-dependent functions. */
  42. struct {
  43. m_close close;
  44. m_write write;
  45. m_read read;
  46. m_error error;
  47. } backend;
  48. /* Common data. */
  49. int error;
  50. unsigned char buffer[MIDI_IO_BUFFER_SIZE];
  51. size_t pos;
  52. size_t len;
  53. unsigned char status;
  54. /* Backend-dependent data. */
  55. union {
  56. #ifdef HAVE_ALSA
  57. struct {
  58. snd_rawmidi_t *in;
  59. snd_rawmidi_t *out;
  60. } alsa;
  61. #endif
  62. #ifdef HAVE_OSS
  63. struct {
  64. int fd;
  65. } oss;
  66. #endif
  67. };
  68. };
  69. /* Backend-specific functions. */
  70. #ifdef HAVE_ALSA
  71. static int
  72. alsa_midi_close(midi_io_t *midi)
  73. {
  74. snd_rawmidi_close(midi->alsa.in);
  75. snd_rawmidi_close(midi->alsa.out);
  76. free(midi);
  77. return 0;
  78. }
  79. static ssize_t
  80. alsa_midi_write(midi_io_t *midi, unsigned char *buffer, size_t len)
  81. {
  82. /* Flush reading buffer. */
  83. midi->pos = midi->len = 0;
  84. midi->error = snd_rawmidi_write(midi->alsa.out, buffer, len);
  85. return midi->error;
  86. }
  87. static ssize_t
  88. alsa_midi_read(midi_io_t *midi, unsigned char *buffer, size_t len)
  89. {
  90. midi->error = snd_rawmidi_read(midi->alsa.in, buffer, len);
  91. return midi->error;
  92. }
  93. static const char *
  94. alsa_midi_error(midi_io_t *midi)
  95. {
  96. if ( midi->error > 0 )
  97. return NULL;
  98. else
  99. return snd_strerror(midi->error);
  100. }
  101. static midi_io_t *
  102. alsa_midi_open(const char *name)
  103. {
  104. midi_io_t *midi;
  105. midi = xmalloc(sizeof(*midi));
  106. midi->alsa.in = midi->alsa.out = NULL;
  107. midi->error = midi->pos = midi->len = midi->status = 0;
  108. midi->backend.close = alsa_midi_close;
  109. midi->backend.write = alsa_midi_write;
  110. midi->backend.read = alsa_midi_read;
  111. midi->backend.error = alsa_midi_error;
  112. if ( snd_rawmidi_open(&(midi->alsa.in), &(midi->alsa.out), name,
  113. SND_RAWMIDI_NONBLOCK) < 0 ) {
  114. free(midi);
  115. midi = NULL;
  116. }
  117. else if ( snd_rawmidi_nonblock(midi->alsa.out, 0) < 0 ) {
  118. midi_close(midi);
  119. midi = NULL;
  120. }
  121. return midi;
  122. }
  123. static void
  124. alsa_midi_get_ports(char ***ports, size_t *n, size_t *max)
  125. {
  126. char name[32];
  127. int card;
  128. card = -1;
  129. while ( snd_card_next(&card) >= 0 && card != -1 ) {
  130. snd_ctl_t *ctl;
  131. snprintf(name, sizeof(name), "hw:%d", card);
  132. if ( snd_ctl_open(&ctl, name, 0) >= 0 ) {
  133. int device;
  134. device = -1;
  135. while ( snd_ctl_rawmidi_next_device(ctl, &device) >= 0 && device >= 0 ) {
  136. snprintf(name, sizeof(name), "hw:%d,%d", card, device);
  137. if ( *n >= *max ) {
  138. *max += 10;
  139. *ports = xrealloc(*ports, *max);
  140. }
  141. *ports[(*n)++] = xstrdup(name);
  142. }
  143. snd_ctl_close(ctl);
  144. }
  145. }
  146. }
  147. #endif /* HAVE_ALSA */
  148. #ifdef HAVE_OSS
  149. static int
  150. oss_midi_close(midi_io_t *midi)
  151. {
  152. close(midi->oss.fd);
  153. free(midi);
  154. return 0;
  155. }
  156. static ssize_t
  157. oss_midi_write(midi_io_t *midi, unsigned char *buffer, size_t len)
  158. {
  159. ssize_t n;
  160. /* Flush reading buffer. */
  161. midi->pos = midi->len = 0;
  162. if ( (n = write(midi->oss.fd, buffer, len)) == -1 )
  163. midi->error = errno;
  164. else
  165. midi->error = 0;
  166. return n;
  167. }
  168. static ssize_t
  169. oss_midi_read(midi_io_t *midi, unsigned char *buffer, size_t len)
  170. {
  171. ssize_t n;
  172. if ( (n = read(midi->oss.fd, buffer, len)) == -1 )
  173. midi->error = errno;
  174. else
  175. midi->error = 0;
  176. return n;
  177. }
  178. static const char *
  179. oss_midi_error(midi_io_t *midi)
  180. {
  181. if ( midi->error == 0 )
  182. return NULL;
  183. else
  184. return strerror(midi->error);
  185. }
  186. static midi_io_t *
  187. oss_midi_open(const char *name)
  188. {
  189. midi_io_t *midi;
  190. assert(name != NULL);
  191. midi = xmalloc(sizeof(*midi));
  192. midi->oss.fd = -1;
  193. midi->error = midi->pos = midi->len = midi->status = 0;
  194. midi->backend.close = oss_midi_close;
  195. midi->backend.write = oss_midi_write;
  196. midi->backend.read = oss_midi_read;
  197. midi->backend.error = oss_midi_error;
  198. if ( (midi->oss.fd = open(name, O_RDWR, 0)) == -1 ) {
  199. free(midi);
  200. midi = NULL;
  201. }
  202. return midi;
  203. }
  204. static int
  205. oss_midi_device(const struct dirent *entry)
  206. {
  207. return strncmp(entry->d_name, "midi", 4) == 0;
  208. }
  209. static void
  210. oss_midi_get_ports(char ***ports, size_t *n, size_t *max)
  211. {
  212. char name[32];
  213. size_t k;
  214. struct dirent **namelist;
  215. if ( (k = scandir("/dev", &namelist, oss_midi_device, alphasort)) != -1 ) {
  216. while ( k-- ) {
  217. snprintf(name, sizeof(name), "/dev/%s", namelist[k]->d_name);
  218. free(namelist[k]);
  219. if ( *n >= *max ) {
  220. *max += 10;
  221. *ports = xrealloc(*ports, *max);
  222. }
  223. ports[(*n)++] = xstrdup(name);
  224. }
  225. }
  226. }
  227. #endif /* HAVE_OSS */
  228. /* Backend-independent functions. */
  229. midi_io_t *
  230. midi_open(const char *name)
  231. {
  232. midi_io_t *midi;
  233. midi = NULL;
  234. #ifdef HAVE_ALSA
  235. if ( midi == NULL )
  236. midi = alsa_midi_open(name);
  237. #endif
  238. #ifdef HAVE_OSS
  239. if ( midi == NULL )
  240. midi = oss_midi_open(name);
  241. #endif
  242. return midi;
  243. }
  244. int
  245. midi_close(midi_io_t *midi)
  246. {
  247. assert(midi != NULL);
  248. return midi->backend.close(midi);
  249. }
  250. ssize_t
  251. midi_write(midi_io_t *midi, unsigned char *buffer, size_t len)
  252. {
  253. assert(midi != NULL);
  254. assert(buffer != NULL);
  255. return midi->backend.write(midi, buffer, len);
  256. }
  257. ssize_t
  258. midi_read(midi_io_t *midi, unsigned char *buffer, size_t len)
  259. {
  260. assert(midi != NULL);
  261. assert(buffer != NULL);
  262. return midi->backend.read(midi, buffer, len);
  263. }
  264. const char *
  265. midi_error(midi_io_t *midi)
  266. {
  267. assert(midi != NULL);
  268. return midi->backend.error(midi);
  269. }
  270. char **
  271. midi_get_ports(void)
  272. {
  273. char **ports, name[32];
  274. size_t n, max;
  275. ports = NULL;
  276. n = max = 0;
  277. #ifdef HAVE_ALSA
  278. alsa_midi_get_ports(&ports, &n, &max);
  279. #endif
  280. #ifdef HAVE_OSS
  281. oss_midi_get_ports(&ports, &n, &max);
  282. #endif
  283. if ( n >= max ) {
  284. max += 1;
  285. ports = xrealloc(ports, max);
  286. }
  287. ports[n++] = NULL;
  288. return ports;
  289. }
  290. int
  291. midi_next(midi_io_t *midi)
  292. {
  293. unsigned char b;
  294. assert(midi != NULL);
  295. if ( midi->pos >= midi->len ) {
  296. ssize_t n;
  297. n = midi_read(midi, midi->buffer, MIDI_IO_BUFFER_SIZE);
  298. if ( n < 0 )
  299. return n;
  300. midi->len = n;
  301. midi->pos = 0;
  302. }
  303. b = (unsigned char) midi->buffer[midi->pos++];
  304. if ( b >= 0x80 && b < 0xF8 ) /* Status byte? */
  305. midi->status = b;
  306. return b;
  307. }
  308. inline unsigned char
  309. midi_status(midi_io_t *midi)
  310. {
  311. return midi->status;
  312. }
  313. int
  314. midi_change_program(midi_io_t *midi,
  315. unsigned char channel,
  316. unsigned short bank,
  317. unsigned char program)
  318. {
  319. unsigned char msg[] = { 0xB0, /* Control Change */
  320. 0x00, /* CC Bank Select MSB */
  321. 0x00, /* Bank number MSB */
  322. 0xB0, /* Control Change */
  323. 0x20, /* CC Bank Select LSB */
  324. 0x00, /* Bank number LSB */
  325. 0xC0, /* Program Change */
  326. 0x00 /* Program number */ };
  327. assert(midi != NULL);
  328. msg[0] |= (channel & 0x0F);
  329. msg[3] |= (channel & 0x0F);
  330. msg[6] |= (channel & 0x0F);
  331. msg[2] |= ((bank >> 7) & 0x7F);
  332. msg[5] |= (bank & 0x7F);
  333. msg[7] |= (program & 0x7F);
  334. return midi_write(midi, msg, sizeof(msg));
  335. }
  336. const char *midi_gm_patches[] = {
  337. "Acoustic Grand Piano",
  338. "Bright Acoustic Piano",
  339. "Electric Grand Piano",
  340. "Honky-tonk Piano",
  341. "Electric Piano 1",
  342. "Electric Piano 2",
  343. "Harpsichord",
  344. "Clavi",
  345. "Celesta",
  346. "Glockenspiel",
  347. "Music Box",
  348. "Vibraphone",
  349. "Marimba",
  350. "Xylophone",
  351. "Tubular Bells",
  352. "Dulcimer",
  353. "Drawbar Organ",
  354. "Percussive Organ",
  355. "Rock Organ",
  356. "Church Organ",
  357. "Reed Organ",
  358. "Accordion",
  359. "Harmonica",
  360. "Tango Accordion",
  361. "Guitar (nylon)",
  362. "Acoustic Guitar (steel)",
  363. "Electric Guitar (jazz)",
  364. "Electric Guitar (clean)",
  365. "Electric Guitar (muted)",
  366. "Overdriven Guitar",
  367. "Distortion Guitar",
  368. "Guitar harmonics",
  369. "Acoustic Bass",
  370. "Electric Bass (finger)",
  371. "Electric Bass (pick)",
  372. "Fretless Bass",
  373. "Slap Bass 1",
  374. "Slap Bass 2",
  375. "Synth Bass 1",
  376. "Synth Bass 2",
  377. "Violin",
  378. "Viola",
  379. "Cello",
  380. "Contrabass",
  381. "Tremolo Strings",
  382. "Pizzicato Strings",
  383. "Orchestral Harp",
  384. "Timpani",
  385. "String Ensemble 1",
  386. "String Ensemble 2",
  387. "SynthStrings 1",
  388. "SynthStrings 2",
  389. "Choir Aahs",
  390. "Voice Oohs",
  391. "Synth Voice",
  392. "Orchestra Hit",
  393. "Trumpet",
  394. "Trombone",
  395. "Tuba",
  396. "Muted Trumpet",
  397. "French Horn",
  398. "Brass Section",
  399. "SynthBrass 1",
  400. "SynthBrass 2",
  401. "Soprano Sax",
  402. "Alto Sax",
  403. "Tenor Sax",
  404. "Baritone Sax",
  405. "Oboe",
  406. "English Horn",
  407. "Bassoon",
  408. "Clarinet",
  409. "Piccolo",
  410. "Flute",
  411. "Recorder",
  412. "Pan Flute",
  413. "Blown Bottle",
  414. "Shakuhachi",
  415. "Whistle",
  416. "Ocarina",
  417. "Lead 1 (square)",
  418. "Lead 2 (sawtooth)",
  419. "Lead 3 (calliope)",
  420. "Lead 4 (chiff)",
  421. "Lead 5 (charang)",
  422. "Lead 6 (voice)",
  423. "Lead 7 (fifths)",
  424. "Lead 8 (bass+lead)",
  425. "Pad 1 (new age)",
  426. "Pad 2 (warm)",
  427. "Pad 3 (polysynth)",
  428. "Pad 4 (choir)",
  429. "Pad 5 (bowed)",
  430. "Pad 6 (metallic)",
  431. "Pad 7 (halo)",
  432. "Pad 8 (sweep)",
  433. "FX 1 (rain)",
  434. "FX 2 (soundtrack)",
  435. "FX 3 (crystal)",
  436. "FX 4 (atmosphere)",
  437. "FX 5 (brightness)",
  438. "FX 6 (goblins)",
  439. "FX 7 (echoes)",
  440. "FX 8 (sci-fi)",
  441. "Sitar",
  442. "Banjo",
  443. "Shamisen",
  444. "Koto",
  445. "Kalimba",
  446. "Bag pipe",
  447. "Fiddle",
  448. "Shanai",
  449. "Tinkle Bell",
  450. "Agogo",
  451. "Steel Drums",
  452. "Woodblock",
  453. "Taiko drum",
  454. "Melodic Tom",
  455. "Synth Drum",
  456. "Reverse Cymbal",
  457. "Guitar Fret Noise",
  458. "Breath Noise",
  459. "Seashore",
  460. "Bird Tweet",
  461. "Telephone Ring",
  462. "Helicopter",
  463. "Applause",
  464. "Gunshot"
  465. };