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; }
//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(); }
sound* wav_load_file(char* filename) { sound* s = malloc(sizeof(sound)); SDL_AudioSpec spec; if( SDL_LoadWAV(filename, &spec, (Uint8**)&s->data, (Uint32*)&s->length) == NULL) { error("Unable to load sound file %s", filename); } if (spec.freq != 44100) { warning("Sound file %s is %f, not 44.1 kHz. May sound incorrect", filename, (spec.freq)/1000.0f); } if (spec.channels != 1) { warning("Sound file %s has %i channels. Currently only mono sounds supported.", filename, spec.channels); } if ((spec.format != AUDIO_S16LSB) && (spec.format != AUDIO_S16MSB)) { error("Unsupported sound format for file %s, id %i.", filename, spec.format); } if (spec.format != AUDIO_S16SYS) { flip_endian(s->data, s->length); } return s; }
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) { /* 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; }
////////////////////////////////////////////////////////////////////// // 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(); }
/* loads a wav file (stored in 'file'), converts it to the mixer's output format, and stores the resulting buffer and length in the sound structure */ void loadSound(const char *file, struct sound *s) { SDL_AudioSpec spec; /* the audio format of the .wav file */ SDL_AudioCVT cvt; /* used to convert .wav to output format when formats differ */ int result; if (SDL_LoadWAV(file, &spec, &s->buffer, &s->length) == NULL) { fatalError("could not load .wav"); } /* build the audio converter */ result = SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq, mixer.outputSpec.format, mixer.outputSpec.channels, mixer.outputSpec.freq); if (result == -1) { fatalError("could not build audio CVT"); } else if (result != 0) { /* this happens when the .wav format differs from the output format. we convert the .wav buffer here */ cvt.buf = (Uint8 *) SDL_malloc(s->length * cvt.len_mult); /* allocate conversion buffer */ cvt.len = s->length; /* set conversion buffer length */ SDL_memcpy(cvt.buf, s->buffer, s->length); /* copy sound to conversion buffer */ if (SDL_ConvertAudio(&cvt) == -1) { /* convert the sound */ fatalError("could not convert .wav"); } SDL_free(s->buffer); /* free the original (unconverted) buffer */ s->buffer = cvt.buf; /* point sound buffer to converted buffer */ s->length = cvt.len_cvt; /* set sound buffer's new length */ } }
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; }
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; }
/* 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; }
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(); }
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); }
Audio::Audio(std::string code, std::string path): code(code) { SDL_AudioSpec a; if(SDL_LoadWAV(path.c_str(), &a, &buffer, &length) == NULL) { std::ostringstream out; out << "Could not load wav file: '" << path << "': " << SDL_GetError(); throw std::runtime_error(out.str()); } }
// 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); }
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(); }
/*========================================================================= // Name: LoadSound() // Desc: Loads a wave file into a Sound structure //=======================================================================*/ void LoadSound(char *soundfile, pSound sound) { SDL_AudioSpec wav_spec; if (!SDL_LoadWAV(soundfile, &wav_spec, &sound->samples, &sound->length)) { fprintf(stderr, "Couldn't load wav file [%s] -> %s\n", soundfile, SDL_GetError()); exit(1); } }
// ------------------- 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; }
int SDLSystem::load_sound(std::string const& filename) { Sound new_sound; if( SDL_LoadWAV((_resource_path + filename).c_str(), &new_sound.wav_spec, &new_sound.wav_buffer, &new_sound.wav_length) == 0 ){ return -1; } new_sound.wav_spec.callback = my_audio_callback; new_sound.wav_spec.userdata = 0; int sound_index = _next_sound++; _sounds.insert(std::make_pair(sound_index, new_sound)); return sound_index; }
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; }
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; }
ZWaveAudioAsset::ZWaveAudioAsset(std::string path) : _impl(new _ZWaveAudioAssetImpl) { uint8_t *buffer = nullptr; uint32_t length = 0; SDL_AudioSpec spec = {0}; SDL_AudioSpec *result_spec = SDL_LoadWAV(path.c_str(), &spec, &buffer, &length); if (result_spec) { _impl->buffer = buffer; _impl->length = length; _impl->spec = spec; } else { ZException exception(ZFILE_EXCEPTION_CODE); exception.extra_info = ZUtil::format("Could not find file %s (%s)", path.c_str(), SDL_GetError()); throw exception; } }
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); }
/* Adds the given sample, assigning it to the given sample ID slot. */ void audio_sample_add(const char *filename, int sample_id) { #ifdef USE_AUDIO SDL_AudioSpec wav_audiospec; Uint8 *buf; Uint32 length; /* If the given sample ID is already occupied, don't do anything. */ if (!audio_sample_is_empty(sample_id)) return; if (!SDL_LoadWAV(filename, &wav_audiospec, &buf, &length)) { err("Couldn't load WAV file.\n", 1); } g_samples[sample_id].buffer = buf; g_samples[sample_id].length = length; #endif }
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(); }
void load_snd(char *file[]){ int i; SDL_AudioSpec wave; Uint8 *data; Uint32 dlen; for (i=0;i<NUM_SOUNDS;++i){ if (!(SDL_LoadWAV(file[i],&wave,&data,&dlen))){ return; } SDL_BuildAudioCVT(&cvt[i],wave.format,wave.channels,wave.freq,AUDIO_S16,2,22050); cvt[i].buf=malloc(dlen*cvt[i].len_mult); memcpy(cvt[i].buf,data,dlen); cvt[i].len=dlen; SDL_ConvertAudio(&cvt[i]); s[i].data=cvt[i].buf; s[i].dlen=cvt[i].len_cvt; SDL_FreeWAV(data); } }
void Sound::init() { if(mSampleData != NULL) deinit(); if(mPath.empty()) return; //load wav file via SDL SDL_AudioSpec wave; Uint8 * data = NULL; Uint32 dlen = 0; if (SDL_LoadWAV(mPath.c_str(), &wave, &data, &dlen) == NULL) { LOG(LogError) << "Error loading sound \"" << mPath << "\"!\n" << " " << SDL_GetError(); return; } //build conversion buffer SDL_AudioCVT cvt; SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq, AUDIO_S16, 2, 44100); //copy data to conversion buffer cvt.len = dlen; cvt.buf = new Uint8[cvt.len * cvt.len_mult]; memcpy(cvt.buf, data, dlen); //convert buffer to stereo, 16bit, 44.1kHz if (SDL_ConvertAudio(&cvt) < 0) { LOG(LogError) << "Error converting sound \"" << mPath << "\" to 44.1kHz, 16bit, stereo format!\n" << " " << SDL_GetError(); delete[] cvt.buf; } else { //worked. set up member data SDL_LockAudio(); mSampleData = cvt.buf; mSampleLength = cvt.len_cvt; mSamplePos = 0; mSampleFormat.channels = 2; mSampleFormat.freq = 44100; mSampleFormat.format = AUDIO_S16; SDL_UnlockAudio(); } //free wav data now SDL_FreeWAV(data); }
//! [static] factory Buffer * loadWAV(const std::string & filename) { Util::Reference<Buffer> b(Buffer::create()); if(b.isNull()) return nullptr; SDL_AudioSpec wav_spec; Uint32 wav_length; Uint8 *wav_buffer; if ( SDL_LoadWAV(filename.c_str(), &wav_spec, &wav_buffer, &wav_length) == nullptr ) { WARN("Could not open : "+ filename+ std::string( SDL_GetError()) ); return nullptr; } unsigned int format=0; unsigned int freq = static_cast<unsigned int>(wav_spec.freq); if (wav_spec.format == AUDIO_U8 ) { if (wav_spec.channels==1) format=AL_FORMAT_MONO8; else format=AL_FORMAT_STEREO8; } else if (wav_spec.format == AUDIO_S16SYS ) { if (wav_spec.channels==1) format=AL_FORMAT_MONO16; else format=AL_FORMAT_STEREO16; } if (format==0) { WARN("Uninplemented WAV-format. Please add conversion routine ;)."); return nullptr; } else b->setData(format,wav_buffer,wav_length,freq); // b->setData(AL_FORMAT_MONO8,noise,size,freq); SDL_FreeWAV(wav_buffer); return checkErrorStatus(__FILE__, __LINE__) ? b.detachAndDecrease() : nullptr; }
void sound_load (ALuint id, char *name) { char filename[BUFSIZ]; Uint32 len = 0, i; Uint8 *buf = NULL; SDL_AudioSpec spec, *spec2; memset (&spec, 0, sizeof (spec)); spec.format = AUDIO_S16MSB; spec.channels = 1; snprintf (filename, BUFSIZ, "%s/%s", DATADIR, name); if ((spec2 = SDL_LoadWAV (filename, &spec, &buf, &len)) == NULL) { printf ("Unable to load sound %s\n", filename); return; } for (i = 0; i < len / 2; ++i) buf[i] = SDL_SwapLE16 (buf[i]); alBufferData (id, AL_FORMAT_MONO16, (Uint16 *)buf, len, spec2->freq); free (buf); return; }
void GameAudio::loadSound(std::string file, SDL_AudioSpec audioDeviceSpec) { SDL_AudioSpec originalAudioSpec; uint8_t* buffer; uint32_t length; std::stringstream fullFilePath; fullFilePath << platform->dataPath << "/data/audio/" << file << ".wav"; if(SDL_LoadWAV(fullFilePath.str().c_str(), &originalAudioSpec, &buffer, &length) == NULL) gameSystem->log(GameSystem::LOG_FATAL, std::string("Unable to open audio file " + fullFilePath.str() + "; Error: " + SDL_GetError())); SDL_AudioCVT conversionInfo; if( SDL_BuildAudioCVT( &conversionInfo, originalAudioSpec.format, originalAudioSpec.channels, originalAudioSpec.freq, audioDeviceSpec.format, audioDeviceSpec.channels, audioDeviceSpec.freq ) < 0 ) gameSystem->log(GameSystem::LOG_FATAL, std::string("Unable to convert audio file " + fullFilePath.str())); conversionInfo.buf = (uint8_t*) malloc(length * conversionInfo.len_mult); memcpy(conversionInfo.buf, buffer, length); conversionInfo.len = length; if(SDL_ConvertAudio(&conversionInfo) < 0) gameSystem->log(GameSystem::LOG_FATAL, std::string("Unable to convert audio file " + fullFilePath.str())); SDL_FreeWAV(buffer); GameSound sound; sound.buffer = conversionInfo.buf; sound.length = conversionInfo.len_cvt; sounds[file] = sound; }
int main(int argc, char **argv) { SDL_AudioSpec spec_voulue; spec_voulue.freq = 44100; spec_voulue.format = AUDIO_S16; /* Tableau d'entiers 16-bit */ spec_voulue.channels = 2; /* 1 = mono, 2 = stereo */ spec_voulue.samples = 1024; /* Une puissance de 2 sympa (pas plus de 4096) */ spec_voulue.callback = remplir_audio; spec_voulue.userdata = NULL; if(SDL_OpenAudio(&spec_voulue, NULL) < 0) { fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError()); return EXIT_FAILURE; } if(argc <= 1) { fprintf(stderr, "Usage: %s <music.wav>\n", argv[0]); return EXIT_FAILURE; } SDL_AudioSpec wav_spec = spec_voulue; if(!SDL_LoadWAV(argv[1], &wav_spec, &son_samples, &son_taille)) { fprintf(stderr, "Could not open `%s': %s\n", argv[1], SDL_GetError()); return EXIT_FAILURE; } son_curseur = son_samples; SDL_PauseAudio(0); /* On attend 1/10 de seconde de manière répétée * tant que le son n'a pas fini de jouer. */ while(son_curseur < son_samples+son_taille) SDL_Delay(100); SDL_FreeWAV(son_samples); SDL_CloseAudio(); return EXIT_SUCCESS; }