2 * Copyright (C) 2014 Antonio Ospite <ao2@ao2.it>
4 * gstalsamidisrc.c: Source element for ALSA MIDI sequencer events
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 * SECTION:element-alsamidisrc
24 * @see_also: #GstPushSrc
26 * The alsamidisrc element is an element that fetches ALSA MIDI sequencer
27 * events and makes them available to elements understanding
28 * audio/x-midi-events in their sink pads.
30 * It can be used to generate notes from a MIDI input device.
32 * ## Example launch line
34 * gst-launch -v alsamidisrc ports=129:0 ! fluiddec ! audioconvert ! autoaudiosink
36 * This pipeline will listen for events from the sequencer device at port 129:0,
37 * and generate notes using the fluiddec element.
45 #include "gstalsaelements.h"
46 #include "gstalsamidisrc.h"
48 GST_DEBUG_CATEGORY_STATIC (gst_alsa_midi_src_debug);
49 #define GST_CAT_DEFAULT gst_alsa_midi_src_debug
52 * The MIDI specification declares some status bytes undefined:
54 * - 0xF4 System common - Undefined (Reserved)
55 * - 0xF5 System common - Undefined (Reserved)
56 * - 0xF9 System real-time - Undefined (Reserved)
57 * - 0xFD System real-time - Undefined (Reserved)
59 * See: http://www.midi.org/techspecs/midimessages.php#2
61 * Some other documents define status 0xf9 as a tick message with a period of
64 * - http://www.blitter.com/~russtopia/MIDI/~jglatt/tech/midispec/tick.htm
65 * - http://www.sequencer.de/synth/index.php/MIDI_Format#0xf9_-_MIDI_Tick
67 * Even if non-standard it looks like this convention is quite widespread.
69 * For instance Fluidsynth uses 0xF9 as a "midi tick" message:
70 * http://sourceforge.net/p/fluidsynth/code-git/ci/master/tree/fluidsynth/src/midi/fluid_midi.h#l62
72 * And then so does the midiparse element in order to be compatible with
73 * Fluidsynth and the fluiddec element.
75 * Do the same to behave like midiparse.
77 #define MIDI_TICK 0xf9
78 #define MIDI_TICK_PERIOD_MS 10
80 /* Functions specific to the Alsa MIDI sequencer API */
82 #define DEFAULT_BUFSIZE 65536
83 #define DEFAULT_CLIENT_NAME "alsamidisrc"
86 init_seq (GstAlsaMidiSrc * alsamidisrc)
90 ret = snd_seq_open (&alsamidisrc->seq, "default", SND_SEQ_OPEN_DUPLEX, 0);
92 GST_ERROR_OBJECT (alsamidisrc, "Cannot open sequencer - %s",
98 * Prevent Valgrind from reporting cached configuration as memory leaks, see:
99 * http://git.alsa-project.org/?p=alsa-lib.git;a=blob;f=MEMORY-LEAK;hb=HEAD
101 snd_config_update_free_global ();
103 ret = snd_seq_set_client_name (alsamidisrc->seq, DEFAULT_CLIENT_NAME);
105 GST_ERROR_OBJECT (alsamidisrc, "Cannot set client name - %s",
107 goto error_seq_close;
113 snd_seq_close (alsamidisrc->seq);
118 /* Parses one or more port addresses from the string */
120 parse_ports (const char *arg, GstAlsaMidiSrc * alsamidisrc)
126 GST_DEBUG_OBJECT (alsamidisrc, "ports: %s", arg);
129 * Assume that ports are separated by commas.
131 * Commas are used instead of spaces because spaces are valid in client
134 ports_list = g_strsplit (arg, ",", 0);
136 alsamidisrc->port_count = g_strv_length (ports_list);
137 alsamidisrc->seq_ports = g_try_new (snd_seq_addr_t, alsamidisrc->port_count);
138 if (!alsamidisrc->seq_ports) {
139 GST_ERROR_OBJECT (alsamidisrc, "Out of memory");
141 goto out_free_ports_list;
144 for (i = 0; i < alsamidisrc->port_count; i++) {
145 gchar *port_name = ports_list[i];
147 ret = snd_seq_parse_address (alsamidisrc->seq, &alsamidisrc->seq_ports[i],
150 GST_ERROR_OBJECT (alsamidisrc, "Invalid port %s - %s", port_name,
152 goto error_free_seq_ports;
156 goto out_free_ports_list;
158 error_free_seq_ports:
159 g_free (alsamidisrc->seq_ports);
161 g_strfreev (ports_list);
166 start_queue_timer (GstAlsaMidiSrc * alsamidisrc)
170 ret = snd_seq_start_queue (alsamidisrc->seq, alsamidisrc->queue, NULL);
172 GST_ERROR_OBJECT (alsamidisrc, "Timer event output error: %s",
177 ret = snd_seq_drain_output (alsamidisrc->seq);
179 GST_ERROR_OBJECT (alsamidisrc, "Drain output error: %s",
186 schedule_next_tick (GstAlsaMidiSrc * alsamidisrc)
189 snd_seq_real_time_t time;
192 snd_seq_ev_clear (&ev);
193 snd_seq_ev_set_source (&ev, 0);
194 snd_seq_ev_set_dest (&ev, snd_seq_client_id (alsamidisrc->seq), 0);
196 ev.type = SND_SEQ_EVENT_TICK;
198 alsamidisrc->tick += 1;
199 GST_TIME_TO_TIMESPEC (alsamidisrc->tick * MIDI_TICK_PERIOD_MS * GST_MSECOND,
202 snd_seq_ev_schedule_real (&ev, alsamidisrc->queue, 0, &time);
204 ret = snd_seq_event_output (alsamidisrc->seq, &ev);
206 GST_ERROR_OBJECT (alsamidisrc, "Event output error: %s",
209 ret = snd_seq_drain_output (alsamidisrc->seq);
211 GST_ERROR_OBJECT (alsamidisrc, "Event drain error: %s", snd_strerror (ret));
215 create_port (GstAlsaMidiSrc * alsamidisrc)
217 snd_seq_port_info_t *pinfo;
220 snd_seq_port_info_alloca (&pinfo);
221 snd_seq_port_info_set_name (pinfo, DEFAULT_CLIENT_NAME);
222 snd_seq_port_info_set_type (pinfo,
223 SND_SEQ_PORT_TYPE_MIDI_GENERIC | SND_SEQ_PORT_TYPE_APPLICATION);
224 snd_seq_port_info_set_capability (pinfo,
225 SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE);
227 ret = snd_seq_alloc_named_queue (alsamidisrc->seq, DEFAULT_CLIENT_NAME);
229 GST_ERROR_OBJECT (alsamidisrc, "Cannot allocate queue: %s",
235 * Sequencer queues are "per-system" entities, so it's important to remember
236 * the queue id to make sure alsamidisrc refers to this very one in future
237 * operations, and not to some other port created by another sequencer user.
239 alsamidisrc->queue = ret;
241 snd_seq_port_info_set_timestamping (pinfo, 1);
242 snd_seq_port_info_set_timestamp_real (pinfo, 1);
243 snd_seq_port_info_set_timestamp_queue (pinfo, alsamidisrc->queue);
245 ret = snd_seq_create_port (alsamidisrc->seq, pinfo);
247 GST_ERROR_OBJECT (alsamidisrc, "Cannot create port - %s",
253 * Conversely, it's not strictly necessary to remember the port id because
254 * ports are per-client and alsamidisrc is only creating one port (id = 0).
256 * If multiple ports were to be created, the ids could be retrieved with
259 * alsamidisrc->port = snd_seq_port_info_get_port(pinfo);
262 ret = start_queue_timer (alsamidisrc);
264 GST_ERROR_OBJECT (alsamidisrc, "Cannot start timer for queue: %d - %s",
265 alsamidisrc->queue, snd_strerror (ret));
271 connect_ports (GstAlsaMidiSrc * alsamidisrc)
276 for (i = 0; i < alsamidisrc->port_count; ++i) {
278 snd_seq_connect_from (alsamidisrc->seq, 0,
279 alsamidisrc->seq_ports[i].client, alsamidisrc->seq_ports[i].port);
281 /* Issue a warning and try the other ports */
282 GST_WARNING_OBJECT (alsamidisrc, "Cannot connect from port %d:%d - %s",
283 alsamidisrc->seq_ports[i].client, alsamidisrc->seq_ports[i].port,
288 /* GStreamer specific functions */
290 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
293 GST_STATIC_CAPS ("audio/x-midi-event"));
295 #define DEFAULT_PORTS NULL
304 #define gst_alsa_midi_src_parent_class parent_class
305 G_DEFINE_TYPE_WITH_CODE (GstAlsaMidiSrc, gst_alsa_midi_src, GST_TYPE_PUSH_SRC,
306 GST_DEBUG_CATEGORY_INIT (gst_alsa_midi_src_debug, "alsamidisrc", 0,
307 "alsamidisrc element"));
308 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (alsamidisrc, "alsamidisrc",
309 GST_RANK_PRIMARY, GST_TYPE_ALSA_MIDI_SRC, alsa_element_init (plugin));
311 static void gst_alsa_midi_src_set_property (GObject * object, guint prop_id,
312 const GValue * value, GParamSpec * pspec);
313 static void gst_alsa_midi_src_get_property (GObject * object, guint prop_id,
314 GValue * value, GParamSpec * pspec);
316 static gboolean gst_alsa_midi_src_start (GstBaseSrc * basesrc);
317 static gboolean gst_alsa_midi_src_stop (GstBaseSrc * basesrc);
318 static gboolean gst_alsa_midi_src_unlock (GstBaseSrc * basesrc);
319 static gboolean gst_alsa_midi_src_unlock_stop (GstBaseSrc * basesrc);
320 static void gst_alsa_midi_src_state_changed (GstElement * element,
321 GstState oldstate, GstState newstate, GstState pending);
324 gst_alsa_midi_src_create (GstPushSrc * src, GstBuffer ** buf);
327 gst_alsa_midi_src_class_init (GstAlsaMidiSrcClass * klass)
329 GObjectClass *gobject_class;
330 GstElementClass *gstelement_class;
331 GstBaseSrcClass *gstbase_src_class;
332 GstPushSrcClass *gstpush_src_class;
334 gobject_class = G_OBJECT_CLASS (klass);
335 gstelement_class = GST_ELEMENT_CLASS (klass);
336 gstbase_src_class = GST_BASE_SRC_CLASS (klass);
337 gstpush_src_class = GST_PUSH_SRC_CLASS (klass);
339 gobject_class->set_property = gst_alsa_midi_src_set_property;
340 gobject_class->get_property = gst_alsa_midi_src_get_property;
342 g_object_class_install_property (gobject_class, PROP_PORTS,
343 g_param_spec_string ("ports", "Ports",
344 "Comma separated list of sequencer ports (e.g. client:port,...)",
345 DEFAULT_PORTS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
347 gst_element_class_set_static_metadata (gstelement_class,
350 "Push ALSA MIDI sequencer events around", "Antonio Ospite <ao2@ao2.it>");
351 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
353 gstbase_src_class->start = GST_DEBUG_FUNCPTR (gst_alsa_midi_src_start);
354 gstbase_src_class->stop = GST_DEBUG_FUNCPTR (gst_alsa_midi_src_stop);
355 gstbase_src_class->unlock = GST_DEBUG_FUNCPTR (gst_alsa_midi_src_unlock);
356 gstbase_src_class->unlock_stop =
357 GST_DEBUG_FUNCPTR (gst_alsa_midi_src_unlock_stop);
358 gstpush_src_class->create = GST_DEBUG_FUNCPTR (gst_alsa_midi_src_create);
359 gstelement_class->state_changed =
360 GST_DEBUG_FUNCPTR (gst_alsa_midi_src_state_changed);
364 gst_alsa_midi_src_init (GstAlsaMidiSrc * alsamidisrc)
366 alsamidisrc->ports = DEFAULT_PORTS;
368 gst_base_src_set_format (GST_BASE_SRC (alsamidisrc), GST_FORMAT_TIME);
369 gst_base_src_set_live (GST_BASE_SRC (alsamidisrc), TRUE);
373 gst_alsa_midi_src_set_property (GObject * object, guint prop_id,
374 const GValue * value, GParamSpec * pspec)
378 src = GST_ALSA_MIDI_SRC (object);
383 src->ports = g_value_dup_string (value);
386 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
392 gst_alsa_midi_src_get_property (GObject * object, guint prop_id, GValue * value,
397 g_return_if_fail (GST_IS_ALSA_MIDI_SRC (object));
399 src = GST_ALSA_MIDI_SRC (object);
403 g_value_set_string (value, src->ports);
406 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
412 push_buffer (GstAlsaMidiSrc * alsamidisrc, gpointer data, guint size,
413 GstClockTime time, GstBufferList * buffer_list)
418 buffer = gst_buffer_new ();
420 GST_BUFFER_DTS (buffer) = time;
421 GST_BUFFER_PTS (buffer) = time;
423 local_data = g_memdup2 (data, size);
425 gst_buffer_append_memory (buffer,
426 gst_memory_new_wrapped (0, local_data, size, 0, size, local_data,
429 GST_MEMDUMP_OBJECT (alsamidisrc, "MIDI data:", local_data, size);
431 gst_buffer_list_add (buffer_list, buffer);
435 push_tick_buffer (GstAlsaMidiSrc * alsamidisrc, GstClockTime time,
436 GstBufferList * buffer_list)
438 alsamidisrc->buffer[0] = MIDI_TICK;
439 push_buffer (alsamidisrc, alsamidisrc->buffer, 1, time, buffer_list);
443 gst_alsa_midi_src_create (GstPushSrc * src, GstBuffer ** buf)
445 GstAlsaMidiSrc *alsamidisrc;
446 GstBufferList *buffer_list;
453 alsamidisrc = GST_ALSA_MIDI_SRC (src);
455 buffer_list = gst_buffer_list_new ();
458 ret = gst_poll_wait (alsamidisrc->poll, GST_CLOCK_TIME_NONE);
460 if (ret < 0 && errno == EBUSY) {
461 GST_INFO_OBJECT (alsamidisrc, "flushing");
462 gst_buffer_list_unref (buffer_list);
463 return GST_FLOW_FLUSHING;
465 GST_ERROR_OBJECT (alsamidisrc, "ERROR in poll: %s", strerror (errno));
467 /* There are events available */
469 snd_seq_event_t *event;
470 err = snd_seq_event_input (alsamidisrc->seq, &event);
472 break; /* Processed all events */
475 time = GST_TIMESPEC_TO_TIME (event->time.time) - alsamidisrc->delay;
478 * Special handling is needed because decoding SND_SEQ_EVENT_TICK is
479 * not supported by alsa-lib.
481 if (event->type == SND_SEQ_EVENT_TICK) {
482 push_tick_buffer (alsamidisrc, time, buffer_list);
483 schedule_next_tick (alsamidisrc);
488 snd_midi_event_decode (alsamidisrc->parser, alsamidisrc->buffer,
489 DEFAULT_BUFSIZE, event);
491 /* ENOENT indicates an event that is not a MIDI message, silently skip it */
492 if (-ENOENT == size_ev) {
493 GST_WARNING_OBJECT (alsamidisrc,
494 "Warning: Received non-MIDI message");
497 GST_ERROR_OBJECT (alsamidisrc,
498 "Error decoding event from ALSA to output: %s",
499 strerror (-size_ev));
503 push_buffer (alsamidisrc, alsamidisrc->buffer, size_ev, time,
510 len = gst_buffer_list_length (buffer_list);
514 /* Pop the _last_ buffer in the list */
515 *buf = gst_buffer_copy (gst_buffer_list_get (buffer_list, len - 1));
516 gst_buffer_list_remove (buffer_list, len - 1, 1);
520 * If there are no more buffers left, free the list, otherwise push all the
521 * _previous_ buffers left in the list.
523 * The one popped above will be pushed last when this function returns.
526 gst_buffer_list_unref (buffer_list);
528 gst_pad_push_list (GST_BASE_SRC (src)->srcpad, buffer_list);
533 gst_buffer_list_unref (buffer_list);
534 return GST_FLOW_ERROR;
538 gst_alsa_midi_src_start (GstBaseSrc * basesrc)
540 GstAlsaMidiSrc *alsamidisrc;
543 alsamidisrc = GST_ALSA_MIDI_SRC (basesrc);
545 alsamidisrc->tick = 0;
546 alsamidisrc->port_count = 0;
548 ret = init_seq (alsamidisrc);
552 if (alsamidisrc->ports) {
553 ret = parse_ports (alsamidisrc->ports, alsamidisrc);
555 goto error_seq_close;
558 ret = create_port (alsamidisrc);
560 goto error_free_seq_ports;
562 connect_ports (alsamidisrc);
564 ret = snd_seq_nonblock (alsamidisrc->seq, 1);
566 GST_ERROR_OBJECT (alsamidisrc, "Cannot set nonblock mode - %s",
568 goto error_free_seq_ports;
571 snd_midi_event_new (DEFAULT_BUFSIZE, &alsamidisrc->parser);
572 snd_midi_event_init (alsamidisrc->parser);
573 snd_midi_event_reset_decode (alsamidisrc->parser);
575 snd_midi_event_no_status (alsamidisrc->parser, 1);
577 alsamidisrc->buffer = g_try_malloc (DEFAULT_BUFSIZE);
578 if (alsamidisrc->buffer == NULL)
579 goto error_free_parser;
585 npfds = snd_seq_poll_descriptors_count (alsamidisrc->seq, POLLIN);
586 pfds = g_newa (struct pollfd, npfds);
588 snd_seq_poll_descriptors (alsamidisrc->seq, pfds, npfds, POLLIN);
590 alsamidisrc->poll = gst_poll_new (TRUE);
591 for (i = 0; i < npfds; ++i) {
592 GstPollFD fd = GST_POLL_FD_INIT;
595 gst_poll_add_fd (alsamidisrc->poll, &fd);
596 gst_poll_fd_ctl_read (alsamidisrc->poll, &fd, TRUE);
597 gst_poll_fd_ctl_write (alsamidisrc->poll, &fd, FALSE);
604 snd_midi_event_free (alsamidisrc->parser);
605 error_free_seq_ports:
606 g_free (alsamidisrc->seq_ports);
608 snd_seq_close (alsamidisrc->seq);
614 gst_alsa_midi_src_stop (GstBaseSrc * basesrc)
616 GstAlsaMidiSrc *alsamidisrc;
618 alsamidisrc = GST_ALSA_MIDI_SRC (basesrc);
620 if (alsamidisrc->poll != NULL) {
621 gst_poll_free (alsamidisrc->poll);
622 alsamidisrc->poll = NULL;
624 g_free (alsamidisrc->ports);
625 g_free (alsamidisrc->buffer);
626 snd_midi_event_free (alsamidisrc->parser);
627 g_free (alsamidisrc->seq_ports);
628 snd_seq_close (alsamidisrc->seq);
634 gst_alsa_midi_src_unlock (GstBaseSrc * basesrc)
636 GstAlsaMidiSrc *alsamidisrc = GST_ALSA_MIDI_SRC (basesrc);
638 gst_poll_set_flushing (alsamidisrc->poll, TRUE);
643 gst_alsa_midi_src_unlock_stop (GstBaseSrc * basesrc)
645 GstAlsaMidiSrc *alsamidisrc = GST_ALSA_MIDI_SRC (basesrc);
647 gst_poll_set_flushing (alsamidisrc->poll, FALSE);
652 gst_alsa_midi_src_state_changed (GstElement * element, GstState oldstate,
653 GstState newstate, GstState pending)
655 GstAlsaMidiSrc *alsamidisrc;
657 alsamidisrc = GST_ALSA_MIDI_SRC (element);
659 if (newstate == GST_STATE_PLAYING) {
660 GstClockTime gst_time;
661 GstClockTime base_time;
662 GstClockTime running_time;
663 GstClockTime queue_time;
665 snd_seq_queue_status_t *status;
667 clock = gst_element_get_clock (element);
669 GST_WARNING_OBJECT (element, "No clock present");
672 gst_time = gst_clock_get_time (clock);
673 gst_object_unref (clock);
674 base_time = gst_element_get_base_time (element);
675 running_time = gst_time - base_time;
677 snd_seq_queue_status_malloc (&status);
678 snd_seq_get_queue_status (alsamidisrc->seq, alsamidisrc->queue, status);
680 GST_TIMESPEC_TO_TIME (*snd_seq_queue_status_get_real_time (status));
681 snd_seq_queue_status_free (status);
684 * The fact that the ALSA sequencer queue started before the pipeline
685 * transition to the PLAYING state ensures that the pipeline delay is
688 alsamidisrc->delay = queue_time - running_time;
690 if (alsamidisrc->tick == 0) {
691 schedule_next_tick (alsamidisrc);