Exemplo n.º 1
0
int AudioMixer::process(UInt32 numInputChannels, float* inputBuffer, UInt32 numOutputChannels, float *outputBuffer, UInt32 numFrames)
{
    mLock.lock();

	int z = 0;
    memset(outputBuffer, 0, sizeof(float) * numFrames * numOutputChannels);  
	for(UInt32 i = 0; i < mSounds.size(); ++i)
	{		
		UNTZ::Sound *s = mSounds[i];
		if(s->getData()->getState() == kPlayStatePlaying)
		{
			++z;
			Int64 totalFramesRead = 0;
			Int64 framesRead = 0;
			do
			{
				framesRead = s->getData()->getSource()->readFrames((float*)&mBuffer[0], numOutputChannels, numFrames - totalFramesRead, s->getData()->mState);
				if(framesRead > 0)
                {
                    for(UInt32 k = 0; k < numOutputChannels; ++k)
                    {
                        float *out = &outputBuffer[k*numFrames + totalFramesRead];
                        float *in = &mBuffer[k*framesRead + totalFramesRead]; 
                        for(UInt32 j = 0; j < framesRead; j++)
                            *(out++) += *(in++) * s->getData()->mVolume;
                    }
                    totalFramesRead += framesRead;
                }
			}
			while(framesRead > 0 && totalFramesRead < numFrames);
            
			if(framesRead <= 0)
            {
                if ( ! s->isLooping() )
                {
				    s->stop();
                }
            }
		}
	}
	
	RPRINT("processed %d sources\n", z);

    mLock.unlock();

	// volume & clipping
	
	// commented out in HBS contrib
    //for(UInt32 k = 0; k < numOutputChannels * numFrames; ++k)
    //{
	//	float val = *outputBuffer * mVolume;;
	//	val = val > 1.0 ? 1.0 : val;
	//	val = val < -1.0 ? -1.0 : val;
	//	*(outputBuffer)++ = val;
    //}

	return 0;
}
Exemplo n.º 2
0
int AudioMixer::process(UInt32 numInputChannels, float* inputBuffer, UInt32 numOutputChannels, float *outputBuffer, UInt32 numFrames)
{
    mLock.lock();

    memset(outputBuffer, 0, sizeof(float) * numFrames * numOutputChannels);  
	for(UInt32 i = 0; i < mSounds.size(); ++i)
	{		
		UNTZ::Sound *s = mSounds[i];
		if(s->getData()->getState() == kPlayStatePlaying)
		{
			Int64 totalFramesRead = 0;
			Int64 framesRead = 0;
			do
			{
				framesRead = s->getData()->getSource()->readFrames((float*)&mBuffer[0], numOutputChannels, numFrames - totalFramesRead);
				if(framesRead > 0)
                {
                    for(UInt32 k = 0; k < numOutputChannels; ++k)
                    {
                        float *out = &outputBuffer[k*numFrames + totalFramesRead];
                        float *in = &mBuffer[k*numFrames + totalFramesRead]; 
                        for(UInt32 j = 0; j < framesRead; j++)
                            *(out++) += *(in++) * s->getData()->mVolume;
                    }
                    totalFramesRead += framesRead;
                }
			}
			while(totalFramesRead > 0 && 
				totalFramesRead < numFrames && 
				framesRead >= 0);
            
			if(framesRead < 0)
            {
                mLock.unlock();
				s->stop();
                mLock.lock();
            }
		}
	}

    mLock.unlock();
    
	return 0;
}