示例#1
0
int HRTFPanner::calculateDesiredAzimuthIndexAndBlend(double azimuth, double& azimuthBlend)
{
    // Convert the azimuth angle from the range -180 -> +180 into the range 0 -> 360.
    // The azimuth index may then be calculated from this positive value.
    if (azimuth < 0)
        azimuth += 360.0;
    
    HRTFDatabase* database = HRTFDatabaseLoader::defaultHRTFDatabase();    
    ASSERT(database);
    
    int numberOfAzimuths = database->numberOfAzimuths();
    const double angleBetweenAzimuths = 360.0 / numberOfAzimuths;

    // Calculate the azimuth index and the blend (0 -> 1) for interpolation.
    double desiredAzimuthIndexFloat = azimuth / angleBetweenAzimuths;
    int desiredAzimuthIndex = static_cast<int>(desiredAzimuthIndexFloat);
    azimuthBlend = desiredAzimuthIndexFloat - static_cast<double>(desiredAzimuthIndex);
    
    // We don't immediately start using this azimuth index, but instead approach this index from the last index we rendered at.
    // This minimizes the clicks and graininess for moving sources which occur otherwise.
    desiredAzimuthIndex = max(0, desiredAzimuthIndex);
    desiredAzimuthIndex = min(numberOfAzimuths - 1, desiredAzimuthIndex);
    return desiredAzimuthIndex;
}
示例#2
0
void HRTFPanner::pan(double desiredAzimuth, double elevation, const AudioBus* inputBus, AudioBus* outputBus, size_t framesToProcess)
{
    unsigned numInputChannels = inputBus ? inputBus->numberOfChannels() : 0;

    bool isInputGood = inputBus &&  numInputChannels >= 1 && numInputChannels <= 2;
    ASSERT(isInputGood);

    bool isOutputGood = outputBus && outputBus->numberOfChannels() == 2 && framesToProcess <= outputBus->length();
    ASSERT(isOutputGood);

    if (!isInputGood || !isOutputGood) {
        if (outputBus)
            outputBus->zero();
        return;
    }

    // This code only runs as long as the context is alive and after database has been loaded.
    HRTFDatabase* database = HRTFDatabaseLoader::defaultHRTFDatabase();    
    ASSERT(database);
    if (!database) {
        outputBus->zero();
        return;
    }

    // IRCAM HRTF azimuths values from the loaded database is reversed from the panner's notion of azimuth.
    double azimuth = -desiredAzimuth;

    bool isAzimuthGood = azimuth >= -180.0 && azimuth <= 180.0;
    ASSERT(isAzimuthGood);
    if (!isAzimuthGood) {
        outputBus->zero();
        return;
    }

    // Normally, we'll just be dealing with mono sources.
    // If we have a stereo input, implement stereo panning with left source processed by left HRTF, and right source by right HRTF.
    const AudioChannel* inputChannelL = inputBus->channelByType(AudioBus::ChannelLeft);
    const AudioChannel* inputChannelR = numInputChannels > 1 ? inputBus->channelByType(AudioBus::ChannelRight) : 0;

    // Get source and destination pointers.
    const float* sourceL = inputChannelL->data();
    const float* sourceR = numInputChannels > 1 ? inputChannelR->data() : sourceL;
    float* destinationL = outputBus->channelByType(AudioBus::ChannelLeft)->mutableData();
    float* destinationR = outputBus->channelByType(AudioBus::ChannelRight)->mutableData();

    double azimuthBlend;
    int desiredAzimuthIndex = calculateDesiredAzimuthIndexAndBlend(azimuth, azimuthBlend);

    // This algorithm currently requires that we process in power-of-two size chunks at least 128.
    ASSERT(1UL << static_cast<int>(log2(framesToProcess)) == framesToProcess);
    ASSERT(framesToProcess >= 128);
    
    const unsigned framesPerSegment = 128;
    const unsigned numberOfSegments = framesToProcess / framesPerSegment;

    for (unsigned segment = 0; segment < numberOfSegments; ++segment) {
        if (m_isFirstRender) {
            // Snap exactly to desired position (first time and after reset()).
            m_azimuthIndex = desiredAzimuthIndex;
            m_isFirstRender = false;
        } else {
            // Each segment renders with an azimuth index closer by one to the desired azimuth index.
            // Because inter-aural time delay is mostly a factor of azimuth and the delay is where the clicks and graininess come from,
            // we don't bother smoothing the elevations.
            int numberOfAzimuths = database->numberOfAzimuths();
            bool wrap = wrapDistance(m_azimuthIndex, desiredAzimuthIndex, numberOfAzimuths);
            if (wrap) {
                if (m_azimuthIndex < desiredAzimuthIndex)
                    m_azimuthIndex = (m_azimuthIndex - 1 + numberOfAzimuths) % numberOfAzimuths;
                else if (m_azimuthIndex > desiredAzimuthIndex)
                    m_azimuthIndex = (m_azimuthIndex + 1) % numberOfAzimuths;
            } else {
                if (m_azimuthIndex < desiredAzimuthIndex)
                    m_azimuthIndex = (m_azimuthIndex + 1) % numberOfAzimuths;
                else if (m_azimuthIndex > desiredAzimuthIndex)
                    m_azimuthIndex = (m_azimuthIndex - 1 + numberOfAzimuths) % numberOfAzimuths;
            }
        }
        
        // Get the HRTFKernels and interpolated delays.    
        HRTFKernel* kernelL;
        HRTFKernel* kernelR;
        double frameDelayL;
        double frameDelayR;
        database->getKernelsFromAzimuthElevation(azimuthBlend, m_azimuthIndex, elevation, kernelL, kernelR, frameDelayL, frameDelayR);

        ASSERT(kernelL && kernelR);
        if (!kernelL || !kernelR) {
            outputBus->zero();
            return;
        }
        
        ASSERT(frameDelayL / sampleRate() < MaxDelayTimeSeconds && frameDelayR / sampleRate() < MaxDelayTimeSeconds);
            
        // Calculate the source and destination pointers for the current segment.
        unsigned offset = segment * framesPerSegment;
        const float* segmentSourceL = sourceL + offset;
        const float* segmentSourceR = sourceR + offset;
        float* segmentDestinationL = destinationL + offset;
        float* segmentDestinationR = destinationR + offset;

        // First run through delay lines for inter-aural time difference.
        m_delayLineL.setDelayFrames(frameDelayL);
        m_delayLineR.setDelayFrames(frameDelayR);
        m_delayLineL.process(segmentSourceL, segmentDestinationL, framesPerSegment);
        m_delayLineR.process(segmentSourceR, segmentDestinationR, framesPerSegment);

        // Now do the convolutions in-place.
        m_convolverL.process(kernelL->fftFrame(), segmentDestinationL, segmentDestinationL, framesPerSegment);
        m_convolverR.process(kernelR->fftFrame(), segmentDestinationR, segmentDestinationR, framesPerSegment);
    }
}
示例#3
0
void HRTFPanner::pan(double desiredAzimuth, double elevation, const AudioChunk* inputBus, AudioChunk* outputBus)
{
    unsigned numInputChannels =
        inputBus->IsNull() ? 0 : inputBus->mChannelData.Length();

    MOZ_ASSERT(numInputChannels <= 2);
    MOZ_ASSERT(inputBus->mDuration == WEBAUDIO_BLOCK_SIZE);

    bool isOutputGood = outputBus && outputBus->mChannelData.Length() == 2 && outputBus->mDuration == WEBAUDIO_BLOCK_SIZE;
    MOZ_ASSERT(isOutputGood);

    if (!isOutputGood) {
        if (outputBus)
            outputBus->SetNull(outputBus->mDuration);
        return;
    }

    HRTFDatabase* database = m_databaseLoader->database();
    if (!database) { // not yet loaded
        outputBus->SetNull(outputBus->mDuration);
        return;
    }

    // IRCAM HRTF azimuths values from the loaded database is reversed from the panner's notion of azimuth.
    double azimuth = -desiredAzimuth;

    bool isAzimuthGood = azimuth >= -180.0 && azimuth <= 180.0;
    MOZ_ASSERT(isAzimuthGood);
    if (!isAzimuthGood) {
        outputBus->SetNull(outputBus->mDuration);
        return;
    }

    // Normally, we'll just be dealing with mono sources.
    // If we have a stereo input, implement stereo panning with left source processed by left HRTF, and right source by right HRTF.

    // Get destination pointers.
    float* destinationL =
        static_cast<float*>(const_cast<void*>(outputBus->mChannelData[0]));
    float* destinationR =
        static_cast<float*>(const_cast<void*>(outputBus->mChannelData[1]));

    double azimuthBlend;
    int desiredAzimuthIndex = calculateDesiredAzimuthIndexAndBlend(azimuth, azimuthBlend);

    // Initially snap azimuth and elevation values to first values encountered.
    if (m_azimuthIndex1 == UninitializedAzimuth) {
        m_azimuthIndex1 = desiredAzimuthIndex;
        m_elevation1 = elevation;
    }
    if (m_azimuthIndex2 == UninitializedAzimuth) {
        m_azimuthIndex2 = desiredAzimuthIndex;
        m_elevation2 = elevation;
    }

    // Cross-fade / transition over a period of around 45 milliseconds.
    // This is an empirical value tuned to be a reasonable trade-off between
    // smoothness and speed.
    const double fadeFrames = sampleRate() <= 48000 ? 2048 : 4096;

    // Check for azimuth and elevation changes, initiating a cross-fade if needed.
    if (!m_crossfadeX && m_crossfadeSelection == CrossfadeSelection1) {
        if (desiredAzimuthIndex != m_azimuthIndex1 || elevation != m_elevation1) {
            // Cross-fade from 1 -> 2
            m_crossfadeIncr = 1 / fadeFrames;
            m_azimuthIndex2 = desiredAzimuthIndex;
            m_elevation2 = elevation;
        }
    }
    if (m_crossfadeX == 1 && m_crossfadeSelection == CrossfadeSelection2) {
        if (desiredAzimuthIndex != m_azimuthIndex2 || elevation != m_elevation2) {
            // Cross-fade from 2 -> 1
            m_crossfadeIncr = -1 / fadeFrames;
            m_azimuthIndex1 = desiredAzimuthIndex;
            m_elevation1 = elevation;
        }
    }

    // Get the HRTFKernels and interpolated delays.
    HRTFKernel* kernelL1;
    HRTFKernel* kernelR1;
    HRTFKernel* kernelL2;
    HRTFKernel* kernelR2;
    double frameDelayL1;
    double frameDelayR1;
    double frameDelayL2;
    double frameDelayR2;
    database->getKernelsFromAzimuthElevation(azimuthBlend, m_azimuthIndex1, m_elevation1, kernelL1, kernelR1, frameDelayL1, frameDelayR1);
    database->getKernelsFromAzimuthElevation(azimuthBlend, m_azimuthIndex2, m_elevation2, kernelL2, kernelR2, frameDelayL2, frameDelayR2);

    bool areKernelsGood = kernelL1 && kernelR1 && kernelL2 && kernelR2;
    MOZ_ASSERT(areKernelsGood);
    if (!areKernelsGood) {
        outputBus->SetNull(outputBus->mDuration);
        return;
    }

    MOZ_ASSERT(frameDelayL1 / sampleRate() < MaxDelayTimeSeconds && frameDelayR1 / sampleRate() < MaxDelayTimeSeconds);
    MOZ_ASSERT(frameDelayL2 / sampleRate() < MaxDelayTimeSeconds && frameDelayR2 / sampleRate() < MaxDelayTimeSeconds);

    // Crossfade inter-aural delays based on transitions.
    double frameDelaysL[WEBAUDIO_BLOCK_SIZE];
    double frameDelaysR[WEBAUDIO_BLOCK_SIZE];
    {
      float x = m_crossfadeX;
      float incr = m_crossfadeIncr;
      for (unsigned i = 0; i < WEBAUDIO_BLOCK_SIZE; ++i) {
        frameDelaysL[i] = (1 - x) * frameDelayL1 + x * frameDelayL2;
        frameDelaysR[i] = (1 - x) * frameDelayR1 + x * frameDelayR2;
        x += incr;
      }
    }

    // First run through delay lines for inter-aural time difference.
    m_delayLine.Write(*inputBus);
    // "Speakers" means a mono input is read into both outputs (with possibly
    // different delays).
    m_delayLine.ReadChannel(frameDelaysL, outputBus, 0,
                            ChannelInterpretation::Speakers);
    m_delayLine.ReadChannel(frameDelaysR, outputBus, 1,
                            ChannelInterpretation::Speakers);
    m_delayLine.NextBlock();

    bool needsCrossfading = m_crossfadeIncr;

    // Have the convolvers render directly to the final destination if we're not cross-fading.
    float* convolutionDestinationL1 = needsCrossfading ? m_tempL1.Elements() : destinationL;
    float* convolutionDestinationR1 = needsCrossfading ? m_tempR1.Elements() : destinationR;
    float* convolutionDestinationL2 = needsCrossfading ? m_tempL2.Elements() : destinationL;
    float* convolutionDestinationR2 = needsCrossfading ? m_tempR2.Elements() : destinationR;

    // Now do the convolutions.
    // Note that we avoid doing convolutions on both sets of convolvers if we're not currently cross-fading.

    if (m_crossfadeSelection == CrossfadeSelection1 || needsCrossfading) {
        m_convolverL1.process(kernelL1->fftFrame(), destinationL, convolutionDestinationL1, WEBAUDIO_BLOCK_SIZE);
        m_convolverR1.process(kernelR1->fftFrame(), destinationR, convolutionDestinationR1, WEBAUDIO_BLOCK_SIZE);
    }

    if (m_crossfadeSelection == CrossfadeSelection2 || needsCrossfading) {
        m_convolverL2.process(kernelL2->fftFrame(), destinationL, convolutionDestinationL2, WEBAUDIO_BLOCK_SIZE);
        m_convolverR2.process(kernelR2->fftFrame(), destinationR, convolutionDestinationR2, WEBAUDIO_BLOCK_SIZE);
    }

    if (needsCrossfading) {
        // Apply linear cross-fade.
        float x = m_crossfadeX;
        float incr = m_crossfadeIncr;
        for (unsigned i = 0; i < WEBAUDIO_BLOCK_SIZE; ++i) {
            destinationL[i] = (1 - x) * convolutionDestinationL1[i] + x * convolutionDestinationL2[i];
            destinationR[i] = (1 - x) * convolutionDestinationR1[i] + x * convolutionDestinationR2[i];
            x += incr;
        }
        // Update cross-fade value from local.
        m_crossfadeX = x;

        if (m_crossfadeIncr > 0 && fabs(m_crossfadeX - 1) < m_crossfadeIncr) {
            // We've fully made the crossfade transition from 1 -> 2.
            m_crossfadeSelection = CrossfadeSelection2;
            m_crossfadeX = 1;
            m_crossfadeIncr = 0;
        } else if (m_crossfadeIncr < 0 && fabs(m_crossfadeX) < -m_crossfadeIncr) {
            // We've fully made the crossfade transition from 2 -> 1.
            m_crossfadeSelection = CrossfadeSelection1;
            m_crossfadeX = 0;
            m_crossfadeIncr = 0;
        }
    }
}
示例#4
0
void HRTFPanner::pan(double desiredAzimuth,
                     double elevation,
                     const AudioBus* inputBus,
                     AudioBus* outputBus,
                     size_t framesToProcess,
                     AudioBus::ChannelInterpretation channelInterpretation) {
  unsigned numInputChannels = inputBus ? inputBus->numberOfChannels() : 0;

  bool isInputGood = inputBus && numInputChannels >= 1 && numInputChannels <= 2;
  ASSERT(isInputGood);

  bool isOutputGood = outputBus && outputBus->numberOfChannels() == 2 &&
                      framesToProcess <= outputBus->length();
  ASSERT(isOutputGood);

  if (!isInputGood || !isOutputGood) {
    if (outputBus)
      outputBus->zero();
    return;
  }

  HRTFDatabase* database = m_databaseLoader->database();
  if (!database) {
    outputBus->copyFrom(*inputBus, channelInterpretation);
    return;
  }

  // IRCAM HRTF azimuths values from the loaded database is reversed from the
  // panner's notion of azimuth.
  double azimuth = -desiredAzimuth;

  bool isAzimuthGood = azimuth >= -180.0 && azimuth <= 180.0;
  ASSERT(isAzimuthGood);
  if (!isAzimuthGood) {
    outputBus->zero();
    return;
  }

  // Normally, we'll just be dealing with mono sources.
  // If we have a stereo input, implement stereo panning with left source
  // processed by left HRTF, and right source by right HRTF.
  const AudioChannel* inputChannelL =
      inputBus->channelByType(AudioBus::ChannelLeft);
  const AudioChannel* inputChannelR =
      numInputChannels > 1 ? inputBus->channelByType(AudioBus::ChannelRight)
                           : nullptr;

  // Get source and destination pointers.
  const float* sourceL = inputChannelL->data();
  const float* sourceR = numInputChannels > 1 ? inputChannelR->data() : sourceL;
  float* destinationL =
      outputBus->channelByType(AudioBus::ChannelLeft)->mutableData();
  float* destinationR =
      outputBus->channelByType(AudioBus::ChannelRight)->mutableData();

  double azimuthBlend;
  int desiredAzimuthIndex =
      calculateDesiredAzimuthIndexAndBlend(azimuth, azimuthBlend);

  // Initially snap azimuth and elevation values to first values encountered.
  if (m_azimuthIndex1 == UninitializedAzimuth) {
    m_azimuthIndex1 = desiredAzimuthIndex;
    m_elevation1 = elevation;
  }
  if (m_azimuthIndex2 == UninitializedAzimuth) {
    m_azimuthIndex2 = desiredAzimuthIndex;
    m_elevation2 = elevation;
  }

  // Cross-fade / transition over a period of around 45 milliseconds.
  // This is an empirical value tuned to be a reasonable trade-off between
  // smoothness and speed.
  const double fadeFrames = sampleRate() <= 48000 ? 2048 : 4096;

  // Check for azimuth and elevation changes, initiating a cross-fade if needed.
  if (!m_crossfadeX && m_crossfadeSelection == CrossfadeSelection1) {
    if (desiredAzimuthIndex != m_azimuthIndex1 || elevation != m_elevation1) {
      // Cross-fade from 1 -> 2
      m_crossfadeIncr = 1 / fadeFrames;
      m_azimuthIndex2 = desiredAzimuthIndex;
      m_elevation2 = elevation;
    }
  }
  if (m_crossfadeX == 1 && m_crossfadeSelection == CrossfadeSelection2) {
    if (desiredAzimuthIndex != m_azimuthIndex2 || elevation != m_elevation2) {
      // Cross-fade from 2 -> 1
      m_crossfadeIncr = -1 / fadeFrames;
      m_azimuthIndex1 = desiredAzimuthIndex;
      m_elevation1 = elevation;
    }
  }

  // This algorithm currently requires that we process in power-of-two size
  // chunks at least AudioUtilities::kRenderQuantumFrames.
  ASSERT(1UL << static_cast<int>(log2(framesToProcess)) == framesToProcess);
  DCHECK_GE(framesToProcess, AudioUtilities::kRenderQuantumFrames);

  const unsigned framesPerSegment = AudioUtilities::kRenderQuantumFrames;
  const unsigned numberOfSegments = framesToProcess / framesPerSegment;

  for (unsigned segment = 0; segment < numberOfSegments; ++segment) {
    // Get the HRTFKernels and interpolated delays.
    HRTFKernel* kernelL1;
    HRTFKernel* kernelR1;
    HRTFKernel* kernelL2;
    HRTFKernel* kernelR2;
    double frameDelayL1;
    double frameDelayR1;
    double frameDelayL2;
    double frameDelayR2;
    database->getKernelsFromAzimuthElevation(azimuthBlend, m_azimuthIndex1,
                                             m_elevation1, kernelL1, kernelR1,
                                             frameDelayL1, frameDelayR1);
    database->getKernelsFromAzimuthElevation(azimuthBlend, m_azimuthIndex2,
                                             m_elevation2, kernelL2, kernelR2,
                                             frameDelayL2, frameDelayR2);

    bool areKernelsGood = kernelL1 && kernelR1 && kernelL2 && kernelR2;
    ASSERT(areKernelsGood);
    if (!areKernelsGood) {
      outputBus->zero();
      return;
    }

    ASSERT(frameDelayL1 / sampleRate() < MaxDelayTimeSeconds &&
           frameDelayR1 / sampleRate() < MaxDelayTimeSeconds);
    ASSERT(frameDelayL2 / sampleRate() < MaxDelayTimeSeconds &&
           frameDelayR2 / sampleRate() < MaxDelayTimeSeconds);

    // Crossfade inter-aural delays based on transitions.
    double frameDelayL =
        (1 - m_crossfadeX) * frameDelayL1 + m_crossfadeX * frameDelayL2;
    double frameDelayR =
        (1 - m_crossfadeX) * frameDelayR1 + m_crossfadeX * frameDelayR2;

    // Calculate the source and destination pointers for the current segment.
    unsigned offset = segment * framesPerSegment;
    const float* segmentSourceL = sourceL + offset;
    const float* segmentSourceR = sourceR + offset;
    float* segmentDestinationL = destinationL + offset;
    float* segmentDestinationR = destinationR + offset;

    // First run through delay lines for inter-aural time difference.
    m_delayLineL.setDelayFrames(frameDelayL);
    m_delayLineR.setDelayFrames(frameDelayR);
    m_delayLineL.process(segmentSourceL, segmentDestinationL, framesPerSegment);
    m_delayLineR.process(segmentSourceR, segmentDestinationR, framesPerSegment);

    bool needsCrossfading = m_crossfadeIncr;

    // Have the convolvers render directly to the final destination if we're not
    // cross-fading.
    float* convolutionDestinationL1 =
        needsCrossfading ? m_tempL1.data() : segmentDestinationL;
    float* convolutionDestinationR1 =
        needsCrossfading ? m_tempR1.data() : segmentDestinationR;
    float* convolutionDestinationL2 =
        needsCrossfading ? m_tempL2.data() : segmentDestinationL;
    float* convolutionDestinationR2 =
        needsCrossfading ? m_tempR2.data() : segmentDestinationR;

    // Now do the convolutions.
    // Note that we avoid doing convolutions on both sets of convolvers if we're
    // not currently cross-fading.

    if (m_crossfadeSelection == CrossfadeSelection1 || needsCrossfading) {
      m_convolverL1.process(kernelL1->fftFrame(), segmentDestinationL,
                            convolutionDestinationL1, framesPerSegment);
      m_convolverR1.process(kernelR1->fftFrame(), segmentDestinationR,
                            convolutionDestinationR1, framesPerSegment);
    }

    if (m_crossfadeSelection == CrossfadeSelection2 || needsCrossfading) {
      m_convolverL2.process(kernelL2->fftFrame(), segmentDestinationL,
                            convolutionDestinationL2, framesPerSegment);
      m_convolverR2.process(kernelR2->fftFrame(), segmentDestinationR,
                            convolutionDestinationR2, framesPerSegment);
    }

    if (needsCrossfading) {
      // Apply linear cross-fade.
      float x = m_crossfadeX;
      float incr = m_crossfadeIncr;
      for (unsigned i = 0; i < framesPerSegment; ++i) {
        segmentDestinationL[i] = (1 - x) * convolutionDestinationL1[i] +
                                 x * convolutionDestinationL2[i];
        segmentDestinationR[i] = (1 - x) * convolutionDestinationR1[i] +
                                 x * convolutionDestinationR2[i];
        x += incr;
      }
      // Update cross-fade value from local.
      m_crossfadeX = x;

      if (m_crossfadeIncr > 0 && fabs(m_crossfadeX - 1) < m_crossfadeIncr) {
        // We've fully made the crossfade transition from 1 -> 2.
        m_crossfadeSelection = CrossfadeSelection2;
        m_crossfadeX = 1;
        m_crossfadeIncr = 0;
      } else if (m_crossfadeIncr < 0 && fabs(m_crossfadeX) < -m_crossfadeIncr) {
        // We've fully made the crossfade transition from 2 -> 1.
        m_crossfadeSelection = CrossfadeSelection1;
        m_crossfadeX = 0;
        m_crossfadeIncr = 0;
      }
    }
  }
}
示例#5
0
文件: Binaural.cpp 项目: eriser/CSL
void BinauralPanner::nextBuffer(Buffer & outputBuffer) throw (CException) {

	HRTFDatabase * hrtfDatabase = HRTFDatabase::Database();
	unsigned numFrames = outputBuffer.mNumFrames;
	unsigned tmpBufPtr, tmpHRTFPtr = 0;
	unsigned hrtfLength = hrtfDatabase->hrtfLength();
	unsigned hrtf2Use = hrtfLength / LEN_DOWNS;
	unsigned winSize = hrtfDatabase->windowSize();
#ifdef CSL_DEBUG
	logMsg("BinauralPanner::nextBuffer");
#endif
	if (outputBuffer.mNumChannels != 2) {
		logMsg(kLogError, "BinauralPanner needs a stereo output buffer");
		return;
	}
	if (numFrames != mFramesPerBlock) {
		logMsg(kLogError, 
			"BinauralPanner::nextBuffer # frames %d wrong for HRTF size %d (use a block resizer).", 
			numFrames, mFramesPerBlock);
		return;
	}
	outputBuffer.zeroBuffers();									// clear output

	for (unsigned i = 0; i < mSources.size(); i++) {			// i loops through sources
	
		SpatialSource * source = (SpatialSource *) mSources[i];	// Get the sound source

		if (source->isActive()) {								// if source is active
			mInBuf.mNumFrames = winSize;						// set in # frames
			mInBuf.zeroBuffers();								// clear buffer
			mInBuf.mNumFrames = numFrames;						// set in # frames
			bool samePos = ! source->positionChanged();			// check this before we grab the input

			source->nextBuffer(mInBuf);							// get input from the source
			
////////														// debugging: copy input to output
//			memcpy(outputBuffer.buffer(0), mInBuf.buffer(0), numFrames * sizeof(sample));
//			memcpy(outputBuffer.buffer(1), mInBuf.buffer(0), numFrames * sizeof(sample));
//			return;
////////														// Get the cached data	
			BinauralSourceCache * tcache = (BinauralSourceCache *) mCache[i]; 
																// set input complex fft pointer
			mTmpBuf.setBuffer(0, (SampleBuffer) tcache->mInSpect[mBlockInd]);
			mInBuf.mNumFrames = winSize;						// dbl length

			mInFFT.nextBuffer(mInBuf, mTmpBuf);					// Calculate input FFT into cache->mInSpect

			if (samePos) {						// if the position of the source is the same.
				if (mBlockInd > 0)				// assign the same HRTF you had the previous round
					tcache->mHRTF[mBlockInd] = tcache->mHRTF[mBlockInd - 1];
				else 
					tcache->mHRTF[mBlockInd] = tcache->mHRTF[mNumBlocks - 1];
					
			} else {							// if the position changed, then find the new HRTF
				unsigned hrtfIndex = hrtfDatabase->hrtfAt(*source->position());
				tcache->mHRTF[mBlockInd] = hrtfIndex;
//				CPoint hrtfPosition = hrtfDatabase->hrtfAt(localHRTF)->mPosition;
//				logMsg("\t\t\t\tHRTF: %5.1f @ %5.1f \t Source: %5.1f @ %5.1f", 
//					hrtfPosition.theta() * CSL_DEGS_PER_RAD, hrtfPosition.ele() * CSL_DEGS_PER_RAD, 
//					source->position()->theta() * CSL_DEGS_PER_RAD, source->position()->ele() * CSL_DEGS_PER_RAD);
			}
												// zero the complex spectrum buffers before summing loop
			memset(mHOutL, 0, hrtf2Use * sizeof(SampleComplex));
			memset(mHOutR, 0, hrtf2Use * sizeof(SampleComplex));
			
												// Do the convolution of HRTF with the input spectra
			tmpBufPtr = mBlockInd;				// set the block counter to the current block
			do {								// Go thru previous HRTFs doing complex multiply/accumulate
				HRTF * tempHRTF = hrtfDatabase->hrtfAt(tcache->mHRTF[tmpBufPtr]);
				for (unsigned j = 0; j < hrtf2Use; j++) {				// loop over buffers of past FFT out
					ComplexPtr in1j = tcache->mInSpect[tmpBufPtr][j];	// input spectrum
					ComplexPtr inLj = tempHRTF->mHrtfL[tmpHRTFPtr][j];	// L/R HRTF
					ComplexPtr inRj = tempHRTF->mHrtfR[tmpHRTFPtr][j];
					ComplexPtr outLj = mHOutL[j];						// L/R summation buffers
					ComplexPtr outRj = mHOutR[j];
					cmac(in1j, inLj, outLj);							// complex MAC macro
					cmac(in1j, inRj, outRj);
												// old-way: complex MAC loop with 2D arrays
//					cmac(cache->mInSpect[tmpBufPtr][j], tempHRTF->mHrtfL[tmpHRTFPtr][j], mHOutL[j]);
//					cmac(cache->mInSpect[tmpBufPtr][j], tempHRTF->mHrtfR[tmpHRTFPtr][j], mHOutR[j]);
				}
				if (++tmpBufPtr >= mNumBlocksToSum)
					tmpBufPtr = 0;
				if (++tmpHRTFPtr >= mNumBlocksToSum)
					tmpHRTFPtr = 0;
			} while (tmpBufPtr != mBlockInd);				// end of loop through prior inputs and HRTF spectra
			
															// debugging: overwrite spectral sum with input
//			memcpy(mHOutL, cache->mInSpect[mBlockInd], hrtfLength * sizeof(SampleComplex));
//			memcpy(mHOutR, cache->mInSpect[mBlockInd], hrtfLength * sizeof(SampleComplex));
															// Output IFFT stage
			mTmpBuf.mNumFrames = winSize;					// set up output buffer
			mTmpBuf.setBuffer(0, (SampleBuffer) mHOutL);	// IFFT input = summation buffer
			mOutBuf.setBuffer(0, mIFFTOutL);				// IFFT out = sample buffer

			mOutFFT.nextBuffer(mTmpBuf, mOutBuf);			// do left IFFT

			mTmpBuf.setBuffer(0, (SampleBuffer) mHOutR);
			mOutBuf.setBuffer(0, mIFFTOutR);

			mOutFFT.nextBuffer(mTmpBuf, mOutBuf);			// do right IFFT

			SampleBuffer outL = outputBuffer.buffer(0);	// Get pointers to output buffers (stereo)
			SampleBuffer outR = outputBuffer.buffer(1);
			SampleBuffer ifftL = mIFFTOutL;					// IFFT real out data ptrs
			SampleBuffer ifftR = mIFFTOutR;
			SampleBuffer cacheL = tcache->mPrevOutL;			// previous real output caches
			SampleBuffer cacheR = tcache->mPrevOutR;
#ifdef USE_FFTW
			sample scale = 1.0f / (float) numFrames;		// window size scale
#else
			sample scale = 1.0f ; //  / sqrtf(numFrames);
//			sample scale = 1.0f  / sqrtf(numFrames);
#endif
			for (unsigned j = 0; j < numFrames; j++) {			// Overlap and add loop
				*outL++ += ((*ifftL++ + *cacheL++) * scale);	// sum/scale IFFT & cache 
				*outR++ += ((*ifftR++ + *cacheR++) * scale);
			}
															// copy top-half of output to cache
			memcpy(tcache->mPrevOutL, mIFFTOutL + numFrames, numFrames * sizeof(sample));
			memcpy(tcache->mPrevOutR, mIFFTOutR + numFrames, numFrames * sizeof(sample));
		}							// end if source active
	}								// end source loop
	
	if (mBlockInd == 0)										// test/decrement panner's block counter
		mBlockInd = mNumBlocksToSum;						// for next call
	mBlockInd--;
}