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