blob: 15766cddeaa09f709f8457a80fed8a9c91f66a40 [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
64 CHANNEL_COUNT = 0x4000,
65 FORMAT = 0x4001,
66 // for TARGET RESAMPLE
67 SAMPLE_RATE = 0x4100,
68 // for TARGET VOLUME (8 channels max)
69 VOLUME0 = 0x4200,
70 VOLUME1 = 0x4201,
71 };
72
73
74 int getTrackName();
75 void deleteTrackName(int name);
76
77 status_t enable(int name);
78 status_t disable(int name);
79
80 status_t setActiveTrack(int track);
81 status_t setParameter(int target, int name, int value);
82
83 status_t setBufferProvider(AudioBufferProvider* bufferProvider);
84 void process(void* output);
85
86 uint32_t trackNames() const { return mTrackNames; }
87
Eric Laurenta553c252009-07-17 12:17:14 -070088 static void ditherAndClamp(int32_t* out, int32_t const *sums, size_t c);
89
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090private:
91
92 enum {
93 NEEDS_CHANNEL_COUNT__MASK = 0x00000003,
94 NEEDS_FORMAT__MASK = 0x000000F0,
95 NEEDS_MUTE__MASK = 0x00000100,
96 NEEDS_RESAMPLE__MASK = 0x00001000,
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
112 static inline int32_t applyVolume(int32_t in, int32_t v) {
113 return in * v;
114 }
115
116
117 struct state_t;
118
119 typedef void (*mix_t)(state_t* state, void* output);
120
121 static const int BLOCKSIZE = 16; // 4 cache lines
122
123 struct track_t {
124 uint32_t needs;
125
126 union {
127 int16_t volume[2]; // [0]3.12 fixed point
128 int32_t volumeRL;
129 };
130
131 int32_t prevVolume[2];
132
133 int32_t volumeInc[2];
134
135 uint16_t frameCount;
136
137 uint8_t channelCount : 4;
138 uint8_t enabled : 1;
139 uint8_t reserved0 : 3;
140 uint8_t format;
141
142 AudioBufferProvider* bufferProvider;
143 mutable AudioBufferProvider::Buffer buffer;
144
145 void (*hook)(track_t* t, int32_t* output, size_t numOutFrames, int32_t* temp);
146 void const* in; // current location in buffer
147
148 AudioResampler* resampler;
149 uint32_t sampleRate;
150
151 bool setResampler(uint32_t sampleRate, uint32_t devSampleRate);
152 bool doesResample() const;
153 void adjustVolumeRamp();
154 };
155
156 // pad to 32-bytes to fill cache line
157 struct state_t {
158 uint32_t enabledTracks;
159 uint32_t needsChanged;
160 size_t frameCount;
161 mix_t hook;
162 int32_t *outputTemp;
163 int32_t *resampleTemp;
164 int32_t reserved[2];
165 track_t tracks[32]; __attribute__((aligned(32)));
166 };
167
168 int mActiveTrack;
169 uint32_t mTrackNames;
170 const uint32_t mSampleRate;
171
172 state_t mState __attribute__((aligned(32)));
173
174 void invalidateState(uint32_t mask);
175
176 static void track__genericResample(track_t* t, int32_t* out, size_t numFrames, int32_t* temp);
177 static void track__nop(track_t* t, int32_t* out, size_t numFrames, int32_t* temp);
178 static void volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp);
179 static void track__16BitsStereo(track_t* t, int32_t* out, size_t numFrames, int32_t* temp);
180 static void track__16BitsMono(track_t* t, int32_t* out, size_t numFrames, int32_t* temp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181
182 static void process__validate(state_t* state, void* output);
183 static void process__nop(state_t* state, void* output);
184 static void process__genericNoResampling(state_t* state, void* output);
185 static void process__genericResampling(state_t* state, void* output);
186 static void process__OneTrack16BitsStereoNoResampling(state_t* state, void* output);
187 static void process__TwoTracks16BitsStereoNoResampling(state_t* state, void* output);
188};
189
190// ----------------------------------------------------------------------------
191}; // namespace android
192
193#endif // ANDROID_AUDIO_MIXER_H