/* This function loads a sound with SDL_LoadWAV and converts it to the specified sample format. Returns 0 on success and 1 on failure. */ int LoadAndConvertSound(char *filename, SDL_AudioSpec *spec, sound_p sound) { SDL_AudioCVT cvt; /* format conversion structure */ SDL_AudioSpec loaded; /* format of the loaded data */ Uint8 *new_buf; /* Load the WAV file in its original sample format. */ if (SDL_LoadWAV(filename, &loaded, &sound->samples, &sound->length) == NULL) { printf("Unable to load sound: %s\n", SDL_GetError()); return 1; } /* Build a conversion structure for converting the samples. This structure contains the data SDL needs to quickly convert between sample formats. */ if (SDL_BuildAudioCVT(&cvt, loaded.format, loaded.channels, loaded.freq, spec->format, spec->channels, spec->freq) < 0) { printf("Unable to convert sound: %s\n", SDL_GetError()); return 1; } /* Since converting PCM samples can result in more data (for instance, converting 8-bit mono to 16-bit stereo), we need to allocate a new buffer for the converted data. Fortunately SDL_BuildAudioCVT supplied the necessary information. */ cvt.len = sound->length; new_buf = (Uint8 *) malloc(cvt.len * cvt.len_mult); if (new_buf == NULL) { printf("Memory allocation failed.\n"); SDL_FreeWAV(sound->samples); return 1; } /* Copy the sound samples into the new buffer. */ memcpy(new_buf, sound->samples, sound->length); /* Perform the conversion on the new buffer. */ cvt.buf = new_buf; if (SDL_ConvertAudio(&cvt) < 0) { printf("Audio conversion error: %s\n", SDL_GetError()); free(new_buf); SDL_FreeWAV(sound->samples); return 1; } /* Swap the converted data for the original. */ SDL_FreeWAV(sound->samples); sound->samples = new_buf; sound->length = sound->length * cvt.len_mult; /* Success! */ printf("’%s’ was loaded and converted successfully.\n", filename); return 0; }
SoundHandle AudioSystem::LoadSound(const char * const filename) { SDL_AudioSpec spec; Uint32 len; Uint8 *wav; if (SDL_LoadWAV(filename, &spec, &wav, &len) == nullptr) { log::Error("Could not load sound ", filename, ": ", SDL_GetError()); return InvalidSound; } ALenum format = GetALFormat(spec); if (format == 0) { log::Error("Could not load sound ", filename, ": Unsupported data format"); SDL_FreeWAV(wav); return InvalidSound; } Sound *sound = m_sounds.Obtain(); alBufferData(sound->buffer, format, wav, len, spec.freq); AL_CHECK; SDL_FreeWAV(wav); return m_sounds.IndexOf(sound); }
int main(int argc, char *argv[]) { char name[32]; /* Load the SDL library */ if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) { fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); return(1); } if ( argv[1] == NULL ) { argv[1] = "sample.wav"; } /* Load the wave file into memory */ if ( SDL_LoadWAV(argv[1], &wave.spec, &wave.sound, &wave.soundlen) == NULL ) { fprintf(stderr, "Couldn't load %s: %s\n", argv[1], SDL_GetError()); quit(1); } wave.spec.callback = fillerup; #if HAVE_SIGNAL_H /* Set the signals */ #ifdef SIGHUP signal(SIGHUP, poked); #endif signal(SIGINT, poked); #ifdef SIGQUIT signal(SIGQUIT, poked); #endif signal(SIGTERM, poked); #endif /* HAVE_SIGNAL_H */ /* Initialize fillerup() variables */ if ( SDL_OpenAudio(&wave.spec, NULL) < 0 ) { fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError()); SDL_FreeWAV(wave.sound); quit(2); } SDL_PauseAudio(0); /* Let the audio run */ printf("Using audio driver: %s\n", SDL_AudioDriverName(name, 32)); while ( ! done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING) ) #ifdef __amigaos4__ { // __check_abort(); #endif SDL_Delay(1000); #ifdef __amigaos4__ } #endif /* Clean up on signal */ SDL_CloseAudio(); SDL_FreeWAV(wave.sound); SDL_Quit(); return(0); }
// This function loads a sound with SDL_LoadWAV and converts // it to the specified sample format. void Sound::loadAndConvertSound(char *filename, SDL_AudioSpec spec) { SDL_AudioCVT cvt; // audio format conversion structure SDL_AudioSpec loaded; // format of the loaded data Uint8 *new_buf; // Load the WAV file in its original sample format. if (SDL_LoadWAV(filename, &loaded, &samples, &length) == NULL) { std::cout << "Unable to load sound: " << SDL_GetError() << std::endl; // throw string("Unable to load sound: ") + string(SDL_GetError()); } // Build a conversion structure for converting the samples. // This structure contains the data SDL needs to quickly // convert between sample formats. if (SDL_BuildAudioCVT(&cvt, loaded.format, loaded.channels, loaded.freq, spec.format, spec.channels, spec.freq) < 0) { std::cout << "Unable to convert sound: " << SDL_GetError() << std::endl; // throw string("Unable to convert sound: ") + string(SDL_GetError()); } // Since converting PCM samples can result in more data // (for instance, converting 8-bit mono to 16-bit stereo), // we need to allocate a new buffer for the converted data. // Fortunately SDL_BuildAudioCVT supplied the necessary // information. cvt.len = length; new_buf = (Uint8 *) malloc(cvt.len * cvt.len_mult); if (new_buf == NULL) { SDL_FreeWAV(samples); std::cout << "Memory allocation failed." << std::endl; // throw string("Memory allocation failed."); } // Copy the sound samples into the new buffer. memcpy(new_buf, samples, length); // Perform the conversion on the new buffer. cvt.buf = new_buf; if (SDL_ConvertAudio(&cvt) < 0) { free(new_buf); SDL_FreeWAV(samples); std::cout << "Audio conversion error: " << SDL_GetError() << std::endl; // throw string("Audio conversion error: ") + string(SDL_GetError()); } // Swap the converted data for the original. SDL_FreeWAV(samples); samples = new_buf; length = length * cvt.len_mult; // Success! printf("'%s' was loaded and converted successfully.\n", filename); }
SDL_AudioCVT *SOUND_LoadWAV(const char *filename) { SDL_AudioCVT *wavecvt; SDL_AudioSpec wavespec, *loaded; unsigned char *buf; unsigned int len; if (!g_fAudioOpened) { return NULL; } wavecvt = (SDL_AudioCVT *)malloc(sizeof(SDL_AudioCVT)); if (wavecvt == NULL) { return NULL; } loaded = SDL_LoadWAV_RW(SDL_RWFromFile(filename, "rb"), 1, &wavespec, &buf, &len); if (loaded == NULL) { free(wavecvt); return NULL; } // Build the audio converter and create conversion buffers if (SDL_BuildAudioCVT(wavecvt, wavespec.format, wavespec.channels, wavespec.freq, audio_spec.format, audio_spec.channels, audio_spec.freq) < 0) { SDL_FreeWAV(buf); free(wavecvt); return NULL; } int samplesize = ((wavespec.format & 0xFF) / 8) * wavespec.channels; wavecvt->len = len & ~(samplesize - 1); wavecvt->buf = (unsigned char *)malloc(wavecvt->len * wavecvt->len_mult); if (wavecvt->buf == NULL) { SDL_FreeWAV(buf); free(wavecvt); return NULL; } memcpy(wavecvt->buf, buf, len); SDL_FreeWAV(buf); // Run the audio converter if (SDL_ConvertAudio(wavecvt) < 0) { free(wavecvt->buf); free(wavecvt); return NULL; } return wavecvt; }
// ------------------- LoadSound ------------------------- static ALuint LoadSound(const char* name) { SDL_AudioSpec wav_spec; Uint32 wav_length; Uint8 *wav_buffer; if (SDL_LoadWAV(name, &wav_spec, &wav_buffer, &wav_length) == NULL) { ERR("LoadSound(%s): SDL_LoadWAV failed: %s\n", name, SDL_GetError()); return 0; } ALenum format = 0; if (wav_spec.channels == 1) { switch(wav_spec.format) { case AUDIO_U8: format = AL_FORMAT_MONO8; break; case AUDIO_S16LSB: format = AL_FORMAT_MONO16; break; case AUDIO_F32LSB: format = AL_FORMAT_MONO_FLOAT32; break; } } else if (wav_spec.channels == 2) { switch(wav_spec.format) { case AUDIO_U8: format = AL_FORMAT_STEREO8; break; case AUDIO_S16LSB: format = AL_FORMAT_STEREO16; break; case AUDIO_F32LSB: format = AL_FORMAT_STEREO_FLOAT32; break; } } if (format == 0) { // TODO: if needed, more channels / other formats. ERR("LoadSound(%s): Unsupported format (TODO): 0x%X\n", name, wav_spec.format); SDL_FreeWAV(wav_buffer); return 0; } ALuint buffer; alGenBuffers(1, &buffer); alBufferData(buffer, format, wav_buffer, wav_length, wav_spec.freq); SDL_FreeWAV(wav_buffer); ALenum err = alGetError(); if(err != AL_NO_ERROR) { ERR("LoadSound(%s): alBufferData Error: %s\n", name, alGetString(err)); if(alIsBuffer(buffer)) alDeleteBuffers(1, &buffer); return 0; } return buffer; }
AUD_Sound* AUD_LoadWAV(const char* wav, int loop) { AUD_Sound* product = malloc(sizeof(AUD_Sound)); if (!product) { return NULL; } if (!SDL_LoadWAV(wav, &product->spec, &product->buf, &product->buflen)) { free(product); return NULL; } product->curlen = product->buflen; product->cur = product->buf; product->spec.callback = AUD_Callback; product->spec.userdata = product; product->loop = loop; product->volume = SDL_MIX_MAXVOLUME; product->dev = SDL_OpenAudioDevice(NULL, 0, &product->spec, NULL, 0); if (!product->dev) { SDL_FreeWAV(product->buf); free(product); return NULL; } return product; }
static SYS_SOUNDHANDLE SYS_LoadWAVFile(char* filename) { // use SDL to load a wave file. SDL_AudioSpec wav_spec; Uint32 wav_length; Uint8 *wav_buffer; char newfilename[512]; sprintf(newfilename,"%s%s",g_DataPath,filename); // Get PCM data from a WAV file if (SDL_LoadWAV(newfilename, &wav_spec, &wav_buffer, &wav_length) == 0) { fprintf(stderr, "Could not open wav %s: %s\n", filename, SDL_GetError()); return 0; } //DumpSDLAudioSpec(&wav_spec); SYS_Wave* wave = GetWaveFromFreePool(); wave->filename = filename; alGenBuffers(1, &wave->buffer); CheckForErrors(); // copy PCM data into buffer int format = SDLFormatToOpenALFormat(wav_spec.format, wav_spec.channels); alBufferData(wave->buffer, format, wav_buffer, wav_length, wav_spec.freq); CheckForErrors(); // free PCM data SDL_FreeWAV(wav_buffer); return (SYS_SOUNDHANDLE)wave; }
////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CSound::CSound(char *filename, int iSoundID) { m_iSoundID = iSoundID; Uint32 len; if (SDL_LoadWAV(filename, &m_spec, &m_data, &len) == NULL ) { fprintf(stderr, "Couldn't load %s: %s\n",filename, SDL_GetError()); m_len = CSoundTime(0); return; } m_len = CSoundTime(len); m_pos = CSoundTime(0); CEasySound *es = CEasySound::Instance(); SDL_AudioSpec obtained = es->GetObtained(); SDL_AudioCVT cvt; SDL_BuildAudioCVT(&cvt, m_spec.format, m_spec.channels, m_spec.freq, obtained.format, obtained.channels, obtained.freq); cvt.buf = (Uint8*)malloc(m_len.GetSDLTime() * cvt.len_mult); memcpy(cvt.buf, m_data, m_len.GetSDLTime()); cvt.len = m_len.GetSDLTime(); SDL_ConvertAudio(&cvt); SDL_FreeWAV(m_data); SDL_LockAudio(); m_data = cvt.buf; // the new length of sound after convert from 8bit to 16bit m_len = CSoundTime( cvt.len_cvt ); m_pos = CSoundTime( 0 ); SDL_UnlockAudio(); }
SDLSystem::~SDLSystem() { for (auto& s : _sounds) { SDL_FreeWAV(s.second.wav_buffer); } SDL_Quit(); }
static void LoadSound(int index, char *file) { SDL_AudioSpec wave; Uint8 *data; Uint32 dlen; SDL_AudioCVT cvt; /* Load the sound file and convert it to 16-bit stereo at 22kHz */ if (SDL_LoadWAV(file, &wave, &data, &dlen) == NULL) { fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError()); return; } SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq, AUDIO_S16, 2, 44100); cvt.buf = malloc(dlen*cvt.len_mult); memcpy(cvt.buf, data, dlen); cvt.len = dlen; SDL_ConvertAudio(&cvt); SDL_FreeWAV(data); if (sounds[index].data) { free(sounds[index].data); } SDL_LockAudio(); sounds[index].data = cvt.buf; sounds[index].dlen = cvt.len_cvt; sounds[index].dpos = cvt.len_cvt; SDL_UnlockAudio(); }
//Play sound straight from file void cSound::PlaySound(char *file) { if(gGameWorld.gMenu.m_sound == 0){return;} SDL_AudioSpec wave; Uint8 *data; Uint32 dlen; SDL_AudioCVT cvt; /* Load the sound file and convert it to 16-bit stereo at 22kHz */ if(SDL_LoadWAV(file, &wave, &data, &dlen) == NULL){return;} SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq, AUDIO_S16, 2, 22050); cvt.buf = (Uint8*)malloc(dlen*cvt.len_mult); memcpy(cvt.buf, data, dlen); cvt.len = dlen; SDL_ConvertAudio(&cvt); SDL_FreeWAV(data); /* Put the sound data in the slot (it starts playing immediately) */ if(snd.data != NULL){free(snd.data);} SDL_LockAudio(); snd.data = cvt.buf; snd.dlen = cvt.len_cvt; snd.dpos = 0; SDL_UnlockAudio(); }
void makeAudio(int i, const char* filename, unsigned char repeat, float volume){ Audio* a = audios + i; if (SDL_LoadWAV(filename, &(a->spec), &(a->sounddata), &(a->soundlength)) == NULL) { printf("Erreur lors du chargement du fichier son: %s\n", SDL_GetError()); return; } if (SDL_BuildAudioCVT(&(a->audioConverter), a->spec.format, a->spec.channels, a->spec.freq, audioSpec.currentConfig.format, audioSpec.currentConfig.channels, audioSpec.currentConfig.freq) < 0) { printf("Impossible de construire le convertisseur audio!\n"); return; } a->audioConverter.buf = malloc(a->soundlength * a->audioConverter.len_mult); a->audioConverter.len = a->soundlength; memcpy(a->audioConverter.buf, a->sounddata, a->soundlength); if (SDL_ConvertAudio(&(a->audioConverter)) != 0) { printf("Erreur lors de la conversion du fichier audio: %s\n", SDL_GetError()); return; } SDL_FreeWAV(a->sounddata); a->sounddata = malloc(a->audioConverter.len_cvt); memcpy(a->sounddata, a->audioConverter.buf, a->audioConverter.len_cvt); free(a->audioConverter.buf); a->soundlength = a->audioConverter.len_cvt; a->soundpos = 0; a->repeat = repeat; a->currentVolume = 0; a->maxVolume = volume; a->play = a->fadeIn = a->fadeOut = 0; }
int main(int argc, char **argv) { /* Enable standard application logging */ SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); /* Load the SDL library */ if (SDL_Init(SDL_INIT_AUDIO) < 0) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError()); return (1); } SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver()); devcount = SDL_GetNumAudioDevices(0); if (devcount < 1) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Don't see any specific audio devices!\n"); } else { if (argv[1] == NULL) { argv[1] = "sample.wav"; } /* Load the wave file into memory */ if (SDL_LoadWAV(argv[1], &spec, &sound, &soundlen) == NULL) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", argv[1], SDL_GetError()); } else { test_multi_audio(); SDL_FreeWAV(sound); } } SDL_Quit(); return 0; }
void SOUND_FreeWAV(SDL_AudioCVT *audio) { if (audio == NULL) { return; } SDL_FreeWAV(audio->buf); free(audio); }
void syssnd_free(sound_t *s) { if (!s) return; if (s->buf) SDL_FreeWAV(s->buf); s->buf = NULL; s->len = 0; }
void es_freeWAV(sdl_data *sd, int len, char *buff) { char *bp; void *ptr; bp = buff; POPGLPTR(ptr, bp); SDL_FreeWAV(ptr); }
void AUD_FreeWav(AUD_Sound* sound) { if (sound) { if (sound->buf) { SDL_FreeWAV(sound->buf); } SDL_CloseAudioDevice(sound->dev); free(sound); } }
void Jukebox::PlayZAttack() //Function to play the background theme song specifically. { const char *file = Z_ATTACK_SOUND; int index; SDL_AudioSpec wave; Uint8 *data; Uint32 dlen; SDL_AudioCVT cvt; //Look for an empty (or finished) sound slot for ( index=0; index<NUM_SOUNDS; ++index ) { if ( sounds[index].dpos == sounds[index].dlen ) { break; } } if ( index == NUM_SOUNDS ) { return; } // Load the sound file and convert it to 16-bit stereo at 22kHz if ( SDL_LoadWAV(file, &wave, &data, &dlen) == NULL ) { fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError()); return; } SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq, AUDIO_S16, 2, 22050); cvt.buf = (Uint8*)malloc(dlen*cvt.len_mult); memcpy(cvt.buf, data, dlen); cvt.len = dlen; SDL_ConvertAudio(&cvt); SDL_FreeWAV(data); // Put the sound data in the slot (it starts playing immediately) if (sounds[index].data) { free(sounds[index].data); } SDL_LockAudio(); sounds[index].data = cvt.buf; sounds[index].dlen = cvt.len_cvt; sounds[index].dpos = 0; SDL_UnlockAudio(); }
void loop() { for (int i = 0; i < devcount; i++) { if ((!cbd[i].dev) || (cbd[i].done)) { emscripten_cancel_main_loop(); SDL_PauseAudioDevice(cbd[0].dev, 1); SDL_CloseAudioDevice(cbd[0].dev); SDL_FreeWAV(sound); SDL_Quit(); } } }
void Source_Sample::Unload() { if (data) { if (issdlwav) { SDL_FreeWAV(data); } else { delete[] data; } data = 0; } issdlwav = false; length = 0; }
void loop() { if(cbd[0].done) { #ifdef __EMSCRIPTEN__ emscripten_cancel_main_loop(); #endif SDL_PauseAudioDevice(cbd[0].dev, 1); SDL_CloseAudioDevice(cbd[0].dev); SDL_FreeWAV(sound); SDL_Quit(); } }
static void wav_close (codec_data_t *ptr) { wav_codec_t *wav; wav = (wav_codec_t *)ptr; if (wav->m_wav_buffer != NULL) { SDL_FreeWAV(wav->m_wav_buffer); wav->m_wav_buffer = NULL; } if (wav->m_sdl_config != NULL) { free(wav->m_sdl_config); wav->m_sdl_config = NULL; } free(ptr); }
void ShutdownAudio() { /* SDL_CloseAudio(); */ SDL_PauseAudio(1); SDL_LockAudio(); int i; for (i = 0; i < SND_NUM; ++ i) { SDL_FreeWAV(wav_sounds[i].samples); } /* At this point the output is paused and we know for certain that the callback is not active, so we can safely unlock the audio system. */ SDL_UnlockAudio(); }
int playAudio(char *file, int loop){ int index; SDL_AudioSpec wave; Uint8 *data; Uint32 dlen; SDL_AudioCVT cvt; /* Look for an empty (or finished) sound slot */ for ( index=0; index<SOINU_KOPURUA; ++index ) { if ( soinuak[index].dpos == soinuak[index].dlen && !soinuak[index].isLoop) { break; } } if ( index == SOINU_KOPURUA ) return; /* Load the sound file and convert it to 16-bit stereo at 22kHz */ if ( SDL_LoadWAV(file, &wave, &data, &dlen) == NULL ) { fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError()); return; } SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq, AUDIO_S16, 2, 22050); cvt.buf =(Uint8*) ( malloc(dlen*cvt.len_mult)); memcpy(cvt.buf, data, dlen); cvt.len = dlen; SDL_ConvertAudio(&cvt); SDL_FreeWAV(data); /* Put the sound data in the slot (it starts playing immediately) */ if ( soinuak[index].data ) { free(soinuak[index].data); } SDL_LockAudio(); soinuak[index].id = index; soinuak[index].data = cvt.buf; soinuak[index].dlen = cvt.len_cvt; soinuak[index].dpos = 0; if (loop){ soinuak[index].isLoop = 1; }else{ soinuak[index].isLoop = 0; } SDL_UnlockAudio(); return index; }
void SDLSoundClose() { if (audioid) SDL_PauseAudioDevice(audioid, 1); for (auto &it : sound_files) { Sound &snd = it.second; if (snd.sfxr) free(snd.buf); else SDL_FreeWAV(snd.buf); } sound_files.clear(); if (audioid) SDL_CloseAudioDevice(audioid); SDL_QuitSubSystem(SDL_INIT_AUDIO); audioid = 0; }
ALvoid *soundLoadMemoryFromWAV (const char *filename, ALenum *format, ALsizei *size, ALfloat *frequency) { SDL_AudioSpec wavSpec; Uint8 *wavBuffer; Uint32 wavLength; ALvoid *buf; if (!SDL_LoadWAV(filename, &wavSpec, &wavBuffer, &wavLength)) return NULL; buf = soundConvertSDLToOpenAL(&wavSpec, wavBuffer, wavLength, format, size, frequency); SDL_FreeWAV(wavBuffer); return buf; }
void cargar_sonido ( char *fichero_wav, int identificador ){ /* Variables locales */ FILE *fichero; SDL_RWops *file; char filename[LON_BUFF]; SDL_AudioSpec wav_spec; Uint32 size; Uint8 *data; /* Generamos buffer, le asignamos un identificador y comprobamos errores */ alGenBuffers( 1, &buffer[identificador] ); if ( !alIsBuffer ( buffer[identificador] )){ log_msj ( "[KO] error al crear los buffers\n"); } /* Establecemos donde estara situado el directorio para los sonidos */ strcpy(filename, "sonidos"); strcat(filename, "/"); strcat(filename, fichero_wav); log_msj("[efectos.c] Cargando sonido %s\n", filename); /* Cargamos ficheros Wave */ fichero = abre_fichero(filename, "rb"); file = SDL_RWFromFP(fichero,0); if( SDL_LoadWAV_RW(file,1, &wav_spec,&data,&size)== NULL ){ log_msj("[KO]Error al cargar %s\n",filename); return; } alBufferData ( buffer[identificador], AL_FORMAT_STEREO16, data, size, wav_spec.freq ); /* Almacenamos en buffer */ SDL_FreeWAV(data);/*Liberamos*/ fclose(fichero); /* Generamos las fuentes de sonido y comprobamos errores */ alGenSources( 1, &source[identificador] ); if ( !alIsSource ( source[identificador])){ log_msj ("[KO] No se pueden crear las fuentes de sonido\n"); } /* Pasamos el archivo wav del buffer a la fuente */ alSourcei ( source[identificador], AL_BUFFER, buffer[identificador]); }
void loadsound(const char *s,int num) { SDL_AudioSpec spec; Uint8 *buffer; Uint32 len; SDL_AudioCVT cvt; SDL_AudioSpec *sound=SDL_LoadWAV(s,&spec,&buffer,&len); if(!sound) error("can't open sound file %s: %s\n",s,SDL_GetError()); /* convert sound to 16-bit stereo, 44khz */ SDL_BuildAudioCVT(&cvt,spec.format,spec.channels,spec.freq,AUDIO_S16,2,FREQ); cvt.buf=(Uint8 *)malloc(len*cvt.len_mult); memcpy(cvt.buf,buffer,len); cvt.len=len; SDL_ConvertAudio(&cvt); sounds[num].buffer=buffer; sounds[num].len=len; sounds[num].loop=0; SDL_FreeWAV(buffer); }
void PlaySound(char *file) { int index; SDL_AudioSpec wave; Uint8 *data; Uint32 dlen; SDL_AudioCVT cvt; /* Look for an empty (or finished) sound slot */ for ( index=0; index<NUM_SOUNDS; ++index ) { if ( sounds[index].dpos == sounds[index].dlen ) { break; } } if ( index == NUM_SOUNDS ) return; /* Load the sound file and convert it to 16-bit stereo at 22kHz */ if ( SDL_LoadWAV(file, &wave, &data, &dlen) == NULL ) { fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError()); return; } printf("channels=%d, freq=%d\n", wave.channels, wave.freq); SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq, AUDIO_S16, 2, 22050); cvt.buf = malloc(dlen*cvt.len_mult); memcpy(cvt.buf, data, dlen); cvt.len = dlen; SDL_ConvertAudio(&cvt); SDL_FreeWAV(data); /* Put the sound data in the slot (it starts playing immediately) */ if ( sounds[index].data ) { free(sounds[index].data); } SDL_LockAudio(); sounds[index].data = cvt.buf; sounds[index].dlen = cvt.len_cvt; sounds[index].dpos = 0; SDL_UnlockAudio(); }