Skip to contentSkip to conversation

MIDI timing question

DISCUSSION Mark Knecht 2 participants 4 messages Aug 27, 2003 – Aug 30, 2003

lore
Share & tools
lore
#0 lore raw

Hi,
Does anyone have a simple page I could read with an explanation about how
one would go about transmitting a given number of MIDI events at a known tempo using the Alsa MIDI services? How does one transmit at 92bpm with say 16th note accuracy and really get the events to be timed correctly on the bus?

Is this possible to do totally within Alsa, or do developers have to
create their own time base and then every MIDI event just goes out raw?

I understand there is possibly a 'sequencer' interface, but when I look
at the online Alsa documentation I seem to be missing it.

Please keep any answers extremely simple. I'm just looking for a little
really basic education.

Thanks in advance,
Mark

-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf

#1 lore raw
Quoted 3 lines · Mark Knecht

Does anyone have a simple page I could read with an explanation about how
one would go about transmitting a given number of MIDI events at a known tempo using the Alsa MIDI services?

You'd use the sequencer. There isn't an easy HOWTO that I'd know of.

To send some events at a specific time:
- open the sequencer
- create your own (source) port
- create a queue
- set the queue's tempo
- send some events through the queue, and start it

To open the sequencer, call snd_seq_open.
(You can get your client number with snd_seq_client_id.)

	snd_seq_t seq;
	snd_seq_open(&seq, "default", SND_SEQ_OPEN_DUPLEX, 0);

To create a port, allocate a port info object with snd_seq_port_info_alloca, set port parameters with snd_seq_port_info_set_xxx, and call snd_seq_create_port. Or simply call snd_seq_create_simple_port.

	int port;
	port = snd_seq_create_simple_port(seq, "my port",
		SND_SEQ_PORT_CAP_READ | SND_SEQ_POR_CAP_WRITE,
		SND_SEQ_PORT_TYPE_APPLICATION);

To create a queue, allocate a queue info object with snd_seq_queue_info_alloca, set queue parameters with snd_seq_queue_info_set_xxx, and call snd_seq_create_queue. Or simply call snd_seq_alloc_queue.

	int queue;
	queue = snd_seq_alloc_queue(seq);

To set the queue's tempo, allocate a queue tempo object with snd_seq_queue_tempo_alloca, set tempo parameters with snd_seq_queue_tempo_set_xxx, and call snd_seq_set_queue_tempo.

	snd_seq_queue_tempo_t *tempo;
	snd_seq_queue_tempo_alloca(&tempo);
	snd_seq_queue_tempo_set_ppq(tempo, 4); /* ??? */
	snd_seq_queue_tempo_set_tempo(tempo, 92);
	snd_seq_set_queue_tempo(seq, queue, tempo);

To send an event though the queue, allocate an event structure (just for a change, you can use a local snd_seq_event_t variable), call snd_seq_ev_clear, call snd_seq_ev_set_dest, call snd_seq_ev_schedule_tick, and specify the actual event you want to send (probably with snd_seq_ev_set_note). Then call snd_seq_event_output, and snd_seq_drain_output after you've sent all events.

	snd_seq_event_t ev;
	snd_seq_ev_clear(&ev);
	snd_seq_ev_set_dest(&ev, 64, 0); /* 64:0 */
	snd_seq_ev_schedule_tick(&ev, queue, 0, 4); /* starts at 4 ticks */
	snd_seq_ev_set_note(&ev, 0, 60, 127, 8); /* length 8 ticks */
	snd_seq_event_output(seq, &ev);

	snd_seq_ev_schedule_tick(&ev, queue, 0, 12);
	snd_seq_ev_set_note(&ev, 0, 67, 127, 8);
	snd_seq_event_output(seq, &ev);

	snd_seq_drain_output(seq);

To start a queue, call snd_seq_start_queue and snd_seq_drain_output.

	snd_seq_start_queue(seq, queue, NULL);
	snd_seq_drain_output(seq);

	/* wait until the notes are played before exiting */

This is off the top of my head, so I'll have to check if this works myself ... ;-)

HTH
Clemens

-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf

#2 lore raw

Clemens,
Thanks very much for writing all this up! I'll study it and maybe come
back with a few questions.

With best regards,
Mark

Quoted 15 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]]On Behalf Of Clemens Ladisch
Sent: Thursday, August 28, 2003 8:56 AM
To: Mark Knecht
Cc: Alsa-Devel
Subject: Re: [Alsa-devel] MIDI timing question

Quoted 1 line · Mark Knecht

Does anyone have a simple page I could read with an

explanation about how

Quoted 2 lines

one would go about transmitting a given number of MIDI events at a known tempo using the Alsa MIDI services?

Quoted 9 lines

You'd use the sequencer. There isn't an easy HOWTO that I'd know of.

To send some events at a specific time:
- open the sequencer
- create your own (source) port
- create a queue
- set the queue's tempo
- send some events through the queue, and start it

<SNIP>

-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf

#3 lore raw
In reply to Clemens Ladisch
Quoted 2 lines · I wrote:

This is off the top of my head, so I'll have to check if this works myself ... ;-)

Well, _of course_ I made errors.

Quoted 1 line
	snd_seq_queue_tempo_set_tempo(tempo, 92);

The tempo value isn't bpm but the MIDI tempo value, i.e., the number of microseconds per quarter note. The correct value would be 652174 (60000000 / 92).

Quoted 1 line

To send an event ...

There is a problem with ALSA's rawmidi ports: ALSA doesn't know if an event to be sent is the last one, so it opens and closes the MIDI port for every event sent to it. This isn't good because it resets the port when closing.

The solution is to make a connection (subscription) to the port, so that it's opened during the entire execution time of the program:

	snd_seq_connect_to(seq, port, 96, 0);

This will cause all events sent "to the subscribers of our port" to be routed to port 96:0.

Quoted 1 line
	snd_seq_ev_set_dest(&ev, 64, 0); /* 64:0 */

I forgot to set the source port (but our port has number 0 anyway, so it didn't matter this time).

And now we sent "to all subscribers" instead of "directly to port 96:0":

	snd_seq_ev_set_subs(&ev);
	snd_seq_ev_set_source(&ev, port);
Quoted 2 lines
	snd_seq_ev_schedule_tick(&ev, queue, 0, 12);
	snd_seq_ev_set_note(&ev, 0, 67, 127, 8);

Instead of using the snd_seq_ev_xxx macros, one can use the fields of the event structure directly:

	ev.time.tick = 12;
	ev.data.note.note = 67;

I have attached a little program which actually works (on my computer, your destination port probably isn't 96:0).

HTH
Clemens

#include <alsa/asoundlib.h>
#include <unistd.h>

int main()

{
	snd_seq_t* seq;
	int port, queue;
	snd_seq_queue_tempo_t* tempo;
	snd_seq_event_t ev;

	snd_seq_open(&seq, "default", SND_SEQ_OPEN_DUPLEX, 0);

	port = snd_seq_create_simple_port(seq, "my port",
					  SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_WRITE,
					  SND_SEQ_PORT_TYPE_APPLICATION);

	queue = snd_seq_alloc_queue(seq);

	snd_seq_queue_tempo_alloca(&tempo);
	snd_seq_queue_tempo_set_tempo(tempo, 60000000 / 96);
	snd_seq_queue_tempo_set_ppq(tempo, 4);
	snd_seq_set_queue_tempo(seq, queue, tempo);

	snd_seq_connect_to(seq, port, 96, 0);

	snd_seq_start_queue(seq, queue, NULL);
	/*snd_seq_drain_output(seq);*/

	snd_seq_ev_clear(&ev);
	snd_seq_ev_set_subs(&ev);
	snd_seq_ev_set_source(&ev, port);

	snd_seq_ev_schedule_tick(&ev, queue, 0, 4);
	snd_seq_ev_set_note(&ev, 0, 60, 127, 8);
	snd_seq_event_output(seq, &ev);

	ev.time.tick = 12;
	ev.data.note.note = 67;
	snd_seq_event_output(seq, &ev);

	snd_seq_drain_output(seq);

	sleep(4);
	return 0;
}