Sponsored Content
Top Forums Programming PLaying a sound file with SDL - C++ Post 302449444 by hakermania on Monday 30th of August 2010 03:12:17 PM
Old 08-30-2010
Question PLaying a sound file with SDL - C++

How do I play a sound file with SDL?
I've downloaded the library files but SDL/SDL_mixer.h (that most of the tutorials include) doesn't exist. So, how do I install it?

As I said mixer.h doesn't exist but, If it is helpful, the default code is this:
Code:
#include "stdlib.h"
#include "SDL/SDL.h"
#include "SDL/SDL_mixer.h"

int main(int argc, char *argv[])
{
        SDL_Surface *screen;
        Mix_Chunk *sound = NULL;
        int channel;
        int audio_rate = 22050;
        Uint16 audio_format = AUDIO_S16SYS;
        int audio_channels = 2;
        int audio_buffers = 4096;
        if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
                printf("Unable to initialize SDL: %s\n", SDL_GetError());
                return 1;
        }
        if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) != 0) {
                printf("Unable to initialize audio: %s\n", Mix_GetError());
                exit(1);
        }
        sound = Mix_LoadWAV("sound.wav");
        if(sound == NULL) {
                printf("Unable to load WAV file: %s\n", Mix_GetError());
        }
        screen = SDL_SetVideoMode(320, 240, 0, SDL_ANYFORMAT);
        if (screen == NULL) {
                printf("Unable to set video mode: %s\n", SDL_GetError());
                return 1;
        }
        channel = Mix_PlayChannel(-1, sound, 0);
        if(channel == -1) {
                printf("Unable to play WAV file: %s\n", Mix_GetError());
        }
        while(Mix_Playing(channel) != 0);
        Mix_FreeChunk(sound);
        Mix_CloseAudio();
        SDL_Quit();
        return 0;
}

Thanks for any replies....Smilie
 

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Insane question : Playing mp3 file from Sun Sparc

hi, i know this is crazy. but i can do this from linux in intel platform. just wonder can my Sun Solaris 8 sparc can do the same thing? (2 Replies)
Discussion started by: champion
2 Replies

2. UNIX for Dummies Questions & Answers

What is SDL?

I get a: Requirements: SDL when I'm browsing (www.happypenguin.org) for games. According to wikipedia SDL can be: where some are more likely than others. However, which one is it? I got an error because of SDL last time I tried and want to make it right before I try... (0 Replies)
Discussion started by: riwa
0 Replies

3. Programming

SDL doubt

Hi, I am working on SDL open-source API's on Debian Linux for my game development project. I am not sure whether posting SDL related queries here is correct. I would like to know what is the difference between: SDL_SetVideoMode() and SDL_VideoModeOK() functions (0 Replies)
Discussion started by: royalibrahim
0 Replies

4. UNIX for Dummies Questions & Answers

SDL error with the version

hi, i've just downloaded a test game to try compiling it with the gcc compiler in Solaris, the problem was that ./configure command return this error *** Could not run SDL test program, checking why... *** The test program compiled, but did not run. This usually means *** that the run-time linker... (0 Replies)
Discussion started by: freeware
0 Replies

5. Ubuntu

Playing wav-file sounds.

I would like to be able to shell out to the command line and play sounds. After installing "sox" I can now do this using the play command. Is there a way to stop the playing once it starts? (5 Replies)
Discussion started by: newyorkpaulie
5 Replies

6. Red Hat

playing sound in rhel5.1 ?

please any body tell me how to play sound using RealPlayer11 loaded in Rhel5.1? (1 Reply)
Discussion started by: rangaprem
1 Replies

7. Programming

SDL Image Filtering

Solved thanks for info!! (8 Replies)
Discussion started by: aLHaNz
8 Replies

8. Programming

Playing Video with MinGW and SDL

Hi All, I was wondering if someone could clarify the best way to display mpeg video with SDL in MinGW. After scouring the internet it seems there is not a lot of information regarding this subject (or at least not up to date) and libraries that are available for this purpose. Any help/... (1 Reply)
Discussion started by: robfwauk
1 Replies
pods::SDL::Mixer(3pm)					User Contributed Perl Documentation				     pods::SDL::Mixer(3pm)

NAME
SDL::Mixer - Sound and music functions CATEGORY
Mixer CONSTANTS
The constants are exported by default. You can avoid this by doing: use SDL::Mixer (); and access them directly: SDL::Mixer::MIX_DEFAULT_FREQUENCY; or by choosing the export tags below: Export tag: ':init' MIX_INIT_FLAC MIX_INIT_MOD MIX_INIT_MP3 MIX_INIT_OGG Export tag: ':defaults' MIX_CHANNELS MIX_DEFAULT_FORMAT MIX_DEFAULT_FREQUENCY MIX_DEFAULT_CHANNELS MIX_MAX_VOLUME MIX_CHANNEL_POST Export tag: ':fading' MIX_NO_FADING MIX_FADING_OUT MIX_FADING_IN Export tag: ':type' MUS_NONE MUS_CMD MUS_WAV MUS_MOD MUS_MID MUS_OGG MUS_MP3 MUS_MP3_MAD MUS_MP3_FLAC Export tag: ':format' AUDIO_U8 AUDIO_S8 AUDIO_U16LSB AUDIO_S16LSB AUDIO_U16MSB AUDIO_S16MSB AUDIO_U16 AUDIO_S16 AUDIO_U16SYS AUDIO_S16SYS Export tag: ':status' SDL_AUDIO_STOPPED SDL_AUDIO_PLAYING SDL_AUDIO_PAUSED DESCRIPTION
SDL::Mixer allows you to enable sound, alter music volume settings, and lets you play, pause and resume, as well as fading the sound and music in and out. Supported Formats The SDL Mixer library is a multi-channel audio mixer. It supports 8 channels of 16 bit stereo audio, and a single channel for music. You can use the channels to load samples (i.e. sound effects) in the following formats: o Microsoft WAVE files (WAV) o Creative Labs VOC files (VOC) o MIDI files (if compiled with Timidity) If you use MIDI, you should note that the process of mixing MIDI files to wave output is very CPU-intensive, so if playing regular WAVE files sound great, but playing MIDI files sound choppy, try using 8-bit audio, mono audio, or lower frequencies. The music channel can play the following formats: o AIFF o MOD (.mod .xm .s3m .669 .it .med and more - if compiled with libmikmod) o OggVorbis (.ogg - if compiled with ogg/vorbis libraries) o MP3 (if compiled with SMPEG or MAD libraries) o FLAC (if compiled with FLAC library) METHODS
init my $init_flags = SDL::Mixer::init( $flags ); Loads dynamic libraries and prepares them for use. Flags should be one or more flags from init flags OR'd together. It returns the flags successfully initialized, or 0 on failure. Example: use SDL::Mixer; my $init_flags = SDL::Mixer::init( MIX_INIT_MP3 | MIX_INIT_MOD | MIX_INIT_FLAC | MIX_INIT_OGG ); print("We have MP3 support! ") if $init_flags & MIX_INIT_MP3; print("We have MOD support! ") if $init_flags & MIX_INIT_MOD; print("We have FLAC support! ") if $init_flags & MIX_INIT_FLAC; print("We have OGG support! ") if $init_flags & MIX_INIT_OGG; Flags: o MIX_INIT_MP3 o MIX_INIT_MOD o MIX_INIT_FLAC o MIX_INIT_OGG Note: Only available for SDL_mixer >= 1.2.10 quit SDL::Mixer::quit(); This function unloads the libraries previously loaded with init(). Note: Only available for SDL_mixer >= 1.2.10 linked_version $version = SDL::Mixer::linked_version(); "linked_version" gives you the major-, minor-, and patchlevel for SDL_mixer. This way you can check if e.g. init() and quit() are available. Example: use SDL::Mixer; use SDL::Version; my $version = SDL::Mixer::linked_version(); printf("%d.%d.%d ", $version->major, $version->minor, $version->patch); # prints "1.2.8" for me open_audio my $audio_opened = SDL::Mixer::open_audio( $frequency, $format, $channels, $chunksize ); "open_audio" will initialize SDL_mixer if it is not yet initialized, see note. SDL_mixer may not be able to provide the exact specifications your provided, however it will automatically translate between the expected format and the real one. You can retrieve the real format using query_spec. Returns 0 on success, -1 on error. Note: You must not use "AUDIO_S16", "AUDIO_U16", "AUDIO_S16LSB", or "AUDIO_U16LSB." They are not portable, and SDL will not return an error code when they fail. The result will be a horrible staticy noise. You can usually use "AUDIO_S16SYS", though not always. Future versions of SDL should take this parameter only as a hint, then read back the value that the OS (for example, OSS or ALSA) has chosen to use in case the desired audio type is not supported. Note: When already initialized, this function will not re-initialize SDL_mixer, nor fail. It will merely increment the number of times SDL::Mixer::close_audio must be called to actually get it to uninitialize. This serves as a very simplistic method for multiple application components to use SDL_mixer without necessitating a great deal of inter-component awareness. Be warned however that in such a situation, the latest components to initialize SDL_mixer will probably not get the SDL_mixer settings they're expecting. Example: use SDL; use SDL::Mixer; printf("Error initializing SDL_mixer: %s ", SDL::get_error()) unless SDL::Mixer::open_audio(44100, AUDIO_S16, 2, 1024) == 0; close_audio SDL::Mixer::close_audio(); Close the mixer and halting all playing audio. This function does not return anything. query_spec my @query_spec = @{ SDL::Mixer::query_spec() }; Find out what the actual audio device parameters are. This function returns 1 as first array element (status) if the audio has been opened, 0 otherwise. Example: use SDL::Mixer; my ($status, $freq, $format, $channels) = @{ SDL::Mixer::query_spec() }; printf("%s, %s, %s, %s ", $status, $freq, $format, $channels); SEE ALSO
perl, SDL::Mixer::Channels, SDL::Mixer::Effects, SDL::Mixer::Groups, SDL::Mixer::Music. AUTHORS
See "AUTHORS" in SDL. perl v5.14.2 2012-05-28 pods::SDL::Mixer(3pm)
All times are GMT -4. The time now is 09:44 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy