本文整理汇总了C++中SDL_FreeWAV函数的典型用法代码示例。如果您正苦于以下问题:C++ SDL_FreeWAV函数的具体用法?C++ SDL_FreeWAV怎么用?C++ SDL_FreeWAV使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SDL_FreeWAV函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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;
}
示例2: SDL_GetError
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);
}
示例3: main
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);
}
示例4: 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);
}
示例5: SDL_LoadWAV_RW
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;
}
示例6: LoadSound
// ------------------- 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;
}
示例7: AUD_LoadWAV
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;
}
示例8: SYS_LoadWAVFile
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;
}
示例9: 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();
}
示例10: SDL_FreeWAV
SDLSystem::~SDLSystem() {
for (auto& s : _sounds) {
SDL_FreeWAV(s.second.wav_buffer);
}
SDL_Quit();
}
示例11: 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();
}
示例12: 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();
}
示例13: 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;
}
示例14: main
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;
}
示例15: SOUND_FreeWAV
void SOUND_FreeWAV(SDL_AudioCVT *audio)
{
if (audio == NULL) {
return;
}
SDL_FreeWAV(audio->buf);
free(audio);
}