[SDL] Audio capture support and new Mac/iOS audio target...

Ryan C. Gordon icculus at icculus.org
Mon Sep 19 11:35:48 PDT 2016


On 09/07/2016 08:25 AM, Fejwin wrote:

(Whoops, I missed this email before, sorry for the late reply!)

> Is there any bare-bone tutorial on how to use sound capture in SDL?

There is not, but you can see the sample program here:

https://hg.libsdl.org/SDL/raw-file/e9c3e64fdc84/test/testaudiocapture.c

The relevant parts are:

If enumerating audio devices, you use SDL_TRUE to get a list of capture 
devices instead of playback devices. Likewise for 
SDL_GetAudioDeviceName()...

https://hg.libsdl.org/SDL/file/e9c3e64fdc84/test/testaudiocapture.c#l115

You open the audio device with SDL_TRUE for the second parameter (you 
want an audio capture device instead of the usual playback device)...

https://hg.libsdl.org/SDL/file/e9c3e64fdc84/test/testaudiocapture.c#l143

Now you would get a callback that fires regularly, just like you would 
for a playback device. Instead of writing data to a buffer in that 
callback, you read data from it. The callback fires when enough data has 
been read from the microphone.

If you don't like doing that, you can use a NULL callback, like that 
example code does:

https://hg.libsdl.org/SDL/file/e9c3e64fdc84/test/testaudiocapture.c#l125

In which case, you can read from the mic with SDL_DequeueAudio():

https://hg.libsdl.org/SDL/file/e9c3e64fdc84/test/testaudiocapture.c#l76

And it'll hand you whatever data is available at the moment, continually 
buffering more as it comes in. If you do this, you have to either call 
SDL_DequeueAudio() regularly, or make sure to SDL_PauseAudioDevice() 
when you don't care about recording, or SDL will keep buffering up audio 
until it runs out of memory. Naturally, the callback avoids this 
problem. Just be careful about times when you're busy doing something 
that blocks, like maybe loading a new game level, that you might be 
consuming memory and not realize it (and then have a lot of 
needlessly-buffered audio when you come back to it later).

When you're done recording and want to clean up, you close capture 
devices the same way you always would with SDL_CloseAudioDevice(). 
Depending on your needs, SDL_Quit() cleans up any open ones for you when 
shutting everything else down, too, and that might be more simple.

Basically, that's all there is to it. It's a handful of function calls 
from start to finish.

> Also, are there functions built in to get let's say the power of the recorded sound signal?

We only provide raw PCM data, but you can calculate this from that. The 
dirt simple way is what ioquake3 does for their VoIP power meter:

https://github.com/ioquake/ioq3/blob/master/code/client/cl_main.c#L513

Which is strictly speaking not _correct_ but works pretty well anyhow.

> Also, what version of SDL should I get to play around with these new
> features?

The latest in revision control, which will become an official 2.0.5 
release in a few weeks, at most.

(I hope.)

--ryan.




More information about the SDL mailing list