2 * Midi Sequencer interface routines.
4 * Copyright (C) 1999 Steve Ratcliffe
5 * Copyright (c) 1999-2000 Takashi Iwai <tiwai@suse.de>
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.
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.
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
22 #include "emux_voice.h"
23 #include <linux/slab.h>
26 /* Prototypes for static functions */
27 static void free_port(void *private);
28 static void snd_emux_init_port(snd_emux_port_t
*p
);
29 static int snd_emux_use(void *private_data
, snd_seq_port_subscribe_t
*info
);
30 static int snd_emux_unuse(void *private_data
, snd_seq_port_subscribe_t
*info
);
31 static int get_client(snd_card_t
*card
, int index
, char *name
);
34 * MIDI emulation operators
36 static snd_midi_op_t emux_ops
= {
40 snd_emux_terminate_note
,
48 * number of MIDI channels
50 #define MIDI_CHANNELS 16
53 * type flags for MIDI sequencer port
55 #define DEFAULT_MIDI_TYPE (SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |\
56 SNDRV_SEQ_PORT_TYPE_MIDI_GM |\
57 SNDRV_SEQ_PORT_TYPE_MIDI_GS |\
58 SNDRV_SEQ_PORT_TYPE_MIDI_XG |\
59 SNDRV_SEQ_PORT_TYPE_DIRECT_SAMPLE)
62 * Initialise the EMUX Synth by creating a client and registering
64 * Each of the ports will contain the 16 midi channels. Applications
65 * can connect to these ports to play midi data.
68 snd_emux_init_seq(snd_emux_t
*emu
, snd_card_t
*card
, int index
)
71 snd_seq_port_callback_t pinfo
;
74 sprintf(tmpname
, "%s WaveTable", emu
->name
);
75 emu
->client
= get_client(card
, index
, tmpname
);
76 if (emu
->client
< 0) {
77 snd_printk("can't create client\n");
81 if (emu
->num_ports
< 0) {
82 snd_printk("seqports must be greater than zero\n");
84 } else if (emu
->num_ports
>= SNDRV_EMUX_MAX_PORTS
) {
85 snd_printk("too many ports."
86 "limited max. ports %d\n", SNDRV_EMUX_MAX_PORTS
);
87 emu
->num_ports
= SNDRV_EMUX_MAX_PORTS
;
90 memset(&pinfo
, 0, sizeof(pinfo
));
91 pinfo
.owner
= THIS_MODULE
;
92 pinfo
.use
= snd_emux_use
;
93 pinfo
.unuse
= snd_emux_unuse
;
94 pinfo
.event_input
= snd_emux_event_input
;
96 for (i
= 0; i
< emu
->num_ports
; i
++) {
99 sprintf(tmpname
, "%s Port %d", emu
->name
, i
);
100 p
= snd_emux_create_port(emu
, tmpname
, MIDI_CHANNELS
,
103 snd_printk("can't create port\n");
107 p
->port_mode
= SNDRV_EMUX_PORT_MODE_MIDI
;
108 snd_emux_init_port(p
);
109 emu
->ports
[i
] = p
->chset
.port
;
110 emu
->portptrs
[i
] = p
;
118 * Detach from the ports that were set up for this synthesizer and
119 * destroy the kernel client.
122 snd_emux_detach_seq(snd_emux_t
*emu
)
125 snd_emux_terminate_all(emu
);
127 down(&emu
->register_mutex
);
128 if (emu
->client
>= 0) {
129 snd_seq_delete_kernel_client(emu
->client
);
132 up(&emu
->register_mutex
);
137 * create a sequencer port and channel_set
141 snd_emux_create_port(snd_emux_t
*emu
, char *name
,
142 int max_channels
, int oss_port
,
143 snd_seq_port_callback_t
*callback
)
148 /* Allocate structures for this channel */
149 if ((p
= kcalloc(1, sizeof(*p
), GFP_KERNEL
)) == NULL
) {
150 snd_printk("no memory\n");
153 p
->chset
.channels
= kcalloc(max_channels
, sizeof(snd_midi_channel_t
), GFP_KERNEL
);
154 if (p
->chset
.channels
== NULL
) {
155 snd_printk("no memory\n");
159 for (i
= 0; i
< max_channels
; i
++)
160 p
->chset
.channels
[i
].number
= i
;
161 p
->chset
.private_data
= p
;
162 p
->chset
.max_channels
= max_channels
;
164 p
->chset
.client
= emu
->client
;
165 #ifdef SNDRV_EMUX_USE_RAW_EFFECT
166 snd_emux_create_effect(p
);
168 callback
->private_free
= free_port
;
169 callback
->private_data
= p
;
171 cap
= SNDRV_SEQ_PORT_CAP_WRITE
;
173 type
= SNDRV_SEQ_PORT_TYPE_SPECIFIC
;
175 type
= DEFAULT_MIDI_TYPE
;
176 cap
|= SNDRV_SEQ_PORT_CAP_SUBS_WRITE
;
179 p
->chset
.port
= snd_seq_event_port_attach(emu
->client
, callback
,
180 cap
, type
, max_channels
,
181 emu
->max_voices
, name
);
188 * release memory block for port
191 free_port(void *private_data
)
197 #ifdef SNDRV_EMUX_USE_RAW_EFFECT
198 snd_emux_delete_effect(p
);
200 kfree(p
->chset
.channels
);
206 #define DEFAULT_DRUM_FLAGS (1<<9)
209 * initialize the port specific parameters
212 snd_emux_init_port(snd_emux_port_t
*p
)
214 p
->drum_flags
= DEFAULT_DRUM_FLAGS
;
217 snd_emux_reset_port(p
);
225 snd_emux_reset_port(snd_emux_port_t
*port
)
229 /* stop all sounds */
230 snd_emux_sounds_off_all(port
);
232 snd_midi_channel_set_clear(&port
->chset
);
234 #ifdef SNDRV_EMUX_USE_RAW_EFFECT
235 snd_emux_clear_effect(port
);
238 /* set port specific control parameters */
239 port
->ctrls
[EMUX_MD_DEF_BANK
] = 0;
240 port
->ctrls
[EMUX_MD_DEF_DRUM
] = 0;
241 port
->ctrls
[EMUX_MD_REALTIME_PAN
] = 1;
243 for (i
= 0; i
< port
->chset
.max_channels
; i
++) {
244 snd_midi_channel_t
*chan
= port
->chset
.channels
+ i
;
245 chan
->drum_channel
= ((port
->drum_flags
>> i
) & 1) ? 1 : 0;
251 * input sequencer event
254 snd_emux_event_input(snd_seq_event_t
*ev
, int direct
, void *private_data
,
257 snd_emux_port_t
*port
;
260 snd_assert(port
!= NULL
&& ev
!= NULL
, return -EINVAL
);
262 snd_midi_process_event(&emux_ops
, ev
, &port
->chset
);
269 * increment usage count
272 snd_emux_inc_count(snd_emux_t
*emu
)
275 if (!try_module_get(emu
->ops
.owner
))
277 if (!try_module_get(emu
->card
->module
)) {
278 module_put(emu
->ops
.owner
);
288 * decrease usage count
291 snd_emux_dec_count(snd_emux_t
*emu
)
293 module_put(emu
->card
->module
);
296 snd_emux_terminate_all(emu
);
297 module_put(emu
->ops
.owner
);
302 * Routine that is called upon a first use of a particular port
305 snd_emux_use(void *private_data
, snd_seq_port_subscribe_t
*info
)
311 snd_assert(p
!= NULL
, return -EINVAL
);
313 snd_assert(emu
!= NULL
, return -EINVAL
);
315 down(&emu
->register_mutex
);
316 snd_emux_init_port(p
);
317 snd_emux_inc_count(emu
);
318 up(&emu
->register_mutex
);
323 * Routine that is called upon the last unuse() of a particular port.
326 snd_emux_unuse(void *private_data
, snd_seq_port_subscribe_t
*info
)
332 snd_assert(p
!= NULL
, return -EINVAL
);
334 snd_assert(emu
!= NULL
, return -EINVAL
);
336 down(&emu
->register_mutex
);
337 snd_emux_sounds_off_all(p
);
338 snd_emux_dec_count(emu
);
339 up(&emu
->register_mutex
);
345 * Create a sequencer client
348 get_client(snd_card_t
*card
, int index
, char *name
)
350 snd_seq_client_callback_t callbacks
;
351 snd_seq_client_info_t cinfo
;
354 memset(&callbacks
, 0, sizeof(callbacks
));
355 callbacks
.private_data
= NULL
;
356 callbacks
.allow_input
= 1;
357 callbacks
.allow_output
= 1;
359 /* Find a free client, start from 1 as the MPU expects to use 0 */
360 client
= snd_seq_create_kernel_client(card
, index
, &callbacks
);
364 memset(&cinfo
, 0, sizeof(cinfo
));
365 cinfo
.client
= client
;
366 cinfo
.type
= KERNEL_CLIENT
;
367 strcpy(cinfo
.name
, name
);
368 snd_seq_kernel_client_ctl(client
, SNDRV_SEQ_IOCTL_SET_CLIENT_INFO
, &cinfo
);
375 * attach virtual rawmidi devices
377 int snd_emux_init_virmidi(snd_emux_t
*emu
, snd_card_t
*card
)
382 if (emu
->midi_ports
<= 0)
385 emu
->vmidi
= kcalloc(emu
->midi_ports
, sizeof(snd_rawmidi_t
*), GFP_KERNEL
);
386 if (emu
->vmidi
== NULL
)
389 for (i
= 0; i
< emu
->midi_ports
; i
++) {
390 snd_rawmidi_t
*rmidi
;
391 snd_virmidi_dev_t
*rdev
;
392 if (snd_virmidi_new(card
, emu
->midi_devidx
+ i
, &rmidi
) < 0)
394 rdev
= rmidi
->private_data
;
395 sprintf(rmidi
->name
, "%s Synth MIDI", emu
->name
);
396 rdev
->seq_mode
= SNDRV_VIRMIDI_SEQ_ATTACH
;
397 rdev
->client
= emu
->client
;
398 rdev
->port
= emu
->ports
[i
];
399 if (snd_device_register(card
, rmidi
) < 0) {
400 snd_device_free(card
, rmidi
);
403 emu
->vmidi
[i
] = rmidi
;
404 //snd_printk("virmidi %d ok\n", i);
409 //snd_printk("error init..\n");
410 snd_emux_delete_virmidi(emu
);
414 int snd_emux_delete_virmidi(snd_emux_t
*emu
)
418 if (emu
->vmidi
== NULL
)
421 for (i
= 0; i
< emu
->midi_ports
; i
++) {
423 snd_device_free(emu
->card
, emu
->vmidi
[i
]);