blob: f442671c82581e5f506ac7ed150bfde2efe3e58a [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:
34 AudioMixer(size_t frameCount, uint32_t sampleRate);
35
Glenn Kastenc19e2242012-01-30 14:54:39 -080036 /*virtual*/ ~AudioMixer(); // non-virtual saves a v-table, restore if sub-classed
Mathias Agopian65ab4712010-07-14 17:59:35 -070037
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 Kasten9c56d4a2011-12-19 15:06:39 -080045 // track names (MAX_NUM_TRACKS units)
Mathias Agopian65ab4712010-07-14 17:59:35 -070046 TRACK0 = 0x1000,
47
Glenn Kasten1c48c3c2011-12-15 14:54:01 -080048 // 0x2000 is unused
Mathias Agopian65ab4712010-07-14 17:59:35 -070049
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 Trivi0d255b22011-05-24 15:53:33 -070058 CHANNEL_MASK = 0x4000,
Mathias Agopian65ab4712010-07-14 17:59:35 -070059 FORMAT = 0x4001,
60 MAIN_BUFFER = 0x4002,
61 AUX_BUFFER = 0x4003,
Glenn Kasten362c4e62011-12-14 10:28:06 -080062 // for target RESAMPLE
Mathias Agopian65ab4712010-07-14 17:59:35 -070063 SAMPLE_RATE = 0x4100,
Eric Laurent243f5f92011-02-28 16:52:51 -080064 RESET = 0x4101,
Glenn Kasten362c4e62011-12-14 10:28:06 -080065 // for target RAMP_VOLUME and VOLUME (8 channels max)
Mathias Agopian65ab4712010-07-14 17:59:35 -070066 VOLUME0 = 0x4200,
67 VOLUME1 = 0x4201,
68 AUXLEVEL = 0x4210,
69 };
70
71
Glenn Kasten9c56d4a2011-12-19 15:06:39 -080072 // For all APIs with "name": TRACK0 <= name < TRACK0 + MAX_NUM_TRACKS
Mathias Agopian65ab4712010-07-14 17:59:35 -070073 int getTrackName();
74 void deleteTrackName(int name);
75
Glenn Kasten9c56d4a2011-12-19 15:06:39 -080076 void enable(int name);
77 void disable(int name);
Mathias Agopian65ab4712010-07-14 17:59:35 -070078
Glenn Kasten9c56d4a2011-12-19 15:06:39 -080079 void setParameter(int name, int target, int param, void *value);
Mathias Agopian65ab4712010-07-14 17:59:35 -070080
Glenn Kasten9c56d4a2011-12-19 15:06:39 -080081 void setBufferProvider(int name, AudioBufferProvider* bufferProvider);
John Grossman4ff14ba2012-02-08 16:37:41 -080082 void process(int64_t pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -070083
84 uint32_t trackNames() const { return mTrackNames; }
85
Glenn Kastenc59c0042012-02-02 14:06:11 -080086 size_t getUnreleasedFrames(int name) const;
Eric Laurent071ccd52011-12-22 16:08:41 -080087
Mathias Agopian65ab4712010-07-14 17:59:35 -070088private:
89
90 enum {
91 NEEDS_CHANNEL_COUNT__MASK = 0x00000003,
92 NEEDS_FORMAT__MASK = 0x000000F0,
93 NEEDS_MUTE__MASK = 0x00000100,
94 NEEDS_RESAMPLE__MASK = 0x00001000,
95 NEEDS_AUX__MASK = 0x00010000,
96 };
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,
109
110 NEEDS_AUX_DISABLED = 0x00000000,
111 NEEDS_AUX_ENABLED = 0x00010000,
112 };
113
Mathias Agopian65ab4712010-07-14 17:59:35 -0700114 struct state_t;
115 struct track_t;
116
John Grossman4ff14ba2012-02-08 16:37:41 -0800117 typedef void (*mix_t)(state_t* state, int64_t pts);
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 Kastenbf71f1e2011-12-13 11:52:35 -0800131 int32_t volumeInc[MAX_NUM_CHANNELS];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700132 int32_t auxLevel;
133 int32_t auxInc;
134 int32_t prevAuxLevel;
135
136 uint16_t frameCount;
137
138 uint8_t channelCount : 4;
139 uint8_t enabled : 1;
140 uint8_t reserved0 : 3;
141 uint8_t format;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700142 uint32_t channelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700143
144 AudioBufferProvider* bufferProvider;
145 mutable AudioBufferProvider::Buffer buffer;
146
147 hook_t hook;
Glenn Kasten54c3b662012-01-06 07:46:30 -0800148 const void* in; // current location in buffer
Mathias Agopian65ab4712010-07-14 17:59:35 -0700149
150 AudioResampler* resampler;
151 uint32_t sampleRate;
152 int32_t* mainBuffer;
153 int32_t* auxBuffer;
154
John Grossman4ff14ba2012-02-08 16:37:41 -0800155 uint64_t localTimeFreq;
156
Mathias Agopian65ab4712010-07-14 17:59:35 -0700157 bool setResampler(uint32_t sampleRate, uint32_t devSampleRate);
Glenn Kastenc59c0042012-02-02 14:06:11 -0800158 bool doesResample() const { return resampler != NULL; }
159 void resetResampler() { if (resampler != NULL) resampler->reset(); }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700160 void adjustVolumeRamp(bool aux);
Glenn Kastenc59c0042012-02-02 14:06:11 -0800161 size_t getUnreleasedFrames() const { return resampler != NULL ?
162 resampler->getUnreleasedFrames() : 0; };
Mathias Agopian65ab4712010-07-14 17:59:35 -0700163 };
164
165 // pad to 32-bytes to fill cache line
166 struct state_t {
167 uint32_t enabledTracks;
168 uint32_t needsChanged;
169 size_t frameCount;
170 mix_t hook;
171 int32_t *outputTemp;
172 int32_t *resampleTemp;
173 int32_t reserved[2];
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800174 track_t tracks[MAX_NUM_TRACKS]; __attribute__((aligned(32)));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700175 };
176
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800177 // bitmask of allocated track names, where bit 0 corresponds to TRACK0 etc.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700178 uint32_t mTrackNames;
179 const uint32_t mSampleRate;
180
181 state_t mState __attribute__((aligned(32)));
182
183 void invalidateState(uint32_t mask);
184
185 static void track__genericResample(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
186 static void track__nop(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
187 static void track__16BitsStereo(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
188 static void track__16BitsMono(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
189 static void volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux);
190 static void volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux);
191
John Grossman4ff14ba2012-02-08 16:37:41 -0800192 static void process__validate(state_t* state, int64_t pts);
193 static void process__nop(state_t* state, int64_t pts);
194 static void process__genericNoResampling(state_t* state, int64_t pts);
195 static void process__genericResampling(state_t* state, int64_t pts);
196 static void process__OneTrack16BitsStereoNoResampling(state_t* state,
197 int64_t pts);
Glenn Kasten81a028f2011-12-15 09:53:12 -0800198#if 0
John Grossman4ff14ba2012-02-08 16:37:41 -0800199 static void process__TwoTracks16BitsStereoNoResampling(state_t* state,
200 int64_t pts);
Glenn Kasten81a028f2011-12-15 09:53:12 -0800201#endif
John Grossman4ff14ba2012-02-08 16:37:41 -0800202
203 static int64_t calculateOutputPTS(const track_t& t, int64_t basePTS,
204 int outputFrameIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700205};
206
207// ----------------------------------------------------------------------------
208}; // namespace android
209
210#endif // ANDROID_AUDIO_MIXER_H