Glenn Kasten | b3db213 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 1 | /* |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 27 | namespace android { |
| 28 | |
| 29 | // ---------------------------------------------------------------------------- |
| 30 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | class AudioMixer |
| 32 | { |
| 33 | public: |
Glenn Kasten | 25f1bc4 | 2012-03-20 17:01:29 -0700 | [diff] [blame] | 34 | AudioMixer(size_t frameCount, uint32_t sampleRate, |
| 35 | uint32_t maxNumTracks = MAX_NUM_TRACKS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
Glenn Kasten | b163138 | 2012-01-30 14:54:39 -0800 | [diff] [blame] | 37 | /*virtual*/ ~AudioMixer(); // non-virtual saves a v-table, restore if sub-classed |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | |
| 39 | static const uint32_t MAX_NUM_TRACKS = 32; |
| 40 | static const uint32_t MAX_NUM_CHANNELS = 2; |
| 41 | |
| 42 | static const uint16_t UNITY_GAIN = 0x1000; |
| 43 | |
| 44 | enum { // names |
| 45 | |
Glenn Kasten | 68deb15 | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 46 | // track names (MAX_NUM_TRACKS units) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | TRACK0 = 0x1000, |
| 48 | |
Glenn Kasten | af62dbc | 2011-12-15 14:54:01 -0800 | [diff] [blame] | 49 | // 0x2000 is unused |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | |
| 51 | // setParameter targets |
| 52 | TRACK = 0x3000, |
| 53 | RESAMPLE = 0x3001, |
| 54 | RAMP_VOLUME = 0x3002, // ramp to new volume |
| 55 | VOLUME = 0x3003, // don't ramp |
| 56 | |
| 57 | // set Parameter names |
| 58 | // for target TRACK |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 59 | CHANNEL_MASK = 0x4000, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | FORMAT = 0x4001, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 61 | MAIN_BUFFER = 0x4002, |
| 62 | AUX_BUFFER = 0x4003, |
Glenn Kasten | e5fb263 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 63 | // for target RESAMPLE |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | SAMPLE_RATE = 0x4100, |
Eric Laurent | 4bb21c4 | 2011-02-28 16:52:51 -0800 | [diff] [blame] | 65 | RESET = 0x4101, |
Glenn Kasten | e5fb263 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 66 | // for target RAMP_VOLUME and VOLUME (8 channels max) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | VOLUME0 = 0x4200, |
| 68 | VOLUME1 = 0x4201, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 69 | AUXLEVEL = 0x4210, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | |
Glenn Kasten | 68deb15 | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 73 | // For all APIs with "name": TRACK0 <= name < TRACK0 + MAX_NUM_TRACKS |
Glenn Kasten | 76b6c0c | 2012-02-14 08:52:15 -0800 | [diff] [blame] | 74 | |
| 75 | // Allocate a track name. Returns new track name if successful, -1 on failure. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | int getTrackName(); |
Glenn Kasten | 76b6c0c | 2012-02-14 08:52:15 -0800 | [diff] [blame] | 77 | |
| 78 | // Free an allocated track by name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | void deleteTrackName(int name); |
| 80 | |
Glenn Kasten | 76b6c0c | 2012-02-14 08:52:15 -0800 | [diff] [blame] | 81 | // Enable or disable an allocated track by name |
Glenn Kasten | 68deb15 | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 82 | void enable(int name); |
| 83 | void disable(int name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | |
Glenn Kasten | 68deb15 | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 85 | void setParameter(int name, int target, int param, void *value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | |
Glenn Kasten | 68deb15 | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 87 | void setBufferProvider(int name, AudioBufferProvider* bufferProvider); |
John Grossman | d8cf296 | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 88 | void process(int64_t pts); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | |
| 90 | uint32_t trackNames() const { return mTrackNames; } |
| 91 | |
Glenn Kasten | eabd94a | 2012-02-02 14:06:11 -0800 | [diff] [blame] | 92 | size_t getUnreleasedFrames(int name) const; |
Eric Laurent | 72dafb2 | 2011-12-22 16:08:41 -0800 | [diff] [blame] | 93 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | private: |
| 95 | |
| 96 | enum { |
| 97 | NEEDS_CHANNEL_COUNT__MASK = 0x00000003, |
| 98 | NEEDS_FORMAT__MASK = 0x000000F0, |
| 99 | NEEDS_MUTE__MASK = 0x00000100, |
| 100 | NEEDS_RESAMPLE__MASK = 0x00001000, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 101 | NEEDS_AUX__MASK = 0x00010000, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | enum { |
| 105 | NEEDS_CHANNEL_1 = 0x00000000, |
| 106 | NEEDS_CHANNEL_2 = 0x00000001, |
| 107 | |
| 108 | NEEDS_FORMAT_16 = 0x00000010, |
| 109 | |
| 110 | NEEDS_MUTE_DISABLED = 0x00000000, |
| 111 | NEEDS_MUTE_ENABLED = 0x00000100, |
| 112 | |
| 113 | NEEDS_RESAMPLE_DISABLED = 0x00000000, |
| 114 | NEEDS_RESAMPLE_ENABLED = 0x00001000, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 115 | |
| 116 | NEEDS_AUX_DISABLED = 0x00000000, |
| 117 | NEEDS_AUX_ENABLED = 0x00010000, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | }; |
| 119 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | struct state_t; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 121 | struct track_t; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 123 | typedef void (*hook_t)(track_t* t, int32_t* output, size_t numOutFrames, int32_t* temp, int32_t* aux); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | static const int BLOCKSIZE = 16; // 4 cache lines |
| 125 | |
| 126 | struct track_t { |
| 127 | uint32_t needs; |
| 128 | |
| 129 | union { |
Glenn Kasten | 1f6f05d | 2011-12-13 11:52:35 -0800 | [diff] [blame] | 130 | int16_t volume[MAX_NUM_CHANNELS]; // [0]3.12 fixed point |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | int32_t volumeRL; |
| 132 | }; |
| 133 | |
Glenn Kasten | 1f6f05d | 2011-12-13 11:52:35 -0800 | [diff] [blame] | 134 | int32_t prevVolume[MAX_NUM_CHANNELS]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | |
Glenn Kasten | 4f22c05 | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 136 | // 16-byte boundary |
| 137 | |
Glenn Kasten | 1f6f05d | 2011-12-13 11:52:35 -0800 | [diff] [blame] | 138 | int32_t volumeInc[MAX_NUM_CHANNELS]; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 139 | int32_t auxInc; |
| 140 | int32_t prevAuxLevel; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | |
Glenn Kasten | 4f22c05 | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 142 | // 16-byte boundary |
| 143 | |
| 144 | int16_t auxLevel; // 0 <= auxLevel <= MAX_GAIN_INT, but signed for mul performance |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | uint16_t frameCount; |
| 146 | |
Glenn Kasten | 4f22c05 | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 147 | uint8_t channelCount; // 1 or 2, redundant with (needs & NEEDS_CHANNEL_COUNT__MASK) |
| 148 | uint8_t format; // always 16 |
| 149 | uint16_t enabled; // actually bool |
| 150 | uint32_t channelMask; // currently under-used |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | |
| 152 | AudioBufferProvider* bufferProvider; |
Glenn Kasten | 4f22c05 | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 153 | |
| 154 | // 16-byte boundary |
| 155 | |
| 156 | mutable AudioBufferProvider::Buffer buffer; // 8 bytes |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 158 | hook_t hook; |
Glenn Kasten | 99c2fd3 | 2012-01-06 07:46:30 -0800 | [diff] [blame] | 159 | const void* in; // current location in buffer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | |
Glenn Kasten | 4f22c05 | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 161 | // 16-byte boundary |
| 162 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | AudioResampler* resampler; |
| 164 | uint32_t sampleRate; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 165 | int32_t* mainBuffer; |
| 166 | int32_t* auxBuffer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | |
Glenn Kasten | 4f22c05 | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 168 | // 16-byte boundary |
| 169 | |
John Grossman | d8cf296 | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 170 | uint64_t localTimeFreq; |
| 171 | |
Glenn Kasten | 4f22c05 | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 172 | int64_t padding; |
| 173 | |
| 174 | // 16-byte boundary |
| 175 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | bool setResampler(uint32_t sampleRate, uint32_t devSampleRate); |
Glenn Kasten | eabd94a | 2012-02-02 14:06:11 -0800 | [diff] [blame] | 177 | bool doesResample() const { return resampler != NULL; } |
| 178 | void resetResampler() { if (resampler != NULL) resampler->reset(); } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 179 | void adjustVolumeRamp(bool aux); |
Glenn Kasten | eabd94a | 2012-02-02 14:06:11 -0800 | [diff] [blame] | 180 | size_t getUnreleasedFrames() const { return resampler != NULL ? |
| 181 | resampler->getUnreleasedFrames() : 0; }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | // pad to 32-bytes to fill cache line |
| 185 | struct state_t { |
| 186 | uint32_t enabledTracks; |
| 187 | uint32_t needsChanged; |
| 188 | size_t frameCount; |
Glenn Kasten | 7d3be3a | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 189 | void (*hook)(state_t* state, int64_t pts); // one of process__*, never NULL |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | int32_t *outputTemp; |
| 191 | int32_t *resampleTemp; |
| 192 | int32_t reserved[2]; |
Glenn Kasten | 25f1bc4 | 2012-03-20 17:01:29 -0700 | [diff] [blame] | 193 | // FIXME allocate dynamically to save some memory when maxNumTracks < MAX_NUM_TRACKS |
Glenn Kasten | 1f6f05d | 2011-12-13 11:52:35 -0800 | [diff] [blame] | 194 | track_t tracks[MAX_NUM_TRACKS]; __attribute__((aligned(32))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | }; |
| 196 | |
Glenn Kasten | 68deb15 | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 197 | // bitmask of allocated track names, where bit 0 corresponds to TRACK0 etc. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | uint32_t mTrackNames; |
Glenn Kasten | 25f1bc4 | 2012-03-20 17:01:29 -0700 | [diff] [blame] | 199 | |
| 200 | // bitmask of configured track names; ~0 if maxNumTracks == MAX_NUM_TRACKS, |
| 201 | // but will have fewer bits set if maxNumTracks < MAX_NUM_TRACKS |
| 202 | const uint32_t mConfiguredNames; |
| 203 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | const uint32_t mSampleRate; |
| 205 | |
| 206 | state_t mState __attribute__((aligned(32))); |
| 207 | |
| 208 | void invalidateState(uint32_t mask); |
| 209 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 210 | static void track__genericResample(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux); |
| 211 | static void track__nop(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux); |
| 212 | static void track__16BitsStereo(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux); |
| 213 | static void track__16BitsMono(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux); |
| 214 | static void volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux); |
| 215 | static void volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | |
John Grossman | d8cf296 | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 217 | static void process__validate(state_t* state, int64_t pts); |
| 218 | static void process__nop(state_t* state, int64_t pts); |
| 219 | static void process__genericNoResampling(state_t* state, int64_t pts); |
| 220 | static void process__genericResampling(state_t* state, int64_t pts); |
| 221 | static void process__OneTrack16BitsStereoNoResampling(state_t* state, |
| 222 | int64_t pts); |
Glenn Kasten | 31ba2e5 | 2011-12-15 09:53:12 -0800 | [diff] [blame] | 223 | #if 0 |
John Grossman | d8cf296 | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 224 | static void process__TwoTracks16BitsStereoNoResampling(state_t* state, |
| 225 | int64_t pts); |
Glenn Kasten | 31ba2e5 | 2011-12-15 09:53:12 -0800 | [diff] [blame] | 226 | #endif |
John Grossman | d8cf296 | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 227 | |
| 228 | static int64_t calculateOutputPTS(const track_t& t, int64_t basePTS, |
| 229 | int outputFrameIndex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | }; |
| 231 | |
| 232 | // ---------------------------------------------------------------------------- |
| 233 | }; // namespace android |
| 234 | |
| 235 | #endif // ANDROID_AUDIO_MIXER_H |