Questions regarding SDL audio

Greetings.

I recently began working on a cross-platform client, and was searching
for a cross-platform library for handling sound. I am relatively new
to multimedia programming, so I was somewhat confused when I studied
the manuals/source. The examples did not answer these questions, and I
wasn’t able to find any mailing list archives or general resources
elsewhere which would help. So, I thought I’d ask you, and hope that
I’m not annoying you with my trivial questions. :slight_smile:

  1. What format does the library expect its sounds to be provided in?
    Or, is it format-independant, requiring the callback function
    specified in the SDLAudioSpec struct to play whatever format is
    fed to it? If a specific format is required, what format is it?
    And, if not, how does SDL_MixAudio know how to format the output?

  2. Is the SDL_AudioCVT structure used to convert sounds from, say,
    MP3->WAV, or to convert from stereo to mono? I’m asking because I
    have several effects stored as MP3’s, and I’m wondering how to go
    about using them, though I suspect that the answer to my first
    question will be somewhat enlightening in this respect. :slight_smile:

  3. I’m using C++. Are there any GPL/LGPL C++ classes which might make
    the task of sound programming a little easier?

Thanks a bunch for your time!–

                                       Nolan Darilek
                                  nolan_d at mail.utexas.edu
                                       nolan at cold.org
                                    @Nolan_Darilek
                                     ICQ UIN: 15709478

       "We apologize for the error in last week's paper in which
        we stated that Mr. Arnold Dogbody was a defective in the
        police force. We meant, of course, that Mr. Dogbody is a
                    detective in the police farce."
           -Correction Notice in the Ely Standard, a British
                               newspaper

I’m not annoying you with my trivial questions. :slight_smile:

Not a problem. I’m in the process of evaluating the way SDL handles
audio at the moment, so the API may change slightly in several months.

  1. What format does the library expect its sounds to be provided in?
    Or, is it format-independant, requiring the callback function
    specified in the SDLAudioSpec struct to play whatever format is
    fed to it? If a specific format is required, what format is it?
    And, if not, how does SDL_MixAudio know how to format the output?

It is semi-format independent. It plays raw digitized audio samples
in the format requested when you call SDL_OpenAudio(). If you look
at the SDL_AudioSpec structure, you’ll see the components that
affect the audio format:

format – this is the data type which is used to represent a single
audio data sample.

freq – this is the frequency at which the audio data is played back.

channels – this is the number of individual channels of sound in the
playback stream. Currently mono and stereo audio are supported.

  1. Is the SDL_AudioCVT structure used to convert sounds from, say,
    MP3->WAV, or to convert from stereo to mono? I’m asking because I
    have several effects stored as MP3’s, and I’m wondering how to go
    about using them, though I suspect that the answer to my first
    question will be somewhat enlightening in this respect. :slight_smile:

It converts from stereo to mono. You’ll have to look at the MP3 audio
format and see how to generate sampled audio stream from it. WAV files
are easy (and supported directly from within SDL) because they are
generally already in sampled audio data format. I don’t know if this
really answers your question.

There are several different ways to represent sound on the computer.
MIDI takes a set of instruments and mixes them with various effects
and durations to create a song, usually mixed in hardware.
MOD does the same thing, except the instruments are included in the
music file and they are mixed in software.
WAV files generally have pre-mixed audio saved in a series of samples
representing audio waveform amplitude. The frequency and data type
of these samples affects the quality of the output – the larger the
data type, the more variation in amplitude can be represented, and
the higher the frequency, the more closely they represent the original
analog sound wave. Most modern audio hardware can represent audio
using 16 bits of signed data, at a frequency of 22050 Hz. This gives
reasonable audio quality with a reasonable amount of CPU power, which
is why I recommend it as a standard audio format.

Because of the way real audio waves mix, a combination of two sounds
happening simultaneously can be represented by the addition of the
two sound waves. This can be done by adding together the samples
of each sound to give the final waveform. This is the process that
occurs when you mix several individual sound effects or instruments
into a single audio output stream. You can add special effects by
performing simple mathematical transformations on the final output.
More realistic audio mixing (handling attenuation, etc.) can be done
by using Fast Fourier Transform on the individual sound waves. This
is generally CPU intensive and not used in real-time audio mixing.

That’s about the limit of my current understanding of audio playback.
I hope that helps. :slight_smile:

  1. I’m using C++. Are there any GPL/LGPL C++ classes which might make
    the task of sound programming a little easier?

You can take a look at the maclib library in the demo archive. It’s
got a C++ class that loads and mixes Macintosh sound resources and
WAV files.

Thanks a bunch for your time!

My pleasure! :slight_smile:
I’m CC’ing this to the SDL list so they can benefit from the explanation.

See ya!
-Sam Lantinga (slouken at devolution.com)

Lead Programmer, Loki Entertainment Software–
Author of Simple DirectMedia Layer -
http://www.devolution.com/~slouken/SDL/

RAW sound files are simply stored exactly as they are in memory, eg an
8 bit mono RAW file consists of bytes that specify the volume of the
sound. Raw files contain no header.
RAW Graphics files are basically the same idea, but with graphics.
<---------->

Which brings up the question, what bytes are used to specify the
attributes of the sound? Is there a resource, preferably text/WWW and
not code, which describes this in more detail? :slight_smile:

. . .

Because of the way real audio waves mix, a combination of two sounds
happening simultaneously can be represented by the addition of the
two sound waves. This can be done by adding together the samples
of each sound to give the final waveform. This is the process that
occurs when you mix several individual sound effects or instruments
into a single audio output stream. You can add special effects by
performing simple mathematical transformations on the final output.
More realistic audio mixing (handling attenuation, etc.) can be done
by using Fast Fourier Transform on the individual sound waves. This
is generally CPU intensive and not used in real-time audio mixing.

That’s about the limit of my current understanding of audio playback.
I hope that helps. :slight_smile:

Makes sense. Can anyone direct me to any general sound resources which
describe these various techniques? I’m familiar with some of the
terminology, and have some knowledge of trig and wave motion, but I’m
just not sure what techniques I can use to manipulate sound waves, and
what they will accomplish.Cc: sdl at surfnetcity.com.au
From: Sam Lantinga
Date: Tue, 29 Dec 1998 19:05:35 -0800

It is semi-format independent. It plays raw digitized audio samples
in the format requested when you call SDL_OpenAudio(). If you look
at the SDL_AudioSpec structure, you’ll see the components that
affect the audio format:

Ah, makes sense.

My next question is, where can I find information on how raw,
digitized audio is formatted? A brief search on www.wotsit.org turned
up a file consisting of the following:

<---------->
From: mc59071@sis.port.ac.uk (Stuart Axon)
To: submit at wotsit.org
Date: Tue, 16 Jun 1998 13:46:25 +0100
Subject: RAW Sound/Graphics format

                                       Nolan Darilek
                                  nolan_d at mail.utexas.edu
                                       nolan at cold.org
                                    @Nolan_Darilek
                                     ICQ UIN: 15709478

       "We apologize for the error in last week's paper in which
        we stated that Mr. Arnold Dogbody was a defective in the
        police force. We meant, of course, that Mr. Dogbody is a
                    detective in the police farce."
           -Correction Notice in the Ely Standard, a British
                               newspaper