Products > Programming

Ubuntu C-language sound peripheral programming - simple C example to read/write?

(1/5) > >>

wb0gaz:
I have a need to read/write sound streams from the sound port (card, motherboard, USB, etc.) peripheral in Ubuntu linux distribution, using (strong preference) C language I can compile (from source.)

The ideal solution would be something that would accept the sound peripheral identifier (for recording/input or playback/output) and related parameters (sample rate, bits/sample, etc.) on command line, then output a continuous stream of bytes to stdout (from the recording input), or accept a stream of bytes on stdin (to go to the playback output.) I realize the sample rate of audio is quite fixed, so presumably there would be modest buffering in the program, and it would be the responsibility of the (software, etc.) linked to stdin/stdout to maintain the required data rate, or accept overruns/dropouts as appropriate.

The important requirements are that the code is in source form so it can be recompiled, and that it just emit or accept (either, doesn't need to do both at the same time, as two instances would be used for simultaneous recording and playback) audio stream using (ideally) stdin/stdout, or alternatively, a named pipe or something like that.

This isn't intended as a media player or recorder (no files need be read or written), it just needs to work with live audio stream at the physical port of the PC.

Any pointers to whether this has been solved/done or otherwise where I might get started?

Thanks!

FlyingDutch:
Hello,

see audio-codecs (and streaming audio) in "FFMPEG" source code; Here is link to it's WWWpage:

https://www.ffmpeg.org/documentation.html

Best Regards

wb0gaz:
Thank you, FlyingDutch!

I also found this while searching some more

https://freedesktop.org/software/pulseaudio/doxygen/simple.html

https://freedesktop.org/software/pulseaudio/doxygen/

https://freedesktop.org/software/pulseaudio/doxygen/examples.html

PKTKS:
Well.. 

I am not sure what level of knowledge you need...

But buntus  are still  bounded to *NIX (Linux) at least..
(and besides all efforts made w/pulseaudio by POTTERIX himself)
(which BTW pulseaudio is a piece of crap.. as well )

and..  you already know  in *NIX everything is a file..

so it is a plain trivial matter to open the file (ANY FILE!) over a FIFO
or even better a named pipe... and redirect elsewhere...

like

--- Code: ---
mkfifo audio.wav && cat audio.wav | <whatever DSP>


--- End code ---

Obviously  audio.wav like any file can be the  port  /dev/audio as well
as long as you do not name the pipe as the device ... of course...
all file operators apply trivial like cat/tee .. on your sound devices..

trivial and used daily in any DAW... mostly combined with a REAL TIME
transport stream like JACK  to route multi-channel named pipes...

in doubt man mkfifo
Paul

ve7xen:
If you want to send the audio in C, which is totally different than your 'preferred' pipe solution, then PulseAudio is probably the way to go on modern Linux, it's almost always the default audio server. The simple API is pretty straightforward:


--- Code: ---pa_simple *s;
pa_sample_spec ss;
 
ss.format = PA_SAMPLE_S16NE;
ss.channels = 2;
ss.rate = 44100;
 
s = pa_simple_new(NULL,               // Use the default server.
                  "Fooapp",           // Our application's name.
                  PA_STREAM_PLAYBACK,
                  NULL,               // Use the default device.
                  "Music",            // Description of our stream.
                  &ss,                // Our sample format.
                  NULL,               // Use default channel map
                  NULL,               // Use default buffering attributes.
                  NULL,               // Ignore error code.
                  );

--- End code ---

Then you just call blocking pa_simple_write to send data. There's an async API too if you prefer.

If you just want to use pipelines and stdin/out, you can use the `pacat` command. Trying to do this with realtime audio is probably not going to go super well.

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version