AudioMixer.h revision f6b1678f8f508b447155a81b44e214475ab634a8
1/* //device/include/server/AudioFlinger/AudioMixer.h 2** 3** Copyright 2007, The Android Open Source Project 4** 5** Licensed under the Apache License, Version 2.0 (the "License"); 6** you may not use this file except in compliance with the License. 7** You may obtain a copy of the License at 8** 9** http://www.apache.org/licenses/LICENSE-2.0 10** 11** Unless required by applicable law or agreed to in writing, software 12** distributed under the License is distributed on an "AS IS" BASIS, 13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14** See the License for the specific language governing permissions and 15** limitations under the License. 16*/ 17 18#ifndef ANDROID_AUDIO_MIXER_H 19#define ANDROID_AUDIO_MIXER_H 20 21#include <stdint.h> 22#include <sys/types.h> 23 24#include "AudioBufferProvider.h" 25#include "AudioResampler.h" 26 27namespace android { 28 29// ---------------------------------------------------------------------------- 30 31class AudioMixer 32{ 33public: 34 AudioMixer(size_t frameCount, uint32_t sampleRate); 35 36 ~AudioMixer(); 37 38 static const uint32_t MAX_NUM_TRACKS = 32; 39 static const uint32_t MAX_NUM_CHANNELS = 2; 40 41 static const uint16_t UNITY_GAIN = 0x1000; 42 43 enum { // names 44 45 // track units (MAX_NUM_TRACKS units) 46 TRACK0 = 0x1000, 47 48 // 0x2000 is unused 49 50 // setParameter targets 51 TRACK = 0x3000, 52 RESAMPLE = 0x3001, 53 RAMP_VOLUME = 0x3002, // ramp to new volume 54 VOLUME = 0x3003, // don't ramp 55 56 // set Parameter names 57 // for target TRACK 58 CHANNEL_MASK = 0x4000, 59 FORMAT = 0x4001, 60 MAIN_BUFFER = 0x4002, 61 AUX_BUFFER = 0x4003, 62 // for target RESAMPLE 63 SAMPLE_RATE = 0x4100, 64 RESET = 0x4101, 65 // for target RAMP_VOLUME and VOLUME (8 channels max) 66 VOLUME0 = 0x4200, 67 VOLUME1 = 0x4201, 68 AUXLEVEL = 0x4210, 69 }; 70 71 72 int getTrackName(); 73 void deleteTrackName(int name); 74 75 void enable(); 76 void disable(); 77 78 void setActiveTrack(int track); 79 void setParameter(int target, int name, void *value); 80 81 void setBufferProvider(AudioBufferProvider* bufferProvider); 82 void process(); 83 84 uint32_t trackNames() const { return mTrackNames; } 85 86private: 87 88 enum { 89 NEEDS_CHANNEL_COUNT__MASK = 0x00000003, 90 NEEDS_FORMAT__MASK = 0x000000F0, 91 NEEDS_MUTE__MASK = 0x00000100, 92 NEEDS_RESAMPLE__MASK = 0x00001000, 93 NEEDS_AUX__MASK = 0x00010000, 94 }; 95 96 enum { 97 NEEDS_CHANNEL_1 = 0x00000000, 98 NEEDS_CHANNEL_2 = 0x00000001, 99 100 NEEDS_FORMAT_16 = 0x00000010, 101 102 NEEDS_MUTE_DISABLED = 0x00000000, 103 NEEDS_MUTE_ENABLED = 0x00000100, 104 105 NEEDS_RESAMPLE_DISABLED = 0x00000000, 106 NEEDS_RESAMPLE_ENABLED = 0x00001000, 107 108 NEEDS_AUX_DISABLED = 0x00000000, 109 NEEDS_AUX_ENABLED = 0x00010000, 110 }; 111 112 struct state_t; 113 struct track_t; 114 115 typedef void (*mix_t)(state_t* state); 116 typedef void (*hook_t)(track_t* t, int32_t* output, size_t numOutFrames, int32_t* temp, int32_t* aux); 117 static const int BLOCKSIZE = 16; // 4 cache lines 118 119 struct track_t { 120 uint32_t needs; 121 122 union { 123 int16_t volume[MAX_NUM_CHANNELS]; // [0]3.12 fixed point 124 int32_t volumeRL; 125 }; 126 127 int32_t prevVolume[MAX_NUM_CHANNELS]; 128 129 int32_t volumeInc[MAX_NUM_CHANNELS]; 130 int32_t auxLevel; 131 int32_t auxInc; 132 int32_t prevAuxLevel; 133 134 uint16_t frameCount; 135 136 uint8_t channelCount : 4; 137 uint8_t enabled : 1; 138 uint8_t reserved0 : 3; 139 uint8_t format; 140 uint32_t channelMask; 141 142 AudioBufferProvider* bufferProvider; 143 mutable AudioBufferProvider::Buffer buffer; 144 145 hook_t hook; 146 void const* in; // current location in buffer 147 148 AudioResampler* resampler; 149 uint32_t sampleRate; 150 int32_t* mainBuffer; 151 int32_t* auxBuffer; 152 153 bool setResampler(uint32_t sampleRate, uint32_t devSampleRate); 154 bool doesResample() const; 155 void resetResampler(); 156 void adjustVolumeRamp(bool aux); 157 }; 158 159 // pad to 32-bytes to fill cache line 160 struct state_t { 161 uint32_t enabledTracks; 162 uint32_t needsChanged; 163 size_t frameCount; 164 mix_t hook; 165 int32_t *outputTemp; 166 int32_t *resampleTemp; 167 int32_t reserved[2]; 168 track_t tracks[MAX_NUM_TRACKS]; __attribute__((aligned(32))); 169 }; 170 171 int mActiveTrack; 172 uint32_t mTrackNames; 173 const uint32_t mSampleRate; 174 175 state_t mState __attribute__((aligned(32))); 176 177 void invalidateState(uint32_t mask); 178 179 static void track__genericResample(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux); 180 static void track__nop(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux); 181 static void track__16BitsStereo(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux); 182 static void track__16BitsMono(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux); 183 static void volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux); 184 static void volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux); 185 186 static void process__validate(state_t* state); 187 static void process__nop(state_t* state); 188 static void process__genericNoResampling(state_t* state); 189 static void process__genericResampling(state_t* state); 190 static void process__OneTrack16BitsStereoNoResampling(state_t* state); 191#if 0 192 static void process__TwoTracks16BitsStereoNoResampling(state_t* state); 193#endif 194}; 195 196// ---------------------------------------------------------------------------- 197}; // namespace android 198 199#endif // ANDROID_AUDIO_MIXER_H 200