LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-21-2010, 09:05 PM   #1
MS3FGX
LQ Guru
 
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
Using "aplay" to play notifications in C program


I am looking to play a simple sound effect as an event notification in a C program I am working on, but want to do it as simply and easily as possible. Portability is of no concern (this is a Linux-only application) nor is the format of the audio file (the file will be bundled with the application, so I can make the format whatever is necessary).

The simplest way seems to be just calling "aplay" from within my program, but I am not sure if this is really a safe approach or not. Is this is a reasonable enough solution for a simple task like this? Other than the program not existing on the host OS (which obviously I would check for on execution), are there any other pitfalls?
 
Old 10-21-2010, 11:32 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MS3FGX View Post
I am looking to play a simple sound effect as an event notification in a C program I am working on, but want to do it as simply and easily as possible. Portability is of no concern (this is a Linux-only application) nor is the format of the audio file (the file will be bundled with the application, so I can make the format whatever is necessary).

The simplest way seems to be just calling "aplay" from within my program, but I am not sure if this is really a safe approach or not. Is this is a reasonable enough solution for a simple task like this? Other than the program not existing on the host OS (which obviously I would check for on execution), are there any other pitfalls?
What do you think is unsafe ?
 
Old 10-22-2010, 10:57 AM   #3
sunnydrake
Member
 
Registered: Jul 2009
Location: Kiev,Ukraine
Distribution: Ubuntu,Slax,RedHat
Posts: 289
Blog Entries: 1

Rep: Reputation: 61
first i must note that im not a linux programmer.
but from my win game bg.. i take that approach
if i need just play/wait for done
use system calls/outside program
if i need to get values, control position/state,effects
use lib!

google search revealed
http://www.ambiera.com/irrklang/

regarding using alsa in C prog
http://equalarea.com/paul/alsa-audio.html
APPENDIX A Minimal Playback Program
 
Old 10-22-2010, 05:28 PM   #4
MS3FGX
LQ Guru
 
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Original Poster
Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
Quote:
Originally Posted by Sergei Steshenko View Post
What do you think is unsafe ?
Well, "safe" probably shouldn't be taken literally. The issue is more along the lines of reliability, performance, and compatibility.

The biggest problem I already see is that by using system(), the application will pause while the notification is playing, which is obviously not great. I will have to see if I can fork the aplay process off into the background. But that is likely going to cause the sound to double or even triple up if events occur fast enough.

I also have some concerns about how well it will work on different distributions. I know some distros like Ubuntu modify ALSA and remove components like alsaconf, so can I be sure that all of the possible systems I will be running on will have an aplay binary that:

1. Accepts the exact same arguments
2. Plays back the sound effect exactly the same

On the other hand, if I actually use ALSA to play audio as intended, it is going to complicate my software considerably and add another dependency. Even the minimal playback application linked by sunnydrake is rather large compared to my program.
 
Old 10-22-2010, 08:22 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MS3FGX View Post
...
The biggest problem I already see is that by using system(), the application will pause while the notification is playing
...
Call 'system' from a separate thread.
 
Old 10-23-2010, 07:26 AM   #6
sunnydrake
Member
 
Registered: Jul 2009
Location: Kiev,Ukraine
Distribution: Ubuntu,Slax,RedHat
Posts: 289
Blog Entries: 1

Rep: Reputation: 61
first regarding ubuntu pulseaudio have alsa emulation
second if you concerned about libraries/headers you can create static binary which will include all deps needed in one file.
regarding wait for return like sergei noted use threads!
look closely at minimal alsa sound play example it's not complex!

Last edited by sunnydrake; 10-23-2010 at 07:28 AM.
 
Old 10-23-2010, 04:38 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by sunnydrake View Post
... you can create static binary ...
If the OP's program has compatible license.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Program to "assemble" still-frame pictures into "animated" show Pantherman Linux - General 3 12-24-2009 11:14 PM
hard drives come up "no partition table" and "in use by another program" marti972 Linux - Newbie 3 03-16-2009 11:44 PM
Getting error: "ALSA device "default" is already in use by another program." brynjarh Debian 7 02-04-2005 11:45 AM
All System Notifications except "KDE is Starting up" not working with Suse 9.0 nrowlan SUSE / openSUSE 0 11-09-2004 01:47 PM
any program to play ".dat" files? l_9_l Linux - General 1 03-19-2002 09:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration