emux_seq.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Midi Sequencer interface routines.
  3. *
  4. * Copyright (C) 1999 Steve Ratcliffe
  5. * Copyright (c) 1999-2000 Takashi Iwai <tiwai@suse.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include "emux_voice.h"
  22. #include <linux/slab.h>
  23. /* Prototypes for static functions */
  24. static void free_port(void *private);
  25. static void snd_emux_init_port(snd_emux_port_t *p);
  26. static int snd_emux_use(void *private_data, snd_seq_port_subscribe_t *info);
  27. static int snd_emux_unuse(void *private_data, snd_seq_port_subscribe_t *info);
  28. static int get_client(snd_card_t *card, int index, char *name);
  29. /*
  30. * MIDI emulation operators
  31. */
  32. static snd_midi_op_t emux_ops = {
  33. snd_emux_note_on,
  34. snd_emux_note_off,
  35. snd_emux_key_press,
  36. snd_emux_terminate_note,
  37. snd_emux_control,
  38. snd_emux_nrpn,
  39. snd_emux_sysex,
  40. };
  41. /*
  42. * number of MIDI channels
  43. */
  44. #define MIDI_CHANNELS 16
  45. /*
  46. * type flags for MIDI sequencer port
  47. */
  48. #define DEFAULT_MIDI_TYPE (SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |\
  49. SNDRV_SEQ_PORT_TYPE_MIDI_GM |\
  50. SNDRV_SEQ_PORT_TYPE_MIDI_GS |\
  51. SNDRV_SEQ_PORT_TYPE_MIDI_XG |\
  52. SNDRV_SEQ_PORT_TYPE_DIRECT_SAMPLE)
  53. /*
  54. * Initialise the EMUX Synth by creating a client and registering
  55. * a series of ports.
  56. * Each of the ports will contain the 16 midi channels. Applications
  57. * can connect to these ports to play midi data.
  58. */
  59. int
  60. snd_emux_init_seq(snd_emux_t *emu, snd_card_t *card, int index)
  61. {
  62. int i;
  63. snd_seq_port_callback_t pinfo;
  64. char tmpname[64];
  65. sprintf(tmpname, "%s WaveTable", emu->name);
  66. emu->client = get_client(card, index, tmpname);
  67. if (emu->client < 0) {
  68. snd_printk("can't create client\n");
  69. return -ENODEV;
  70. }
  71. if (emu->num_ports < 0) {
  72. snd_printk("seqports must be greater than zero\n");
  73. emu->num_ports = 1;
  74. } else if (emu->num_ports >= SNDRV_EMUX_MAX_PORTS) {
  75. snd_printk("too many ports."
  76. "limited max. ports %d\n", SNDRV_EMUX_MAX_PORTS);
  77. emu->num_ports = SNDRV_EMUX_MAX_PORTS;
  78. }
  79. memset(&pinfo, 0, sizeof(pinfo));
  80. pinfo.owner = THIS_MODULE;
  81. pinfo.use = snd_emux_use;
  82. pinfo.unuse = snd_emux_unuse;
  83. pinfo.event_input = snd_emux_event_input;
  84. for (i = 0; i < emu->num_ports; i++) {
  85. snd_emux_port_t *p;
  86. sprintf(tmpname, "%s Port %d", emu->name, i);
  87. p = snd_emux_create_port(emu, tmpname, MIDI_CHANNELS,
  88. 0, &pinfo);
  89. if (p == NULL) {
  90. snd_printk("can't create port\n");
  91. return -ENOMEM;
  92. }
  93. p->port_mode = SNDRV_EMUX_PORT_MODE_MIDI;
  94. snd_emux_init_port(p);
  95. emu->ports[i] = p->chset.port;
  96. emu->portptrs[i] = p;
  97. }
  98. return 0;
  99. }
  100. /*
  101. * Detach from the ports that were set up for this synthesizer and
  102. * destroy the kernel client.
  103. */
  104. void
  105. snd_emux_detach_seq(snd_emux_t *emu)
  106. {
  107. if (emu->voices)
  108. snd_emux_terminate_all(emu);
  109. down(&emu->register_mutex);
  110. if (emu->client >= 0) {
  111. snd_seq_delete_kernel_client(emu->client);
  112. emu->client = -1;
  113. }
  114. up(&emu->register_mutex);
  115. }
  116. /*
  117. * create a sequencer port and channel_set
  118. */
  119. snd_emux_port_t *
  120. snd_emux_create_port(snd_emux_t *emu, char *name,
  121. int max_channels, int oss_port,
  122. snd_seq_port_callback_t *callback)
  123. {
  124. snd_emux_port_t *p;
  125. int i, type, cap;
  126. /* Allocate structures for this channel */
  127. if ((p = kcalloc(1, sizeof(*p), GFP_KERNEL)) == NULL) {
  128. snd_printk("no memory\n");
  129. return NULL;
  130. }
  131. p->chset.channels = kcalloc(max_channels, sizeof(snd_midi_channel_t), GFP_KERNEL);
  132. if (p->chset.channels == NULL) {
  133. snd_printk("no memory\n");
  134. kfree(p);
  135. return NULL;
  136. }
  137. for (i = 0; i < max_channels; i++)
  138. p->chset.channels[i].number = i;
  139. p->chset.private_data = p;
  140. p->chset.max_channels = max_channels;
  141. p->emu = emu;
  142. p->chset.client = emu->client;
  143. #ifdef SNDRV_EMUX_USE_RAW_EFFECT
  144. snd_emux_create_effect(p);
  145. #endif
  146. callback->private_free = free_port;
  147. callback->private_data = p;
  148. cap = SNDRV_SEQ_PORT_CAP_WRITE;
  149. if (oss_port) {
  150. type = SNDRV_SEQ_PORT_TYPE_SPECIFIC;
  151. } else {
  152. type = DEFAULT_MIDI_TYPE;
  153. cap |= SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
  154. }
  155. p->chset.port = snd_seq_event_port_attach(emu->client, callback,
  156. cap, type, max_channels,
  157. emu->max_voices, name);
  158. return p;
  159. }
  160. /*
  161. * release memory block for port
  162. */
  163. static void
  164. free_port(void *private_data)
  165. {
  166. snd_emux_port_t *p;
  167. p = private_data;
  168. if (p) {
  169. #ifdef SNDRV_EMUX_USE_RAW_EFFECT
  170. snd_emux_delete_effect(p);
  171. #endif
  172. kfree(p->chset.channels);
  173. kfree(p);
  174. }
  175. }
  176. #define DEFAULT_DRUM_FLAGS (1<<9)
  177. /*
  178. * initialize the port specific parameters
  179. */
  180. static void
  181. snd_emux_init_port(snd_emux_port_t *p)
  182. {
  183. p->drum_flags = DEFAULT_DRUM_FLAGS;
  184. p->volume_atten = 0;
  185. snd_emux_reset_port(p);
  186. }
  187. /*
  188. * reset port
  189. */
  190. void
  191. snd_emux_reset_port(snd_emux_port_t *port)
  192. {
  193. int i;
  194. /* stop all sounds */
  195. snd_emux_sounds_off_all(port);
  196. snd_midi_channel_set_clear(&port->chset);
  197. #ifdef SNDRV_EMUX_USE_RAW_EFFECT
  198. snd_emux_clear_effect(port);
  199. #endif
  200. /* set port specific control parameters */
  201. port->ctrls[EMUX_MD_DEF_BANK] = 0;
  202. port->ctrls[EMUX_MD_DEF_DRUM] = 0;
  203. port->ctrls[EMUX_MD_REALTIME_PAN] = 1;
  204. for (i = 0; i < port->chset.max_channels; i++) {
  205. snd_midi_channel_t *chan = port->chset.channels + i;
  206. chan->drum_channel = ((port->drum_flags >> i) & 1) ? 1 : 0;
  207. }
  208. }
  209. /*
  210. * input sequencer event
  211. */
  212. int
  213. snd_emux_event_input(snd_seq_event_t *ev, int direct, void *private_data,
  214. int atomic, int hop)
  215. {
  216. snd_emux_port_t *port;
  217. port = private_data;
  218. snd_assert(port != NULL && ev != NULL, return -EINVAL);
  219. snd_midi_process_event(&emux_ops, ev, &port->chset);
  220. return 0;
  221. }
  222. /*
  223. * increment usage count
  224. */
  225. int
  226. snd_emux_inc_count(snd_emux_t *emu)
  227. {
  228. emu->used++;
  229. if (!try_module_get(emu->ops.owner))
  230. goto __error;
  231. if (!try_module_get(emu->card->module)) {
  232. module_put(emu->ops.owner);
  233. __error:
  234. emu->used--;
  235. return 0;
  236. }
  237. return 1;
  238. }
  239. /*
  240. * decrease usage count
  241. */
  242. void
  243. snd_emux_dec_count(snd_emux_t *emu)
  244. {
  245. module_put(emu->card->module);
  246. emu->used--;
  247. if (emu->used <= 0)
  248. snd_emux_terminate_all(emu);
  249. module_put(emu->ops.owner);
  250. }
  251. /*
  252. * Routine that is called upon a first use of a particular port
  253. */
  254. static int
  255. snd_emux_use(void *private_data, snd_seq_port_subscribe_t *info)
  256. {
  257. snd_emux_port_t *p;
  258. snd_emux_t *emu;
  259. p = private_data;
  260. snd_assert(p != NULL, return -EINVAL);
  261. emu = p->emu;
  262. snd_assert(emu != NULL, return -EINVAL);
  263. down(&emu->register_mutex);
  264. snd_emux_init_port(p);
  265. snd_emux_inc_count(emu);
  266. up(&emu->register_mutex);
  267. return 0;
  268. }
  269. /*
  270. * Routine that is called upon the last unuse() of a particular port.
  271. */
  272. static int
  273. snd_emux_unuse(void *private_data, snd_seq_port_subscribe_t *info)
  274. {
  275. snd_emux_port_t *p;
  276. snd_emux_t *emu;
  277. p = private_data;
  278. snd_assert(p != NULL, return -EINVAL);
  279. emu = p->emu;
  280. snd_assert(emu != NULL, return -EINVAL);
  281. down(&emu->register_mutex);
  282. snd_emux_sounds_off_all(p);
  283. snd_emux_dec_count(emu);
  284. up(&emu->register_mutex);
  285. return 0;
  286. }
  287. /*
  288. * Create a sequencer client
  289. */
  290. static int
  291. get_client(snd_card_t *card, int index, char *name)
  292. {
  293. snd_seq_client_callback_t callbacks;
  294. snd_seq_client_info_t cinfo;
  295. int client;
  296. memset(&callbacks, 0, sizeof(callbacks));
  297. callbacks.private_data = NULL;
  298. callbacks.allow_input = 1;
  299. callbacks.allow_output = 1;
  300. /* Find a free client, start from 1 as the MPU expects to use 0 */
  301. client = snd_seq_create_kernel_client(card, index, &callbacks);
  302. if (client < 0)
  303. return client;
  304. memset(&cinfo, 0, sizeof(cinfo));
  305. cinfo.client = client;
  306. cinfo.type = KERNEL_CLIENT;
  307. strcpy(cinfo.name, name);
  308. snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, &cinfo);
  309. return client;
  310. }
  311. /*
  312. * attach virtual rawmidi devices
  313. */
  314. int snd_emux_init_virmidi(snd_emux_t *emu, snd_card_t *card)
  315. {
  316. int i;
  317. emu->vmidi = NULL;
  318. if (emu->midi_ports <= 0)
  319. return 0;
  320. emu->vmidi = kcalloc(emu->midi_ports, sizeof(snd_rawmidi_t*), GFP_KERNEL);
  321. if (emu->vmidi == NULL)
  322. return -ENOMEM;
  323. for (i = 0; i < emu->midi_ports; i++) {
  324. snd_rawmidi_t *rmidi;
  325. snd_virmidi_dev_t *rdev;
  326. if (snd_virmidi_new(card, emu->midi_devidx + i, &rmidi) < 0)
  327. goto __error;
  328. rdev = rmidi->private_data;
  329. sprintf(rmidi->name, "%s Synth MIDI", emu->name);
  330. rdev->seq_mode = SNDRV_VIRMIDI_SEQ_ATTACH;
  331. rdev->client = emu->client;
  332. rdev->port = emu->ports[i];
  333. if (snd_device_register(card, rmidi) < 0) {
  334. snd_device_free(card, rmidi);
  335. goto __error;
  336. }
  337. emu->vmidi[i] = rmidi;
  338. //snd_printk("virmidi %d ok\n", i);
  339. }
  340. return 0;
  341. __error:
  342. //snd_printk("error init..\n");
  343. snd_emux_delete_virmidi(emu);
  344. return -ENOMEM;
  345. }
  346. int snd_emux_delete_virmidi(snd_emux_t *emu)
  347. {
  348. int i;
  349. if (emu->vmidi == NULL)
  350. return 0;
  351. for (i = 0; i < emu->midi_ports; i++) {
  352. if (emu->vmidi[i])
  353. snd_device_free(emu->card, emu->vmidi[i]);
  354. }
  355. kfree(emu->vmidi);
  356. emu->vmidi = NULL;
  357. return 0;
  358. }