Home | History | Annotate | Download | only in audio
      1 /*
      2  * Copyright (C) 2010, Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1.  Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2.  Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     16  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
     20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     23  */
     24 
     25 #include "config.h"
     26 
     27 #if ENABLE(WEB_AUDIO)
     28 
     29 #include "EqualPowerPanner.h"
     30 
     31 #include "AudioBus.h"
     32 #include "AudioUtilities.h"
     33 #include <wtf/MathExtras.h>
     34 
     35 // Use a 50ms smoothing / de-zippering time-constant.
     36 const double SmoothingTimeConstant = 0.050;
     37 
     38 namespace WebCore {
     39 
     40 EqualPowerPanner::EqualPowerPanner(double sampleRate)
     41     : Panner(PanningModelEqualPower)
     42     , m_isFirstRender(true)
     43     , m_gainL(0.0)
     44     , m_gainR(0.0)
     45 {
     46     m_smoothingConstant = AudioUtilities::discreteTimeConstantForSampleRate(SmoothingTimeConstant, sampleRate);
     47 }
     48 
     49 void EqualPowerPanner::pan(double azimuth, double /*elevation*/, AudioBus* inputBus, AudioBus* outputBus, size_t framesToProcess)
     50 {
     51     // FIXME: implement stereo sources
     52     bool isInputSafe = inputBus && inputBus->numberOfChannels() == 1 && framesToProcess <= inputBus->length();
     53     ASSERT(isInputSafe);
     54     if (!isInputSafe)
     55         return;
     56 
     57     bool isOutputSafe = outputBus && outputBus->numberOfChannels() == 2 && framesToProcess <= outputBus->length();
     58     ASSERT(isOutputSafe);
     59     if (!isOutputSafe)
     60         return;
     61 
     62     AudioChannel* channel = inputBus->channel(0);
     63     float* sourceP = channel->data();
     64     float* destinationL = outputBus->channelByType(AudioBus::ChannelLeft)->data();
     65     float* destinationR = outputBus->channelByType(AudioBus::ChannelRight)->data();
     66 
     67     if (!sourceP || !destinationL || !destinationR)
     68         return;
     69 
     70     // Pan smoothly from left to right with azimuth going from -30 -> +30 degrees.
     71     double desiredPanPosition;
     72     if (azimuth > 30.0)
     73         desiredPanPosition = 1.0;
     74     else if (azimuth < -30.0)
     75         desiredPanPosition = 0.0;
     76     else
     77         desiredPanPosition = (azimuth + 30.0) / 60.0;
     78 
     79     double desiredGainL = 0.5 * cos(piDouble * desiredPanPosition) + 0.5;
     80     double desiredGainR = sqrt(1.0 - desiredGainL*desiredGainL);
     81 
     82     // Don't de-zipper on first render call.
     83     if (m_isFirstRender) {
     84         m_isFirstRender = false;
     85         m_gainL = desiredGainL;
     86         m_gainR = desiredGainR;
     87     }
     88 
     89     // Cache in local variables.
     90     double gainL = m_gainL;
     91     double gainR = m_gainR;
     92 
     93     // Get local copy of smoothing constant.
     94     const double SmoothingConstant = m_smoothingConstant;
     95 
     96     int n = framesToProcess;
     97 
     98     while (n--) {
     99         float input = *sourceP++;
    100         gainL += (desiredGainL - gainL) * SmoothingConstant;
    101         gainR += (desiredGainR - gainR) * SmoothingConstant;
    102         *destinationL++ = static_cast<float>(input * gainL);
    103         *destinationR++ = static_cast<float>(input * gainR);
    104     }
    105 
    106     m_gainL = gainL;
    107     m_gainR = gainR;
    108 }
    109 
    110 } // namespace WebCore
    111 
    112 #endif // ENABLE(WEB_AUDIO)
    113