本文整理汇总了C++中SDL_ConvertAudio函数的典型用法代码示例。如果您正苦于以下问题:C++ SDL_ConvertAudio函数的具体用法?C++ SDL_ConvertAudio怎么用?C++ SDL_ConvertAudio使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SDL_ConvertAudio函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: sndDoubleBackProc
/* 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;
}
示例2: LoadSound
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();
}
示例3: SDL_BuildAudioCVT
//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();
}
示例4: fprintf
//////////////////////////////////////////////////////////////////////
// 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();
}
示例5: nacl_audio_callback
/* 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;
}
示例6: tNewArray
//+----------------------------------------------------------------------------+
//|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;
}
示例7: makeAudio
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;
}
示例8: mixdigi_convert_sound
/*
* 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
}
}
示例9: mix_buffer
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);
}
示例10: GME_playAudio
/* 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;
}
示例11:
bool Audio::CVT::Convert(void)
{
if(0 == SDL_ConvertAudio(this)) return true;
std::cerr << "Audio::CVT::Convert: " << SDL_GetError() << std::endl;
return false;
}
示例12: FillSound
/* 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;
}
示例13: file
/*
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 */
}
}
示例14: LoadAndConvertSound
/* 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;
}
示例15: SDL_GetError
// 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);
}