Skip to contentSkip to conversation

Question regarding period/buffer and error handling

alsa-dev Cournapeau David 3 participants 6 messages May 28, 2004 – May 31, 2004

lore
Share & tools
lore
#0 lore raw

Hi there,

For my research, I need to use audio with matlab under linux, and
sound support of matlab is kind of... well, crappy (basically, it is opening the /dev/audio file and write to it; on my computer, it doesn't seem to work). That's why I am thinking about using a wrapper to alsa.
Thanks to some tutorials (by the way, thanks for your tutorial,
Paul), I managed to play sound with matlab and alsa, but I would like my wrapper to be full proof against errors. Basically, here is my method :

- parse the input arguments, convert from matlab representation to
'normal C' representation (that is, vectors of double for samples).
- To see if any soundcard is present, I am using
snd_card_next(card), with card value to -1.
- I allocate memory for a snd_pcm_hw_params_t structure, open a pcm
device in NON_BLOCK_MODE, set the mode to blocking mode, set the format (16 bits LE), sampling rate, interleaved format and number of channels
- I write to the pcm device using writei.

My first problem is with writei: I cut my audio input in blocks of a
small size before sending them to writei( blocks are 1024 frames long, for example), and depending of the sampling rate, writei doesn't write the whole block. For example, at 44100 Hz, if the block is 1024 frames long, only the 940 first frames are written. I think this problem is related to period size and so on, but I don't really understand the differences between period and buffer. Where can I find some informations about that ?
My second problem is related to snd_card_next: If the card value
returned by snd_card_next is different from -1, does that always mean than a alsa audio device is present on the system ? Is it the best method to probe a soundcard with alsa support?
My third problem is related to the error handling: right now, with
some hack, my wrapper works. But if I stop the wrapper with ctl+C, the pcm device is not closed properly, which means I cannot use the pcm device again (except by closing matlab and launching it again....). Is there a way to close the pcm device before opening it ? ( I first thought about catching the ctrl+C 'signal' from matlab in my wrapper, to exit cleanly, but for now, I didn't find any way to do it).

Thank you,

David

-------------------------------------------------------
This SF.Net email is sponsored by: Oracle 10g
Get certified on the hottest thing ever to hit the market... Oracle 10g. Take an Oracle 10g class now, and we'll give you the exam FREE.
http://ads.osdn.com/?ad_id=3149&alloc_id=8166&op=click

#1 lore raw
Quoted 7 lines · Cournapeau David

Hi there,

For my research, I need to use audio with matlab under linux, and sound support of matlab is kind of... well, crappy (basically, it is opening the /dev/audio file and write to it; on my computer, it doesn't seem to work). That's why I am thinking about using a wrapper to alsa.

If you are playing fairly short pieces of sound you might be able to use this function I wrote some time ago:

function playsnd(y,fs,bits)

wf = tempname;
ws = warning;
warning off
wavwrite(y, fs, bits, wf);
warning ws
wf = [wf '.wav'];
[s,o] = unix(sprintf('aplay %s', wf));
delete(wf);

Signature · 10 lines
Måns Rullgård
[email protected]



-------------------------------------------------------
This SF.Net email is sponsored by: Oracle 10g
Get certified on the hottest thing ever to hit the market... Oracle 10g. 
Take an Oracle 10g class now, and we'll give you the exam FREE.
http://ads.osdn.com/?ad_id=3149&alloc_id=8166&op=click
#2 lore raw
Quoted 6 lines · Måns Rullgård

If you are playing fairly short pieces of sound you might be able to use this function I wrote some time ago:

function playsnd(y,fs,bits)

Quoted 12 lines

wf = tempname;
ws = warning;
warning off
wavwrite(y, fs, bits, wf);
warning ws
wf = [wf '.wav'];
[s,o] = unix(sprintf('aplay %s', wf));
delete(wf);

Well, I precisely want to avoid writing to a temporary file :)

But right now, my function seems to work, I just used UNIX signals to close cleanly the audio device, it was much simpler than what I thought at first.

Cheers,

David

-------------------------------------------------------
This SF.Net email is sponsored by: Oracle 10g
Get certified on the hottest thing ever to hit the market... Oracle 10g. Take an Oracle 10g class now, and we'll give you the exam FREE.
http://ads.osdn.com/?ad_id149&alloc_id�66&op=click

#3 lore raw
Quoted 17 lines · Cournapeau David
Quoted 6 lines · Måns Rullgård

If you are playing fairly short pieces of sound you might be able to use this function I wrote some time ago:

function playsnd(y,fs,bits)

Quoted 9 lines

wf = tempname;
ws = warning;
warning off
wavwrite(y, fs, bits, wf);
warning ws
wf = [wf '.wav'];
[s,o] = unix(sprintf('aplay %s', wf));
delete(wf);

Quoted 1 line

Well, I precisely want to avoid writing to a temporary file :)

If the files are small they will probably never hit the physical disk. Of course, your sounds might be longer than that.

Signature · 10 lines
Måns Rullgård
[email protected]



-------------------------------------------------------
This SF.Net email is sponsored by: Oracle 10g
Get certified on the hottest thing ever to hit the market... Oracle 10g. 
Take an Oracle 10g class now, and we'll give you the exam FREE.
http://ads.osdn.com/?ad_id=3149&alloc_id=8166&op=click
#4 lore raw
In reply to Cournapeau David
Quoted 3 lines · Cournapeau David

My second problem is related to snd_card_next: If the card value
returned by snd_card_next is different from -1, does that always mean than a alsa audio device is present on the system ?

It means that same kind of device with an ALSA driver is present. This does not mean that you'll be able to play PCM data to it. And there may be software devices available.

Quoted 1 line

Is it the best method to probe a soundcard with alsa support?

snd_card_next() and snd_ctl_pcm_next_device() will give you a list of hardware devices only.

Usually, you should use the device named "default" and let the user allow to specify another device name.

HTH
Clemens

-------------------------------------------------------
This SF.Net email is sponsored by: Oracle 10g
Get certified on the hottest thing ever to hit the market... Oracle 10g. Take an Oracle 10g class now, and we'll give you the exam FREE.
http://ads.osdn.com/?ad_id=3149&alloc_id=8166&op=click

#5 lore raw
Quoted 19 lines · Clemens Ladisch
Quoted 5 lines · Cournapeau David

My second problem is related to snd_card_next: If the card value
returned by snd_card_next is different from -1, does that always mean than a alsa audio device is present on the system ?

It means that same kind of device with an ALSA driver is present. This does not mean that you'll be able to play PCM data to it. And there may be software devices available.

Quoted 3 lines

Is it the best method to probe a soundcard with alsa support?

Quoted 8 lines

snd_card_next() and snd_ctl_pcm_next_device() will give you a list of hardware devices only.

Usually, you should use the device named "default" and let the user allow to specify another device name.

Ok, that's what I thought reading the different sources I checked, concerning the device name. But I am still a bit confused how to probe a soundcard to play a pcm stream to it: what should I do after having found an hardware device with snd_card_next ?

Is there always a "default" name ? Or is it user dependant ?

cheers,

David

-------------------------------------------------------
This SF.Net email is sponsored by: Oracle 10g
Get certified on the hottest thing ever to hit the market... Oracle 10g. Take an Oracle 10g class now, and we'll give you the exam FREE.
http://ads.osdn.com/?ad_id=3149&alloc_id=8166&op=click