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 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 21 | #include <pthread.h> |
| 22 | #include <sstream> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 23 | #include <stdint.h> |
| 24 | #include <sys/types.h> |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 25 | #include <unordered_map> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 26 | |
Dan Albert | 36802bd | 2014-11-20 11:31:17 -0800 | [diff] [blame] | 27 | #include <media/AudioBufferProvider.h> |
Andy Hung | 068561c | 2017-01-03 17:09:32 -0800 | [diff] [blame] | 28 | #include <media/AudioResampler.h> |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 29 | #include <media/AudioResamplerPublic.h> |
Andy Hung | 068561c | 2017-01-03 17:09:32 -0800 | [diff] [blame] | 30 | #include <media/BufferProviders.h> |
Glenn Kasten | 8589ce7 | 2017-09-08 17:03:42 -0700 | [diff] [blame] | 31 | #include <media/nblog/NBLog.h> |
Dan Albert | 36802bd | 2014-11-20 11:31:17 -0800 | [diff] [blame] | 32 | #include <system/audio.h> |
| 33 | #include <utils/Compat.h> |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 34 | #include <utils/threads.h> |
| 35 | |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 36 | // FIXME This is actually unity gain, which might not be max in future, expressed in U.12 |
Andy Hung | 97ae824 | 2014-05-30 10:35:47 -0700 | [diff] [blame] | 37 | #define MAX_GAIN_INT AudioMixer::UNITY_GAIN_INT |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 38 | |
Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 39 | // This must match frameworks/av/services/audioflinger/Configuration.h |
| 40 | #define FLOAT_AUX |
| 41 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 42 | namespace android { |
| 43 | |
| 44 | // ---------------------------------------------------------------------------- |
| 45 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 46 | class AudioMixer |
| 47 | { |
| 48 | public: |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 49 | // Do not change these unless underlying code changes. |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 50 | // This mixer has a hard-coded upper limit of 8 channels for output. |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 51 | static constexpr uint32_t MAX_NUM_CHANNELS = FCC_8; |
| 52 | static constexpr uint32_t MAX_NUM_VOLUMES = FCC_2; // stereo volume only |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 53 | // maximum number of channels supported for the content |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 54 | static const uint32_t MAX_NUM_CHANNELS_TO_DOWNMIX = AUDIO_CHANNEL_COUNT_MAX; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 55 | |
Andy Hung | 97ae824 | 2014-05-30 10:35:47 -0700 | [diff] [blame] | 56 | static const uint16_t UNITY_GAIN_INT = 0x1000; |
Dan Albert | 36802bd | 2014-11-20 11:31:17 -0800 | [diff] [blame] | 57 | static const CONSTEXPR float UNITY_GAIN_FLOAT = 1.0f; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 58 | |
| 59 | enum { // names |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 60 | // setParameter targets |
| 61 | TRACK = 0x3000, |
| 62 | RESAMPLE = 0x3001, |
| 63 | RAMP_VOLUME = 0x3002, // ramp to new volume |
| 64 | VOLUME = 0x3003, // don't ramp |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 65 | TIMESTRETCH = 0x3004, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 66 | |
| 67 | // set Parameter names |
| 68 | // for target TRACK |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 69 | CHANNEL_MASK = 0x4000, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 70 | FORMAT = 0x4001, |
| 71 | MAIN_BUFFER = 0x4002, |
| 72 | AUX_BUFFER = 0x4003, |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 73 | DOWNMIX_TYPE = 0X4004, |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 74 | MIXER_FORMAT = 0x4005, // AUDIO_FORMAT_PCM_(FLOAT|16_BIT) |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 75 | MIXER_CHANNEL_MASK = 0x4006, // Channel mask for mixer output |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 76 | // for target RESAMPLE |
Glenn Kasten | 4e2293f | 2012-04-12 09:39:07 -0700 | [diff] [blame] | 77 | SAMPLE_RATE = 0x4100, // Configure sample rate conversion on this track name; |
| 78 | // parameter 'value' is the new sample rate in Hz. |
| 79 | // Only creates a sample rate converter the first time that |
| 80 | // the track sample rate is different from the mix sample rate. |
| 81 | // If the new sample rate is the same as the mix sample rate, |
| 82 | // and a sample rate converter already exists, |
| 83 | // then the sample rate converter remains present but is a no-op. |
| 84 | RESET = 0x4101, // Reset sample rate converter without changing sample rate. |
| 85 | // This clears out the resampler's input buffer. |
| 86 | REMOVE = 0x4102, // Remove the sample rate converter on this track name; |
| 87 | // the track is restored to the mix sample rate. |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 88 | // for target RAMP_VOLUME and VOLUME (8 channels max) |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 89 | // FIXME use float for these 3 to improve the dynamic range |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 90 | VOLUME0 = 0x4200, |
| 91 | VOLUME1 = 0x4201, |
| 92 | AUXLEVEL = 0x4210, |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 93 | // for target TIMESTRETCH |
| 94 | PLAYBACK_RATE = 0x4300, // Configure timestretch on this track name; |
| 95 | // parameter 'value' is a pointer to the new playback rate. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 98 | AudioMixer(size_t frameCount, uint32_t sampleRate) |
| 99 | : mSampleRate(sampleRate) |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 100 | , mFrameCount(frameCount) { |
| 101 | pthread_once(&sOnceControl, &sInitRoutine); |
| 102 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 103 | |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 104 | // Create a new track in the mixer. |
| 105 | // |
| 106 | // \param name a unique user-provided integer associated with the track. |
| 107 | // If name already exists, the function will abort. |
| 108 | // \param channelMask output channel mask. |
| 109 | // \param format PCM format |
| 110 | // \param sessionId Session id for the track. Tracks with the same |
| 111 | // session id will be submixed together. |
| 112 | // |
| 113 | // \return OK on success. |
| 114 | // BAD_VALUE if the format does not satisfy isValidFormat() |
| 115 | // or the channelMask does not satisfy isValidChannelMask(). |
| 116 | status_t create( |
| 117 | int name, audio_channel_mask_t channelMask, audio_format_t format, int sessionId); |
Glenn Kasten | 17a736c | 2012-02-14 08:52:15 -0800 | [diff] [blame] | 118 | |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 119 | bool exists(int name) const { |
| 120 | return mTracks.count(name) > 0; |
| 121 | } |
Glenn Kasten | 17a736c | 2012-02-14 08:52:15 -0800 | [diff] [blame] | 122 | |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 123 | // Free an allocated track by name. |
| 124 | void destroy(int name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 125 | |
Glenn Kasten | 17a736c | 2012-02-14 08:52:15 -0800 | [diff] [blame] | 126 | // Enable or disable an allocated track by name |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 127 | void enable(int name); |
| 128 | void disable(int name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 129 | |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 130 | void setParameter(int name, int target, int param, void *value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 131 | |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 132 | void setBufferProvider(int name, AudioBufferProvider* bufferProvider); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 133 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 134 | void process() { |
| 135 | (this->*mHook)(); |
| 136 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 137 | |
Glenn Kasten | c59c004 | 2012-02-02 14:06:11 -0800 | [diff] [blame] | 138 | size_t getUnreleasedFrames(int name) const; |
Eric Laurent | 071ccd5 | 2011-12-22 16:08:41 -0800 | [diff] [blame] | 139 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 140 | std::string trackNames() const { |
| 141 | std::stringstream ss; |
| 142 | for (const auto &pair : mTracks) { |
| 143 | ss << pair.first << " "; |
Andy Hung | abdb990 | 2015-01-12 15:08:22 -0800 | [diff] [blame] | 144 | } |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 145 | return ss.str(); |
| 146 | } |
| 147 | |
| 148 | void setNBLogWriter(NBLog::Writer *logWriter) { |
| 149 | mNBLogWriter = logWriter; |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 152 | static inline bool isValidFormat(audio_format_t format) { |
| 153 | switch (format) { |
| 154 | case AUDIO_FORMAT_PCM_8_BIT: |
| 155 | case AUDIO_FORMAT_PCM_16_BIT: |
| 156 | case AUDIO_FORMAT_PCM_24_BIT_PACKED: |
| 157 | case AUDIO_FORMAT_PCM_32_BIT: |
| 158 | case AUDIO_FORMAT_PCM_FLOAT: |
| 159 | return true; |
| 160 | default: |
| 161 | return false; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | static inline bool isValidChannelMask(audio_channel_mask_t channelMask) { |
| 166 | return audio_channel_mask_is_valid(channelMask); // the RemixBufferProvider is flexible. |
| 167 | } |
| 168 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 169 | private: |
| 170 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 171 | /* For multi-format functions (calls template functions |
| 172 | * in AudioMixerOps.h). The template parameters are as follows: |
| 173 | * |
| 174 | * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration) |
| 175 | * USEFLOATVOL (set to true if float volume is used) |
| 176 | * ADJUSTVOL (set to true if volume ramp parameters needs adjustment afterwards) |
| 177 | * TO: int32_t (Q4.27) or float |
| 178 | * TI: int32_t (Q4.27) or int16_t (Q0.15) or float |
| 179 | * TA: int32_t (Q4.27) |
| 180 | */ |
| 181 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 182 | enum { |
Glenn Kasten | d6fadf0 | 2013-10-30 14:37:29 -0700 | [diff] [blame] | 183 | // FIXME this representation permits up to 8 channels |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 184 | NEEDS_CHANNEL_COUNT__MASK = 0x00000007, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | enum { |
Glenn Kasten | d6fadf0 | 2013-10-30 14:37:29 -0700 | [diff] [blame] | 188 | NEEDS_CHANNEL_1 = 0x00000000, // mono |
| 189 | NEEDS_CHANNEL_2 = 0x00000001, // stereo |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 190 | |
Glenn Kasten | d6fadf0 | 2013-10-30 14:37:29 -0700 | [diff] [blame] | 191 | // sample format is not explicitly specified, and is assumed to be AUDIO_FORMAT_PCM_16_BIT |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 192 | |
Glenn Kasten | d6fadf0 | 2013-10-30 14:37:29 -0700 | [diff] [blame] | 193 | NEEDS_MUTE = 0x00000100, |
| 194 | NEEDS_RESAMPLE = 0x00001000, |
| 195 | NEEDS_AUX = 0x00010000, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 196 | }; |
| 197 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 198 | // hook types |
| 199 | enum { |
| 200 | PROCESSTYPE_NORESAMPLEONETRACK, // others set elsewhere |
| 201 | }; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 202 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 203 | enum { |
| 204 | TRACKTYPE_NOP, |
| 205 | TRACKTYPE_RESAMPLE, |
| 206 | TRACKTYPE_NORESAMPLE, |
| 207 | TRACKTYPE_NORESAMPLEMONO, |
| 208 | }; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 209 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 210 | // process hook functionality |
| 211 | using process_hook_t = void(AudioMixer::*)(); |
| 212 | |
| 213 | struct Track; |
| 214 | using hook_t = void(Track::*)(int32_t* output, size_t numOutFrames, int32_t* temp, int32_t* aux); |
| 215 | |
| 216 | struct Track { |
| 217 | Track() |
| 218 | : bufferProvider(nullptr) |
| 219 | { |
| 220 | // TODO: move additional initialization here. |
| 221 | } |
| 222 | |
| 223 | ~Track() |
| 224 | { |
| 225 | // bufferProvider, mInputBufferProvider need not be deleted. |
| 226 | mResampler.reset(nullptr); |
| 227 | // Ensure the order of destruction of buffer providers as they |
| 228 | // release the upstream provider in the destructor. |
| 229 | mTimestretchBufferProvider.reset(nullptr); |
| 230 | mPostDownmixReformatBufferProvider.reset(nullptr); |
| 231 | mDownmixerBufferProvider.reset(nullptr); |
| 232 | mReformatBufferProvider.reset(nullptr); |
| 233 | } |
| 234 | |
| 235 | bool needsRamp() { return (volumeInc[0] | volumeInc[1] | auxInc) != 0; } |
| 236 | bool setResampler(uint32_t trackSampleRate, uint32_t devSampleRate); |
| 237 | bool doesResample() const { return mResampler.get() != nullptr; } |
| 238 | void resetResampler() { if (mResampler.get() != nullptr) mResampler->reset(); } |
| 239 | void adjustVolumeRamp(bool aux, bool useFloat = false); |
| 240 | size_t getUnreleasedFrames() const { return mResampler.get() != nullptr ? |
| 241 | mResampler->getUnreleasedFrames() : 0; }; |
| 242 | |
| 243 | status_t prepareForDownmix(); |
| 244 | void unprepareForDownmix(); |
| 245 | status_t prepareForReformat(); |
| 246 | void unprepareForReformat(); |
| 247 | bool setPlaybackRate(const AudioPlaybackRate &playbackRate); |
| 248 | void reconfigureBufferProviders(); |
| 249 | |
| 250 | static hook_t getTrackHook(int trackType, uint32_t channelCount, |
| 251 | audio_format_t mixerInFormat, audio_format_t mixerOutFormat); |
| 252 | |
| 253 | void track__nop(int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux); |
| 254 | |
| 255 | template <int MIXTYPE, bool USEFLOATVOL, bool ADJUSTVOL, |
| 256 | typename TO, typename TI, typename TA> |
| 257 | void volumeMix(TO *out, size_t outFrames, const TI *in, TA *aux, bool ramp); |
| 258 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 259 | uint32_t needs; |
| 260 | |
Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 261 | // TODO: Eventually remove legacy integer volume settings |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 262 | union { |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 263 | int16_t volume[MAX_NUM_VOLUMES]; // U4.12 fixed point (top bit should be zero) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 264 | int32_t volumeRL; |
| 265 | }; |
| 266 | |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 267 | int32_t prevVolume[MAX_NUM_VOLUMES]; |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 268 | int32_t volumeInc[MAX_NUM_VOLUMES]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 269 | int32_t auxInc; |
| 270 | int32_t prevAuxLevel; |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 271 | int16_t auxLevel; // 0 <= auxLevel <= MAX_GAIN_INT, but signed for mul performance |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 272 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 273 | uint16_t frameCount; |
| 274 | |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 275 | uint8_t channelCount; // 1 or 2, redundant with (needs & NEEDS_CHANNEL_COUNT__MASK) |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 276 | uint8_t unused_padding; // formerly format, was always 16 |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 277 | uint16_t enabled; // actually bool |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 278 | audio_channel_mask_t channelMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 279 | |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 280 | // actual buffer provider used by the track hooks, see DownmixerBufferProvider below |
| 281 | // for how the Track buffer provider is wrapped by another one when dowmixing is required |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 282 | AudioBufferProvider* bufferProvider; |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 283 | |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 284 | mutable AudioBufferProvider::Buffer buffer; // 8 bytes |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 285 | |
| 286 | hook_t hook; |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 287 | const void *mIn; // current location in buffer |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 288 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 289 | std::unique_ptr<AudioResampler> mResampler; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 290 | uint32_t sampleRate; |
| 291 | int32_t* mainBuffer; |
| 292 | int32_t* auxBuffer; |
| 293 | |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 294 | /* Buffer providers are constructed to translate the track input data as needed. |
| 295 | * |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 296 | * TODO: perhaps make a single PlaybackConverterProvider class to move |
| 297 | * all pre-mixer track buffer conversions outside the AudioMixer class. |
| 298 | * |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 299 | * 1) mInputBufferProvider: The AudioTrack buffer provider. |
| 300 | * 2) mReformatBufferProvider: If not NULL, performs the audio reformat to |
| 301 | * match either mMixerInFormat or mDownmixRequiresFormat, if the downmixer |
| 302 | * requires reformat. For example, it may convert floating point input to |
| 303 | * PCM_16_bit if that's required by the downmixer. |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 304 | * 3) mDownmixerBufferProvider: If not NULL, performs the channel remixing to match |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 305 | * the number of channels required by the mixer sink. |
| 306 | * 4) mPostDownmixReformatBufferProvider: If not NULL, performs reformatting from |
| 307 | * the downmixer requirements to the mixer engine input requirements. |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 308 | * 5) mTimestretchBufferProvider: Adds timestretching for playback rate |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 309 | */ |
Andy Hung | 1b2fdcb | 2014-07-16 17:44:34 -0700 | [diff] [blame] | 310 | AudioBufferProvider* mInputBufferProvider; // externally provided buffer provider. |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 311 | std::unique_ptr<PassthruBufferProvider> mReformatBufferProvider; |
| 312 | std::unique_ptr<PassthruBufferProvider> mDownmixerBufferProvider; |
| 313 | std::unique_ptr<PassthruBufferProvider> mPostDownmixReformatBufferProvider; |
| 314 | std::unique_ptr<PassthruBufferProvider> mTimestretchBufferProvider; |
Jean-Michel Trivi | d06e132 | 2012-09-12 15:47:07 -0700 | [diff] [blame] | 315 | |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 316 | int32_t sessionId; |
| 317 | |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 318 | audio_format_t mMixerFormat; // output mix format: AUDIO_FORMAT_PCM_(FLOAT|16_BIT) |
| 319 | audio_format_t mFormat; // input track format |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 320 | audio_format_t mMixerInFormat; // mix internal format AUDIO_FORMAT_PCM_(FLOAT|16_BIT) |
| 321 | // each track must be converted to this format. |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 322 | audio_format_t mDownmixRequiresFormat; // required downmixer format |
| 323 | // AUDIO_FORMAT_PCM_16_BIT if 16 bit necessary |
| 324 | // AUDIO_FORMAT_INVALID if no required format |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 325 | |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 326 | float mVolume[MAX_NUM_VOLUMES]; // floating point set volume |
| 327 | float mPrevVolume[MAX_NUM_VOLUMES]; // floating point previous volume |
| 328 | float mVolumeInc[MAX_NUM_VOLUMES]; // floating point volume increment |
Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 329 | |
| 330 | float mAuxLevel; // floating point set aux level |
| 331 | float mPrevAuxLevel; // floating point prev aux level |
| 332 | float mAuxInc; // floating point aux increment |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 333 | |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 334 | audio_channel_mask_t mMixerChannelMask; |
| 335 | uint32_t mMixerChannelCount; |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 336 | |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 337 | AudioPlaybackRate mPlaybackRate; |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 338 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 339 | private: |
| 340 | // hooks |
| 341 | void track__genericResample(int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux); |
| 342 | void track__16BitsStereo(int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux); |
| 343 | void track__16BitsMono(int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux); |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 344 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 345 | void volumeRampStereo(int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux); |
| 346 | void volumeStereo(int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux); |
| 347 | |
| 348 | // multi-format track hooks |
| 349 | template <int MIXTYPE, typename TO, typename TI, typename TA> |
| 350 | void track__Resample(TO* out, size_t frameCount, TO* temp __unused, TA* aux); |
| 351 | template <int MIXTYPE, typename TO, typename TI, typename TA> |
| 352 | void track__NoResample(TO* out, size_t frameCount, TO* temp __unused, TA* aux); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 353 | }; |
| 354 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 355 | // TODO: remove BLOCKSIZE unit of processing - it isn't needed anymore. |
| 356 | static constexpr int BLOCKSIZE = 16; |
Glenn Kasten | 4e2293f | 2012-04-12 09:39:07 -0700 | [diff] [blame] | 357 | |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 358 | bool setChannelMasks(int name, |
| 359 | audio_channel_mask_t trackChannelMask, audio_channel_mask_t mixerChannelMask); |
| 360 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 361 | // Called when track info changes and a new process hook should be determined. |
| 362 | void invalidate() { |
| 363 | mHook = &AudioMixer::process__validate; |
| 364 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 365 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 366 | void process__validate(); |
| 367 | void process__nop(); |
| 368 | void process__genericNoResampling(); |
| 369 | void process__genericResampling(); |
| 370 | void process__oneTrack16BitsStereoNoResampling(); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 371 | |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 372 | template <int MIXTYPE, typename TO, typename TI, typename TA> |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 373 | void process__noResampleOneTrack(); |
Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 374 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 375 | static process_hook_t getProcessHook(int processType, uint32_t channelCount, |
| 376 | audio_format_t mixerInFormat, audio_format_t mixerOutFormat); |
Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 377 | |
| 378 | static void convertMixerFormat(void *out, audio_format_t mixerOutFormat, |
| 379 | void *in, audio_format_t mixerInFormat, size_t sampleCount); |
| 380 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 381 | static void sInitRoutine(); |
| 382 | |
| 383 | // initialization constants |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 384 | const uint32_t mSampleRate; |
| 385 | const size_t mFrameCount; |
| 386 | |
| 387 | NBLog::Writer *mNBLogWriter = nullptr; // associated NBLog::Writer |
| 388 | |
| 389 | process_hook_t mHook = &AudioMixer::process__nop; // one of process__*, never nullptr |
| 390 | |
| 391 | // the size of the type (int32_t) should be the largest of all types supported |
| 392 | // by the mixer. |
| 393 | std::unique_ptr<int32_t[]> mOutputTemp; |
| 394 | std::unique_ptr<int32_t[]> mResampleTemp; |
| 395 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 396 | // track names grouped by main buffer, in no particular order of main buffer. |
| 397 | // however names for a particular main buffer are in order (by construction). |
| 398 | std::unordered_map<void * /* mainBuffer */, std::vector<int /* name */>> mGroups; |
| 399 | |
| 400 | // track names that are enabled, in increasing order (by construction). |
| 401 | std::vector<int /* name */> mEnabled; |
| 402 | |
| 403 | // track smart pointers, by name, in increasing order of name. |
| 404 | std::map<int /* name */, std::shared_ptr<Track>> mTracks; |
| 405 | |
| 406 | static pthread_once_t sOnceControl; // initialized in constructor by first new |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 407 | }; |
| 408 | |
| 409 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 410 | } // namespace android |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 411 | |
| 412 | #endif // ANDROID_AUDIO_MIXER_H |