alsa sequencer: How to eliminate choking
Hello, everyone!
Before describing my problem, I would like to comment on the great degree of transparancy of the ALSA library. Sure enough, it took a while to get used to its wealth, but the example programs + the former posts on this list give great indications as to how to make use of this wealth.
I am using ALSA to stream pieces of music contained in a class called score to an ALSA client with a port of type MIDI_GENERIC. One way to construct a score is from a Midi file, so I can compare the sound of playing the Midi file with pmidi with the result of streaming a score constructed from that midifile to the alsa sequencer. The streaming works for pieces that dont contain many parts, but when many notes are played simultaneously (e.g. Beethoven's 5 Symphony, Movement 1), the order of notes gets mixed up and the output devices choke (same problem for a keyboard and with timidity). However, the midifile I made the score from plays nicely with pmidi. I am pretty convinced that the problem is not queue overflow, since I flush queues every two seconds.
Does someone have an idea where the choking could come from and what measures can be used to get rid of it?
There are two more items I have been wondering about that dont appear worth to put into a post of their own.
Is there a Macro that tells me whether an ALSA sequencer client is an internal or external device? Thus, a user could choose what kind of device to play to (without having to specify client and port). Second, is ALSA used purely on linux or also on OS X or BSD?
If not, are there indications that it may be in the future?
Thanks in advance for replies to these questions,
Michael Geis
---------------------------------
Do you Yahoo!?
Read only the mail you want - Yahoo! Mail SpamGuard.
Quoted 3 lines · Michael Geis
The streaming works for pieces that dont contain many parts, but when many notes are played simultaneously (e.g. Beethoven's 5 Symphony, Movement 1), the order of notes gets mixed up
This shouldn't happens, the ordering of events in a queue is stable.
Quoted 3 lines
and the output devices choke (same problem for a keyboard and with timidity). However, the midifile I made the score from plays nicely with pmidi.
How do you send the events? Is the timing done with a queue, or do you send the events directly?
Quoted 2 lines
I am pretty convinced that the problem is not queue overflow, since I flush queues every two seconds.
What do you mean by "flush"?
Quoted 2 lines
Is there a Macro that tells me whether an ALSA sequencer client is an internal or external device?
No.
Drivers for internal devices often know the number of voices etc., but these information fields are optional.
Quoted 2 lines
Thus, a user could choose what kind of device to play to (without having to specify client and port).
And what if there is more than one device of a specific type?
Just show a list and allow the user to select from it.
Quoted 1 line
Second, is ALSA used purely on linux or also on OS X or BSD?
The "L" stands for Linux. :)
In theory, it would be possible to write plugins for other driver architectures (like the Jack pcm plugin).
Clemens
-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now.
http://productguide.itmanagersjournal.com/
Quoted 9 lines · Michael Geis
The streaming works for pieces that dont contain many parts, but when many notes are played simultaneously (e.g. Beethoven's 5 Symphony, Movement 1), the order of notes gets mixed up and the output devices choke (same problem for a keyboard and with timidity). However, the midifile I made the score from plays nicely with pmidi.
I am pretty convinced that the problem is not queue overflow, since I flush queues every two seconds.
The problem apparently lays with the event feeding loops in your app. This is more likely to arise with midi files containing several tracks and many notes.
When the user space (external) output buffer becomes full (or when you explicitely call snd_seq_drain_output), this buffer's contents are passed over to the sequencer's internal memory pool. At this moment your app (or midi thread) may have to wait if there is not enough room to complete the transfer instantly. When the contents of the buffer have been successfully transferred, you app will catch up feeding the buffer with scheduled events. If your events are scheduled relative, ev->time.tick is just the delay between the call to snd_seq_event_output and the actual sound production. So if a transfer pause occurred before this call, the event might be processed later than you expected. The tracks might even get mixed up.
Two solutions are:
1. Either you monitor more strictly the calls to snd_seq_event_output and snd_seq_drain_output so that you are sure that you will never have to wait for the transfer.
2. Either you schedule your events absolute, that is relative=0 in snd_seq_ev_schedule_tick(ev, q, relative, ttick ). ev->time.tick is then the time interval between the time when you called snd_seq_start_queue and the actual sound production. snd_seq_get_queue_status and snd_seq_queue_status_get_tick_time might then come in handy if you have more than a straightforward scheduling.
Quoted 3 lines
Does someone have an idea where the choking could come from and what measures can be used to get rid of it?
Hope this helps.
It is based on what I found in the mailing list as well as my own experiments, so don't hesitate to tell me if I am plain wrong.
Alexandre Oberlin
-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now.
http://productguide.itmanagersjournal.com/
Thanks a lot Alexandre. I will make sure I am really doing absolute
timing (which I thought I was).
Michael
---------------------------------
Do you Yahoo!?
Send holiday email and support a worthy cause. Do good.