blob: 7a614678b43fb4befa156edf92c8fded84b151e2 [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
Mathias Agopian65ab4712010-07-14 17:59:35 -07002**
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
Mathias Agopian65ab4712010-07-14 17:59:35 -070031class AudioMixer
32{
33public:
Glenn Kasten5c94b6c2012-03-20 17:01:29 -070034 AudioMixer(size_t frameCount, uint32_t sampleRate,
35 uint32_t maxNumTracks = MAX_NUM_TRACKS);
Mathias Agopian65ab4712010-07-14 17:59:35 -070036
Glenn Kastenc19e2242012-01-30 14:54:39 -080037 /*virtual*/ ~AudioMixer(); // non-virtual saves a v-table, restore if sub-classed
Mathias Agopian65ab4712010-07-14 17:59:35 -070038
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 Kasten9c56d4a2011-12-19 15:06:39 -080046 // track names (MAX_NUM_TRACKS units)
Mathias Agopian65ab4712010-07-14 17:59:35 -070047 TRACK0 = 0x1000,
48
Glenn Kasten1c48c3c2011-12-15 14:54:01 -080049 // 0x2000 is unused
Mathias Agopian65ab4712010-07-14 17:59:35 -070050
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 Trivi0d255b22011-05-24 15:53:33 -070059 CHANNEL_MASK = 0x4000,
Mathias Agopian65ab4712010-07-14 17:59:35 -070060 FORMAT = 0x4001,
61 MAIN_BUFFER = 0x4002,
62 AUX_BUFFER = 0x4003,
Glenn Kasten362c4e62011-12-14 10:28:06 -080063 // for target RESAMPLE
Mathias Agopian65ab4712010-07-14 17:59:35 -070064 SAMPLE_RATE = 0x4100,
Eric Laurent243f5f92011-02-28 16:52:51 -080065 RESET = 0x4101,
Glenn Kasten362c4e62011-12-14 10:28:06 -080066 // for target RAMP_VOLUME and VOLUME (8 channels max)
Mathias Agopian65ab4712010-07-14 17:59:35 -070067 VOLUME0 = 0x4200,
68 VOLUME1 = 0x4201,
69 AUXLEVEL = 0x4210,
70 };
71
72
Glenn Kasten9c56d4a2011-12-19 15:06:39 -080073 // For all APIs with "name": TRACK0 <= name < TRACK0 + MAX_NUM_TRACKS
Mathias Agopian65ab4712010-07-14 17:59:35 -070074 int getTrackName();
75 void deleteTrackName(int name);
76
Glenn Kasten9c56d4a2011-12-19 15:06:39 -080077 void enable(int name);
78 void disable(int name);
Mathias Agopian65ab4712010-07-14 17:59:35 -070079
Glenn Kasten9c56d4a2011-12-19 15:06:39 -080080 void setParameter(int name, int target, int param, void *value);
Mathias Agopian65ab4712010-07-14 17:59:35 -070081
Glenn Kasten9c56d4a2011-12-19 15:06:39 -080082 void setBufferProvider(int name, AudioBufferProvider* bufferProvider);
John Grossman4ff14ba2012-02-08 16:37:41 -080083 void process(int64_t pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -070084
85 uint32_t trackNames() const { return mTrackNames; }
86
Glenn Kastenc59c0042012-02-02 14:06:11 -080087 size_t getUnreleasedFrames(int name) const;
Eric Laurent071ccd52011-12-22 16:08:41 -080088
Mathias Agopian65ab4712010-07-14 17:59:35 -070089private:
90
91 enum {
92 NEEDS_CHANNEL_COUNT__MASK = 0x00000003,
93 NEEDS_FORMAT__MASK = 0x000000F0,
94 NEEDS_MUTE__MASK = 0x00000100,
95 NEEDS_RESAMPLE__MASK = 0x00001000,
96 NEEDS_AUX__MASK = 0x00010000,
97 };
98
99 enum {
100 NEEDS_CHANNEL_1 = 0x00000000,
101 NEEDS_CHANNEL_2 = 0x00000001,
102
103 NEEDS_FORMAT_16 = 0x00000010,
104
105 NEEDS_MUTE_DISABLED = 0x00000000,
106 NEEDS_MUTE_ENABLED = 0x00000100,
107
108 NEEDS_RESAMPLE_DISABLED = 0x00000000,
109 NEEDS_RESAMPLE_ENABLED = 0x00001000,
110
111 NEEDS_AUX_DISABLED = 0x00000000,
112 NEEDS_AUX_ENABLED = 0x00010000,
113 };
114
Mathias Agopian65ab4712010-07-14 17:59:35 -0700115 struct state_t;
116 struct track_t;
117
Mathias Agopian65ab4712010-07-14 17:59:35 -0700118 typedef void (*hook_t)(track_t* t, int32_t* output, size_t numOutFrames, int32_t* temp, int32_t* aux);
119 static const int BLOCKSIZE = 16; // 4 cache lines
120
121 struct track_t {
122 uint32_t needs;
123
124 union {
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800125 int16_t volume[MAX_NUM_CHANNELS]; // [0]3.12 fixed point
Mathias Agopian65ab4712010-07-14 17:59:35 -0700126 int32_t volumeRL;
127 };
128
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800129 int32_t prevVolume[MAX_NUM_CHANNELS];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700130
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800131 // 16-byte boundary
132
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800133 int32_t volumeInc[MAX_NUM_CHANNELS];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700134 int32_t auxInc;
135 int32_t prevAuxLevel;
136
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800137 // 16-byte boundary
138
139 int16_t auxLevel; // 0 <= auxLevel <= MAX_GAIN_INT, but signed for mul performance
Mathias Agopian65ab4712010-07-14 17:59:35 -0700140 uint16_t frameCount;
141
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800142 uint8_t channelCount; // 1 or 2, redundant with (needs & NEEDS_CHANNEL_COUNT__MASK)
143 uint8_t format; // always 16
144 uint16_t enabled; // actually bool
145 uint32_t channelMask; // currently under-used
Mathias Agopian65ab4712010-07-14 17:59:35 -0700146
147 AudioBufferProvider* bufferProvider;
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800148
149 // 16-byte boundary
150
151 mutable AudioBufferProvider::Buffer buffer; // 8 bytes
Mathias Agopian65ab4712010-07-14 17:59:35 -0700152
153 hook_t hook;
Glenn Kasten54c3b662012-01-06 07:46:30 -0800154 const void* in; // current location in buffer
Mathias Agopian65ab4712010-07-14 17:59:35 -0700155
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800156 // 16-byte boundary
157
Mathias Agopian65ab4712010-07-14 17:59:35 -0700158 AudioResampler* resampler;
159 uint32_t sampleRate;
160 int32_t* mainBuffer;
161 int32_t* auxBuffer;
162
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800163 // 16-byte boundary
164
John Grossman4ff14ba2012-02-08 16:37:41 -0800165 uint64_t localTimeFreq;
166
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800167 int64_t padding;
168
169 // 16-byte boundary
170
Mathias Agopian65ab4712010-07-14 17:59:35 -0700171 bool setResampler(uint32_t sampleRate, uint32_t devSampleRate);
Glenn Kastenc59c0042012-02-02 14:06:11 -0800172 bool doesResample() const { return resampler != NULL; }
173 void resetResampler() { if (resampler != NULL) resampler->reset(); }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700174 void adjustVolumeRamp(bool aux);
Glenn Kastenc59c0042012-02-02 14:06:11 -0800175 size_t getUnreleasedFrames() const { return resampler != NULL ?
176 resampler->getUnreleasedFrames() : 0; };
Mathias Agopian65ab4712010-07-14 17:59:35 -0700177 };
178
179 // pad to 32-bytes to fill cache line
180 struct state_t {
181 uint32_t enabledTracks;
182 uint32_t needsChanged;
183 size_t frameCount;
Glenn Kastena1117922012-01-26 10:53:32 -0800184 void (*hook)(state_t* state, int64_t pts); // one of process__*, never NULL
Mathias Agopian65ab4712010-07-14 17:59:35 -0700185 int32_t *outputTemp;
186 int32_t *resampleTemp;
187 int32_t reserved[2];
Glenn Kasten5c94b6c2012-03-20 17:01:29 -0700188 // FIXME allocate dynamically to save some memory when maxNumTracks < MAX_NUM_TRACKS
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800189 track_t tracks[MAX_NUM_TRACKS]; __attribute__((aligned(32)));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700190 };
191
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800192 // bitmask of allocated track names, where bit 0 corresponds to TRACK0 etc.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700193 uint32_t mTrackNames;
Glenn Kasten5c94b6c2012-03-20 17:01:29 -0700194
195 // bitmask of configured track names; ~0 if maxNumTracks == MAX_NUM_TRACKS,
196 // but will have fewer bits set if maxNumTracks < MAX_NUM_TRACKS
197 const uint32_t mConfiguredNames;
198
Mathias Agopian65ab4712010-07-14 17:59:35 -0700199 const uint32_t mSampleRate;
200
201 state_t mState __attribute__((aligned(32)));
202
203 void invalidateState(uint32_t mask);
204
205 static void track__genericResample(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
206 static void track__nop(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
207 static void track__16BitsStereo(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
208 static void track__16BitsMono(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
209 static void volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux);
210 static void volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux);
211
John Grossman4ff14ba2012-02-08 16:37:41 -0800212 static void process__validate(state_t* state, int64_t pts);
213 static void process__nop(state_t* state, int64_t pts);
214 static void process__genericNoResampling(state_t* state, int64_t pts);
215 static void process__genericResampling(state_t* state, int64_t pts);
216 static void process__OneTrack16BitsStereoNoResampling(state_t* state,
217 int64_t pts);
Glenn Kasten81a028f2011-12-15 09:53:12 -0800218#if 0
John Grossman4ff14ba2012-02-08 16:37:41 -0800219 static void process__TwoTracks16BitsStereoNoResampling(state_t* state,
220 int64_t pts);
Glenn Kasten81a028f2011-12-15 09:53:12 -0800221#endif
John Grossman4ff14ba2012-02-08 16:37:41 -0800222
223 static int64_t calculateOutputPTS(const track_t& t, int64_t basePTS,
224 int outputFrameIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700225};
226
227// ----------------------------------------------------------------------------
228}; // namespace android
229
230#endif // ANDROID_AUDIO_MIXER_H