/**\brief Audio system shutdown. */ bool Audio::Shutdown( void ){ /* This is the cleaning up part */ this->HaltAll(); #if defined(SDL_MIXER_MAJOR_VERSION) && (SDL_MIXER_MAJOR_VERSION>1) \ && (SDL_MIXER_MINOR_VERSON>2) && (SDL_MIXER_PATCHLEVEL>=10) // Free every library loaded const SDL_version *mix_version=Mix_Linked_Version(); if( (mix_version->major>=1) && (mix_version->minor>=2) && (mix_version->patch>=10) ){ while(Mix_Init(0)) Mix_Quit(); } #endif // SDL_Mixer version requirements // Query number of times audio device was opened (should be 1) int freq, chan, ntimes; Uint16 format; if ( (ntimes = Mix_QuerySpec( &freq, &format, &chan )) != 1 ) LogMsg(WARN,"Audio was initialized multiple times."); // Close as many times as opened. for ( int i = 0; i < ntimes; i++ ) Mix_CloseAudio(); return true; }
AudioSDLMixer::AudioSDLMixer(AudioConfig &sc) : Audio(sc), audio_open(0), music(NULL), oldPlaying(false) { if ( SDL_InitSubSystem(SDL_INIT_AUDIO) < 0 ) throw std::runtime_error(std::string("Couldn't initialize SDL: ")+SDL_GetError()); /* Open the audio device */ int audio_rate = m_sc.srate; Uint16 audio_format=AUDIO_S8; if (m_sc.sbits==16) audio_format=AUDIO_S16; int audio_channels=1; if (m_sc.stereo) audio_channels=2; int audio_buffers = m_sc.sbuffers; if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) { throw std::runtime_error(std::string("Couldn't open audio: ")+SDL_GetError()); } else { Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); /* printf("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate, (audio_format&0xFF), (audio_channels > 1) ? "stereo" : "mono", audio_buffers ); */ } audio_open = 1; /* Set the external music player, if any */ // TODO: perhaps we should not use this for security reasons Mix_SetMusicCMD(getenv("MUSIC_CMD")); }
/** * @brief Loads a sound into the sound_list. * * @param filename Name fo the file to load. * @return The SDL_Mixer of the loaded chunk. * * @sa sound_makeList */ int sound_mix_load( alSound *s, const char *filename ) { SDL_RWops *rw; int freq, bytes, channels; Uint16 format; /* get the file data buffer from packfile */ rw = ndata_rwops( filename ); /* bind to buffer */ s->u.mix.buf = Mix_LoadWAV_RW(rw,1); if (s->u.mix.buf == NULL) { DEBUG("Unable to load sound '%s': %s", filename, Mix_GetError()); return -1; } /* Get spec. */ Mix_QuerySpec( &freq, &format, &channels ); switch (format) { case AUDIO_U8: case AUDIO_S8: bytes = 1; break; default: bytes = 2; break; } /* Set length. */ s->length = (double)s->u.mix.buf->alen / (double)(freq*bytes*channels); return 0; }
/** * @brief Prints the current and compiled SDL_Mixer versions. */ static void print_MixerVersion (void) { int frequency; Uint16 format; int channels; SDL_version compiled; const SDL_version *linked; char device[PATH_MAX]; /* Query stuff. */ Mix_QuerySpec(&frequency, &format, &channels); MIX_VERSION(&compiled); linked = Mix_Linked_Version(); SDL_AudioDriverName(device, PATH_MAX); /* Version itself. */ DEBUG("SDL_Mixer Started: %d Hz %s", frequency, (channels == 2) ? "Stereo" : "Mono" ); /* Check if major/minor version differ. */ if ((linked->major*100 + linked->minor) > compiled.major*100 + compiled.minor) WARN("SDL_Mixer is newer than compiled version"); if ((linked->major*100 + linked->minor) < compiled.major*100 + compiled.minor) WARN("SDL_Mixer is older than compiled version."); /* Print other debug info. */ DEBUG("Renderer: %s",device); DEBUG("Version: %d.%d.%d [compiled: %d.%d.%d]", compiled.major, compiled.minor, compiled.patch, linked->major, linked->minor, linked->patch); DEBUG(); }
void initSound() { int audio_rate; Uint16 audio_format; int audio_channels; int audio_buffers; if ( SDL_InitSubSystem(SDL_INIT_AUDIO) < 0 ) { fprintf(stderr, "Unable to initialize SDL_AUDIO: %s\n", SDL_GetError()); return; } audio_rate = 44100; audio_format = AUDIO_S16; audio_channels = 1; audio_buffers = 4096; if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) { fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError()); return; } else { Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); } useAudio = 1; loadSounds(); }
static dboolean SDLIsInitialized(void) { int freq, channels; Uint16 format; return (!!Mix_QuerySpec(&freq, &format, &channels)); }
/**\brief Audio system initialization. */ bool Audio::Initialize( void ) { if( initialized ) { return false; } if(OPTION(bool, "options/sound/disable-audio")) { LogMsg(INFO, "Not initializing audio as it is disabled."); return true; // audio is disabled } if(SDL_Init(SDL_INIT_AUDIO) < 0) { LogMsg(ERR, "SDL could not initialize audio: %s", SDL_GetError()); return false; } if(Mix_OpenAudio(this->audio_rate, this->audio_format, this->audio_channels, this->audio_buffers)) { LogMsg(ERR, "Audio initialization failed!: %s", Mix_GetError()); return false; } Mix_QuerySpec(&this->audio_rate, &this->audio_format, &this->audio_channels); LogMsg(DEBUG, "SDL_mixer gave us rate %d, format %d, channels %d", this->audio_rate, this->audio_format, this->audio_channels); // Load MOD and OGG libraries Mix_Init( MIX_INIT_MOD | MIX_INIT_OGG ); // Allocate channels Mix_AllocateChannels( this->max_chan); assert( this->max_chan == static_cast<unsigned int>(this->GetTotalChannels()) ); return true; }
/**\brief Audio system shutdown. */ bool Audio::Shutdown( void ) { if(OPTION(bool, "options/sound/disable-audio")) { return true; // audio is disabled } /* Halt all currently playing sounds */ Mix_HaltChannel( -1 ); // Free every library loaded while(Mix_Init(0)) { Mix_Quit(); } // Query number of times audio device was opened (should be 1) int freq, chan, ntimes; Uint16 format; ntimes = Mix_QuerySpec( &freq, &format, &chan ); LogMsg(DEBUG, "Audio Query: %d Frequencies, Format: %d, Channels: %s.", freq, format, (chan==2?"Stereo":"Mono")); if(ntimes != 1 ) { LogMsg(WARN, "Audio was initialized %d times.", ntimes); } // Close only if open if( ntimes ) { Mix_CloseAudio(); } return true; }
static bool start_sound_engine() { /* This is where we open up our audio device. Mix_OpenAudio takes as its parameters the audio format we'd /like/ to have. */ if(Mix_OpenAudio(info.rate, info.format, info.channels, info.buffers)) { GB.Error("Unable to open audio"); return TRUE; } if (pipe(_ch_pipe)) { GB.Error("Unable to initialize channel pipe"); return TRUE; } Mix_QuerySpec(&info.rate, &info.format, &info.channels); //fprintf(stderr, "Mix_QuerySpec: %d %d %d\n", info.rate, info.format, info.channels); channel_count = Mix_AllocateChannels(-1); Mix_ChannelFinished(channel_finished); return FALSE; }
void SoundManager::ShutdownMix() { // for each Mix_OpenAudio call, there must be corresponding Mix_CloseAudio call int numOpen = Mix_QuerySpec(nullptr, nullptr, nullptr); for (int i = 0; i < numOpen; ++i) Mix_CloseAudio(); }
/**\brief Audio system shutdown. */ bool Audio::Shutdown( void ){ /* This is the cleaning up part */ this->HaltAll(); #if defined(SDL_MIXER_MAJOR_VERSION) && (SDL_MIXER_MAJOR_VERSION>1) \ && (SDL_MIXER_MINOR_VERSON>2) && (SDL_MIXER_PATCHLEVEL>=10) // Free every library loaded const SDL_version *mix_version=Mix_Linked_Version(); if( (mix_version->major>=1) && (mix_version->minor>=2) && (mix_version->patch>=10) ){ while(Mix_Init(0)) Mix_Quit(); } #endif // SDL_Mixer version requirements // Query number of times audio device was opened (should be 1) int freq, chan, ntimes; Uint16 format; ntimes = Mix_QuerySpec( &freq, &format, &chan ); LogMsg(INFO,"Audio Query: %d Frequencies, Format: %d, Channels: %s.", freq, format, (chan==2?"Stereo":"Mono")); if(ntimes != 1 ) { LogMsg(WARN,"Audio was initialized %d times.", ntimes); } // Close only if open if( ntimes ) { Mix_CloseAudio(); } return true; }
SoundEffect::SoundEffect(const string& filename, Uint8 volume) : sound(NULL), channel(-1) { if (++loadCount == 1) { if (Mix_OpenAudio(audioRate, audioFormat, audioChannels, audioBuffers)) { cerr << "Failed to open audio: " << Mix_GetError() << endl; cerr << "(Disabling sound effects)" << endl; SetEnabled(false); return; } // Get the actual settings used Mix_QuerySpec(&audioRate, &audioFormat, &audioChannels); } if (!enabled) return; if (!(sound = Mix_LoadWAV(filename.c_str()))) { ostringstream ss; ss << "Error loading " << filename << ": "; ss << Mix_GetError(); throw runtime_error(ss.str()); } sound->volume = volume; }
/* * S_Init */ void S_Init(void) { int freq, channels; unsigned short format; memset(&s_env, 0, sizeof(s_env)); if (Cvar_GetValue("s_disable")) { Com_Warn("Sound disabled.\n"); return; } Com_Print("Sound initialization...\n"); s_rate = Cvar_Get("s_rate", "44100", CVAR_ARCHIVE | CVAR_S_DEVICE, "Sound sampling rate in Hz."); s_reverse = Cvar_Get("s_reverse", "0", CVAR_ARCHIVE, "Reverse left and right channels."); s_volume = Cvar_Get("s_volume", "1.0", CVAR_ARCHIVE, "Global sound volume level."); Cmd_AddCommand("s_restart", S_Restart_f, "Restart the sound subsystem"); Cmd_AddCommand("s_play", S_Play_f, NULL); Cmd_AddCommand("s_stop", S_Stop_f, NULL); Cmd_AddCommand("s_list", S_List_f, NULL); if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) { if (SDL_Init(SDL_INIT_AUDIO) < 0) { Com_Warn("S_Init: %s.\n", SDL_GetError()); return; } } else if (SDL_WasInit(SDL_INIT_AUDIO) == 0) { if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) { Com_Warn("S_Init: %s.\n", SDL_GetError()); return; } } if (Mix_OpenAudio(s_rate->integer, MIX_DEFAULT_FORMAT, 2, 1024) == -1) { Com_Warn("S_Init: %s\n", Mix_GetError()); return; } if (Mix_QuerySpec(&freq, &format, &channels) == 0) { Com_Warn("S_Init: %s\n", Mix_GetError()); return; } if (Mix_AllocateChannels(MAX_CHANNELS) != MAX_CHANNELS) { Com_Warn("S_Init: %s\n", Mix_GetError()); return; } Mix_ChannelFinished(S_FreeChannel); Com_Print("Sound initialized %dKHz %d channels.\n", freq, channels); s_env.initialized = true; S_InitMusic(); }
bool AudioDevice::init(Log* _debugLog) { debugLog = _debugLog; if (debugLog != NULL) { debugLog->printf("Initializing audio system with default settings\n"); } int audioRate = MIX_DEFAULT_FREQUENCY; uint16 audioFormat = MIX_DEFAULT_FORMAT; int audioChannels = 2; // stereo bool ret = Mix_OpenAudio(audioRate, audioFormat, audioChannels, 4096) >= 0; if (debugLog != NULL) { if (ret) { debugLog->println("Audio initalization Ok"); } else { debugLog->println("Audio initalization FAIL"); debugLog->printf("Couldn't open audio: %s\n", SDL_GetError()); } } if (ret) { Mix_QuerySpec(&audioRate, &audioFormat, &audioChannels); } return ret; }
void close_sound() { int numtimesopened, frequency, channels; Uint16 format; if (mix_ok) { stop_bell(); stop_UI_sound(); stop_sound(); sound_cache.clear(); stop_music(); mix_ok = false; numtimesopened = Mix_QuerySpec(&frequency, &format, &channels); if(numtimesopened == 0) { ERR_AUDIO << "Error closing audio device: " << Mix_GetError() << "\n"; } while (numtimesopened) { Mix_CloseAudio(); --numtimesopened; } } if (SDL_WasInit(SDL_INIT_AUDIO) != 0) { SDL_QuitSubSystem(SDL_INIT_AUDIO); } LOG_AUDIO << "Audio device released.\n"; }
static int SDLIsInitialized(void) { int freq, channels; Uint16 format; return Mix_QuerySpec(&freq, &format, &channels); }
void CleanupT4KCommon(void) { int frequency, channels, n_timesopened; Uint16 format; // Close the audio mixer. We have to do this at least as many times // as it was opened. n_timesopened = Mix_QuerySpec(&frequency, &format, &channels); while (n_timesopened) { Mix_CloseAudio(); n_timesopened--; } T4K_UnloadMenus(); // Unload SDL_Pango or SDL_ttf: T4K_Cleanup_SDL_Text(); #ifdef HAVE_LIBSDL_NET /* Quit networking if appropriate: */ SDLNet_Quit(); #endif // Finally, quit SDL SDL_Quit(); }
int snd_hz(void) { int freq = 0; if (sound_on) Mix_QuerySpec(&freq, NULL, NULL); return freq; }
void Snd_Init() { printf( "Initializing Sound\n" ); //return; /* Initialize variables */ audio_rate = MIX_DEFAULT_FREQUENCY; audio_format = MIX_DEFAULT_FORMAT; audio_channels = 2; /* Open Device */ if( Mix_OpenAudio( audio_rate, audio_format, audio_channels, 4096 ) < 0 ) { printf( "ERROR: Couldn't open audio device\n" ); } else { printf( "Trying to query audio device\n" ); Mix_QuerySpec( &audio_rate, &audio_format, &audio_channels ); printf( "Successfully opened audio device\n" ); printf("Opened audio at %d Hz %d bit %s\n", audio_rate, (audio_format&0xFF), (audio_channels > 1) ? "stereo" : "mono"); } audioOpen = True; soundcount = 0; bPlaySounds = True; nullsound.soundid = -1; }
/**\brief Audio system initialization. */ bool Audio::Initialize( void ){ if ( this -> initstatus ) return false; // Already initialized if(SDL_Init(SDL_INIT_AUDIO) < 0) { LogMsg(CRITICAL,"SDL could not initialize audio: %s", SDL_GetError()); } if(Mix_OpenAudio(this->audio_rate, this->audio_format, this->audio_channels, this->audio_buffers)){ LogMsg(CRITICAL,"Audio initialization failed!: %s", Mix_GetError()); return false; } Mix_QuerySpec(&this->audio_rate, &this->audio_format, &this->audio_channels); LogMsg(INFO,"SDL_mixer gave us rate %d, format %d, channels %d", this->audio_rate, this->audio_format, this->audio_channels); #if defined(SDL_MIXER_MAJOR_VERSION) && (SDL_MIXER_MAJOR_VERSION>1) \ && (SDL_MIXER_MINOR_VERSON>2) && (SDL_MIXER_PATCHLEVEL>=10) // Load MOD and OGG libraries (If SDL_mixer version supports it) const SDL_version *mix_version=Mix_Linked_Version(); if( (mix_version->major >= 1) && (mix_version->minor >= 2) && (mix_version->patch >= 10) ){ LogMsg(INFO,"This SDL_mixer version supports dynamic library loading."); Mix_Init( MIX_INIT_MOD | MIX_INIT_OGG ); } #endif // SDL_MIXER version requirements // Allocate channels Mix_AllocateChannels( this->max_chan); assert( this->max_chan == static_cast<unsigned int>(this->GetTotalChannels()) ); return true; }
SDLSoundChannel(const ByteArray &inBytes, const SoundTransform &inTransform) { Mix_QuerySpec(&mFrequency, &mFormat, &mChannels); if (mFrequency!=44100) ELOG("Warning - Frequency mismatch %d",mFrequency); if (mFormat!=32784) ELOG("Warning - Format mismatch %d",mFormat); if (mChannels!=2) ELOG("Warning - channe mismatch %d",mChannels); mChunk = 0; mDynamicBuffer = new short[BUF_SIZE * STEREO_SAMPLES]; memset(mDynamicBuffer,0,BUF_SIZE*sizeof(short)); mSound = 0; mChannel = -1; mDynamicChunk.allocated = 0; mDynamicChunk.abuf = (Uint8 *)mDynamicBuffer; mDynamicChunk.alen = BUF_SIZE * sizeof(short) * STEREO_SAMPLES; // bytes mDynamicChunk.volume = MIX_MAX_VOLUME; #ifndef WEBOS mDynamicChunk.length_ticks = 0; #endif mDynamicFillPos = 0; mDynamicStartPos = 0; mDynamicDataDue = 0; // Allocate myself a channel for(int i=0;i<sMaxChannels;i++) if (!sUsedChannel[i]) { IncRef(); sDoneChannel[i] = false; sUsedChannel[i] = true; mChannel = i; break; } if (mChannel>=0) { FillBuffer(inBytes); // Just once ... if (mDynamicFillPos<1024) { mDynamicDone = true; mDynamicChunk.alen = mDynamicFillPos * sizeof(short) * STEREO_SAMPLES; Mix_PlayChannel( mChannel , &mDynamicChunk, 0 ); } else { mDynamicDone = false; // TODO: Lock? Mix_PlayChannel( mChannel , &mDynamicChunk, -1 ); mDynamicStartPos = sSoundPos; } Mix_Volume( mChannel, inTransform.volume*MIX_MAX_VOLUME ); } }
int32_t MUSIC_StopSong(void) { #if defined FORK_EXEC_MIDI if (external_midi) { if (external_midi_pid > 0) { int32_t ret; struct timespec ts; external_midi_restart = 0; // make SIGCHLD handler a no-op ts.tv_sec = 0; ts.tv_nsec = 5000000; // sleep 5ms at most kill(external_midi_pid, SIGTERM); nanosleep(&ts, NULL); ret = waitpid(external_midi_pid, NULL, WNOHANG|WUNTRACED); // printf("(%d)", ret); if (ret != external_midi_pid) { if (ret==-1) perror("waitpid"); else { // we tried to be nice, but no... kill(external_midi_pid, SIGKILL); initprintf("%s: wait for SIGTERM timed out.\n", __func__); if (waitpid(external_midi_pid, NULL, WUNTRACED)==-1) perror("waitpid (2)"); } } external_midi_pid = -1; } return(MUSIC_Ok); } #endif //if (!fx_initialized) if (!Mix_QuerySpec(NULL, NULL, NULL)) { setErrorMessage("Need FX system initialized, too. Sorry."); return(MUSIC_Error); } // if if ((Mix_PlayingMusic()) || (Mix_PausedMusic())) Mix_HaltMusic(); if (music_musicchunk) Mix_FreeMusic(music_musicchunk); music_musicchunk = NULL; return(MUSIC_Ok); } // MUSIC_StopSong
int _main(int arvc , char * argv[]) { SDL_Surface *screen; SDL_Event event; int done = 0; /* We're going to be requesting certain things from our audio device, so we set them up beforehand */ int audio_rate = 22050; Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */ int audio_channels = 2; int audio_buffers = 4096; SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO); /* This is where we open up our audio device. Mix_OpenAudio takes as its parameters the audio format we'd /like/ to have. */ if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) { printf("Unable to open audio!\n"); exit(1); } /* If we actually care about what we got, we can ask here. In this program we don't, but I'm showing the function call here anyway in case we'd want to know later. */ Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); /* We're going to be using a window onscreen to register keypresses in. We don't really care what it has in it, since we're not doing graphics, so we'll just throw something up there. */ screen = SDL_SetVideoMode(320, 240, 0, 0); while(!done) { while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_QUIT: done = 1; break; case SDL_KEYDOWN: case SDL_KEYUP: handleKey(event.key); break; } } /* So we don't hog the CPU */ SDL_Delay(50); } /* This is the cleaning up part */ Mix_CloseAudio(); SDL_Quit(); return 0; }
void SoundProcessor::printSoundInformation() const { #ifdef _FMOD_SOUND FMOD_RESULT result; int driver_num; result = soundEngine->getNumDrivers(&driver_num); ERRCHECK(result); std::ostringstream os; os << "* Available sound drivers: "; for(unsigned int i = driver_num; i--;) { char driver_name[256]; result = soundEngine->getDriverInfo(i, driver_name, 256, 0); ERRCHECK(result); os << driver_name << " "; } toInfoLog(os.str()); os.str(""); int current_driver; result = soundEngine->getDriver(¤t_driver); ERRCHECK(result); os << "* Driver used: "; if(current_driver == -1) { os << "Primary or main sound device as selected by the operating system settings"; if(driver_num == 1) { char driver_name[256]; result = soundEngine->getDriverInfo(current_driver, driver_name, 256, 0); ERRCHECK(result); os << "(probably '" << driver_name << "')"; } } else { char driver_name[256]; result = soundEngine->getDriverInfo(current_driver, driver_name, 256, 0); ERRCHECK(result); os << driver_name; } toInfoLog(os.str()); #elif _SDL_MIXER_SOUND int audio_rate, audio_channels; Uint16 audio_format; int success = Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); if(!success) { std::ostringstream os; os << "ERROR (SoundProcessor::printSoundInformation()): " << Mix_GetError(); throw SDLException(os.str()); } int bits = audio_format & 0xFF; std::ostringstream os; os << "* Opened audio at " << audio_rate << "Hz " << bits << "bit " << ((audio_channels>1)?"stereo":"mono");// << ", " << audio_buffers << " bytes audio buffer"; toInfoLog(os.str()); #endif }
/*! Returns the status of the audio device \return True if audio has been opened, False otherwise \author jfpatry \date Created: 2000-09-02 \date Modified: 2000-09-02 */ bool_t is_audio_open() { int tmp_freq; Uint16 tmp_format; int tmp_channels; return (bool_t) Mix_QuerySpec( &tmp_freq, &tmp_format, &tmp_channels ); }
int32_t SDLDrv_PCM_Init(int32_t *mixrate, int32_t *numchannels, void * initdata) { int32_t err = 0; int32_t chunksize; uint16_t fmt; UNREFERENCED_PARAMETER(numchannels); UNREFERENCED_PARAMETER(initdata); if (Initialised) { SDLDrv_PCM_Shutdown(); } chunksize = 512; #ifdef __ANDROID__ chunksize = droidinfo.audio_buffer_size; #endif if (*mixrate >= 16000) chunksize *= 2; if (*mixrate >= 32000) chunksize *= 2; err = Mix_OpenAudio(*mixrate, AUDIO_S16SYS, *numchannels, chunksize); if (err < 0) { ErrorCode = SDLErr_OpenAudio; return SDLErr_Error; } if (Mix_QuerySpec(mixrate, &fmt, numchannels)) { if (fmt == AUDIO_U8 || fmt == AUDIO_S8) { ErrorCode = SDLErr_OpenAudio; return SDLErr_Error; } } //Mix_SetPostMix(fillData, NULL); EffectFence = SDL_CreateMutex(); // channel 0 and 1 are actual sounds // dummy channel 2 runs our fillData() callback as an effect Mix_RegisterEffect(2, fillData, NULL, NULL); DummyBuffer = (uint8_t *) calloc(1, chunksize); DummyChunk = Mix_QuickLoad_RAW(DummyBuffer, chunksize); Mix_PlayChannel(2, DummyChunk, -1); Initialised = 1; return SDLErr_Ok; }
void initSound() { // Initialize SDL_Mixer if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) { fprintf(stderr, "Failed to initialize SDL_Mixer: %s\n", Mix_GetError()); exit(1); } atexit(cleanupSound); Mix_QuerySpec(&audio_rate, &audio_format, &audio_buffers); }
void I_InitAudio(void) { sysaudio.obtained.freq = sysaudio.desired.freq = SAMPLERATE; sysaudio.obtained.format = sysaudio.desired.format = AUDIO_S16SYS; sysaudio.obtained.channels = sysaudio.desired.channels = 2; sysaudio.obtained.samples = sysaudio.desired.samples = SAMPLECOUNT; sysaudio.obtained.size = sysaudio.desired.size = (SAMPLECOUNT*sysaudio.obtained.channels*((sysaudio.obtained.format&0xff)>>3)); if (!sysaudio.enabled) return; #ifdef ENABLE_SDLMIXER { int freq, channels; Uint16 format; if (Mix_OpenAudio(sysaudio.desired.freq, sysaudio.desired.format, sysaudio.desired.channels, sysaudio.desired.samples)<0) { I_Error("Could not open audio: %s\n", SDL_GetError()); } if (!Mix_QuerySpec(&freq, &format, &channels)) { I_Error("Could not open audio: %s\n", SDL_GetError()); } sysaudio.obtained.freq = freq; sysaudio.obtained.channels = channels; sysaudio.obtained.format = format; sysaudio.obtained.size = (SAMPLECOUNT*channels*((format&0xff)>>3)); I_InitMusic(); I_InitSound(); Mix_SetPostMix(I_UpdateSound, NULL); /* Mix_ReserveChannels(NUM_CHANNELS);*/ } #else sysaudio.desired.callback = I_UpdateAudio; sysaudio.desired.userdata = NULL; if (SDL_OpenAudio(&sysaudio.desired, &sysaudio.obtained)<0) { I_Error("Could not open audio: %s\n", SDL_GetError()); } I_InitMusic(); I_InitSound(); SDL_PauseAudio(0); #endif }
void AudioEngine::play_music(std::string file_location) { if(currently_playing.compare(file_location) != 0) { //only change the song if it's not already playing! //Initialise audio variables int audio_rate; //output sampling rate (in Hz) Uint16 audio_format; //output sample format. int audio_channels; //the number of channels for the output. int audio_buffers; //the chunk size of the output, larger is better for slower machines but means the sound will be delayed more. int audio_volume; //the volume at which you wish to play the music int looping; //wether the music should loop or not // Initialize variables audio_rate = MIX_DEFAULT_FREQUENCY; //22050 audio_format = AUDIO_S16; //Signed 16-bit samples, in little-endian byte order audio_channels = 2; //1 = mono, 2 = stereo audio_buffers = 4096; //good value for slower machines and doesn't have too much delay (shouldn't matter too much for music vs sound effects anyway audio_volume = get_music_volume(); looping = -1; //loop through the music forever //Initialise the audio API, returns -1 on errors so check it initialises correctly if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) { LOG(ERROR) << "Couldn't open audio: " << SDL_GetError(); //LOG why Audio couldn't be initialised audio_open = false; return; } //Query the auddio device for it's settings. Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); // Set the music volume set_music_volume(audio_volume); const char* c_file_location = file_location.c_str(); //Convert the file_location to a c string for the SDL API music = Mix_LoadMUS(c_file_location); //Load in the music if (music == nullptr ) { LOG(ERROR) << "Couldn't load " << c_file_location << ": " << SDL_GetError(); //print why music couldn't be loaded to the screen Mix_CloseAudio(); //close down the audio system audio_open = false; return; } //while(!Mix_FadeOutMusic(1000) && Mix_PlayingMusic()) { // wait for any fades to complete // SDL_Delay(100); //} //TODO: implement some sort of music (and screen!) fade //Start playing the music. if(Mix_FadeInMusic(music, looping, 2000) == -1) { LOG(ERROR) << "Mix_FadeInMusic: " << Mix_GetError(); // well, there's no music, but most games don't break without music... } currently_playing = file_location; } return; }
static void print_format() { int audio_channels; int audio_rate; Uint16 audio_format; Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); printf("Opened audio at %d Hz %d bit %s\n", audio_rate, (audio_format & 0xFF), (audio_channels > 2) ? "surround" : (audio_channels > 1) ? "stereo" : "mono"); }