예제 #1
0
/*
bool BufferedAudioSource::init(float* interleavedData, Int64 numSamples)
{
    return true;
}
*/
bool BufferedAudioSource::init(const RString& path, bool loadIntoMemory)
{
	if(loadIntoMemory)
	{
		RPRINT("loading sound into memory...\n");
        int channels = getNumChannels();
        double length = getLength();
		double srate = getSampleRate();
		UInt32 totalFrames = getSampleRate() * getLength();
		mBuffer.resize(getNumChannels(), totalFrames);
		float *pWritePos = mBuffer.getData();
		UInt32 numFrames = (UInt32)(getSampleRate());
		UInt32 framesRead = 0;
		UInt32 remainingFrames = totalFrames;
		UInt32 tf = 0;
		do
		{
            int version = 0;
			framesRead = decodeData(pWritePos, remainingFrames >= numFrames ? numFrames : remainingFrames, version);
			pWritePos += framesRead * getNumChannels();
			tf += framesRead;
			remainingFrames -= framesRead;
		}
		while(remainingFrames > 0 && framesRead > 0);
		mEOF = true;
		mLoadedInMemory = loadIntoMemory;
		mBuffer.resize(getNumChannels(), tf);
	}
	else
	{
		mBuffer = RAudioBuffer(getNumChannels(), 0);
	}

	return true;
}
void ExtAudioFileAudioSource::doneDecoding()
{
	RPRINT("freeing decoder memory.\n");
	
	mReadBuffer.clear();
	std::vector<float>().swap(mReadBuffer);
}
Int64 ExtAudioFileAudioSource::decodeData(float* buffer, UInt32 numFrames)
{
	RPRINT("decoding data\n");
    UInt32 numChannels = getNumChannels();
    OSStatus err = noErr;
    
    mReadBuffer.resize(numChannels * numFrames);
    
    // Set up the buffers
    setUpBuffers(&mReadBuffer[0], numChannels, numFrames);
    
    // Read the data out of our file, filling our output buffer
    UInt32 framesRead = numFrames;
	if(!isLoadedInMemory())
	{
		RScopedLock l(&mDecodeLock);
		err = ExtAudioFileRead (mAudioFile, &framesRead, mpBufferList);
	}
	else
	{
		err = ExtAudioFileRead (mAudioFile, &framesRead, mpBufferList);
	}
		
    if(err || framesRead == 0)
    {
        mEOF = true;
		RPRINT("done decoding data\n");
        return 0;
    }
    
    // The data is expected to be interlaced
    for(UInt32 j = 0; j < numChannels; ++j)
    {
        float *pTemp = &mReadBuffer[j * numFrames];
        float *pOut = &buffer[j];
        for(UInt32 i = j; i < framesRead; i++)
        {
            *pOut = *pTemp++;
            pOut += numChannels;
        }
    }
    
	RPRINT("done decoding data\n");
    return framesRead;    
}
void ExtAudioFileAudioSource::setDecoderPosition(Int64 startFrame)
{
	RScopedLock l(&mDecodeLock);

	RPRINT("setting decoder position\n");
    ExtAudioFileSeek(mAudioFile, startFrame);  
	if(startFrame < getLength() * getSampleRate())
		mEOF = false;
}
void ExtAudioFileAudioSource::close()
{
	RPRINT("closing...\n");
    BufferedAudioSource::close();
    
    if(mAudioFile)
    {
        ExtAudioFileDispose(mAudioFile);
        mAudioFile = 0;
    }

	if(mpBufferList)
    {
	    free (mpBufferList);
        mpBufferList = 0;
    }
    
	RPRINT("done closing.\n");
}