LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-12-2017, 09:31 AM   #1
Arius
LQ Newbie
 
Registered: Mar 2017
Posts: 4

Rep: Reputation: Disabled
How do I write a program that will play a sound?


Hello.

I am very new to programming.

I want to create a simple timer program with audio cues, just to assist me with my own personal exercise and meditation program. So for example, I can press a key when I'm ready, the timer will count down for 60 seconds, and when it's done it will make a little noise.

I've been using C, and so far I can do everything I need to do except make a noise. Should be a pretty basic task, but I can't figure it out or find any helpful information. What would be optimal is if I could launch an .mp3 or .wav file so that I could create my own sounds. Midi would be an acceptable option. I would even at this point accept computer blips/bleeps.

I'm using lubuntu.

I'm open to using whatever language will work. I'm starting to get a sense that C might not be appropriate. I downloaded some libraries, but I don't really know how to use them. I'm hearing "ALSA" a lot but don't know how to use that.

Any help would be much appreciated.

Thanks,
Arius
 
Old 03-12-2017, 09:48 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,635

Rep: Reputation: 6895Reputation: 6895Reputation: 6895Reputation: 6895Reputation: 6895Reputation: 6895Reputation: 6895Reputation: 6895Reputation: 6895Reputation: 6895Reputation: 6895
Quote:
Originally Posted by Arius View Post
Hello.
I am very new to programming.

I want to create a simple timer program with audio cues, just to assist me with my own personal exercise and meditation program. So for example, I can press a key when I'm ready, the timer will count down for 60 seconds, and when it's done it will make a little noise.

I've been using C, and so far I can do everything I need to do except make a noise. Should be a pretty basic task, but I can't figure it out or find any helpful information. What would be optimal is if I could launch an .mp3 or .wav file so that I could create my own sounds. Midi would be an acceptable option. I would even at this point accept computer blips/bleeps.

I'm using lubuntu.

I'm open to using whatever language will work. I'm starting to get a sense that C might not be appropriate. I downloaded some libraries, but I don't really know how to use them. I'm hearing "ALSA" a lot but don't know how to use that.
You can use all sorts of libraries to play a sound file, but libvlc may be your easiest option:
https://wiki.videolan.org/LibVLC_Tutorial/

...will give you a tutorial, and sample code. Also, if you're just wanting to play the file and exit, you can just use a system call to use any MP3 player on your system that works from command-line, like mpg123:
Code:
execlp("mpg123", "mpg123", "-q", "/some/path/my.mp3", 0);
execlp better in this case, in my opinion, but system may work fine too. Your call. And I know I risk the wrath of the programming gods by suggesting a system call.
 
Old 03-12-2017, 12:26 PM   #3
DavidMcCann
LQ Veteran
 
Registered: Jul 2006
Location: London
Distribution: PCLinuxOS, Xubuntu
Posts: 5,741

Rep: Reputation: 2115Reputation: 2115Reputation: 2115Reputation: 2115Reputation: 2115Reputation: 2115Reputation: 2115Reputation: 2115Reputation: 2115Reputation: 2115Reputation: 2115
If you want to use simple notification noise, there are quite a few at /usr/share/sounds. With bash, you use the command "aplay" but don't ask me what to do in C!
 
Old 03-13-2017, 02:33 AM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 16,896
Blog Entries: 10

Rep: Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024
Quote:
Originally Posted by Arius View Post
I want to create a simple timer program with audio cues, just to assist me with my own personal exercise and meditation program. So for example, I can press a key when I'm ready, the timer will count down for 60 seconds, and when it's done it will make a little noise.
i'd write a quick shell script.
e.g.:
Code:
#!/bin/sh

read -p "Please press enter to continue" x
sleep 60
aplay somesound.ogg
etc.
 
1 members found this post helpful.
Old 03-14-2017, 02:34 PM   #5
Mill J
Senior Member
 
Registered: Feb 2017
Location: 127.0.0.1 Sweet 127.0.0.1
Distribution: Mint, Void, MX, Haiku, Mobian, and many others
Posts: 1,208
Blog Entries: 2

Rep: Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511
In c you can use:
Code:
printf("\a");
To trigger the system bell.

Last edited by Mill J; 03-14-2017 at 02:37 PM.
 
Old 03-14-2017, 04:44 PM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,985

Rep: Reputation: 4568Reputation: 4568Reputation: 4568Reputation: 4568Reputation: 4568Reputation: 4568Reputation: 4568Reputation: 4568Reputation: 4568Reputation: 4568Reputation: 4568
I've seen this:
Code:
system('mplayer sound.ogg');
 
Old 03-15-2017, 07:43 AM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,635

Rep: Reputation: 6895Reputation: 6895Reputation: 6895Reputation: 6895Reputation: 6895Reputation: 6895Reputation: 6895Reputation: 6895Reputation: 6895Reputation: 6895Reputation: 6895
Quote:
Originally Posted by dugan View Post
I've seen this:
Code:
system('mplayer sound.ogg');
Yep, mentioned that in my reply, along with the execlp function. Both will work, but the execlp will clean things up a bit better in C, in my opinion, after the system call is done. The VLC libraries are pretty easy to use, and let you avoid being eaten by velociraptors for making system calls.

The programming gods are harsh.
 
Old 03-15-2017, 09:12 AM   #8
GazL
LQ Veteran
 
Registered: May 2008
Posts: 5,794

Rep: Reputation: 3707Reputation: 3707Reputation: 3707Reputation: 3707Reputation: 3707Reputation: 3707Reputation: 3707Reputation: 3707Reputation: 3707Reputation: 3707Reputation: 3707
If you are using pulseaudio then the 'simple' api is quite handy for doing this sort of thing without a lot of messing about:
Code:
/* pulse-simple-play.c:
 *
 * Example of using the Pulseaudio simple API to play a raw sample
 * embedded within the executable.  
 *
 * Compile:
 *   cc -Wall -O2 -lpulse-simple pulse-simple-play.c
 */

#include <pulse/simple.h>

/* Create the pcm_raw.h as follows:
 *
 *   sox sample.wav -t raw -e signed -b 16 -L -r 44100 -c 2 >pcm.raw
 *   xxd -i pcm.raw > pcm_raw.h
 *
 */
#include "pcm_raw.h"


int main(int argc, char *argv[])
{
        int error;
        pa_simple *s = NULL;
        static const pa_sample_spec ss = {
                .format = PA_SAMPLE_S16LE,
                .rate = 44100,
                .channels = 2
        };

        s = pa_simple_new(NULL, argv[0], PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &error);
        if (s) {
                pa_simple_write(s, pcm_raw, pcm_raw_len, &error);
                pa_simple_drain(s, &error);
                pa_simple_free(s);
        }

        return 0;
}
I've no idea what the maximum allowable size of the write is but it seems to work for playing small samples like those you typically use for notifications. Oh, and feel free to add more robust error catching, I've done the bare minimum here.

ref: https://freedesktop.org/software/pul...en/simple.html

Last edited by GazL; 03-15-2017 at 11:30 AM. Reason: fixed comment.
 
1 members found this post helpful.
Old 03-16-2017, 03:34 PM   #9
Arius
LQ Newbie
 
Registered: Mar 2017
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks for the helpful replies!

I tried doing what T-Bone suggested (thank you!), but i have to figure out what linking is and how to do it before I can make that work.

I think triggering the system bell is probably more appropriate to my present skill level. I put the printf("\a") code in that Mill J suggested, but it did nothing. It compiles fine but then runs the program without making a sound.

I then wrote the following basic program just as a test, and it also did nothing apparent (unless the system bell is only audible to dogs):


#include <stdio.h>

int main()
{
printf("\a");
}


Any idea what I'm doing wrong? Maybe I don't have a system bell?
 
Old 03-16-2017, 03:55 PM   #10
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,581
Blog Entries: 11

Rep: Reputation: 3596Reputation: 3596Reputation: 3596Reputation: 3596Reputation: 3596Reputation: 3596Reputation: 3596Reputation: 3596Reputation: 3596Reputation: 3596Reputation: 3596
Quote:
Originally Posted by Arius View Post
Maybe I don't have a system bell?
That is possible, but on many systems the system bell may be enabled/disabled in the BIOS. Worth checking.
 
1 members found this post helpful.
Old 03-17-2017, 07:10 PM   #11
Mill J
Senior Member
 
Registered: Feb 2017
Location: 127.0.0.1 Sweet 127.0.0.1
Distribution: Mint, Void, MX, Haiku, Mobian, and many others
Posts: 1,208
Blog Entries: 2

Rep: Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511
Quote:
Originally Posted by Arius View Post
Thanks for the helpful replies!

I tried doing what T-Bone suggested (thank you!), but i have to figure out what linking is and how to do it before I can make that work.

I think triggering the system bell is probably more appropriate to my present skill level. I put the printf("\a") code in that Mill J suggested, but it did nothing. It compiles fine but then runs the program without making a sound.

I then wrote the following basic program just as a test, and it also did nothing apparent (unless the system bell is only audible to dogs):


#include <stdio.h>

int main()
{
printf("\a");
}


Any idea what I'm doing wrong? Maybe I don't have a system bell?
This works for me.

Code:
 
#include <stdio.h>

int main(void) 
{
   printf("\a");
   return 0;
}
 
Old 03-17-2017, 09:57 PM   #12
Arius
LQ Newbie
 
Registered: Mar 2017
Posts: 4

Original Poster
Rep: Reputation: Disabled
Okay. I tried to look in the BIOS (I think - I pressed F2 on bootup and got a menu full of stuff) but there was nothing about a system bell.

I modified my simple "bell" program to make it exactly like Mill J's, and got the same result as before: it compiles fine and runs no problem, but fails to make any sort of noise.

I intend to read up on linking when I get a chance. This is sort of a side-project for me.
 
Old 03-17-2017, 10:27 PM   #13
Arius
LQ Newbie
 
Registered: Mar 2017
Posts: 4

Original Poster
Rep: Reputation: Disabled
I did some googling. Seems like a non-functional system bell is pretty standard for later versions of ubuntu. (I'm running lubuntu). There are some replies to this here: https://askubuntu.com/questions/1990...pt-not-working but none of them have helped me. I did remove the pulseaudio-x11 package referenced, but to no avail.

People keep talking about /usr/ and other directories I don't have and have never heard of. I assume this is because I have Lubuntu?
 
Old 03-18-2017, 02:27 AM   #14
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 16,896
Blog Entries: 10

Rep: Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024
Quote:
Originally Posted by Arius View Post
People keep talking about /usr/ and other directories I don't have and have never heard of. I assume this is because I have Lubuntu?
i'm 100% sure that you DO have a /usr directory.
 
Old 03-18-2017, 10:56 AM   #15
Mill J
Senior Member
 
Registered: Feb 2017
Location: 127.0.0.1 Sweet 127.0.0.1
Distribution: Mint, Void, MX, Haiku, Mobian, and many others
Posts: 1,208
Blog Entries: 2

Rep: Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511
Quote:
Originally Posted by Arius View Post
Okay. I tried to look in the BIOS (I think - I pressed F2 on bootup and got a menu full of stuff) but there was nothing about a system bell.

I modified my simple "bell" program to make it exactly like Mill J's, and got the same result as before: it compiles fine and runs no problem, but fails to make any sort of noise.

I intend to read up on linking when I get a chance. This is sort of a side-project for me.
Sometimes the program executes to fast to trigger the bell try it in a longer program. Such as one that you need to enter some text after the bell. Also check your system sound. I think you can change/on/off the bell.
 
  


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
[SOLVED] How to play sound directly from input on USB sound card, a Sound Blaster X-Fi THX JunctaJuvant Other *NIX 10 06-14-2014 02:36 AM
Could someone program a SH / Bash program with Zenity to play radio based on a site? frenchn00b Programming 2 02-25-2011 04:16 PM
How to write C Code to play MP3 files using Open sound System programming Under Linux shayad15 Programming 3 06-05-2006 12:46 PM

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

All times are GMT -5. The time now is 07:09 AM.

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