WavStreamInstance::WavStreamInstance(WavStream *aParent) { mParent = aParent; mOffset = 0; mOgg = 0; mFile = 0; if (aParent->mFilename == NULL) return; mFile = fopen(aParent->mFilename, "rb"); if (mFile) { if (mParent->mOgg) { int e; mOgg = stb_vorbis_open_file(mFile, 0, &e, NULL); if (!mOgg) { fclose(mFile); mFile = 0; } mOggFrameSize = 0; mOggFrameOffset = 0; mOggOutputs = 0; } else { fseek(mFile, aParent->mDataOffset, SEEK_SET); } } }
int Wav::testAndLoadFile(DataReader *aReader) { delete[] mData; mData = 0; mSampleCount = 0; int tag = aReader->read32(); if (tag == MAKEDWORD('O','g','g','S')) { aReader->seek(0); int e = 0; stb_vorbis *v = 0; if (aReader->isMemoryFile()) { v = stb_vorbis_open_memory(aReader->currentData(), aReader->dataLength(), &e, 0); } else { v = stb_vorbis_open_file(aReader->filePtr(), 0, &e, 0); } if (0 != v) { return loadogg(v); } return FILE_LOAD_FAILED; } else if (tag == MAKEDWORD('R','I','F','F')) { return loadwav(aReader); } return FILE_LOAD_FAILED; }
WavStreamInstance::WavStreamInstance(WavStream *aParent) { mParent = aParent; mOffset = 0; mOgg = 0; mFile = 0; if (aParent->mMemFile) { MemoryFile *mf = new MemoryFile(); mFile = mf; mf->openMem(aParent->mMemFile->getMemPtr(), aParent->mMemFile->length(), false, false); } else if (aParent->mFilename) { DiskFile *df = new DiskFile; mFile = df; df->open(aParent->mFilename); } else if (aParent->mStreamFile) { mFile = aParent->mStreamFile; mFile->seek(0); // stb_vorbis assumes file offset to be at start of ogg } else { return; } if (mFile) { if (mParent->mOgg) { int e; mOgg = stb_vorbis_open_file((Soloud_Filehack *)mFile, 0, &e, 0); if (!mOgg) { if (mFile != mParent->mStreamFile) delete mFile; mFile = 0; } mOggFrameSize = 0; mOggFrameOffset = 0; mOggOutputs = 0; } else { mFile->seek(aParent->mDataOffset); } } }
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; }
static unsigned char *loadSound_ogg( String path,int *length,int *channels,int *format,int *hertz ){ FILE *f=fopenFile( path,"rb" ); if( !f ) return 0; int error; stb_vorbis *v=stb_vorbis_open_file( f,0,&error,0 ); if( !v ){ fclose( f ); return 0; } stb_vorbis_info info=stb_vorbis_get_info( v ); int limit=info.channels*4096; int offset=0,data_len=0,total=limit; short *data=(short*)malloc( total*sizeof(short) ); for(;;){ int n=stb_vorbis_get_frame_short_interleaved( v,info.channels,data+offset,total-offset ); if( !n ) break; data_len+=n; offset+=n*info.channels; if( offset+limit>total ){ total*=2; data=(short*)realloc( data,total*sizeof(short) ); } } *length=data_len; *channels=info.channels; *format=2; *hertz=info.sample_rate; stb_vorbis_close(v); fclose( f ); return (unsigned char*)data; }
result WavStream::loadogg(File * fp) { fp->seek(0); int e; stb_vorbis *v; v = stb_vorbis_open_file((Soloud_Filehack *)fp, 0, &e, 0); if (v == NULL) return FILE_LOAD_FAILED; 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; return 0; }
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); }