Skip to contentSkip to conversation

External MIDI Sync using OSS/Free

linux-audio-dev Paul Barton-Davis 3 participants 5 messages Oct 26, 1999 Sound

lore
Share & tools
lore
#0 lore raw
Quoted 3 lines

My sequencer (http://www.div8.net/ttrk) mainly runs off of external MIDI
sync, and drives a mess of synths and stuff. I use /dev/sequencer for MIDI input and output.

advice #1: do not use /dev/sequencer

two reasons. it doesn't exist in ALSA, which will (soon-ish) be the default sound driver system for Linux. but more importantly, its a horrible system. i won't list the problems with it here, but there are many.

instead, just let the user specify the MIDI device files to use, and then use them as "raw" read(2)/write(2) files. this works with OSS, ALSA, Solaris and a number of other systems. then do most of your timing stuff at user level. i don't know what your resolution needs to be, but my "analog MIDI sequencer" program SoftWerk does all its timing in user space (it has too - it doesn't know what to send to the MIDI interface until its actually time to send it) and is accurate down to about 20ms without any special priviledges. it can get a lot more accurate than that if it needs to. OTOH, SoftWerk is an "old school" sequencer, from back before the days of the first digital sequencers. it has a fixed number of event steps per fixed number of event steps, which is not what most people these days mean by "sequencer".

advice #2: if you really have external MIDI sync (do you mean MIDI clock, or other stuff ?), then just use that to provide timing for your application. if you have sensible hardware for your MIDI interface, then your application will be woken up very rapidly (at least if it runs SCHED_FIFO) after the MIDI clock byte (or any other MIDI message) is ready to read.

if you don't have external clock, then you'd be better off using the real time clock for timing.

advice 3: install ALSA, and use the ALSA sequencer. its a vastly more evolved kernel sequencer system, though its still going through some beta-testing. we would welcome your involvement in that process.

Quoted 4 lines

Is there a way that I could do this more efficiently? Since my app
might not get swapped in until late after the start arrived, and then I have to call the ioctl. This error then remains for the entire duration of the song.

use SCHED_FIFO if you can. the only downside is that it does require that either your program, or a "helper/starter" program run setuid root. this will reduce to the delay between your program being "ready to run because MIDI data is available" and "running" to a virtually insignificant time.

--p

#1 lore raw
Quoted 1 line · Paul Barton-Davis

advice #1: do not use /dev/sequencer

[ solution, use /dev/midi* ]

This is pretty useless IMHO. My sequencer started off writing out
/dev/midi, and it was consistently off-sync. Badly off sync. Up to half a second off sync. I don't know if you have a significantly better box than I do, but mine just puked.

Oh yeah, and I got flamed real bad by diz. :)

Trying to up the priority on the process helped significantly, but a
simple switch to /dev/sequencer let my app stay usermode and still be reasonably responsive.

I wish there were a better way to sync audio though, specifically sync
of audio to MIDI. /dev/dsp has a crappy interface to this. Does ALSA provide any reasonable mechanism of syncing different inferfaces?

In OSS, the clock between outgoing and incomming MIDI aren't the same.
In ALSA, I'd hope that would be fixed. Does audio and sequencer also have the same clock?

What about syncing multiple soundcards? I've found this to be next to
impossible under thud using OSS.

Quoted 3 lines

advice 3: install ALSA, and use the ALSA sequencer. its a vastly more evolved kernel sequencer system, though its still going through some beta-testing. we would welcome your involvement in that process.

I know that I should definitely look into this. Promise I will.
Honest.

--
Billy Biggs [email protected]
http://www.div8.net/billy [email protected]

#2 lore raw
In reply to Paul Barton-Davis
Quoted 7 lines
Quoted 1 line

advice #1: do not use /dev/sequencer

[ solution, use /dev/midi* ]

This is pretty useless IMHO. My sequencer started off writing out
/dev/midi, and it was consistently off-sync. Badly off sync. Up to half a second off sync. I don't know if you have a significantly better box than I do, but mine just puked.

did you open it O_NONBLOCK, and which kernel were you using ? support for O_NONBLOCK was added (by me) somewhere during the 2.1 development series. if you tried /dev/midi on 2.0 or earlier, you'll find it works a *lot* better under 2.2.

previously, the driver would block until there were 32 bytes of data to read. this is a disaster! if you use select(2) and open (...O_NONBLOCK...) i think you'll find you stay totally in sync.

this used to be a standard problem with /dev/midi, which was one of the reasons I fixed it :)

Quoted 3 lines

I wish there were a better way to sync audio though, specifically sync
of audio to MIDI. /dev/dsp has a crappy interface to this. Does ALSA provide any reasonable mechanism of syncing different inferfaces?

well, it has a more evolved timer interface, but its not clear that anyone has tried using it for anything real.

personally, most of my current applications are so phase-locked to a soundcard DAC that i can get "perfect" sync timing from that. some applications (and MIDI-only sequencers are a very good example of them) are not like this, however, since they do no audio output :)

Quoted 3 lines

In OSS, the clock between outgoing and incomming MIDI aren't the same.
In ALSA, I'd hope that would be fixed. Does audio and sequencer also have the same clock?

i don't know the answer to either of these questions. jaroslav reads linux-audio-dev, so he may be able to help you.

Quoted 2 lines

What about syncing multiple soundcards? I've found this to be next to
impossible under thud using OSS.

as has been discussed here a number of times, this is far from trivial. clock rates from card to card, even the same brand, and even from (warm) day to (cold) day vary, so its not just a matter of finding a way to get them to all start at the same time.

in addition, you *cannot* simultaneously issue a command to every card to start playing/recording, so unless the soundcard h/w supports a "start when first sample > XXX is seen" command (not many do), you can *never* get them truly synchronized. ALSA does let you get pretty close though. I think that Benno or someone else did some tests of this.

#3 lore raw
In reply to Paul Barton-Davis
Quoted 7 lines · Billy Biggs

I wish there were a better way to sync audio though, specifically sync
of audio to MIDI. /dev/dsp has a crappy interface to this. Does ALSA provide any reasonable mechanism of syncing different inferfaces?

In OSS, the clock between outgoing and incomming MIDI aren't the same.
In ALSA, I'd hope that would be fixed. Does audio and sequencer also have the same clock?

Yes, if you choose the PCM device as the timer source.

Quoted 2 lines

What about syncing multiple soundcards? I've found this to be next to
impossible under thud using OSS.

No probs.

Jaroslav

-----
Jaroslav Kysela <[email protected]>
SuSE Linux http://www.suse.com
ALSA project http://www.alsa-project.org

#4 lore raw
In reply to Paul Barton-Davis
Quoted 4 lines · Jaroslav Kysela
Quoted 2 lines

What about syncing multiple soundcards? I've found this to be next to
impossible under thud using OSS.

No probs.

Oops. Before I receive some flames, the correction is here:

There are problems with different clock sources for the chips on multiple soundcards. ALSA driver currently doesn't support multiple soundcards which can be synced together. The new PCM API covers this possibility, but the driver for some hardware, which supports this feature, has not been written yet.

Jaroslav

-----
Jaroslav Kysela <[email protected]>
SuSE Linux http://www.suse.com
ALSA project http://www.alsa-project.org