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; }
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; }
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; }