void SourceSample::Load(char *filename) { #define BUFSIZE 1024 * 1024 SDL_RWops *rwops; rwops = SDL_RWFromFile(filename, "rb"); Sound_Sample *sample = Sound_NewSample(rwops, NULL, _system->GetAudioInfo(), _buffersize ); if(sample == NULL) { fprintf(stderr, "[error] failed loading sample from '%s': %s\n", filename, Sound_GetError()); return; } Sound_DecodeAll(sample); _buffersize = sample->buffer_size; _buffer = new Uint8[_buffersize]; memcpy(_buffer, sample->buffer, _buffersize); Sound_FreeSample(sample); // fprintf(stderr, "done decoding sample '%s'\n", filename); _position = 0; }
static void start(sstate *data) { // data->sample = Sound_NewSampleFromFile("rain.mtm", NULL, 65536); data->rw = SDL_RWFromConstMem(song, song_size); data->sample = Sound_NewSample(data->rw, ".mtm", NULL, 65536); if (data->sample == NULL) { fprintf(stderr, "Failed to init sound: %s\n", Sound_GetError()); return; } data->devformat.freq = data->sample->actual.rate; data->devformat.format = data->sample->actual.format; data->devformat.channels = data->sample->actual.channels; data->devformat.samples = 4096; /* I just picked a largish number here. */ data->devformat.callback = audio_callback; data->devformat.userdata = data; if (SDL_OpenAudio(&data->devformat, NULL) < 0) { fprintf(stderr, "Couldn't open audio device: %s.\n", SDL_GetError()); Sound_FreeSample(data->sample); return; } SDL_PauseAudio(0); data->done_flag = 0; }
bool tryRead(SDL_RWops &ops, const char *ext) { Sound_Sample *sample = Sound_NewSample(&ops, ext, 0, STREAM_BUF_SIZE); if (!sample) { SDL_RWclose(&ops); return false; } /* Do all of the decoding in the handler so we don't have * to keep the source ops around */ uint32_t decBytes = Sound_DecodeAll(sample); uint8_t sampleSize = formatSampleSize(sample->actual.format); uint32_t sampleCount = decBytes / sampleSize; buffer = new SoundBuffer; buffer->bytes = sampleSize * sampleCount; ALenum alFormat = chooseALFormat(sampleSize, sample->actual.channels); AL::Buffer::uploadData(buffer->alBuffer, alFormat, sample->buffer, buffer->bytes, sample->actual.rate); Sound_FreeSample(sample); return true; }
void SourceMusic::CleanUp(void) { _read = 0; _decoded = 0; if(_sample != NULL) { Sound_FreeSample(_sample); _sample = NULL; } }
void Sound_temporary_release_music(void) { int i; if (sound_enabled) { playing_music=false; for(i=0;i<3;i++) { if (music_loaded[i]) Sound_FreeSample(music_sound[i]); } /* if */ } /* if */ } /* Sound_release_music */
static void end(sstate *data) { SDL_PauseAudio(1); // SDL_Delay(2 * 1000 * data->devformat.samples / data->devformat.freq); if (data->sample->flags & SOUND_SAMPLEFLAG_ERROR) fprintf(stderr, "Error decoding file: %s\n", Sound_GetError()); Sound_FreeSample(data->sample); SDL_CloseAudio(); }
int Sound_Quit(void) { ErrMsg *err; ErrMsg *nexterr = NULL; size_t i; BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0); while (((volatile Sound_Sample *) sample_list) != NULL) Sound_FreeSample(sample_list); initialized = 0; SDL_DestroyMutex(samplelist_mutex); samplelist_mutex = NULL; sample_list = NULL; for (i = 0; decoders[i].funcs != NULL; i++) { if (decoders[i].available) { decoders[i].funcs->quit(); decoders[i].available = 0; } /* if */ } /* for */ if (available_decoders != NULL) free((void *) available_decoders); available_decoders = NULL; /* clean up error state for each thread... */ SDL_LockMutex(errorlist_mutex); for (err = error_msgs; err != NULL; err = nexterr) { nexterr = err->next; free(err); } /* for */ error_msgs = NULL; SDL_UnlockMutex(errorlist_mutex); SDL_DestroyMutex(errorlist_mutex); errorlist_mutex = NULL; return(1); } /* Sound_Quit */
int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "usage: %s <filename>\n", argv[0]); return 1; } if (SDL_Init(SDL_INIT_AUDIO) != 0) { fprintf(stderr, "Error: %s\n", SDL_GetError()); return 1; } if (Sound_Init() == 0) { fprintf(stderr, "Error: %s\n", Sound_GetError()); return 1; } SDL_RWops* rw = SDL_RWFromFile(argv[1], "r"); if (rw == NULL) { fprintf(stderr, "Error: %s\n", SDL_GetError()); return 1; } Sound_AudioInfo wantedFormat; wantedFormat.channels = 2; wantedFormat.rate = 44100; wantedFormat.format = AUDIO_S16LSB; Sound_Sample* sample = Sound_NewSample(rw, 0, &wantedFormat, 8192); if (sample == 0) { fprintf(stderr, "Error: %s\n", Sound_GetError()); return 1; } Sound_DecodeAll(sample); printf("Format: %s\n", sample->decoder->description); printf("Decoded %d bytes of data.\n", sample->buffer_size); Sound_FreeSample(sample); return 0; }
static void cleanup_channel(schanid_t chan) { if (chan->sdl_rwops) { if (!chan->decode) SDL_FreeRW(chan->sdl_rwops); else Sound_FreeSample(chan->decode); chan->sdl_rwops = 0; chan->decode = 0; chan->buffered = 0; } if (chan->sdl_memory) { free(chan->sdl_memory); chan->sdl_memory = 0; } switch (chan->status) { case CHANNEL_SOUND: if (chan->sample) Mix_FreeChunk(chan->sample); if (chan->sdl_channel >= 0) { Mix_GroupChannel(chan->sdl_channel, FREE); sound_channels[chan->sdl_channel] = 0; } break; case CHANNEL_MUSIC: if (chan->music) { Mix_FreeMusic(chan->music); music_channel = 0; } break; } chan->status = CHANNEL_IDLE; chan->sdl_channel = -1; chan->music = 0; }
/* ** Audio stream callback. ** ** Called by the SDL background thread each time the audio buffer is ready ** to receive more data. The function decodes PCM samples from the sound ** stream and copies them to the buffer for playback. When an end-of-stream ** is reached, closes the audio stream and exits cleanly. */ static void stream_callback (void *userdata, Uint8 *buffer, int length) { Sound_Sample *stream = (Sound_Sample *) userdata; Uint32 bytes_decoded; /* Decode a chunk of PCM samples from the sound stream. */ Sound_SetBufferSize (stream, length); bytes_decoded = Sound_Decode (stream); if (bytes_decoded == 0) { /* End-of-stream reached; exit cleanly. */ Sound_FreeSample (stream); Sound_Quit (); SDL_Quit (); exit (0); } /* Copy the decoded samples to the audio buffer. */ memcpy (buffer, stream->buffer, length); }
SourceMusic::~SourceMusic() { // fprintf(stderr, "nebu_SourceMusic destructor called\n"); #ifndef macintosh SDL_SemWait(_sem); #else SDL_LockAudio(); #endif free(_buffer); if(_sample) { Sound_FreeSample( _sample ); _sample = NULL; } if(_filename) free(_filename); #ifndef macintosh SDL_SemPost(_sem); #else SDL_UnlockAudio(); #endif }
void closeAudio_platform(AudioInstance* instance) { Sound_FreeSample(instance->mAudio.mSample); instance->mAudio.mSample = NULL; }
~SDLSoundSource() { /* This also closes 'srcOps' */ Sound_FreeSample(sample); }
static void playOneSoundFile(const char *fname) { PlaysoundAudioCallbackData data; memset(&data, '\0', sizeof (PlaysoundAudioCallbackData)); data.sample = Sound_NewSampleFromFile(fname, NULL, 65536); if (data.sample == NULL) { fprintf(stderr, "Couldn't load '%s': %s.\n", fname, Sound_GetError()); return; } /* if */ /* * Open device in format of the the sound to be played. * We open and close the device for each sound file, so that SDL * handles the data conversion to hardware format; this is the * easy way out, but isn't practical for most apps. Usually you'll * want to pick one format for all the data or one format for the * audio device and convert the data when needed. This is a more * complex issue than I can describe in a source code comment, though. */ data.devformat.freq = data.sample->actual.rate; data.devformat.format = data.sample->actual.format; data.devformat.channels = data.sample->actual.channels; data.devformat.samples = 4096; /* I just picked a largish number here. */ data.devformat.callback = audio_callback; data.devformat.userdata = &data; if (SDL_OpenAudio(&data.devformat, NULL) < 0) { fprintf(stderr, "Couldn't open audio device: %s.\n", SDL_GetError()); Sound_FreeSample(data.sample); return; } /* if */ printf("Now playing [%s]...\n", fname); SDL_PauseAudio(0); /* SDL audio device is "paused" right after opening. */ global_done_flag = 0; /* the audio callback will flip this flag. */ while (!global_done_flag) SDL_Delay(10); /* just wait for the audio callback to finish. */ /* at this point, we've played the entire audio file. */ SDL_PauseAudio(1); /* so stop the device. */ /* * Sleep two buffers' worth of audio before closing, in order * to allow the playback to finish. This isn't always enough; * perhaps SDL needs a way to explicitly wait for device drain? * Most apps don't have this issue, since they aren't explicitly * closing the device as soon as a sound file is done playback. * As an alternative for this app, you could also change the callback * to write silence for a call or two before flipping global_done_flag. */ SDL_Delay(2 * 1000 * data.devformat.samples / data.devformat.freq); /* if there was an error, tell the user. */ if (data.sample->flags & SOUND_SAMPLEFLAG_ERROR) fprintf(stderr, "Error decoding file: %s\n", Sound_GetError()); Sound_FreeSample(data.sample); /* clean up SDL_Sound resources... */ SDL_CloseAudio(); /* will reopen with next file's format. */ } /* playOneSoundFile */