-
Notifications
You must be signed in to change notification settings - Fork 2
/
TinyHardware.cpp
441 lines (373 loc) · 11.5 KB
/
TinyHardware.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
** Copyright 2011 Wolfson Microelectronics plc
**
** Portions taken from Nexus S AudioHardware implementation:
**
** Copyright 2010, The Android Open-Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#include <math.h>
#define LOG_TAG "AudioHardware"
extern "C" {
#include "tinyalsa/asoundlib.h"
};
#include "TinyHardware.h"
namespace android {
TinyAudioHardware::TinyAudioHardware()
: mOutput(NULL), mInput(NULL), mMixer(::mixer_open(0)), mMicMute(false)
{
}
TinyAudioHardware::~TinyAudioHardware()
{
closeOutputStream((AudioStreamOut *)mOutput);
closeInputStream((AudioStreamIn *)mInput);
if (mMixer)
mixer_close(mMixer);
}
status_t TinyAudioHardware::initCheck()
{
if (mMixer)
return NO_ERROR;
else
return NO_INIT;
}
void mixer_ctl_set(struct mixer *mixer, const char *name, int val)
{
struct mixer_ctl *ctl = mixer_get_ctl_by_name(mixer, name);
int ret = mixer_ctl_set_value(ctl, 0, val);
if (ret != 0)
LOGE("Failed to set %s to %d\n", name, val);
mixer_ctl_set_value(ctl, 1, val);
}
void mixer_ctl_set(struct mixer *mixer, const char *name, const char *val)
{
struct mixer_ctl *ctl = mixer_get_ctl_by_name(mixer, name);
int ret = mixer_ctl_set_enum_by_string(ctl, val);
if (ret != 0)
LOGE("Failed to set %s to %s\n", name, val);
}
AudioStreamOut* TinyAudioHardware::openOutputStream(uint32_t devices,
int *format,
uint32_t *channels,
uint32_t *sampleRate,
status_t *status)
{
AutoMutex lock(mLock);
LOGI("OPENOUT: %p\n", mOutput);
// Currently we only allow one output stream, in future we should
// support systems with multiple paths to the hardware.
if (mOutput) {
if (status) {
*status = INVALID_OPERATION;
}
return 0;
}
// Hard code an output path for Speyside speakers for now
mixer_ctl_set(mMixer, "DSP1RX", "AIF1");
mixer_ctl_set(mMixer, "DAC1L Mixer DSP1 Switch", 1);
mixer_ctl_set(mMixer, "DAC1R Mixer DSP1 Switch", 1);
mixer_ctl_set(mMixer, "DSP1 Playback Switch", 1);
mixer_ctl_set(mMixer, "DSP1 Playback Volume", 96);
mixer_ctl_set(mMixer, "DAC1 Switch", 1);
mixer_ctl_set(mMixer, "DAC1 Volume", 96);
mixer_ctl_set(mMixer, "DSP2RX", "AIF1");
mixer_ctl_set(mMixer, "DAC2L Mixer DSP1 Switch", 0);
mixer_ctl_set(mMixer, "DAC2R Mixer DSP1 Switch", 0);
mixer_ctl_set(mMixer, "DAC2L Mixer DSP2 Switch", 1);
mixer_ctl_set(mMixer, "DAC2R Mixer DSP2 Switch", 1);
mixer_ctl_set(mMixer, "DSP2 Playback Switch", 1);
mixer_ctl_set(mMixer, "DSP2 Playback Volume", 96);
mixer_ctl_set(mMixer, "DAC2 Switch", 1);
mixer_ctl_set(mMixer, "DAC2 Volume", 96);
mixer_ctl_set(mMixer, "DAC2 Sidetone", 24);
mixer_ctl_set(mMixer, "Sub Speaker Switch", 1);
mixer_ctl_set(mMixer, "Sub Speaker Volume", 57);
mixer_ctl_set(mMixer, "Sub Speaker DC Volume", 3);
mixer_ctl_set(mMixer, "Sub Speaker AC Volume", 3);
mixer_ctl_set(mMixer, "Speaker Switch", 1);
mixer_ctl_set(mMixer, "Output 2 Volume", 15);
mixer_ctl_set(mMixer, "DSP1 EQ Mode", "Sub HPF");
mixer_ctl_set(mMixer, "DSP2 EQ Mode", "Sub LPF");
mixer_ctl_set(mMixer, "Sub IN1 Switch", 1);
mixer_ctl_set(mMixer, "Sub IN2 Switch", 1);
mixer_ctl_set(mMixer, "SPKL", "DAC1L");
mixer_ctl_set(mMixer, "SPKR", "DAC1R");
mixer_ctl_set(mMixer, "DSP1 EQ Switch", 1);
mixer_ctl_set(mMixer, "DSP2 EQ Switch", 1);
TinyAudioStreamOut* out = new TinyAudioStreamOut();
status_t lStatus = out->set(this, format, channels, sampleRate);
if (status) {
*status = lStatus;
}
if (lStatus == NO_ERROR) {
mOutput = out;
} else {
delete out;
}
LOGI("OPENOUT DONE: %p %d\n", mOutput, lStatus);
return mOutput;
}
void TinyAudioHardware::closeOutputStream(AudioStreamOut* out) {
LOGI("CLOSE OUTPUT");
if (mOutput && out == mOutput) {
delete mOutput;
mOutput = 0;
}
}
AudioStreamIn* TinyAudioHardware::openInputStream(
uint32_t devices, int *format, uint32_t *channels,
uint32_t *sampleRate, status_t *status,
AudioSystem::audio_in_acoustics acoustics)
{
// check for valid input source
if (!AudioSystem::isInputDevice((AudioSystem::audio_devices)devices)) {
return 0;
}
AutoMutex lock(mLock);
// Only one input stream allowed for now
if (mInput) {
if (status) {
*status = INVALID_OPERATION;
}
return 0;
}
// create new output stream
TinyAudioStreamIn* in = new TinyAudioStreamIn();
status_t lStatus = in->set(this, format, channels, sampleRate, acoustics);
if (status) {
*status = lStatus;
}
if (lStatus == NO_ERROR) {
mInput = in;
} else {
delete in;
}
return mInput;
}
void TinyAudioHardware::closeInputStream(AudioStreamIn* in) {
if (mInput && in == mInput) {
delete mInput;
mInput = 0;
}
}
status_t TinyAudioHardware::setVoiceVolume(float v)
{
// If we ever want to do this via the RIL that'll be fun...
return NO_ERROR;
}
status_t TinyAudioHardware::setMasterVolume(float v)
{
// Implement: set master volume
// return error - software mixer will handle it
return INVALID_OPERATION;
}
status_t TinyAudioHardware::setMicMute(bool state)
{
mMicMute = state;
return NO_ERROR;
}
status_t TinyAudioHardware::getMicMute(bool* state)
{
*state = mMicMute;
return NO_ERROR;
}
status_t TinyAudioHardware::dumpInternals(int fd, const Vector<String16>& args)
{
const size_t SIZE = 256;
char buffer[SIZE];
String8 result;
::write(fd, result.string(), result.size());
return NO_ERROR;
}
status_t TinyAudioHardware::dump(int fd, const Vector<String16>& args)
{
dumpInternals(fd, args);
if (mInput) {
mInput->dump(fd, args);
}
if (mOutput) {
mOutput->dump(fd, args);
}
return NO_ERROR;
}
// ----------------------------------------------------------------------------
status_t TinyAudioStreamOut::set(
TinyAudioHardware *hw,
int *pFormat,
uint32_t *pChannels,
uint32_t *pRate)
{
LOGI("TinyAudioStreamOut::set(): asked for rate %dHz, %d channels",
*pRate, *pChannels);
struct pcm_config pcmConfig;
memset(&pcmConfig, 0, sizeof(pcmConfig));
pcmConfig.channels = 2;
pcmConfig.period_size = 2048;
pcmConfig.period_count = 12;
pcmConfig.format = PCM_FORMAT_S16_LE;
// Try 44.1kHz first...
pcmConfig.rate = 44100;
mPcm = pcm_open(0, 0, PCM_OUT, &pcmConfig);
// ...but fall back to 48kHz
if (!mPcm || !::pcm_is_ready(mPcm)) {
pcm_close(mPcm);
pcmConfig.rate = 48000;
mPcm = pcm_open(0, 0, PCM_OUT, &pcmConfig);
}
if (!mPcm || !::pcm_is_ready(mPcm)) {
LOGE("TinyAudioStreamOut::set() failed: %s\n",
::pcm_get_error(mPcm));
pcm_close(mPcm);
return BAD_VALUE;
}
mSampleRate = pcmConfig.rate;
*pRate = pcmConfig.rate;
*pFormat = AudioSystem::PCM_16_BIT;
*pChannels = AudioSystem::CHANNEL_OUT_STEREO;
LOGI("TinyAudioStreamOut::set(): rate %dHz, %d channels",
*pRate, *pChannels);
return NO_ERROR;
}
TinyAudioStreamOut::~TinyAudioStreamOut()
{
pcm_close(mPcm);
}
ssize_t TinyAudioStreamOut::write(const void* buffer, size_t bytes)
{
Mutex::Autolock _l(mLock);
if (!::pcm_is_ready(mPcm)) {
// This should never happen...
return BAD_VALUE;
}
if (pcm_write(mPcm, buffer, bytes) != 0)
LOGE("TinyAudioStreamOut::write() failed: %s\n",
::pcm_get_error(mPcm));
return NO_ERROR;
}
status_t TinyAudioStreamOut::standby()
{
if (pcm_stop(mPcm) != 0)
LOGE("standby() failed: %s\n",
::pcm_get_error(mPcm));
return NO_ERROR;
}
status_t TinyAudioStreamOut::dump(int fd, const Vector<String16>& args)
{
String8 result;
::write(fd, result.string(), result.size());
return NO_ERROR;
}
status_t TinyAudioStreamOut::setParameters(const String8& keyValuePairs)
{
AudioParameter param = AudioParameter(keyValuePairs);
String8 key = String8(AudioParameter::keyRouting);
status_t status = NO_ERROR;
int device;
LOGV("setParameters() %s", keyValuePairs.string());
if (param.getInt(key, device) == NO_ERROR) {
mDevice = device;
param.remove(key);
}
if (param.size()) {
status = BAD_VALUE;
}
return status;
}
String8 TinyAudioStreamOut::getParameters(const String8& keys)
{
AudioParameter param = AudioParameter(keys);
String8 value;
String8 key = String8(AudioParameter::keyRouting);
if (param.get(key, value) == NO_ERROR) {
param.addInt(key, (int)mDevice);
}
LOGV("getParameters() %s", param.toString().string());
return param.toString();
}
status_t TinyAudioStreamOut::getRenderPosition(uint32_t *dspFrames)
{
// Don't bother just yet, there are no actual users of this.
return INVALID_OPERATION;
}
// ----------------------------------------------------------------------------
// record functions
status_t TinyAudioStreamIn::set(
TinyAudioHardware *hw,
int *pFormat,
uint32_t *pChannels,
uint32_t *pRate,
AudioSystem::audio_in_acoustics acoustics)
{
if (pFormat == 0 || pChannels == 0 || pRate == 0) return BAD_VALUE;
LOGV("TinyAudioStreamIn::set(%p, %d, %d, %d, %u)", hw, fd, *pFormat, *pChannels, *pRate);
// check values
if ((*pFormat != format()) ||
(*pChannels != channels()) ||
(*pRate != sampleRate())) {
LOGE("Error opening input channel");
*pFormat = format();
*pChannels = channels();
*pRate = sampleRate();
return BAD_VALUE;
}
mAudioHardware = hw;
return NO_ERROR;
}
TinyAudioStreamIn::~TinyAudioStreamIn()
{
}
ssize_t TinyAudioStreamIn::read(void* buffer, ssize_t bytes)
{
AutoMutex lock(mLock);
return NO_INIT;
}
status_t TinyAudioStreamIn::dump(int fd, const Vector<String16>& args)
{
String8 result;
return NO_ERROR;
}
status_t TinyAudioStreamIn::setParameters(const String8& keyValuePairs)
{
AudioParameter param = AudioParameter(keyValuePairs);
String8 key = String8(AudioParameter::keyRouting);
status_t status = NO_ERROR;
int device;
LOGV("setParameters() %s", keyValuePairs.string());
if (param.getInt(key, device) == NO_ERROR) {
mDevice = device;
param.remove(key);
}
if (param.size()) {
status = BAD_VALUE;
}
return status;
}
String8 TinyAudioStreamIn::getParameters(const String8& keys)
{
AudioParameter param = AudioParameter(keys);
String8 value;
String8 key = String8(AudioParameter::keyRouting);
if (param.get(key, value) == NO_ERROR) {
param.addInt(key, (int)mDevice);
}
LOGV("getParameters() %s", param.toString().string());
return param.toString();
}
//------------------------------------------------------------------------------
// Factory
//------------------------------------------------------------------------------
extern "C" AudioHardwareInterface* createAudioHardware(void) {
return new TinyAudioHardware();
}
}; // namespace android