3

Are there any C++ (LGPL or LGPL-like) sound libraries which allow me to play a sound by purely specifying stuff like frequency and volume etc?

My target platform is Linux/Ubuntu and I don't really care about cross-platform.

It would be nice if I could send an array of sound and it would be able to compress it into a common sound file like for example mp3.

I'm looking for something OpenGL-like where you can just 'draw' the sound and it'll be played. I have heard about OpenAL but that only seems to be a library for loading and playing sounds.

Jeroen
  • 15,257
  • 12
  • 59
  • 102
  • 2
    Most simple and low-level audio libraries (e.g OpenAL, SDL, etc.) can play *PCM* waveforms quite easily. You fill a buffer with the wave you want to play, and play it. To make a waveform from stuff like frequency and volume and whatnot, you need to write a bit of code (probably a couple of lines for simple waves) and need to know basic trigonometry. – yzt Aug 05 '13 at 19:21
  • @yzt Do you know of any references on this for OpenAL? Only thing I can find is how to load in wav files... – Jeroen Aug 05 '13 at 19:23
  • Sounds like you're thinking of the `PLAY` command available in some versions of Basic. Have a look at http://stackoverflow.com/q/11355353/103167 – Ben Voigt Aug 05 '13 at 19:42
  • @Binero: Playing a buffer that contains audio samples is the most basic thing that it does (even simpler than loading a WAV file.) You just need to fill the buffer, set up the parameters (sampling rate, bits per sample, etc.) and play it (after setting up the playback device.) Unfortunately, it has been several years since I have used OpenAL, and I cannot give a more detailed answer. But I'm sure there are examples in their releases or around the net, and even learning and doing it from their documentation shouldn't take more than a couple of hours. – yzt Aug 05 '13 at 19:44
  • @yzt The problem is that most programs use some magic to read the wave files so I don't actually know what they contain... – Jeroen Aug 05 '13 at 19:47
  • @Binero: I seem to have misunderstood your problem. Is your problem playing a simple waveform or a WAV *file*?! If you know how to generate the waveform you want, but don't know how to play it, read my answer below. If you don't know how to even generate the sound you want to play, then I have no particular expertise there and can't help you. If your problem is just playing a PCM (uncompressed) WAV file, then also read my answer below. – yzt Aug 05 '13 at 19:56
  • @yzt My problem is, if I have a noise, how do I load it in to be playable? – Jeroen Aug 05 '13 at 19:58

1 Answers1

6

Most simple and low-level audio libraries (e.g OpenAL, SDL, etc.) can play PCM waveforms quite easily. You fill a buffer with the wave you want to play, and play it. To make a waveform from stuff like frequency and volume and whatnot, you need to write a bit of code (probably a couple of lines for simple waves) and need to know basic trigonometry.

OpenAL is a cross-platform API targeted towards 3D games. Its interface is philosophically similar to OpenGL and provides functions to manage audio sources in a virtual 3D environment (position, speed, etc.) and provides some sound and environment effects (reverb, etc.) I know that it can decode some compressed formats (MP3, Vorbis,...) using extensions, but I'm not sure whether it has any encoding functionality.

SDL (or Simple Direct-media Layer) is also cross-platform and game-oriented, but it offers much more than audio. But any functionality that it does offer is very basic and this is intentional and by design. SDL is a platform abstraction layer. It's audio capabilities are similarly very basic and low-level; providing only playing and recording PCM waves. Of course, there are extention libraries (e.g. SDL_mixer) that have more functionality.

References:

  1. OpenAL on Wikipedia
  2. OpenAL homepage (seems down, as of Aug 5th, 2013)
  3. OpenAL Soft, a fork of the OpenAL library available from Creative Labs
  4. SDL homepage
  5. SDL_mixer

As far as I know, both projects ship documentation and examples along with their source code, so you might want to get their source code and start experimenting.

If I've understood what you want to do correctly, either of these libraries can do what you want rather easily, but in my personal opinion, SDL is simpler and easier to use (unless you want 3D positional audio and effects.)

yzt
  • 8,873
  • 1
  • 35
  • 44