/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Simon Hofmann co-programmer: */ #include "sound_control.h" using namespace std; SoundControl* SoundControl::sound = SoundControl::getInstance(); SoundControl* SoundControl::singletonRef = NULL; /** \brief standard constructor This constructor builds a SoundControl Object and initialises it . All sound output is handled by this singleton object. */ SoundControl::SoundControl() { this->init(); } /** \brief Default destructor */ SoundControl::~SoundControl() { singletonRef = NULL; } /** \brief Returns a reference to the SoundControl singleton */ SoundControl* SoundControl::getInstance() { if (SoundControl::singletonRef) return singletonRef; else return singletonRef = new SoundControl; } /** \brief Is called by SoundControl object to initiate all values and to output some text */ void SoundControl::init(void) { this->sfx_channel1 = -1; this->sfx_channel2 = -1; this->finished = 0; this->volume = SDL_MIX_MAXVOLUME; this->track_number = 1; this->music = NULL; this->audio_rate = 44100; this->audio_channels = MIX_DEFAULT_CHANNELS; this->audio_buffers = 16384; this->bits = 0; this->audio_format = MIX_DEFAULT_FORMAT; // initialize SDL this->isInit = false; if (SDL_Init(SDL_INIT_AUDIO)) { PRINTF(1)("SDL-sound could not be initialized\n"); return; } else this->isInit = true; Mix_QuerySpec(&this->audio_rate, &this->audio_format, &this->audio_channels); this->bits=this->audio_format&0xFF; PRINTF(3)("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", this->audio_rate, this->bits, this->audio_channels > 1 ? "stereo" : "mono", this->audio_buffers ); Mix_VolumeMusic(this->volume); if(Mix_OpenAudio(this->audio_rate, this->audio_format, this->audio_channels, this->audio_buffers)) PRINTF(1)("Mix_OpenAudio: Failed to open audio!\n"); } /** \brief Sets the number of output Channels */ void SoundControl::setNumberOfChannels(int number_of_channels) { Mix_AllocateChannels(number_of_channels); } /** \brief Static function to play a .xm file \param filename: self-explanatory */ void SoundControl::playMod(char* fileName) { Mix_Chunk* chunk = Mix_LoadWAV(fileName); if(Mix_PlayChannel(-1, chunk, 0) == -1) { printf("Mix_PlayChannel: %s\n", Mix_GetError()); } } /** \brief Static function to play a .wav file \param filename: self-explanatory */ void SoundControl::playWav(char* fileName) { Mix_Chunk* chunk = Mix_LoadWAV(fileName); if(Mix_PlayChannel(-1, chunk, 0) == -1) { printf("Mix_PlayChannel: %s\n", Mix_GetError()); } } /** \brief Static function to play an .ogg file \param filename: self-explanatory */ void SoundControl::playOgg(char* fileName) { Mix_Music* music = Mix_LoadMUS(fileName); if(Mix_PlayMusic(music, 1) == -1) { printf("Mix_PlayMusic: %s\n",Mix_GetError()); } Mix_HookMusicFinished(musicDone); } /** \brief Heightens the overall volume of output */ void SoundControl::volumeUp() { this->volume = (this->volume++) << 1; if(this->volume > SDL_MIX_MAXVOLUME) this->volume = SDL_MIX_MAXVOLUME; Mix_VolumeMusic(this->volume); } /** \brief Lowers the overall volume of output */ void SoundControl::volumeDown() { this->volume >>= 1; if(this->volume < 0) this->volume = 1; Mix_VolumeMusic(this->volume); } /** \brief Rewinds music to the beginning */ void SoundControl::trackRewind() { Mix_RewindMusic(); } /** \brief Rewinds the music 5 seconds */ void SoundControl::forwardMusic() { Mix_SetMusicPosition(+5); } /** \brief Forwards the music 5 seconds */ void SoundControl::rewindMusic () { Mix_SetMusicPosition(-5); } /** \brief Pauses music output */ void SoundControl::pauseMusic() { Mix_PauseMusic(); } /** \brief Pauses music output */ void SoundControl::resumeMusic() { Mix_ResumeMusic(); } /** \brief Fades in music */ void fadeInMusic(int time) { } /** \brief Fades out music */ void SoundControl::fadeOutMusic(int time) { } /** \brief Hooked by playOgg at end of .ogg playback */ void SoundControl::musicDone() { Mix_Music* music = SoundControl::getInstance()->music; Mix_HaltMusic(); Mix_FreeMusic(music); music = NULL; }