blob: 0137185d936fac87206df68eb7390f204a0b0475 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/* //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
50 // track units (32 units)
51 TRACK0 = 0x1000,
52
53 // enable/disable
54 MIXING = 0x2000,
55
56 // setParameter targets
57 TRACK = 0x3000,
58 RESAMPLE = 0x3001,
59 RAMP_VOLUME = 0x3002, // ramp to new volume
60 VOLUME = 0x3003, // don't ramp
61
62 // set Parameter names
63 // for target TRACK
Jean-Michel Trivi54392232011-05-24 15:53:33 -070064 CHANNEL_MASK = 0x4000,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 FORMAT = 0x4001,
Eric Laurent65b65452010-06-01 23:49:17 -070066 MAIN_BUFFER = 0x4002,
67 AUX_BUFFER = 0x4003,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 // for TARGET RESAMPLE
69 SAMPLE_RATE = 0x4100,
Eric Laurent4bb21c42011-02-28 16:52:51 -080070 RESET = 0x4101,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 // for TARGET VOLUME (8 channels max)
72 VOLUME0 = 0x4200,
73 VOLUME1 = 0x4201,
Eric Laurent65b65452010-06-01 23:49:17 -070074 AUXLEVEL = 0x4210,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 };
76
77
78 int getTrackName();
79 void deleteTrackName(int name);
80
81 status_t enable(int name);
82 status_t disable(int name);
83
84 status_t setActiveTrack(int track);
Eric Laurent65b65452010-06-01 23:49:17 -070085 status_t setParameter(int target, int name, void *value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
87 status_t setBufferProvider(AudioBufferProvider* bufferProvider);
Eric Laurent65b65452010-06-01 23:49:17 -070088 void process();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089
90 uint32_t trackNames() const { return mTrackNames; }
91
Eric Laurenta553c252009-07-17 12:17:14 -070092 static void ditherAndClamp(int32_t* out, int32_t const *sums, size_t c);
93
Eric Laurent72dafb22011-12-22 16:08:41 -080094 size_t getUnreleasedFrames(int name);
95
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096private:
97
98 enum {
99 NEEDS_CHANNEL_COUNT__MASK = 0x00000003,
100 NEEDS_FORMAT__MASK = 0x000000F0,
101 NEEDS_MUTE__MASK = 0x00000100,
102 NEEDS_RESAMPLE__MASK = 0x00001000,
Eric Laurent65b65452010-06-01 23:49:17 -0700103 NEEDS_AUX__MASK = 0x00010000,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 };
105
106 enum {
107 NEEDS_CHANNEL_1 = 0x00000000,
108 NEEDS_CHANNEL_2 = 0x00000001,
109
110 NEEDS_FORMAT_16 = 0x00000010,
111
112 NEEDS_MUTE_DISABLED = 0x00000000,
113 NEEDS_MUTE_ENABLED = 0x00000100,
114
115 NEEDS_RESAMPLE_DISABLED = 0x00000000,
116 NEEDS_RESAMPLE_ENABLED = 0x00001000,
Eric Laurent65b65452010-06-01 23:49:17 -0700117
118 NEEDS_AUX_DISABLED = 0x00000000,
119 NEEDS_AUX_ENABLED = 0x00010000,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 };
121
122 static inline int32_t applyVolume(int32_t in, int32_t v) {
123 return in * v;
124 }
125
126
127 struct state_t;
Eric Laurent65b65452010-06-01 23:49:17 -0700128 struct track_t;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129
Eric Laurent65b65452010-06-01 23:49:17 -0700130 typedef void (*mix_t)(state_t* state);
131 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 -0800132 static const int BLOCKSIZE = 16; // 4 cache lines
133
134 struct track_t {
135 uint32_t needs;
136
137 union {
138 int16_t volume[2]; // [0]3.12 fixed point
139 int32_t volumeRL;
140 };
141
142 int32_t prevVolume[2];
143
144 int32_t volumeInc[2];
Eric Laurent65b65452010-06-01 23:49:17 -0700145 int32_t auxLevel;
146 int32_t auxInc;
147 int32_t prevAuxLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148
149 uint16_t frameCount;
150
151 uint8_t channelCount : 4;
152 uint8_t enabled : 1;
153 uint8_t reserved0 : 3;
154 uint8_t format;
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700155 uint32_t channelMask;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156
157 AudioBufferProvider* bufferProvider;
158 mutable AudioBufferProvider::Buffer buffer;
159
Eric Laurent65b65452010-06-01 23:49:17 -0700160 hook_t hook;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 void const* in; // current location in buffer
162
163 AudioResampler* resampler;
164 uint32_t sampleRate;
Eric Laurent65b65452010-06-01 23:49:17 -0700165 int32_t* mainBuffer;
166 int32_t* auxBuffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167
168 bool setResampler(uint32_t sampleRate, uint32_t devSampleRate);
169 bool doesResample() const;
Eric Laurent4bb21c42011-02-28 16:52:51 -0800170 void resetResampler();
Eric Laurent65b65452010-06-01 23:49:17 -0700171 void adjustVolumeRamp(bool aux);
Eric Laurent72dafb22011-12-22 16:08:41 -0800172 size_t getUnreleasedFrames();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 };
174
175 // pad to 32-bytes to fill cache line
176 struct state_t {
177 uint32_t enabledTracks;
178 uint32_t needsChanged;
179 size_t frameCount;
180 mix_t hook;
181 int32_t *outputTemp;
182 int32_t *resampleTemp;
183 int32_t reserved[2];
184 track_t tracks[32]; __attribute__((aligned(32)));
185 };
186
187 int mActiveTrack;
188 uint32_t mTrackNames;
189 const uint32_t mSampleRate;
190
191 state_t mState __attribute__((aligned(32)));
192
193 void invalidateState(uint32_t mask);
194
Eric Laurent65b65452010-06-01 23:49:17 -0700195 static void track__genericResample(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
196 static void track__nop(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
197 static void track__16BitsStereo(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
198 static void track__16BitsMono(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
199 static void volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux);
200 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 -0800201
Eric Laurent65b65452010-06-01 23:49:17 -0700202 static void process__validate(state_t* state);
203 static void process__nop(state_t* state);
204 static void process__genericNoResampling(state_t* state);
205 static void process__genericResampling(state_t* state);
206 static void process__OneTrack16BitsStereoNoResampling(state_t* state);
207 static void process__TwoTracks16BitsStereoNoResampling(state_t* state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208};
209
210// ----------------------------------------------------------------------------
211}; // namespace android
212
213#endif // ANDROID_AUDIO_MIXER_H