1 /*
   2  * Copyright (c) 2014, 2016, 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 
  32 #include <memory>
  33 
  34 #include "AVFKernelProcessor.h"
  35 #include "AUEffectBase.h"
  36 
  37 #define kDefaultSoundLevelParam_Volume 1.0f
  38 #define kDefaultSoundLevelParam_Balance 0.0f
  39 
  40 /*
  41  * This unit applies the volume/balance controls.
  42  */
  43 class AVFSoundLevelUnit : public AVFKernelProcessor {
  44 public:
  45     AVFSoundLevelUnit() :
  46         AVFKernelProcessor(),
  47         mVolume(kDefaultSoundLevelParam_Volume),
  48         mBalance(kDefaultSoundLevelParam_Balance)
  49     {}
  50 
  51     virtual ~AVFSoundLevelUnit() {}
  52 
  53     virtual AUKernelBase *NewKernel();
  54 
  55     Float32 volume() {
  56         return mVolume;
  57     }
  58     void setVolume(Float32 volume) {
  59         if (volume < 0.0) {
  60             volume = 0.0;
  61         } else if (volume > 1.0) {
  62             volume = 1.0;
  63         }
  64         mVolume = volume;
  65     }
  66 
  67     Float32 balance() {
  68         return mBalance;
  69     }
  70     void setBalance(Float32 balance) {
  71         if (balance < -1.0) {
  72             balance = -1.0;
  73         } else if (balance > 1.0) {
  74             balance = 1.0;
  75         }
  76         mBalance = balance;
  77     }
  78 
  79     // For stereo (2 channel), channel 0 is left, channel 1 is right
  80     Float32 CalculateChannelLevel(int channelNum, int channelCount) {
  81         Float32 volume = mVolume;
  82         Float32 balance = mBalance;
  83         Float32 level = volume;
  84 
  85         if (channelCount == 2) {
  86             // balance is only done on stereo audio
  87             if (((balance < 0.0) && channelNum == 1) ||
  88                 ((balance > 0.0) && channelNum == 0)) {
  89                 // attenuate according to balance
  90                 balance = 1.0 - fabsf(balance);
  91                 level *= balance; // invert so it ramps the right direction
  92             }
  93         }
  94         return level;
  95     }
  96 
  97 private:
  98     Float32 mVolume;
  99     Float32 mBalance;
 100 };
 101 
 102 typedef std::shared_ptr<AVFSoundLevelUnit> AVFSoundLevelUnitPtr;
 103 
 104 #endif /* defined(__JFXMedia__AVFSoundLevelUnit__) */