/* This function is called by Sound Manager when it has exhausted one of the buffers, so we'll zero it to silence and fill it with audio if we're not paused. */ static pascal void sndDoubleBackProc (SndChannelPtr chan, SndDoubleBufferPtr newbuf) { SDL_AudioDevice *audio = (SDL_AudioDevice *)newbuf->dbUserInfo[0]; /* If audio is quitting, don't do anything */ if ( ! audio->enabled ) { return; } memset (newbuf->dbSoundData, 0, audio->spec.size); newbuf->dbNumFrames = audio->spec.samples; if ( ! audio->paused ) { if ( audio->convert.needed ) { audio->spec.callback(audio->spec.userdata, (Uint8 *)audio->convert.buf,audio->convert.len); SDL_ConvertAudio(&audio->convert); #if 0 if ( audio->convert.len_cvt != audio->spec.size ) { /* Uh oh... probably crashes here */; } #endif memcpy(newbuf->dbSoundData, audio->convert.buf, audio->convert.len_cvt); } else { audio->spec.callback(audio->spec.userdata, (Uint8 *)newbuf->dbSoundData, audio->spec.size); } } newbuf->dbFlags |= dbBufferReady; }
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(); }
////////////////////////////////////////////////////////////////////// // 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(); }
/* FIXME: Make use of latency if needed */ static void nacl_audio_callback(void* samples, uint32_t buffer_size, PP_TimeDelta latency, void* data) { SDL_AudioDevice* _this = (SDL_AudioDevice*) data; SDL_LockMutex(private->mutex); if (_this->enabled && !_this->paused) { if (_this->convert.needed) { SDL_LockMutex(_this->mixer_lock); (*_this->spec.callback) (_this->spec.userdata, (Uint8 *) _this->convert.buf, _this->convert.len); SDL_UnlockMutex(_this->mixer_lock); SDL_ConvertAudio(&_this->convert); SDL_memcpy(samples, _this->convert.buf, _this->convert.len_cvt); } else { SDL_LockMutex(_this->mixer_lock); (*_this->spec.callback) (_this->spec.userdata, (Uint8 *) samples, buffer_size); SDL_UnlockMutex(_this->mixer_lock); } } else { SDL_memset(samples, 0, buffer_size); } return; }
//+----------------------------------------------------------------------------+ //|void ConvertBufferData(AudioBuffer* &buffer, SDL_AudioCVT* &audioCVT) //|Converts the input buffer into the audio format used by the engine //\----------------------------------------------------------------------------+ bool SoundSDL::ConvertBufferData(AudioBuffer* &buffer, SDL_AudioCVT* &audioCVT) { ///Start conversion and delete the loaded buffer const Uint32 convertedBufferSize = buffer->chunkSize * audioCVT->len_mult; audioCVT->buf = tNewArray(Uint8, convertedBufferSize); audioCVT->len = buffer->chunkSize; SDL_memcpy(audioCVT->buf, buffer->chunk, buffer->chunkSize); ///Delete old buffer data since its not useful anymore tDeleteArray(buffer->chunk); ///Convert the audio stored in the CVT into engine format if(SDL_ConvertAudio(audioCVT) < 0) { return false; } ///Copy the converted data back into the original, resized buffer buffer->chunk = tNewArray(Uint8, convertedBufferSize); SDL_memcpy(buffer->chunk, audioCVT->buf, convertedBufferSize); ///Delete converted data since its stored in the original buffer again tDeleteArray(audioCVT->buf); return true; }
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; }
/* * Play-time conversion. Performs output conversion only once per sound effect used. * Once the sound sample has been converted, it is cached in SoundChunks[] */ void mixdigi_convert_sound(int i) { SDL_AudioCVT cvt; Uint8 *data = GameSounds[i].data; Uint32 dlen = GameSounds[i].length; int freq = GameSounds[i].freq; //int bits = GameSounds[i].bits; if (SoundChunks[i].abuf) return; //proceed only if not converted yet if (data) { if (MIX_DIGI_DEBUG) con_printf(CON_DEBUG,"converting %d (%d)\n", i, dlen); SDL_BuildAudioCVT(&cvt, AUDIO_U8, 1, freq, MIX_OUTPUT_FORMAT, MIX_OUTPUT_CHANNELS, digi_sample_rate); cvt.buf = malloc(dlen * cvt.len_mult); cvt.len = dlen; memcpy(cvt.buf, data, dlen); if (SDL_ConvertAudio(&cvt)) con_printf(CON_DEBUG,"conversion of %d failed\n", i); SoundChunks[i].abuf = cvt.buf; SoundChunks[i].alen = dlen * cvt.len_mult; SoundChunks[i].allocated = 1; SoundChunks[i].volume = 128; // Max volume = 128 } }
static void mix_buffer(SDL_AudioDevice * audio, UInt8 * buffer) { if (!audio->paused) { #ifdef __MACOSX__ SDL_mutexP(audio->mixer_lock); #endif if (audio->convert.needed) { audio->spec.callback(audio->spec.userdata, (Uint8 *) audio->convert.buf, audio->convert.len); SDL_ConvertAudio(&audio->convert); if (audio->convert.len_cvt != audio->spec.size) { /* Uh oh... probably crashes here */ ; } SDL_memcpy(buffer, audio->convert.buf, audio->convert.len_cvt); } else { audio->spec.callback(audio->spec.userdata, buffer, audio->spec.size); } #ifdef __MACOSX__ SDL_mutexV(audio->mixer_lock); #endif } DecrementAtomic((SInt32 *) & need_to_mix); }
/* Play some of a stream previously started with GME_play() */ int GME_playAudio(struct MUSIC_GME *music, Uint8 *stream, int len) { if(music==NULL) return 1; if(music->game_emu==NULL) return 1; if(music->playing==-1) return 1; if( len<0 ) return 0; int srgArraySize = len/music->cvt.len_ratio; short buf[srgArraySize]; int srcLen = (int)((double)(len/2)/music->cvt.len_ratio); char *err = (char*)gme_play( music->game_emu, srcLen, buf ); if( err != NULL) { Mix_SetError("GAME-EMU: %s", err); return 0; } int dest_len = srcLen*2; if( music->cvt.needed ) { music->cvt.len = dest_len; music->cvt.buf = (Uint8*)buf; SDL_ConvertAudio(&music->cvt); dest_len = music->cvt.len_cvt; } if ( music->volume == MIX_MAX_VOLUME ) { SDL_memcpy(stream, (Uint8*)buf, dest_len); } else { SDL_MixAudioFormat(stream, (Uint8*)buf, mixer.format, dest_len, music->volume); } return len-dest_len; }
bool Audio::CVT::Convert(void) { if(0 == SDL_ConvertAudio(this)) return true; std::cerr << "Audio::CVT::Convert: " << SDL_GetError() << std::endl; return false; }
/* The Dingoo callback for handling the audio buffer */ static void FillSound(void *stream, uint32_t len) { SDL_AudioDevice *audio = (SDL_AudioDevice *)sdl_dingoo_audiodevice; /* Silence the buffer, since it's ours */ SDL_memset(stream, audio->spec.silence, len); /* Only do something if audio is enabled */ if ( ! audio->enabled ) return; if ( ! audio->paused ) { if ( audio->convert.needed ) { SDL_LockAudio(); (*audio->spec.callback)(audio->spec.userdata, (Uint8 *)audio->convert.buf,audio->convert.len); SDL_UnlockAudio(); SDL_ConvertAudio(&audio->convert); SDL_memcpy(stream,audio->convert.buf,audio->convert.len_cvt); } else { SDL_LockAudio(); (*audio->spec.callback)(audio->spec.userdata, (Uint8 *)stream, len); SDL_UnlockAudio(); } } return; }
/* 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 */ } }
/* 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; }
// 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(); }
void AudioQueue_push (AudioQueue* self) { SDL_ConvertAudio(&self->cvt); // the node is ready to be used SDL_SemPost(self->full_node); // move to the next node and wrap if necessary if (++self->tail - self->nodes >= self->n) self->tail = self->nodes; }
void system_sound_update(int nframes) { int i; int consumed; /* SDL_LockAudio(); */ /* number of unread frames */ i = FRAME_DIFF(sound_frame_write, sound_frame_read); /* number of frames read since last call */ consumed = FRAME_DIFF(sound_frame_read, sound_frame_read_old); sound_frame_read_old = sound_frame_read; /* SDL_UnlockAudio(); */ for (; i<nframes; i++) { if (mute || paused) memset(sound_buffer+sound_frame_write, silence_value, bpf); else { dac_update(dac_data, dac_bpf); /* convert to standard format */ acvt.buf = dac_data; acvt.len = dac_bpf; if (SDL_ConvertAudio(&acvt) == -1) { fprintf(stderr, "DAC data conversion failed: %s\n", SDL_GetError()); return; } /* get sound data */ sound_update((_u16 *)(sound_buffer+sound_frame_write), bpf); /* mix both streams into one */ SDL_MixAudio(sound_buffer+sound_frame_write, dac_data, bpf, SDL_MIX_MAXVOLUME); } sound_frame_write = FRAME_INC(sound_frame_write); if (sound_frame_write == sound_frame_read) { fprintf(stderr, "your machine is much too slow.\n"); /* XXX: handle this */ exit(1); } SDL_SemPost(rsem); } if (nframes > 1) { for (i=0; i<consumed; i++) SDL_SemWait(wsem); } else SDL_SemWait(wsem); }
void SDL_MoSync_AudioBufferFill() { int startTime; if ( sDevice->convert.needed ) { (*sDevice->spec.callback)(sDevice->spec.userdata, (Uint8 *)sDevice->convert.buf,sDevice->convert.len); SDL_ConvertAudio(&sDevice->convert); SDL_memcpy(sDevice->hidden->info.buffer,sDevice->convert.buf,sDevice->convert.len_cvt); } else { (*sDevice->spec.callback)(sDevice->spec.userdata, (Uint8 *)sDevice->hidden->info.buffer, sDevice->hidden->info.bufferSize); } maAudioBufferReady(); }
static void ExpandSoundData(byte *data, int samplerate, int length, Mix_Chunk *destination) { SDL_AudioCVT convertor; if (samplerate <= mixer_freq && ConvertibleRatio(samplerate, mixer_freq) && SDL_BuildAudioCVT(&convertor, AUDIO_U8, 1, samplerate, mixer_format, mixer_channels, mixer_freq)) { convertor.buf = destination->abuf; convertor.len = length; memcpy(convertor.buf, data, length); SDL_ConvertAudio(&convertor); } else { Sint16 *expanded = (Sint16 *) destination->abuf; int expanded_length; int expand_ratio; int i; // Generic expansion if conversion does not work: // // SDL's audio conversion only works for rate conversions that are // powers of 2; if the two formats are not in a direct power of 2 // ratio, do this naive conversion instead. // number of samples in the converted sound expanded_length = ((uint64_t) length * mixer_freq) / samplerate; expand_ratio = (length << 8) / expanded_length; for (i=0; i<expanded_length; ++i) { Sint16 sample; int src; src = (i * expand_ratio) >> 8; sample = data[src] | (data[src] << 8); sample -= 32768; // expand 8->16 bits, mono->stereo expanded[i * 2] = expanded[i * 2 + 1] = sample; } } }
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; }
void audio_play(){ audio_len = nBytesRead; cvt.buf = (Uint8 *)malloc(audio_len * cvt.len_mult); memcpy(cvt.buf, audio_chunk, audio_len); SDL_ConvertAudio(&cvt); memset(audio_chunk, 0, audio_len); audio_pos = cvt.buf; SDL_PauseAudio(0); while ( audio_len > 0 ) { SDL_Delay(100); /* Sleep 1/10 second */ } audio_len = AUDIO_BUFFER_SIZE; nBytesRead = 0; SDL_PauseAudio(1); }
bool Mixer::Convert(SDL_AudioCVT& cvt, const uint8* data, unsigned long length, uint8** dataout) { if (length == 0 || cvt.len_mult == 0) { return false; } cvt.len = length; cvt.buf = (Uint8*)new uint8[cvt.len * cvt.len_mult]; memcpy(cvt.buf, data, length); if (SDL_ConvertAudio(&cvt) < 0) { delete[] cvt.buf; return false; } *dataout = cvt.buf; return true; }
static void loadSampleJob( void* data ) { if( data == NULL ) { llog( LOG_ERROR, "NULL data passed into loadSampleJob!" ); return; } ThreadedSoundLoadData* loadData = (ThreadedSoundLoadData*)data; // read the entire file into memory and decode it int channels; int rate; short* buffer; int numSamples = stb_vorbis_decode_filename( loadData->fileName, &channels, &rate, &buffer ); if( numSamples < 0 ) { llog( LOG_ERROR, "Error decoding sound sample %s", loadData->fileName ); goto error; } // convert it if( SDL_BuildAudioCVT( &( loadData->loadConverter ), AUDIO_S16, (Uint8)channels, rate, WORKING_FORMAT, loadData->desiredChannels, WORKING_RATE ) < 0 ) { llog( LOG_ERROR, "Unable to create converter for sound." ); goto error; } loadData->loadConverter.len = numSamples * channels * sizeof( buffer[0] ); size_t totLen = loadData->loadConverter.len * loadData->loadConverter.len_mult; if( loadData->loadConverter.len_mult > 1 ) { buffer = mem_Resize( buffer, loadData->loadConverter.len * loadData->loadConverter.len_mult ); // need to make sure there's enough room if( buffer == NULL ) { llog( LOG_ERROR, "Unable to allocate more memory for converting." ); goto error; } } loadData->loadConverter.buf = (Uint8*)buffer; SDL_ConvertAudio( &( loadData->loadConverter ) ); jq_AddMainThreadJob( bindSampleJob, (void*)loadData ); return; error: cleanUpThreadedSoundLoadData( loadData ); }
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; }
/* Read some Ogg stream data and convert it for output */ static void OGG_getsome(OGG_music *music) { int section; int len; char data[4096]; SDL_AudioCVT *cvt; #ifdef OGG_USE_TREMOR len = vorbis.ov_read(&music->vf, data, sizeof(data), §ion); #else len = vorbis.ov_read(&music->vf, data, sizeof(data), 0, 2, 1, §ion); #endif if ( len <= 0 ) { if ( len == 0 ) { music->playing = 0; } return; } cvt = &music->cvt; if ( section != music->section ) { vorbis_info *vi; vi = vorbis.ov_info(&music->vf, -1); SDL_BuildAudioCVT(cvt, AUDIO_S16, vi->channels, vi->rate, mixer.format,mixer.channels,mixer.freq); if ( cvt->buf ) { SDL_free(cvt->buf); } cvt->buf = (Uint8 *)SDL_malloc(sizeof(data)*cvt->len_mult); music->section = section; } if ( cvt->buf ) { memcpy(cvt->buf, data, len); if ( cvt->needed ) { cvt->len = len; SDL_ConvertAudio(cvt); } else { cvt->len_cvt = len; } music->len_available = music->cvt.len_cvt; music->snd_available = music->cvt.buf; } else { SDL_SetError("Out of memory"); music->playing = 0; } }
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(); }
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); }