emux_seq.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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(struct snd_emux_port *p);
  26. static int snd_emux_use(void *private_data, struct snd_seq_port_subscribe *info);
  27. static int snd_emux_unuse(void *private_data, struct snd_seq_port_subscribe *info);
  28. /*
  29. * MIDI emulation operators
  30. */
  31. static struct snd_midi_op emux_ops = {
  32. snd_emux_note_on,
  33. snd_emux_note_off,
  34. snd_emux_key_press,
  35. snd_emux_terminate_note,
  36. snd_emux_control,
  37. snd_emux_nrpn,
  38. snd_emux_sysex,
  39. };
  40. /*
  41. * number of MIDI channels
  42. */
  43. #define MIDI_CHANNELS 16
  44. /*
  45. * type flags for MIDI sequencer port
  46. */
  47. #define DEFAULT_MIDI_TYPE (SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |\
  48. SNDRV_SEQ_PORT_TYPE_MIDI_GM |\
  49. SNDRV_SEQ_PORT_TYPE_MIDI_GS |\
  50. SNDRV_SEQ_PORT_TYPE_MIDI_XG)
  51. /*
  52. * Initialise the EMUX Synth by creating a client and registering
  53. * a series of ports.
  54. * Each of the ports will contain the 16 midi channels. Applications
  55. * can connect to these ports to play midi data.
  56. */
  57. int
  58. snd_emux_init_seq(struct snd_emux *emu, struct snd_card *card, int index)
  59. {
  60. int i;
  61. struct snd_seq_port_callback pinfo;
  62. char tmpname[64];
  63. emu->client = snd_seq_create_kernel_client(card, index,
  64. "%s WaveTable", emu->name);
  65. if (emu->client < 0) {
  66. snd_printk("can't create client\n");
  67. return -ENODEV;
  68. }
  69. if (emu->num_ports < 0) {
  70. snd_printk("seqports must be greater than zero\n");
  71. emu->num_ports = 1;
  72. } else if (emu->num_ports >= SNDRV_EMUX_MAX_PORTS) {
  73. snd_printk("too many ports."
  74. "limited max. ports %d\n", SNDRV_EMUX_MAX_PORTS);
  75. emu->num_ports = SNDRV_EMUX_MAX_PORTS;
  76. }
  77. memset(&pinfo, 0, sizeof(pinfo));
  78. pinfo.owner = THIS_MODULE;
  79. pinfo.use = snd_emux_use;
  80. pinfo.unuse = snd_emux_unuse;
  81. pinfo.event_input = snd_emux_event_input;
  82. for (i = 0; i < emu->num_ports; i++) {
  83. struct snd_emux_port *p;
  84. sprintf(tmpname, "%s Port %d", emu->name, i);
  85. p = snd_emux_create_port(emu, tmpname, MIDI_CHANNELS,
  86. 0, &pinfo);
  87. if (p == NULL) {
  88. snd_printk("can't create port\n");
  89. return -ENOMEM;
  90. }
  91. p->port_mode = SNDRV_EMUX_PORT_MODE_MIDI;
  92. snd_emux_init_port(p);
  93. emu->ports[i] = p->chset.port;
  94. emu->portptrs[i] = p;
  95. }
  96. return 0;
  97. }
  98. /*
  99. * Detach from the ports that were set up for this synthesizer and
  100. * destroy the kernel client.
  101. */
  102. void
  103. snd_emux_detach_seq(struct snd_emux *emu)
  104. {
  105. if (emu->voices)
  106. snd_emux_terminate_all(emu);
  107. mutex_lock(&emu->register_mutex);
  108. if (emu->client >= 0) {
  109. snd_seq_delete_kernel_client(emu->client);
  110. emu->client = -1;
  111. }
  112. mutex_unlock(&emu->register_mutex);
  113. }
  114. /*
  115. * create a sequencer port and channel_set
  116. */
  117. struct snd_emux_port *
  118. snd_emux_create_port(struct snd_emux *emu, char *name,
  119. int max_channels, int oss_port,
  120. struct snd_seq_port_callback *callback)
  121. {
  122. struct snd_emux_port *p;
  123. int i, type, cap;
  124. /* Allocate structures for this channel */
  125. if ((p = kzalloc(sizeof(*p), GFP_KERNEL)) == NULL) {
  126. snd_printk("no memory\n");
  127. return NULL;
  128. }
  129. p->chset.channels = kcalloc(max_channels, sizeof(struct snd_midi_channel), GFP_KERNEL);
  130. if (p->chset.channels == NULL) {
  131. snd_printk("no memory\n");
  132. kfree(p);
  133. return NULL;
  134. }
  135. for (i = 0; i < max_channels; i++)
  136. p->chset.channels[i].number = i;
  137. p->chset.private_data = p;
  138. p->chset.max_channels = max_channels;
  139. p->emu = emu;
  140. p->chset.client = emu->client;
  141. #ifdef SNDRV_EMUX_USE_RAW_EFFECT
  142. snd_emux_create_effect(p);
  143. #endif
  144. callback->private_free = free_port;
  145. callback->private_data = p;
  146. cap = SNDRV_SEQ_PORT_CAP_WRITE;
  147. if (oss_port) {
  148. type = SNDRV_SEQ_PORT_TYPE_SPECIFIC;
  149. } else {
  150. type = DEFAULT_MIDI_TYPE;
  151. cap |= SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
  152. }
  153. p->chset.port = snd_seq_event_port_attach(emu->client, callback,
  154. cap, type, max_channels,
  155. emu->max_voices, name);
  156. return p;
  157. }
  158. /*
  159. * release memory block for port
  160. */
  161. static void
  162. free_port(void *private_data)
  163. {
  164. struct snd_emux_port *p;
  165. p = private_data;
  166. if (p) {
  167. #ifdef SNDRV_EMUX_USE_RAW_EFFECT
  168. snd_emux_delete_effect(p);
  169. #endif
  170. kfree(p->chset.channels);
  171. kfree(p);
  172. }
  173. }
  174. #define DEFAULT_DRUM_FLAGS (1<<9)
  175. /*
  176. * initialize the port specific parameters
  177. */
  178. static void
  179. snd_emux_init_port(struct snd_emux_port *p)
  180. {
  181. p->drum_flags = DEFAULT_DRUM_FLAGS;
  182. p->volume_atten = 0;
  183. snd_emux_reset_port(p);
  184. }
  185. /*
  186. * reset port
  187. */
  188. void
  189. snd_emux_reset_port(struct snd_emux_port *port)
  190. {
  191. int i;
  192. /* stop all sounds */
  193. snd_emux_sounds_off_all(port);
  194. snd_midi_channel_set_clear(&port->chset);
  195. #ifdef SNDRV_EMUX_USE_RAW_EFFECT
  196. snd_emux_clear_effect(port);
  197. #endif
  198. /* set port specific control parameters */
  199. port->ctrls[EMUX_MD_DEF_BANK] = 0;
  200. port->ctrls[EMUX_MD_DEF_DRUM] = 0;
  201. port->ctrls[EMUX_MD_REALTIME_PAN] = 1;
  202. for (i = 0; i < port->chset.max_channels; i++) {
  203. struct snd_midi_channel *chan = port->chset.channels + i;
  204. chan->drum_channel = ((port->drum_flags >> i) & 1) ? 1 : 0;
  205. }
  206. }
  207. /*
  208. * input sequencer event
  209. */
  210. int
  211. snd_emux_event_input(struct snd_seq_event *ev, int direct, void *private_data,
  212. int atomic, int hop)
  213. {
  214. struct snd_emux_port *port;
  215. port = private_data;
  216. snd_assert(port != NULL && ev != NULL, return -EINVAL);
  217. snd_midi_process_event(&emux_ops, ev, &port->chset);
  218. return 0;
  219. }
  220. /*
  221. * increment usage count
  222. */
  223. int
  224. snd_emux_inc_count(struct snd_emux *emu)
  225. {
  226. emu->used++;
  227. if (!try_module_get(emu->ops.owner))
  228. goto __error;
  229. if (!try_module_get(emu->card->module)) {
  230. module_put(emu->ops.owner);
  231. __error:
  232. emu->used--;
  233. return 0;
  234. }
  235. return 1;
  236. }
  237. /*
  238. * decrease usage count
  239. */
  240. void
  241. snd_emux_dec_count(struct snd_emux *emu)
  242. {
  243. module_put(emu->card->module);
  244. emu->used--;
  245. if (emu->used <= 0)
  246. snd_emux_terminate_all(emu);
  247. module_put(emu->ops.owner);
  248. }
  249. /*
  250. * Routine that is called upon a first use of a particular port
  251. */
  252. static int
  253. snd_emux_use(void *private_data, struct snd_seq_port_subscribe *info)
  254. {
  255. struct snd_emux_port *p;
  256. struct snd_emux *emu;
  257. p = private_data;
  258. snd_assert(p != NULL, return -EINVAL);
  259. emu = p->emu;
  260. snd_assert(emu != NULL, return -EINVAL);
  261. mutex_lock(&emu->register_mutex);
  262. snd_emux_init_port(p);
  263. snd_emux_inc_count(emu);
  264. mutex_unlock(&emu->register_mutex);
  265. return 0;
  266. }
  267. /*
  268. * Routine that is called upon the last unuse() of a particular port.
  269. */
  270. static int
  271. snd_emux_unuse(void *private_data, struct snd_seq_port_subscribe *info)
  272. {
  273. struct snd_emux_port *p;
  274. struct snd_emux *emu;
  275. p = private_data;
  276. snd_assert(p != NULL, return -EINVAL);
  277. emu = p->emu;
  278. snd_assert(emu != NULL, return -EINVAL);
  279. mutex_lock(&emu->register_mutex);
  280. snd_emux_sounds_off_all(p);
  281. snd_emux_dec_count(emu);
  282. mutex_unlock(&emu->register_mutex);
  283. return 0;
  284. }
  285. /*
  286. * attach virtual rawmidi devices
  287. */
  288. int snd_emux_init_virmidi(struct snd_emux *emu, struct snd_card *card)
  289. {
  290. int i;
  291. emu->vmidi = NULL;
  292. if (emu->midi_ports <= 0)
  293. return 0;
  294. emu->vmidi = kcalloc(emu->midi_ports, sizeof(struct snd_rawmidi *), GFP_KERNEL);
  295. if (emu->vmidi == NULL)
  296. return -ENOMEM;
  297. for (i = 0; i < emu->midi_ports; i++) {
  298. struct snd_rawmidi *rmidi;
  299. struct snd_virmidi_dev *rdev;
  300. if (snd_virmidi_new(card, emu->midi_devidx + i, &rmidi) < 0)
  301. goto __error;
  302. rdev = rmidi->private_data;
  303. sprintf(rmidi->name, "%s Synth MIDI", emu->name);
  304. rdev->seq_mode = SNDRV_VIRMIDI_SEQ_ATTACH;
  305. rdev->client = emu->client;
  306. rdev->port = emu->ports[i];
  307. if (snd_device_register(card, rmidi) < 0) {
  308. snd_device_free(card, rmidi);
  309. goto __error;
  310. }
  311. emu->vmidi[i] = rmidi;
  312. //snd_printk("virmidi %d ok\n", i);
  313. }
  314. return 0;
  315. __error:
  316. //snd_printk("error init..\n");
  317. snd_emux_delete_virmidi(emu);
  318. return -ENOMEM;
  319. }
  320. int snd_emux_delete_virmidi(struct snd_emux *emu)
  321. {
  322. int i;
  323. if (emu->vmidi == NULL)
  324. return 0;
  325. for (i = 0; i < emu->midi_ports; i++) {
  326. if (emu->vmidi[i])
  327. snd_device_free(emu->card, emu->vmidi[i]);
  328. }
  329. kfree(emu->vmidi);
  330. emu->vmidi = NULL;
  331. return 0;
  332. }