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"?
Related
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.
I'm thinking of developing a game-like piece of software. It will probably require a bit of OpenGL, MIDI input, and math. I'd like to eventually sell the software, so it needs to be installable on PCs with different OSes. And I don't want to have to spend a lot of time on memory management and other low-level details.
My question is this: what language/framework would you use for such software?
You have got a lot of options my friend, here are just a few which allow you to use a high level language to develop.
Torque 3D http://www.garagegames.com/
I've used this a bit and can tell you its a pretty good solution. You can build you game logic in their TorqueScript. Using it also gets you the option to release on pretty much every major platform including consoles and the browser. The only snag is it does cost money, but is very affordable for indies.
Panda3d http://www.panda3d.org/
This a completely free open source engine. I provides a lot of functionality and also allows you to program your game logic in python. The platforms it supports is Linux/Mac/PC.
Mono http://mono-project.com/Main_Page
I have not played around with this too much, and am not sure how good their 3d is (it isn't known for it anyway). It does allow you to program in a number of high level languages (C# and python to name a few). It also allows you to deploy to a number of platforms including embeded devices and the iphone (MonoTouch).
I would check these out and see if any are a fit for your situation. If none are then there are a large number of other options out there.
I think the closest thing to what you're after is Java. It has decent support for OpenGL(JOGL) and a good standard library that works on most systems.
Despite what some people will tell you, Java isn't as fast as C, and this can rear it's head doubly so in a game. It is cross platform though, and you don't have to bother with all that tiresome memory-management.
I would use C# for scripting & the Unity 3D Engine. http://unity3d.com/
Unity has a reasonable licensing fee but it's also free to download and get started. Check the details for when a licensing fee is payable.
Anyways, Unity3D takes care of:
3D rendering
Memory management
Input
Audio & Video
Networking
Asset pipelines
Scripting through the Mono CLR (ie, you can use C#)
And has a great level/world editor
If you are willing to trade raw speed for ease of use then this is for you. We started a project intending to use Unity. Our project needed greater customization than we could get from the engine. We wanted source code and to run at 60fps so we upgraded. But I would still recommend it as a solid multi-platform multi-OS solution.
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.
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.
When I started using SoundEngine (from CrashLanding and TouchFighter), I had read about a few people recommending not to use it, for it was, according to them, not stable enough. Still it was the only solution I knew of to play sounds with pitch and position control without learning C++ and OpenAL, so I ignored the warnings and went on with it.
But now I'm starting to worry. The 2.2 SDK introduced AVFoundation. Using both SoundEngine from CrashLanding (for sounds) and AVAudioPlayer (for music), I found out SoundEngine behaves strangely when the only existing AVAudioPlayer is released (all sounds stop until a new AVAudioPlayer is initiated). Around the same time as the 2.2 SDK came out, the CrashLanding sample code was mysteriously removed from the ADC site. I'm worried there are more bad surprises to come.
My question is, is anyone aware of an Open Source alternative to SoundEngine? Maybe even a C++ library that uses OpenAL?
Look at this library, but i don't know is this what you need.
The Kowalski project provides a data driven and portable sound engine that currently runs on iOS, OS X and Windows. The engine is released under the zlib license and provides positional audio, pitch control etc.
ObjectAL for iPhone
Clone it. Use it. Love it. Enjoy the freedom.
Why not just use AVFoundation? It's pretty simple to handle and nicely flexible - apart from if you need exact timing (says the Apple documentation - but I've been testing it fairly extensively and yet to find any significant practical issues) I don't see any reason for not leveraging it.
AVFoundation lacks sound placement. This makes me sad.
I’ve written a simple sound engine around OpenAL. There are no position controls (I didn’t need them), but they would be trivial to add if you find the rest to your liking. And there is also some experimental sound code in the Cocos2D engine. It has both pitch and position controls and looks quite usable.