How to play a sound in C++? - c++

I want to play a sound using C++. I'm not playing an audio file, but a certain voice. (Maybe functions like play_sound(frequency, amplitude, time).).
I want it to be cross-platform.

Ycao,
As far as I know, you will need some third party library.
Personally, I would use FMOD.
It's quite simple to use, well documented, you will find a lot code snippet to bootstart your project and it has a lot of interesting features.
FMOD Website

Related

How to play more than 1 .wav sounds in C++ at the same time using PlaySound() or any basic function/method?

I am trying to make a game where I need to play many sounds simultaneously I know v.basic or rather just know about the func. PlaySound() to run .wav sounds.
The problem is that with SND_ASYNC I cannot play multiple sound at once but the sound which plays first skips in the middle and plays the next sound.
Is there any way (simple and easy to understand) so that I can play multiple sounds at once?
Playsound is not meant for mixing sounds. Essentially, it's an older technology that can easily be outdone by more recent ones.
You can use DirectX for sound development in games (a popular choice).
I personally use FMod since it's really easy to use. Here is a tutorial to get you started.
I recommend you to use the latest version of fmod (fmod Studio) that give you posibilities to create diferen channels and play various sounds on each channel and aply effects for each one.

Simple C++ Sound API

My commercial embedded C++ Linux project requires playing wav files and tones at individual volume levels concurrently. A few examples of the sounds:
• “Click” sounds each time user presses screen played at a user-specified volume
• Warning sounds played at max-volume
• Warning tones requested by other applications at app-specified volume level (0-100%)
• Future support for MP3 player and/or video playback (with sound) at user-specified volume. All other sounds should continue while song/video is playing.
We're using Qt as our UI framework which has QtMultimedia and Phonon support. However, I heard the former has spotty sound support on Linux and the latter is an older version and may be deprecated in an upcoming Qt release.
I've done some research and here are a few APIs I've come across:
KDE Phonon
SFML
PortAudio
SDL_Mixer
OpenAL Soft
FMOD (though I'd prefer to avoid license fees)
ALSA (perhaps a bit too low-level...)
Other considerations:
Cross-platform isn't required but preferred. We'd like to limit dependencies as much as possible. There is no need for advanced features like 3D audio or special effects in the foreseeable future. My team doesn't have much audio experience so ease-of-use is important.
Are any of these overkill for my application? Which seems like the best fit?
Update:
It turns out we were already dependent on SDL for other reasons so we decided on SDL_Mixer. For other Embedded applications, however, I'd take a long at the PortAudio/libsndfile combo as well due to their minimal dependencies.
libao is simple, cross-platform, Xiphy goodness.
There's documentation too!
Usage is outlined here - simple usage goes like this:
Initialize (ao_initialize())
Call ao_open_live() or ao_open_file()
Play sound using ao_play()
Close device/file using ao_close() and then ao_shutdown() to clean up.
Go for PortAudio. For just plain audio without unneeded overhead such as complex streaming pipelines, or 3D, it is the best lib out there. In addition you have really nice cross-platform support. It is used by several professional audio programs and has really high quality.
i have used SDL_Mixer time and time again, lovely library, it should serve well for your needs, the license is flexible and its heavily documented. i have also experimented with SFML, while more modern and fairly documented, i find it a bit bulky and cumbersome to work with even tho both libraries are very similar. imo SDL_Mixer is the best.
however you might also want to check out this one i found a few weeks ago http://www.mpg123.de/, i haven't delved too much into it, but it is very lightweight and again the license is flexible.
There is a sound library called STK that would meet most of your requirements:
https://ccrma.stanford.edu/software/stk/faq.html
Don't forget about:
FFmpeg: is a complete, cross-platform solution to record, convert and stream audio and video.
GStreamer: is a library for constructing graphs of media-handling components. The applications it supports range from simple Ogg/Vorbis playback, audio/video streaming to complex audio (mixing) and video (non-linear editing) processing.

Playing Sound in C++

I need to play sound in C++.
It should be compatible across all platforms.
Thanks,
goerdy
Try OpenAL, FMOD or SDL_mixer. The choice is wide.
std::cout << "\a\a\a\a\a\a\a\a";
)))
My suggestion is to go with OpenAl (3D cross platform Audio API)
It's fairly easy to learn and meant to have a similar flow of programming as OpenGl. It's free and has different extensions to take advantage of sound cards. OpenAl has a library akin to OpenGl's Utility Toolkit (GLUT) that's called freealut. It makes things even simpler.
This might be a good place to get started:
http://www.devmaster.net/articles/openal/
Some of the functions in that code are deprecated but reading the documentation, you'll be able to update it.
The devmaster tutorial also teaches you how to load ogg vorbis (royalty free) files into memory and play them. You'll need to download and build libvorbis and libogg to do that but there's good documentation and, in case you're using Visual Studio, already made solutions.
The fact that Creative Labs (largest sound card manufacturer) is a major contributor to the project is a great point in it's favor. Hardware will not be ignored.
Last thing, remember to download the OpenAl SDK and link to the proper libraries and include directories. There's always a lot of people asking for specific files stored inside it.
"Compatible to all platforms" is a bit of a tall order. After all, "all platforms" includes the controller in your car, the regulator in a nuclear plant, and the router in your network.
If you mean "all desktop platforms", you may have a bit more luck. While it is impossible to produce sound in 100% standards C++, there are libraries that are available. However, I don't think that even these will get you "all platforms". Are you willing to settle for "most common platforms"?

Powerful audio lib

Can you recommend a powerful audio lib?
I need it to timestrech & pitchshift independently, as well as give me full access to the raw audio data and let me stream bytes into its pipeline.
Other effects like eq, filtering, distortion are a plus.
Needs to be accessible from C++ / Linux.
Maybe gstreamer, xine or mplayer would work? Or what would you suggest.
I think FMod is widely recognized as one of the most powerful audio engine available for free until you do something commercial with it, and cross-platform, like in console-mac-pc cross-platform.
Now, OpenAL is worth giving a try.
OpenAL, PulseAudio, JACK, and Phonon, I believe, each have these features in some form.
If you willing to pay for it Miles is very nice. I can't recommend FMOD for much outside of hobby projects. It's had some truly nasty bugs, and I've seen new versions introduce as many as they fix.
I've used soundtouch in the past. Focused on changing speed/pitch/etc.
ALSA looks like the big one.
JACK for Linux also looks promising.

Playing sounds with C++?

How would i make it so in my program there is a button when that button is clicked i want it to play a .wma file without opening and media player?
The C++ standard does not include this functionality. That means it depends on what your system offers.
For Windows, you can try something like PlaySound.
However, you best bet is to use a pre-existing library, like:
OpenAL
BASS
FMOD
SDL's: Sound.
Searching for C++ Sound Library brings up a lot of information.
Also, check out these three other SO topics:
What Is The Best C++ Sound API For Windows?
How to play MP3 files in C?
Learning to work with audio in C++
Although the above answer mentions it (and everything else, it doesn't give any recommendations so...), FMOD is the king of C++ sound (the most used) and works great so I'd recommend FMOD in particular.
Gstreamer is a free, cross-platform multimedia framework written in C (using GObject) that allows encoding/decoding for many types of media, including wma. Very easy to use and well documented.
KDE's Phonon is a cross-platform(!) C++ multimedia library. It supports native sound systems as backends. Worth a look if you haven't seen it before, and a simple video player can be written in a mere 83 lines of code.
For Windows, in all honesty your best bet is DirectShow. The RenderFile API allows you to play most audio file types with just a few lines of code.
The best part about DirectShow is that it's a part of the Windows platform so you don't need to bundle an external component.