blob: 45365be4e494daff13dcbc5c000f79a271c49b33 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/* //device/include/server/AudioFlinger/AudioMixer.h
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
27namespace android {
28
29// ----------------------------------------------------------------------------
30
31#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
32#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
33
34// ----------------------------------------------------------------------------
35
36class AudioMixer
37{
38public:
39 AudioMixer(size_t frameCount, uint32_t sampleRate);
40
41 ~AudioMixer();
42
43 static const uint32_t MAX_NUM_TRACKS = 32;
44 static const uint32_t MAX_NUM_CHANNELS = 2;
45
46 static const uint16_t UNITY_GAIN = 0x1000;
47
48 enum { // names
49
Glenn Kasten9c56d4a2011-12-19 15:06:39 -080050 // track names (MAX_NUM_TRACKS units)
Mathias Agopian65ab4712010-07-14 17:59:35 -070051 TRACK0 = 0x1000,
52
Glenn Kasten1c48c3c2011-12-15 14:54:01 -080053 // 0x2000 is unused
Mathias Agopian65ab4712010-07-14 17:59:35 -070054
55 // setParameter targets
56 TRACK = 0x3000,
57 RESAMPLE = 0x3001,
58 RAMP_VOLUME = 0x3002, // ramp to new volume
59 VOLUME = 0x3003, // don't ramp
60
61 // set Parameter names
62 // for target TRACK
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -070063 CHANNEL_MASK = 0x4000,
Mathias Agopian65ab4712010-07-14 17:59:35 -070064 FORMAT = 0x4001,
65 MAIN_BUFFER = 0x4002,
66 AUX_BUFFER = 0x4003,
Glenn Kasten362c4e62011-12-14 10:28:06 -080067 // for target RESAMPLE
Mathias Agopian65ab4712010-07-14 17:59:35 -070068 SAMPLE_RATE = 0x4100,
Eric Laurent243f5f92011-02-28 16:52:51 -080069 RESET = 0x4101,
Glenn Kasten362c4e62011-12-14 10:28:06 -080070 // for target RAMP_VOLUME and VOLUME (8 channels max)
Mathias Agopian65ab4712010-07-14 17:59:35 -070071 VOLUME0 = 0x4200,
72 VOLUME1 = 0x4201,
73 AUXLEVEL = 0x4210,
74 };
75
76
Glenn Kasten9c56d4a2011-12-19 15:06:39 -080077 // For all APIs with "name": TRACK0 <= name < TRACK0 + MAX_NUM_TRACKS
Mathias Agopian65ab4712010-07-14 17:59:35 -070078 int getTrackName();
79 void deleteTrackName(int name);
80
Glenn Kasten9c56d4a2011-12-19 15:06:39 -080081 void enable(int name);
82 void disable(int name);
Mathias Agopian65ab4712010-07-14 17:59:35 -070083
Glenn Kasten9c56d4a2011-12-19 15:06:39 -080084 void setParameter(int name, int target, int param, void *value);
Mathias Agopian65ab4712010-07-14 17:59:35 -070085
Glenn Kasten9c56d4a2011-12-19 15:06:39 -080086 void setBufferProvider(int name, AudioBufferProvider* bufferProvider);
Mathias Agopian65ab4712010-07-14 17:59:35 -070087 void process();
88
89 uint32_t trackNames() const { return mTrackNames; }
90
Mathias Agopian65ab4712010-07-14 17:59:35 -070091private:
92
93 enum {
94 NEEDS_CHANNEL_COUNT__MASK = 0x00000003,
95 NEEDS_FORMAT__MASK = 0x000000F0,
96 NEEDS_MUTE__MASK = 0x00000100,
97 NEEDS_RESAMPLE__MASK = 0x00001000,
98 NEEDS_AUX__MASK = 0x00010000,
99 };
100
101 enum {
102 NEEDS_CHANNEL_1 = 0x00000000,
103 NEEDS_CHANNEL_2 = 0x00000001,
104
105 NEEDS_FORMAT_16 = 0x00000010,
106
107 NEEDS_MUTE_DISABLED = 0x00000000,
108 NEEDS_MUTE_ENABLED = 0x00000100,
109
110 NEEDS_RESAMPLE_DISABLED = 0x00000000,
111 NEEDS_RESAMPLE_ENABLED = 0x00001000,
112
113 NEEDS_AUX_DISABLED = 0x00000000,
114 NEEDS_AUX_ENABLED = 0x00010000,
115 };
116
117 static inline int32_t applyVolume(int32_t in, int32_t v) {
118 return in * v;
119 }
120
121
122 struct state_t;
123 struct track_t;
124
125 typedef void (*mix_t)(state_t* state);
126 typedef void (*hook_t)(track_t* t, int32_t* output, size_t numOutFrames, int32_t* temp, int32_t* aux);
127 static const int BLOCKSIZE = 16; // 4 cache lines
128
129 struct track_t {
130 uint32_t needs;
131
132 union {
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800133 int16_t volume[MAX_NUM_CHANNELS]; // [0]3.12 fixed point
Mathias Agopian65ab4712010-07-14 17:59:35 -0700134 int32_t volumeRL;
135 };
136
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800137 int32_t prevVolume[MAX_NUM_CHANNELS];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700138
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800139 int32_t volumeInc[MAX_NUM_CHANNELS];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700140 int32_t auxLevel;
141 int32_t auxInc;
142 int32_t prevAuxLevel;
143
144 uint16_t frameCount;
145
146 uint8_t channelCount : 4;
147 uint8_t enabled : 1;
148 uint8_t reserved0 : 3;
149 uint8_t format;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700150 uint32_t channelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700151
152 AudioBufferProvider* bufferProvider;
153 mutable AudioBufferProvider::Buffer buffer;
154
155 hook_t hook;
156 void const* in; // current location in buffer
157
158 AudioResampler* resampler;
159 uint32_t sampleRate;
160 int32_t* mainBuffer;
161 int32_t* auxBuffer;
162
163 bool setResampler(uint32_t sampleRate, uint32_t devSampleRate);
164 bool doesResample() const;
Eric Laurent243f5f92011-02-28 16:52:51 -0800165 void resetResampler();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700166 void adjustVolumeRamp(bool aux);
167 };
168
169 // pad to 32-bytes to fill cache line
170 struct state_t {
171 uint32_t enabledTracks;
172 uint32_t needsChanged;
173 size_t frameCount;
174 mix_t hook;
175 int32_t *outputTemp;
176 int32_t *resampleTemp;
177 int32_t reserved[2];
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800178 track_t tracks[MAX_NUM_TRACKS]; __attribute__((aligned(32)));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700179 };
180
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800181 // bitmask of allocated track names, where bit 0 corresponds to TRACK0 etc.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700182 uint32_t mTrackNames;
183 const uint32_t mSampleRate;
184
185 state_t mState __attribute__((aligned(32)));
186
187 void invalidateState(uint32_t mask);
188
189 static void track__genericResample(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
190 static void track__nop(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
191 static void track__16BitsStereo(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
192 static void track__16BitsMono(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
193 static void volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux);
194 static void volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux);
195
196 static void process__validate(state_t* state);
197 static void process__nop(state_t* state);
198 static void process__genericNoResampling(state_t* state);
199 static void process__genericResampling(state_t* state);
200 static void process__OneTrack16BitsStereoNoResampling(state_t* state);
201 static void process__TwoTracks16BitsStereoNoResampling(state_t* state);
202};
203
204// ----------------------------------------------------------------------------
205}; // namespace android
206
207#endif // ANDROID_AUDIO_MIXER_H