1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #ifndef __JFXMedia__AVFSoundLevelUnit__
  27 #define __JFXMedia__AVFSoundLevelUnit__
  28 
  29 #include <AudioUnit/AudioUnit.h>
  30 #include <Accelerate/Accelerate.h>
  31 #include "AVFKernelProcessor.h"
  32 #include "AUEffectBase.h"
  33 
  34 #define kDefaultSoundLevelParam_Volume 1.0f
  35 #define kDefaultSoundLevelParam_Balance 0.0f
  36 
  37 /*
  38  * This unit applies the volume/balance controls.
  39  */
  40 class AVFSoundLevelUnit : public AVFKernelProcessor {
  41 public:
  42     AVFSoundLevelUnit() :
  43         AVFKernelProcessor(),
  44         mVolume(kDefaultSoundLevelParam_Volume),
  45         mBalance(kDefaultSoundLevelParam_Balance)
  46     {}
  47 
  48     virtual ~AVFSoundLevelUnit() {}
  49 
  50     virtual AUKernelBase *NewKernel();
  51 
  52     Float32 volume() {
  53         return mVolume;
  54     }
  55     void setVolume(Float32 volume) {
  56         if (volume < 0.0) {
  57             volume = 0.0;
  58         } else if (volume > 1.0) {
  59             volume = 1.0;
  60         }
  61         mVolume = volume;
  62     }
  63 
  64     Float32 balance() {
  65         return mBalance;
  66     }
  67     void setBalance(Float32 balance) {
  68         if (balance < -1.0) {
  69             balance = -1.0;
  70         } else if (balance > 1.0) {
  71             balance = 1.0;
  72         }
  73         mBalance = balance;
  74     }
  75 
  76     // For stereo (2 channel), channel 0 is left, channel 1 is right
  77     Float32 CalculateChannelLevel(int channelNum, int channelCount) {
  78         Float32 volume = mVolume;
  79         Float32 balance = mBalance;
  80         Float32 level = volume;
  81 
  82         if (channelCount == 2) {
  83             // balance is only done on stereo audio
  84             if (((balance < 0.0) && channelNum == 1) ||
  85                 ((balance > 0.0) && channelNum == 0)) {
  86                 // attenuate according to balance
  87                 balance = 1.0 - fabsf(balance);
  88                 level *= balance; // invert so it ramps the right direction
  89             }
  90         }
  91         return level;
  92     }
  93 
  94 private:
  95     Float32 mVolume;
  96     Float32 mBalance;
  97 };
  98 
  99 #endif /* defined(__JFXMedia__AVFSoundLevelUnit__) */