You are not logged in.

#1 2013-02-17 05:06:48

milo64
Banned
From: Chiang Mai, Thailand
Registered: 2013-01-18
Posts: 86
Website

The best & simplest way to play sound in C

My Plan is to use the ALSA lib (saw it in /usr/include), what would be the best and simplest way to play a .wav file in C?
And, hopefully, runs at the background (Plays the .wav file and let the program continue to do something else).


milo64.
Registered Linux User: #555436
My Blog @ http://milo64.blogspot.com/

Offline

#2 2013-02-17 05:43:58

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: The best & simplest way to play sound in C

I've had to do this, and I just use system() to call aplay or speaker-test.

I thought about removing the system call and writing for alsa directly, but it would take a good bit of code, and would require forking to background, and I realized I'd really just be reinventing the wheel of aplay.

Last edited by Trilby (2013-02-17 06:26:35)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2013-02-17 06:29:22

milo64
Banned
From: Chiang Mai, Thailand
Registered: 2013-01-18
Posts: 86
Website

Re: The best & simplest way to play sound in C

Trilby wrote:

I've had to do this, and I just use system() to call aplay or speaker-test.

I thought about removing the system call and writing for alsa directly, but it would take a good bit of code, and would require forking to background, and I realized I'd really just be reinventing the wheel of aplay.

Hi, thanks...
The point of asking here is to avoid using "system()" since there were risks.


milo64.
Registered Linux User: #555436
My Blog @ http://milo64.blogspot.com/

Offline

#4 2013-02-17 10:32:59

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: The best & simplest way to play sound in C

I think the best and simplest way to play a WAV file in C is to use a C library that was designed to make playing WAV files incredibly simple, such as SDL: www.libsdl.org/intro.en/usingsound.html

Anything else will require you to have to get pretty "low level", but maybe just by me saying that will make some punk respond to tell me I'm wrong and that there's a better way. wink

Offline

#5 2013-02-17 12:30:58

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: The best & simplest way to play sound in C

milo64 wrote:

The point of asking here is to avoid using "system()" since there were risks.

Then just grab alsa-utils from the abs, grab the code for speaker-test, and remove all the sine wave, white noise and pink noise code.  You'll then have the code needed to play a wav.  I'm curious though what risks system() presents.  Would popen() or fork()+execv() present the same risks?  Either of these would work just as well.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#6 2013-02-18 15:06:34

milo64
Banned
From: Chiang Mai, Thailand
Registered: 2013-01-18
Posts: 86
Website

Re: The best & simplest way to play sound in C

Trilby wrote:
milo64 wrote:

The point of asking here is to avoid using "system()" since there were risks.

Then just grab alsa-utils from the abs, grab the code for speaker-test, and remove all the sine wave, white noise and pink noise code.  You'll then have the code needed to play a wav.  I'm curious though what risks system() presents.  Would popen() or fork()+execv() present the same risks?  Either of these would work just as well.

Well, here are 3 reasons that I know:
1. You must #include the <stdlib.h> header for just "system()".
2. It's a very very resource heavy function, here's what it does (Step-by-step):
     * Suspend your program.
     * Call the operating system.
     * Open an operating system shell (relaunches the O/S in a sub-process).
     * The OS must now find the "const char *command" command.
     * Allocate the memory to execute the command.
     * Execute the command.
     * De-allocate the memory.
     * Exit the OS.
     * Resume your program.
3. Commands aren't portable! "cat" for example:
      If you run the "cat" command from system(); in GNU/Linux, it works fine.
      BUT, if you run the "cat" command from system(); in Windows, it doesn't work.

**BUT** again, If you really need to use system();, you can use it.
There's also another problem I experience when using system();, it's that, If i ask for help on IRC, they say "Don't use system();" and no help.


milo64.
Registered Linux User: #555436
My Blog @ http://milo64.blogspot.com/

Offline

#7 2013-02-18 15:09:34

milo64
Banned
From: Chiang Mai, Thailand
Registered: 2013-01-18
Posts: 86
Website

Re: The best & simplest way to play sound in C

drcouzelis wrote:

I think the best and simplest way to play a WAV file in C is to use a C library that was designed to make playing WAV files incredibly simple, such as SDL: www.libsdl.org/intro.en/usingsound.html

Anything else will require you to have to get pretty "low level", but maybe just by me saying that will make some punk respond to tell me I'm wrong and that there's a better way. wink

Ugh, that's like re-inventing a wheel (re-inventing aplay)... anyway, thanks for the help.


milo64.
Registered Linux User: #555436
My Blog @ http://milo64.blogspot.com/

Offline

#8 2013-02-18 15:51:50

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: The best & simplest way to play sound in C

I'd love to learn more about this, but those three reasons don't really add up for me.  Here's what I'm missing:

1) your program wouldn't include stdlib otherwise?  Unless this is some highly specialized and minimal embeded system, stdlib is ... well standard.  Many compilers include it implicitly.  This caries effectily no cost as on any functional system the (shared) standard libraries will already be loaded as they are used by nearly every process running on your system.  I don't think there is any way to launch a subprocess without (at least implicitly) including stdlib.

2) This is mostly true - except for the operating system stuff.  It does not stop or restart the kernel ... that would be pretty crazy.  It does open a new process and starts a shell.  If you want your program to continue running, however, there is no way to avoid spawning a new process to play the audio.  You can avoid launching a shell by using fork()+execv().  But is the cost of starting a shell really a problem?  If this is playing audio that will continue to play, there would never be a need to do this rapidly in a loop ... you wouldn't start thousands of audio files within a fraction of a second, would you?

3) You were asking about alsa.  Alsa (Advanced Linux Sound Architecture) is not portable.  Any system that has alsa, has a POSIX shell.  Cat is an odd example, you are not using cat.  system("cat filename") would indeed be silly - that doesn't mean system() has no purpose.

The fact that someone would "scold" you, without giving any reasons for their claims makes me think they don't know what they are doing.  System() does have some limitations, and there are many things it really shouldn't be used for.  But this seems like a perfectly good use of it.  I might actually favor fork()+execv(), but either would work well.

Last edited by Trilby (2013-02-18 15:57:02)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#9 2013-02-19 00:11:21

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: The best & simplest way to play sound in C

milo64 wrote:

Ugh, that's like re-inventing a wheel

Wait, did I misunderstand something? You want an easy way to play a WAV file from C code, but you don't want to use a library that was designed to make it easy to play a WAV file from C code? yikes

Offline

#10 2013-02-19 08:15:41

jakobcreutzfeldt
Member
Registered: 2011-05-12
Posts: 1,041

Re: The best & simplest way to play sound in C

And wouldn't it be more of a re-invention of the wheel to construct a WAV playback routine from the low-level libraries, when one has already been constructed in SDL?

Offline

#11 2013-02-20 00:07:19

AaronBP
Member
Registered: 2012-08-06
Posts: 149
Website

Re: The best & simplest way to play sound in C

milo64 wrote:

Commands aren't portable!

Well, no. But the OS interfaces for handling sound aren't, either.

Offline

#12 2013-02-20 02:58:10

milo64
Banned
From: Chiang Mai, Thailand
Registered: 2013-01-18
Posts: 86
Website

Re: The best & simplest way to play sound in C

OKay, Thanks all, solved.


milo64.
Registered Linux User: #555436
My Blog @ http://milo64.blogspot.com/

Offline

#13 2013-02-20 03:58:15

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: The best & simplest way to play sound in C

Awww, no update? Boo! sad

Offline

Board footer

Powered by FluxBB