void MusicPlayer::play(const char *filename) { #ifdef USE_OGG stb_vorbis_close(v); stb_vorbis_alloc t; t.alloc_buffer=this->buf; t.alloc_buffer_length_in_bytes=STB_BUFSIZE; // stb_vorbis has char* pointer even though it is not really writing to buffer v = stb_vorbis_open_filename((char *)filename, &error, &t); #endif #ifdef USE_MP3 mpg123_close(mh); error = mpg123_open(mh, filename); if (!error) { error = mpg123_getformat(mh, &rate, &channels, &encoding); rate = AUDIO_FREQUENCY; channels = 2; mpg123_format_none(mh); mpg123_format(mh, rate, channels, encoding); } #endif }
void play_file() { DIR *dir; struct dirent *ent; if ((dir = opendir (cur_dir.c_str())) != NULL) { u32 cur = 1; while ((ent = readdir (dir)) != NULL) { if (strncmp(ent->d_name, ".", 1) == 0) continue; if (cur == cursor_pos) { if (v) { stb_vorbis_close(v); } std::string name = cur_dir + "/" + ent->d_name; play_file_from_filename(name); closedir (dir); return; } cur++; } closedir (dir); } else { /* could not open directory */ perror (""); } }
bool Sound::LoadOggVorbis(Deserializer& source) { unsigned dataSize = source.GetSize(); SharedArrayPtr<signed char> data(new signed char[dataSize]); source.Read(data.Get(), dataSize); // Check for validity of data int error; stb_vorbis* vorbis = stb_vorbis_open_memory((unsigned char*)data.Get(), dataSize, &error, 0); if (!vorbis) { LOGERROR("Could not read Ogg Vorbis data from " + source.GetName()); return false; } // Store length, frequency and stereo flag stb_vorbis_info info = stb_vorbis_get_info(vorbis); compressedLength_ = stb_vorbis_stream_length_in_seconds(vorbis); frequency_ = info.sample_rate; stereo_ = info.channels > 1; stb_vorbis_close(vorbis); data_ = data; dataSize_ = dataSize; sixteenBit_ = true; compressed_ = true; SetMemoryUse(dataSize); return true; }
AudioStream::~AudioStream() { AudioStream* delThis = this; stb_vorbis_close(stream); streams.Remove(delThis); alDeleteBuffers(2, buffers); }
Sound::Sound(const std::string& fileName) : _channel_count(0), _sample_count(0), _sample_rate(0) { stb_vorbis* file = stb_vorbis_open_filename(const_cast<char*>(fileName.c_str()), 0, 0); if (file) { stb_vorbis_info info = stb_vorbis_get_info(file); _channel_count = info.channels; _sample_rate = info.sample_rate; int samples = stb_vorbis_stream_length_in_samples(file) * _channel_count; _sample_count = samples; _samples.resize(samples); stb_vorbis_get_samples_short_interleaved(file, _channel_count, &_samples.front(), samples); stb_vorbis_close(file); } else { throw std::runtime_error("Unable to open audio file"); } }
static void punp_sound_load_stbv(Sound *sound, stb_vorbis *stream) { stb_vorbis_info info = stb_vorbis_get_info(stream); sound->volume = PUNP_SOUND_DEFAULT_SOUND_VOLUME; sound->rate = info.sample_rate; sound->samples_count = stb_vorbis_stream_length_in_samples(stream); sound->samples = (i16 *)bank_push(CORE->storage, PUNP_SOUND_SAMPLES_TO_BYTES(sound->samples_count)); { static i16 buffer[1024]; i16 *it = sound->samples; int samples_read_per_channel; for (; ;) { int samples_read_per_channel = stb_vorbis_get_samples_short_interleaved(stream, PUNP_SOUND_CHANNELS, buffer, 1024); if (samples_read_per_channel == 0) { break; } // 2 channels, 16 bits per sample. memcpy(it, buffer, PUNP_SOUND_SAMPLES_TO_BYTES(samples_read_per_channel)); it += samples_read_per_channel * PUNP_SOUND_CHANNELS; } } stb_vorbis_close(stream); }
void Sound::FreeDecoder(void* decoder) { if (!decoder) return; stb_vorbis* vorbis = static_cast<stb_vorbis*>(decoder); stb_vorbis_close(vorbis); }
virtual ~OGGStream() { /*#if XIPH_OGG if (valid) ov_clear(&ogg); #else*/ if (valid) stb_vorbis_close(ogg); //cout << "OGG ENDED" << endl; //#endif }
void LoadTestChannel(ShadertoyTestResource *channel_data, ShadertoyState *state, ShadertoyInputs *inputs, ShadertoyOutputs *outputs, ShadertoyPass pass, int channel_id) { ImageFile *image = &image_files[channel_id][pass]; memset(image->filepath, 0, sizeof(image->filepath)); switch (channel_data->type) { case SHADERTOY_RESOURCE_TEXTURE: { GetFilePath(image->filepath, channel_data->filename[0]); LoadTexture(state, image->filepath, pass, channel_id); break; } case SHADERTOY_RESOURCE_CUBE_MAP: { char *cubemap_paths[6]; for (int j = 0; j < 6; ++j) { cubemap_paths[j] = new char[1024]; GetFilePath(cubemap_paths[j], channel_data->filename[j]); if (j == 0) { strcpy(image->filepath, cubemap_paths[j]); } } LoadTexture(state, cubemap_paths, pass, channel_id); for (int j = 0; j < 6; ++j) { delete[] cubemap_paths[j]; } break; } case SHADERTOY_RESOURCE_MUSIC: { AudioFile *file = &audio_files[channel_id]; memset(file->filepath, 0, sizeof(file->filepath)); GetFilePath(file->filepath, channel_data->filename[0]); int ogg_error; stb_vorbis *ogg_data = stb_vorbis_open_filename(file->filepath, &ogg_error, NULL); file->channels = ogg_data->channels; file->samples_count = stb_vorbis_stream_length_in_samples(ogg_data) * ogg_data->channels; file->samples = (float*)malloc(sizeof(float) * file->samples_count); stb_vorbis_get_samples_float_interleaved(ogg_data, ogg_data->channels, file->samples, file->samples_count); audio_data[channel_id] = { &inputs->audio_played_samples[channel_id], &file->samples, (int)ogg_data->sample_rate, ogg_data->channels, file->samples_count / ogg_data->channels }; ShadertoyLoadAudio(state, audio_data + channel_id, SHADERTOY_IMAGE_PASS, channel_id); outputs->music_data_param[channel_id] = init_audio_output(ogg_data->channels, ogg_data->sample_rate, 32, file->samples_count * sizeof(float)); stb_vorbis_close(ogg_data); break; } case SHADERTOY_RESOURCE_MICROPHONE: { // 512 * sizeof(float) == FFT sampling data, so should be fine to use inputs->micro_data_param = init_audio_input(2, 44100, 32, 2048 * sizeof(float)); break; } case SHADERTOY_RESOURCE_KEYBOARD: case SHADERTOY_RESOURCE_NONE: break; } }
void test_push_mode_forever(FILE *g, char *filename) { int p,q, len, error, used; uint8 *data = stb_file(filename, &len); stb_vorbis *v; if (!data) stb_fatal("Couldn't open {%s}", filename); p = 0; q = 1; retry: v = stb_vorbis_open_pushdata(data, q, &used, &error, NULL); if (v == NULL) { if (error == VORBIS_need_more_data) { q += 1; goto retry; } printf("Error %d\n", error); exit(1); } p += used; show_info(v); for(;;) { int k=0; int n; float *left, *right; float **outputs; int num_c; q = 32; retry3: if (q > len-p) q = len-p; used = stb_vorbis_decode_frame_pushdata(v, data+p, q, &num_c, &outputs, &n); if (used == 0) { if (p+q == len) { // seek randomly when at end... this makes sense when listening to it, but dumb when writing to file p = stb_rand(); if (p < 0) p = -p; p %= (len - 8000); stb_vorbis_flush_pushdata(v); q = 128; goto retry3; } if (q < 128) q = 128; q *= 2; goto retry3; } p += used; if (n == 0) continue; left = outputs[0]; right = num_c > 1 ? outputs[1] : outputs[0]; write_floats(g, n, left, right); } stb_vorbis_close(v); }
WavStreamInstance::~WavStreamInstance() { if (mOgg) { stb_vorbis_close(mOgg); } if (mFile) { fclose(mFile); } }
WavStreamInstance::~WavStreamInstance() { if (mOgg) { stb_vorbis_close(mOgg); } if (mFile != mParent->mStreamFile) { delete mFile; } }
OggVorbisSoundStream::~OggVorbisSoundStream() { // Close decoder if (decoder_) { stb_vorbis* vorbis = static_cast<stb_vorbis*>(decoder_); stb_vorbis_close(vorbis); decoder_ = 0; } }
void snd_UnloadStream( int streamID ) { assert( ( streamID >= 0 ) && ( streamID < MAX_STREAMING_SOUNDS ) ); SDL_LockAudioDevice( devID ); { streamingSounds[streamID].playing = false; stb_vorbis_close( streamingSounds[streamID].access ); streamingSounds[streamID].access = NULL; SDL_FreeAudioStream( streamingSounds[streamID].sdlStream ); streamingSounds[streamID].sdlStream = NULL; } SDL_UnlockAudioDevice( devID ); }
sBool sOGGDecoder::Init(sInt songnr) { if(dec) { stb_vorbis_close(dec); dec = 0; } int error = 0; dec = stb_vorbis_open_memory(Stream,StreamSize,&error,0); return dec!=0; }
MusicPlayer::~MusicPlayer() { #ifdef USE_OGG stb_vorbis_close(v); #endif #ifdef USE_MP3 mpg123_close(mh); mpg123_delete(mh); mpg123_exit(); #endif }
bool cSoundFileOgg::IsFileSupported( const std::string& Filename, bool Read ) { if ( Read ) { // Open the vorbis stream stb_vorbis* Stream = stb_vorbis_open_filename( const_cast<char*>( Filename.c_str() ), NULL, NULL ); if ( NULL != Stream ) { stb_vorbis_close( Stream ); return true; } else return false; } else // No support for writing ogg files yet... return false; }
bool cSoundFileOgg::IsFileSupported( const char* Data, std::size_t SizeInBytes ) { // Open the vorbis stream unsigned char* Buffer = reinterpret_cast<unsigned char*>( const_cast<char*>( Data ) ); int Length = static_cast<int>( SizeInBytes ); stb_vorbis * Stream = stb_vorbis_open_memory( Buffer, Length, NULL, NULL ); if ( NULL != Stream ) { stb_vorbis_close(Stream); return true; } else return false; }
void test_get_frame_short_interleaved(FILE *g, char *filename) { short sbuffer[8000]; int n, error; stb_vorbis *v = stb_vorbis_open_filename(filename, &error, NULL); if (!v) stb_fatal("Couldn't open {%s}", filename); show_info(v); while (0 != (n=stb_vorbis_get_frame_short_interleaved(v, 2, sbuffer, 8000))) { fwrite(sbuffer, 2, n*2, g); } stb_vorbis_close(v); }
// Load OGG file into Wave structure // NOTE: Using stb_vorbis library static Wave LoadOGG(char *fileName) { Wave wave; stb_vorbis *oggFile = stb_vorbis_open_filename(fileName, NULL, NULL); if (oggFile == NULL) { TraceLog(WARNING, "[%s] OGG file could not be opened", fileName); wave.data = NULL; } else { stb_vorbis_info info = stb_vorbis_get_info(oggFile); wave.sampleRate = info.sample_rate; wave.bitsPerSample = 16; wave.channels = info.channels; TraceLog(DEBUG, "[%s] Ogg sample rate: %i", fileName, info.sample_rate); TraceLog(DEBUG, "[%s] Ogg channels: %i", fileName, info.channels); int totalSamplesLength = (stb_vorbis_stream_length_in_samples(oggFile) * info.channels); wave.dataSize = totalSamplesLength*sizeof(short); // Size must be in bytes TraceLog(DEBUG, "[%s] Samples length: %i", fileName, totalSamplesLength); float totalSeconds = stb_vorbis_stream_length_in_seconds(oggFile); TraceLog(DEBUG, "[%s] Total seconds: %f", fileName, totalSeconds); if (totalSeconds > 10) TraceLog(WARNING, "[%s] Ogg audio lenght is larger than 10 seconds (%f), that's a big file in memory, consider music streaming", fileName, totalSeconds); int totalSamples = totalSeconds*info.sample_rate*info.channels; TraceLog(DEBUG, "[%s] Total samples calculated: %i", fileName, totalSamples); wave.data = malloc(sizeof(short)*totalSamplesLength); int samplesObtained = stb_vorbis_get_samples_short_interleaved(oggFile, info.channels, wave.data, totalSamplesLength); TraceLog(DEBUG, "[%s] Samples obtained: %i", fileName, samplesObtained); TraceLog(INFO, "[%s] OGG file loaded successfully (SampleRate: %i, BitRate: %i, Channels: %i)", fileName, wave.sampleRate, wave.bitsPerSample, wave.channels); stb_vorbis_close(oggFile); } return wave; }
result Wav::loadogg(File *aReader) { aReader->seek(0); MemoryFile memoryFile; memoryFile.openFileToMem(aReader); int e = 0; stb_vorbis *vorbis = 0; vorbis = stb_vorbis_open_memory(memoryFile.getMemPtr(), memoryFile.length(), &e, 0); if (0 == vorbis) { return FILE_LOAD_FAILED; } stb_vorbis_info info = stb_vorbis_get_info(vorbis); mBaseSamplerate = (float)info.sample_rate; int samples = stb_vorbis_stream_length_in_samples(vorbis); int readchannels = 1; if (info.channels > 1) { readchannels = 2; mChannels = 2; } mData = new float[samples * readchannels]; mSampleCount = samples; samples = 0; while(1) { float **outputs; int n = stb_vorbis_get_frame_float(vorbis, NULL, &outputs); if (n == 0) { break; } if (readchannels == 1) { memcpy(mData + samples, outputs[0],sizeof(float) * n); } else { memcpy(mData + samples, outputs[0],sizeof(float) * n); memcpy(mData + samples + mSampleCount, outputs[1],sizeof(float) * n); } samples += n; } stb_vorbis_close(vorbis); return 0; }
// in push mode, you can load your data any way you want, then // feed it a little bit at a time. this is the preferred way to // handle reading from a packed file or some custom stream format; // instead of putting callbacks inside stb_vorbis, you just keep // a little buffer (it needs to be big enough for one packet of // audio, except at the beginning where you need to buffer up the // entire header). // // for this test, I just load all the data and just lie to stb_vorbis // and claim I only have a little of it void test_decode_frame_pushdata(FILE *g, char *filename) { int p,q, len, error, used; stb_vorbis *v; uint8 *data = stb_file(filename, &len); if (!data) stb_fatal("Couldn't open {%s}", filename); p = 0; q = 1; retry: v = stb_vorbis_open_pushdata(data, q, &used, &error, NULL); if (v == NULL) { if (error == VORBIS_need_more_data) { q += 1; goto retry; } fprintf(stderr, "Error %d\n", error); exit(1); } p += used; show_info(v); for(;;) { int k=0; int n; float *left, *right; uint32 next_t=0; float **outputs; int num_c; q = 32; retry3: if (q > len-p) q = len-p; used = stb_vorbis_decode_frame_pushdata(v, data+p, q, &num_c, &outputs, &n); if (used == 0) { if (p+q == len) break; // no more data, stop if (q < 128) q = 128; q *= 2; goto retry3; } p += used; if (n == 0) continue; // seek/error recovery left = outputs[0]; right = num_c > 1 ? outputs[1] : outputs[0]; write_floats(g, n, left, right); } stb_vorbis_close(v); }
int stb_vorbis_decode_memory(unsigned char *input_data, int input_len, int *channels, short **output) { int data_len, offset, total, limit, error; short *data; stb_vorbis *v = stb_vorbis_open_memory(input_data, input_len, &error, NULL); if (v == NULL) return -1; limit = v->channels * 4096; *channels = v->channels; offset = data_len = 0; total = limit; data = malloc(total * sizeof(*data)); if (data == NULL) { stb_vorbis_close(v); return -2; } for (;;) { int n = stb_vorbis_get_frame_short_interleaved(v, v->channels, data+offset, total-offset); if (n == 0) break; data_len += n; offset += n * v->channels; if (offset + limit > total) { short *data2; total *= 2; data2 = realloc(data, total * sizeof(*data)); if (data2 == NULL) { free(data); stb_vorbis_close(v); return -2; } data = data2; } } *output = data; return data_len; }
void MusicPlayer::play(const char *mem, int length ) { #ifdef USE_OGG stb_vorbis_close(v); stb_vorbis_alloc t; t.alloc_buffer=this->buf; t.alloc_buffer_length_in_bytes=STB_BUFSIZE; // stb_vorbis has char* pointer even though it is not really writing to buffer v = stb_vorbis_open_memory( (unsigned char*)mem, length, &error, &t); #endif #ifdef USE_MP3 #warning MusicPlayer::play(const char *mem, int length ) not implemented #endif }
// Stop music playing (close stream) void StopMusicStream() { if (musicEnabled) { alSourceStop(currentMusic.source); EmptyMusicStream(); // Empty music buffers alDeleteSources(1, ¤tMusic.source); alDeleteBuffers(2, currentMusic.buffers); stb_vorbis_close(currentMusic.stream); } musicEnabled = false; }
void test_get_samples_short_interleaved(FILE *g, char *filename) { int error; stb_vorbis *v = stb_vorbis_open_filename(filename, &error, NULL); if (!v) stb_fatal("Couldn't open {%s}", filename); show_info(v); for(;;) { int16 sbuffer[333]; int n; n = stb_vorbis_get_samples_short_interleaved(v, 2, sbuffer, 333); if (n == 0) break; fwrite(sbuffer, 2, n*2, g); } stb_vorbis_close(v); }
result Wav::loadogg(MemoryFile *aReader) { int e = 0; stb_vorbis *vorbis = 0; vorbis = stb_vorbis_open_memory(aReader->getMemPtr(), aReader->length(), &e, 0); if (0 == vorbis) { return FILE_LOAD_FAILED; } stb_vorbis_info info = stb_vorbis_get_info(vorbis); mBaseSamplerate = (float)info.sample_rate; int samples = stb_vorbis_stream_length_in_samples(vorbis); if (info.channels > MAX_CHANNELS) { mChannels = MAX_CHANNELS; } else { mChannels = info.channels; } mData = new float[samples * mChannels]; mSampleCount = samples; samples = 0; while(1) { float **outputs; int n = stb_vorbis_get_frame_float(vorbis, NULL, &outputs); if (n == 0) { break; } unsigned int ch; for (ch = 0; ch < mChannels; ch++) memcpy(mData + samples + mSampleCount * ch, outputs[ch], sizeof(float) * n); samples += n; } stb_vorbis_close(vorbis); return 0; }
void PCMAudioManager::stopStreamingSound(const char *assetName) { for (S32 i = 0; i < streamCount; i++) { PCMStream *stream = &pcmStreams[i]; if (stream->isPlaying && stream->isStreaming && strcmp(assetName, stream->assetName) == 0) { stream->isPlaying = FALSE; stream->bufferPosition = 0; stream->overallPosition = 0; stream->loopsRemaining = 0; if (stream->audioHandle) { stb_vorbis_close((stb_vorbis*)stream->audioHandle); stream->audioHandle = NULL; } // we previously allocated the name in playStreamingSound delete [] stream->assetName; stream->assetName = NULL; } } }
void WavStream::loadogg(FILE * fp) { fseek(fp,0,SEEK_SET); int e; stb_vorbis *v = stb_vorbis_open_file(fp, 0, &e, NULL); if (!v) return; stb_vorbis_info info = stb_vorbis_get_info(v); if (info.channels > 1) { mChannels = 2; } mBaseSamplerate = (float)info.sample_rate; int samples = stb_vorbis_stream_length_in_samples(v); stb_vorbis_close(v); mOgg = 1; mSampleCount = samples; }
void MusicOGG::stop() { if (!m_isPlaying) return; m_isPlaying = false; m_paused = false; if (oAudioEngine) oAudioEngine->removeInstance(OThis); m_thread.join(); stb_vorbis_close(m_pStream); m_pStream = nullptr; for (auto pBuffer : m_buffers) delete pBuffer; m_buffers.clear(); m_bufferCount = 0; }