static inline void LoadNextFrameIfNeeded(VorbisPlayer *self) { if(self->framepos>=self->framelength) { self->framepos=0; self->framelength=stb_vorbis_get_frame_float(self->vorbis,NULL,&self->currentframe); if(!self->framelength) { stb_vorbis_seek_start(self->vorbis); self->framelength=stb_vorbis_get_frame_float(self->vorbis,NULL,&self->currentframe); } } }
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; }
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 test_get_frame_float(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(;;) { int n; float *left, *right; float **outputs; int num_c; n = stb_vorbis_get_frame_float(v, &num_c, &outputs); if (n == 0) break; left = outputs[0]; right = outputs[num_c > 1]; write_floats(g, n, left, right); } stb_vorbis_close(v); }
void LoadSoundStream(SoundStream* stream, const char * file_path) { FILE *file = Global::platform()->OpenFile(file_path, "rb"); int error; stb_vorbis *v = stb_vorbis_open_file(file, false, &error, nullptr); if (v == nullptr) return; stb_vorbis_info info = stb_vorbis_get_info(v); stream->mChannels = info.channels; stream->mSampleRate = info.sample_rate; for (;;) { float** frame_data; int n = stb_vorbis_get_frame_float(v, &(info.channels), &frame_data); if (n == 0) break; for (int idx = 0; idx < n; ++idx) { for (int channel_idx = 0; channel_idx < info.channels; ++channel_idx) { stream->mAudio.push_back(frame_data[channel_idx][idx]); } } } stb_vorbis_close(v); }
int Wav::loadogg(stb_vorbis *aVorbis) { stb_vorbis_info info = stb_vorbis_get_info(aVorbis); mBaseSamplerate = (float)info.sample_rate; int samples = stb_vorbis_stream_length_in_samples(aVorbis); 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(aVorbis, 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(aVorbis); return 0; }
void WavStreamInstance::getAudio(float *aBuffer, unsigned int aSamples) { unsigned int channels = mChannels; if (mFile == NULL) return; if (mOgg) { unsigned int offset = 0; if (mOggFrameOffset < mOggFrameSize) { int b = getOggData(mOggOutputs, aBuffer, aSamples, aSamples, mOggFrameSize, mOggFrameOffset, channels); mOffset += b; offset += b; mOggFrameOffset += b; } while (offset < aSamples) { mOggFrameSize = stb_vorbis_get_frame_float(mOgg, NULL, &mOggOutputs); mOggFrameOffset = 0; int b; b = getOggData(mOggOutputs, aBuffer + offset, aSamples - offset, aSamples, mOggFrameSize, mOggFrameOffset, channels); mOffset += b; offset += b; mOggFrameOffset += b; if (mOffset >= mParent->mSampleCount) { if (mFlags & AudioSourceInstance::LOOPING) { stb_vorbis_seek_start(mOgg); mOffset = aSamples - offset; mLoopCount++; } else { unsigned int i; for (i = 0; i < channels; i++) memset(aBuffer + offset + i * aSamples, 0, sizeof(float) * (aSamples - offset)); mOffset += aSamples - offset; offset = aSamples; } } } } else { unsigned int copysize = aSamples; if (copysize + mOffset > mParent->mSampleCount) { copysize = mParent->mSampleCount - mOffset; } getWavData(mFile, aBuffer, copysize, aSamples, channels, mParent->mChannels, mParent->mBits); if (copysize != aSamples) { if (mFlags & AudioSourceInstance::LOOPING) { mFile->seek(mParent->mDataOffset); getWavData(mFile, aBuffer + copysize, aSamples - copysize, aSamples, channels, mParent->mChannels, mParent->mBits); mOffset = aSamples - copysize; mLoopCount++; } else { unsigned int i; for (i = 0; i < channels; i++) memset(aBuffer + copysize + i * aSamples, 0, sizeof(float) * (aSamples - copysize)); mOffset += aSamples; } } else { mOffset += aSamples; } } }