blob: b2102123646ead50291c1a6e038a06f65d3d9598 [file] [log] [blame]
Glenn Kastenb3db2132012-01-19 08:59:58 -08001/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002**
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031class AudioMixer
32{
33public:
34 AudioMixer(size_t frameCount, uint32_t sampleRate);
35
Glenn Kastenb1631382012-01-30 14:54:39 -080036 /*virtual*/ ~AudioMixer(); // non-virtual saves a v-table, restore if sub-classed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
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 Kasten68deb152011-12-19 15:06:39 -080045 // track names (MAX_NUM_TRACKS units)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 TRACK0 = 0x1000,
47
Glenn Kastenaf62dbc2011-12-15 14:54:01 -080048 // 0x2000 is unused
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
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 Trivi54392232011-05-24 15:53:33 -070058 CHANNEL_MASK = 0x4000,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 FORMAT = 0x4001,
Eric Laurent65b65452010-06-01 23:49:17 -070060 MAIN_BUFFER = 0x4002,
61 AUX_BUFFER = 0x4003,
Glenn Kastene5fb2632011-12-14 10:28:06 -080062 // for target RESAMPLE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 SAMPLE_RATE = 0x4100,
Eric Laurent4bb21c42011-02-28 16:52:51 -080064 RESET = 0x4101,
Glenn Kastene5fb2632011-12-14 10:28:06 -080065 // for target RAMP_VOLUME and VOLUME (8 channels max)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 VOLUME0 = 0x4200,
67 VOLUME1 = 0x4201,
Eric Laurent65b65452010-06-01 23:49:17 -070068 AUXLEVEL = 0x4210,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 };
70
71
Glenn Kasten68deb152011-12-19 15:06:39 -080072 // For all APIs with "name": TRACK0 <= name < TRACK0 + MAX_NUM_TRACKS
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 int getTrackName();
74 void deleteTrackName(int name);
75
Glenn Kasten68deb152011-12-19 15:06:39 -080076 void enable(int name);
77 void disable(int name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078
Glenn Kasten68deb152011-12-19 15:06:39 -080079 void setParameter(int name, int target, int param, void *value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
Glenn Kasten68deb152011-12-19 15:06:39 -080081 void setBufferProvider(int name, AudioBufferProvider* bufferProvider);
John Grossmand8cf2962012-02-08 16:37:41 -080082 void process(int64_t pts);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083
84 uint32_t trackNames() const { return mTrackNames; }
85
Glenn Kasteneabd94a2012-02-02 14:06:11 -080086 size_t getUnreleasedFrames(int name) const;
Eric Laurent72dafb22011-12-22 16:08:41 -080087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088private:
89
90 enum {
91 NEEDS_CHANNEL_COUNT__MASK = 0x00000003,
92 NEEDS_FORMAT__MASK = 0x000000F0,
93 NEEDS_MUTE__MASK = 0x00000100,
94 NEEDS_RESAMPLE__MASK = 0x00001000,
Eric Laurent65b65452010-06-01 23:49:17 -070095 NEEDS_AUX__MASK = 0x00010000,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 };
97
98 enum {
99 NEEDS_CHANNEL_1 = 0x00000000,
100 NEEDS_CHANNEL_2 = 0x00000001,
101
102 NEEDS_FORMAT_16 = 0x00000010,
103
104 NEEDS_MUTE_DISABLED = 0x00000000,
105 NEEDS_MUTE_ENABLED = 0x00000100,
106
107 NEEDS_RESAMPLE_DISABLED = 0x00000000,
108 NEEDS_RESAMPLE_ENABLED = 0x00001000,
Eric Laurent65b65452010-06-01 23:49:17 -0700109
110 NEEDS_AUX_DISABLED = 0x00000000,
111 NEEDS_AUX_ENABLED = 0x00010000,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 };
113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 struct state_t;
Eric Laurent65b65452010-06-01 23:49:17 -0700115 struct track_t;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116
Eric Laurent65b65452010-06-01 23:49:17 -0700117 typedef void (*hook_t)(track_t* t, int32_t* output, size_t numOutFrames, int32_t* temp, int32_t* aux);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 static const int BLOCKSIZE = 16; // 4 cache lines
119
120 struct track_t {
121 uint32_t needs;
122
123 union {
Glenn Kasten1f6f05d2011-12-13 11:52:35 -0800124 int16_t volume[MAX_NUM_CHANNELS]; // [0]3.12 fixed point
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 int32_t volumeRL;
126 };
127
Glenn Kasten1f6f05d2011-12-13 11:52:35 -0800128 int32_t prevVolume[MAX_NUM_CHANNELS];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129
Glenn Kasten4f22c052012-01-27 15:26:23 -0800130 // 16-byte boundary
131
Glenn Kasten1f6f05d2011-12-13 11:52:35 -0800132 int32_t volumeInc[MAX_NUM_CHANNELS];
Eric Laurent65b65452010-06-01 23:49:17 -0700133 int32_t auxInc;
134 int32_t prevAuxLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135
Glenn Kasten4f22c052012-01-27 15:26:23 -0800136 // 16-byte boundary
137
138 int16_t auxLevel; // 0 <= auxLevel <= MAX_GAIN_INT, but signed for mul performance
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 uint16_t frameCount;
140
Glenn Kasten4f22c052012-01-27 15:26:23 -0800141 uint8_t channelCount; // 1 or 2, redundant with (needs & NEEDS_CHANNEL_COUNT__MASK)
142 uint8_t format; // always 16
143 uint16_t enabled; // actually bool
144 uint32_t channelMask; // currently under-used
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145
146 AudioBufferProvider* bufferProvider;
Glenn Kasten4f22c052012-01-27 15:26:23 -0800147
148 // 16-byte boundary
149
150 mutable AudioBufferProvider::Buffer buffer; // 8 bytes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151
Eric Laurent65b65452010-06-01 23:49:17 -0700152 hook_t hook;
Glenn Kasten99c2fd32012-01-06 07:46:30 -0800153 const void* in; // current location in buffer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154
Glenn Kasten4f22c052012-01-27 15:26:23 -0800155 // 16-byte boundary
156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 AudioResampler* resampler;
158 uint32_t sampleRate;
Eric Laurent65b65452010-06-01 23:49:17 -0700159 int32_t* mainBuffer;
160 int32_t* auxBuffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161
Glenn Kasten4f22c052012-01-27 15:26:23 -0800162 // 16-byte boundary
163
John Grossmand8cf2962012-02-08 16:37:41 -0800164 uint64_t localTimeFreq;
165
Glenn Kasten4f22c052012-01-27 15:26:23 -0800166 int64_t padding;
167
168 // 16-byte boundary
169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 bool setResampler(uint32_t sampleRate, uint32_t devSampleRate);
Glenn Kasteneabd94a2012-02-02 14:06:11 -0800171 bool doesResample() const { return resampler != NULL; }
172 void resetResampler() { if (resampler != NULL) resampler->reset(); }
Eric Laurent65b65452010-06-01 23:49:17 -0700173 void adjustVolumeRamp(bool aux);
Glenn Kasteneabd94a2012-02-02 14:06:11 -0800174 size_t getUnreleasedFrames() const { return resampler != NULL ?
175 resampler->getUnreleasedFrames() : 0; };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 };
177
178 // pad to 32-bytes to fill cache line
179 struct state_t {
180 uint32_t enabledTracks;
181 uint32_t needsChanged;
182 size_t frameCount;
Glenn Kasten7d3be3a2012-01-26 10:53:32 -0800183 void (*hook)(state_t* state, int64_t pts); // one of process__*, never NULL
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 int32_t *outputTemp;
185 int32_t *resampleTemp;
186 int32_t reserved[2];
Glenn Kasten1f6f05d2011-12-13 11:52:35 -0800187 track_t tracks[MAX_NUM_TRACKS]; __attribute__((aligned(32)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 };
189
Glenn Kasten68deb152011-12-19 15:06:39 -0800190 // bitmask of allocated track names, where bit 0 corresponds to TRACK0 etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 uint32_t mTrackNames;
192 const uint32_t mSampleRate;
193
194 state_t mState __attribute__((aligned(32)));
195
196 void invalidateState(uint32_t mask);
197
Eric Laurent65b65452010-06-01 23:49:17 -0700198 static void track__genericResample(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
199 static void track__nop(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
200 static void track__16BitsStereo(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
201 static void track__16BitsMono(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
202 static void volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux);
203 static void volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204
John Grossmand8cf2962012-02-08 16:37:41 -0800205 static void process__validate(state_t* state, int64_t pts);
206 static void process__nop(state_t* state, int64_t pts);
207 static void process__genericNoResampling(state_t* state, int64_t pts);
208 static void process__genericResampling(state_t* state, int64_t pts);
209 static void process__OneTrack16BitsStereoNoResampling(state_t* state,
210 int64_t pts);
Glenn Kasten31ba2e52011-12-15 09:53:12 -0800211#if 0
John Grossmand8cf2962012-02-08 16:37:41 -0800212 static void process__TwoTracks16BitsStereoNoResampling(state_t* state,
213 int64_t pts);
Glenn Kasten31ba2e52011-12-15 09:53:12 -0800214#endif
John Grossmand8cf2962012-02-08 16:37:41 -0800215
216 static int64_t calculateOutputPTS(const track_t& t, int64_t basePTS,
217 int outputFrameIndex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218};
219
220// ----------------------------------------------------------------------------
221}; // namespace android
222
223#endif // ANDROID_AUDIO_MIXER_H