Int64 DShowAudioSource::readFrames(float* data, UInt32 numChannels, UInt32 numFrames) { memset(data, 0, sizeof(float) * numChannels * numFrames); // Check if we are done decoding... long evCode; mpMediaControl->Run(); HRESULT hr = mpMediaEvent->WaitForCompletion(0, &evCode); if(evCode == EC_COMPLETE) { mEOF = true; } RScopedLock l(&mLock); Int64 framesRead = numFrames; if(mBuffer.size() > 0) { if(mBuffer.size() / mpWaveFormatEx->nChannels < framesRead) { framesRead = mBuffer.size() / mpWaveFormatEx->nChannels; } for(UInt32 j = 0; j < numChannels; ++j) { float *in = NULL; if(mpWaveFormatEx->nChannels == 1) in = &mBuffer[0]; else in = &mBuffer[j]; for(UInt32 i = 0; i < framesRead; ++i) { *(data++) = *in; in += mpWaveFormatEx->nChannels; } } mBuffer.erase(mBuffer.begin(), mBuffer.begin()+(framesRead * mpWaveFormatEx->nChannels)); } mCurrentPosition += framesRead; // Check if we need to decode more data if(mBuffer.size() < numFrames * mpWaveFormatEx->nChannels * 2) { if(mEOF && isLooping()) { setDecoderPosition(0); } } if(mEOF && framesRead == 0) { return -1; } return framesRead; }
void DShowAudioSource::setPosition(double position) { setDecoderPosition(position); RScopedLock l(&mLock); mBuffer.clear(); mCurrentPosition = (Int64)(position * getSampleRate()); mpMediaControl->Run(); }
void BufferedAudioSource::setPosition(double seconds) { seconds = seconds < 0 ? 0.0f : seconds; seconds = seconds > getLength() ? getLength() : seconds; RScopedLock l(&mLock); Int64 frames = (Int64)(seconds * getSampleRate()); if(!isLoadedInMemory()) { mBuffer.clear(); setDecoderPosition(frames); BufferedAudioSourceThread::getInstance()->readMore(); } }