Solved

How to play a sound in the shell ?

Posted on 2004-03-30
12
382 Views
Last Modified: 2010-04-21
I have a very simple program running in the shell made in C++.

I want this program to be able to play a sound when I prees "s". how and what class should I include ?

cin >> option;
if(option=="s")
   //play the .wav or what ever format I can play in the shell....


Thank you.
0
Comment
Question by:digi_mind
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 10713417
Hi digi_mind,
The simplest way is

system("start your_wav.wav");

...this will be like a doubleclick on the wav in Explorer.

Cheers,
Stefan
0
 

Author Comment

by:digi_mind
ID: 10713479
Do I need to include some class to use "system" ? because so far I have a file called email.wav, but it is not working... have a look:

#include <iostream>
using namespace std;
main()
{
   system("start /usr/share/sounds/email.wav");
}
0
 
LVL 5

Expert Comment

by:g0rath
ID: 10713581
For simple sounds...as in speaker beeps...

int main( int argc, char **argv )
{
      int i;

      for  (i=1; i < 5; i++)
              putchar(7);

      return 0;
}

for real audio as in .wav files or .mp3 files you need a library already built...
check out this one...writting in C++
http://osalp.sourceforge.net/

or use a cheater method...

fork() off a new process and call an already made audio program, and then when the child exists contil in your program....

#include <signal.h>
#include <iostream>
#define __USE_XOPEN 1
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
using namespace std;

int main( void )
{
   pid_t pid;
   int status, died;

   switch(pid=fork())
   {
       case -1: cout << "can't fork\n";
      exit(-1);
       case 0 : cout << "   I'm the child of PID " << getppid() << ".\n";
      cout << "   My PID is " <<  getpid() << endl;
      sleep(2);
      exit(3);
       default: cout << "I'm the parent.\n";
      cout << "My PID is " <<  getpid() << endl;
      // kill the child in 50% of runs
      if (pid & 1)
         kill(pid,SIGKILL);
      died= wait(&status);
      if(WIFEXITED(status))
         cout << "The child, pid=" << pid << ", has returned " << WEXITSTATUS(status) << endl;
      else
         cout << "The child process was sent a " << WTERMSIG(status) << " signal\n";
  }
   return 0;
}
0
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 
LVL 5

Expert Comment

by:g0rath
ID: 10713617
use the man pages there are your friend

man system

NAME
       system - execute a shell command

SYNOPSIS
       #include  <stdlib.h>

       int  system  (const  char  *  string);

it bascially does what I showed you above.....
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 50 total points
ID: 10713844
#include <iostream>
#include <stdlib.h>
using namespace std;
main()
{
   system("start /usr/share/sounds/email.wav");
}
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10713878
digi_mind,
Errm... Wrong implication - you don't need Windows to play .wav

use "apropos sound" to get the appropriate tool. I only have Solaris here :-/

Stefan
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10713901
If you have ALSA (and who doesn't these days :-) you can use the aplay command:

aplay /path/to/your/file.wav

0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10713914
... and you would put this command into the system() function, or use the fork() example from above.
0
 
LVL 5

Expert Comment

by:g0rath
ID: 10713961
or if mp3s then everyone should have mpg123
0
 

Author Comment

by:digi_mind
ID: 10717504
let me tell you all something... I never said windows. I said linux... no problem the code is so simple:

System("play music.wav");

Can I get my points refunded ?
0
 
LVL 5

Expert Comment

by:g0rath
ID: 10717907
every one of these examples is for linux....by windows they were meaning a window manager? like gnome? etc....

the system() call was a suggestion from stefan73 by which you didn't know about it, hence your next question...and then you got the it correct but I would venture to guess that means that stefan73 deserves the points....every linux distro has different default progs installed so hence all the various system() suggestions

just my .6 cents
0
 

Author Comment

by:digi_mind
ID: 10722578
Yes, Stefan73 will get the points. Agree with your help. Thank you
0

Featured Post

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question