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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
|
/*
* Copyright (C) 2009 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.
*/
#define LOG_TAG "Equalizer"
#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
//#define LOG_NDEBUG 0
#include <cutils/log.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <new>
#include "AudioEqualizer.h"
#include "AudioBiquadFilter.h"
#include "AudioFormatAdapter.h"
#include <media/EffectEqualizerApi.h>
// effect_interface_t interface implementation for equalizer effect
extern "C" const struct effect_interface_s gEqualizerInterface;
namespace android {
namespace {
// Google Graphic Equalizer UUID: e25aa840-543b-11df-98a5-0002a5d5c51b
const effect_descriptor_t gEqualizerDescriptor = {
{0x0bed4300, 0xddd6, 0x11db, 0x8f34, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // type
{0xe25aa840, 0x543b, 0x11df, 0x98a5, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // uuid
EFFECT_API_VERSION,
(EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_LAST),
"Graphic Equalizer",
"Google Inc.",
};
static int gEffectIndex;
/////////////////// BEGIN EQ PRESETS ///////////////////////////////////////////
const int kNumBands = 5;
const uint32_t gFreqs[kNumBands] = { 50000, 125000, 900000, 3200000, 6300000 };
const uint32_t gBandwidths[kNumBands] = { 0, 3600, 3600, 2400, 0 };
const AudioEqualizer::BandConfig gBandsClassic[kNumBands] = {
{ 300, gFreqs[0], gBandwidths[0] },
{ 400, gFreqs[1], gBandwidths[1] },
{ 0, gFreqs[2], gBandwidths[2] },
{ 200, gFreqs[3], gBandwidths[3] },
{ -300, gFreqs[4], gBandwidths[4] }
};
const AudioEqualizer::BandConfig gBandsJazz[kNumBands] = {
{ -600, gFreqs[0], gBandwidths[0] },
{ 200, gFreqs[1], gBandwidths[1] },
{ 400, gFreqs[2], gBandwidths[2] },
{ -400, gFreqs[3], gBandwidths[3] },
{ -600, gFreqs[4], gBandwidths[4] }
};
const AudioEqualizer::BandConfig gBandsPop[kNumBands] = {
{ 400, gFreqs[0], gBandwidths[0] },
{ -400, gFreqs[1], gBandwidths[1] },
{ 300, gFreqs[2], gBandwidths[2] },
{ -400, gFreqs[3], gBandwidths[3] },
{ 600, gFreqs[4], gBandwidths[4] }
};
const AudioEqualizer::BandConfig gBandsRock[kNumBands] = {
{ 700, gFreqs[0], gBandwidths[0] },
{ 400, gFreqs[1], gBandwidths[1] },
{ -400, gFreqs[2], gBandwidths[2] },
{ 400, gFreqs[3], gBandwidths[3] },
{ 200, gFreqs[4], gBandwidths[4] }
};
const AudioEqualizer::PresetConfig gEqualizerPresets[] = {
{ "Classic", gBandsClassic },
{ "Jazz", gBandsJazz },
{ "Pop", gBandsPop },
{ "Rock", gBandsRock }
};
/////////////////// END EQ PRESETS /////////////////////////////////////////////
static const size_t kBufferSize = 32;
typedef AudioFormatAdapter<AudioEqualizer, kBufferSize> FormatAdapter;
struct EqualizerContext {
const struct effect_interface_s *itfe;
effect_config_t config;
FormatAdapter adapter;
AudioEqualizer * pEqualizer;
};
//--- local function prototypes
int Equalizer_init(EqualizerContext *pContext);
int Equalizer_configure(EqualizerContext *pContext, effect_config_t *pConfig);
int Equalizer_getParameter(AudioEqualizer * pEqualizer, int32_t *pParam, size_t *pValueSize, void *pValue);
int Equalizer_setParameter(AudioEqualizer * pEqualizer, int32_t *pParam, void *pValue);
//
//--- Effect Library Interface Implementation
//
extern "C" int EffectQueryNumberEffects(uint32_t *pNumEffects) {
*pNumEffects = 1;
gEffectIndex = 0;
return 0;
} /* end EffectQueryNumberEffects */
extern "C" int EffectQueryNext(effect_descriptor_t *pDescriptor) {
if (pDescriptor == NULL) {
return -EINVAL;
}
if (gEffectIndex++ > 0) {
return -ENOENT;
}
memcpy(pDescriptor, &gEqualizerDescriptor, sizeof(effect_descriptor_t));
return 0;
} /* end EffectQueryNext */
extern "C" int EffectCreate(effect_uuid_t *uuid,
effect_interface_t *pInterface) {
int ret;
int i;
LOGV("EffectLibCreateEffect start");
if (pInterface == NULL || uuid == NULL) {
return -EINVAL;
}
if (memcmp(uuid, &gEqualizerDescriptor.uuid, sizeof(effect_uuid_t)) != 0) {
return -EINVAL;
}
EqualizerContext *pContext = new EqualizerContext;
pContext->itfe = &gEqualizerInterface;
pContext->pEqualizer = NULL;
ret = Equalizer_init(pContext);
if (ret < 0) {
LOGW("EffectLibCreateEffect() init failed");
delete pContext;
return ret;
}
*pInterface = (effect_interface_t)pContext;
LOGV("EffectLibCreateEffect %p", pContext);
return 0;
} /* end EffectCreate */
extern "C" int EffectRelease(effect_interface_t interface) {
EqualizerContext * pContext = (EqualizerContext *)interface;
LOGV("EffectLibReleaseEffect %p", interface);
if (pContext == NULL) {
return -EINVAL;
}
pContext->pEqualizer->free();
delete pContext;
return 0;
} /* end EffectRelease */
//
//--- local functions
//
#define CHECK_ARG(cond) { \
if (!(cond)) { \
LOGV("Invalid argument: "#cond); \
return -EINVAL; \
} \
}
//----------------------------------------------------------------------------
// Equalizer_configure()
//----------------------------------------------------------------------------
// Purpose: Set input and output audio configuration.
//
// Inputs:
// pContext: effect engine context
// pConfig: pointer to effect_config_t structure holding input and output
// configuration parameters
//
// Outputs:
//
//----------------------------------------------------------------------------
int Equalizer_configure(EqualizerContext *pContext, effect_config_t *pConfig)
{
LOGV("Equalizer_configure start");
CHECK_ARG(pContext != NULL);
CHECK_ARG(pConfig != NULL);
CHECK_ARG(pConfig->inputCfg.samplingRate == pConfig->outputCfg.samplingRate);
CHECK_ARG(pConfig->inputCfg.channels == pConfig->outputCfg.channels);
CHECK_ARG(pConfig->inputCfg.format == pConfig->outputCfg.format);
CHECK_ARG((pConfig->inputCfg.channels == CHANNEL_MONO) || (pConfig->inputCfg.channels == CHANNEL_STEREO));
CHECK_ARG(pConfig->outputCfg.accessMode == EFFECT_BUFFER_ACCESS_WRITE
|| pConfig->outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE);
CHECK_ARG(pConfig->inputCfg.format == PCM_FORMAT_S7_24
|| pConfig->inputCfg.format == PCM_FORMAT_S15);
int channelCount;
if (pConfig->inputCfg.channels == CHANNEL_MONO) {
channelCount = 1;
} else {
channelCount = 2;
}
CHECK_ARG(channelCount <= AudioBiquadFilter::MAX_CHANNELS);
pContext->pEqualizer->configure(channelCount,
pConfig->inputCfg.samplingRate);
pContext->adapter.configure(*pContext->pEqualizer, channelCount,
pConfig->inputCfg.format,
pConfig->outputCfg.accessMode);
return 0;
} // end Equalizer_configure
//----------------------------------------------------------------------------
// Equalizer_init()
//----------------------------------------------------------------------------
// Purpose: Initialize engine with default configuration and creates
// AudioEqualizer instance.
//
// Inputs:
// pContext: effect engine context
//
// Outputs:
//
//----------------------------------------------------------------------------
int Equalizer_init(EqualizerContext *pContext)
{
int status;
LOGV("Equalizer_init start");
CHECK_ARG(pContext != NULL);
if (pContext->pEqualizer != NULL) {
pContext->pEqualizer->free();
}
pContext->config.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
pContext->config.inputCfg.channels = CHANNEL_STEREO;
pContext->config.inputCfg.format = PCM_FORMAT_S15;
pContext->config.inputCfg.samplingRate = 44100;
pContext->config.inputCfg.bufferProvider.getBuffer = NULL;
pContext->config.inputCfg.bufferProvider.releaseBuffer = NULL;
pContext->config.inputCfg.bufferProvider.cookie = NULL;
pContext->config.inputCfg.mask = EFFECT_CONFIG_ALL;
pContext->config.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
pContext->config.outputCfg.channels = CHANNEL_STEREO;
pContext->config.outputCfg.format = PCM_FORMAT_S15;
pContext->config.outputCfg.samplingRate = 44100;
pContext->config.outputCfg.bufferProvider.getBuffer = NULL;
pContext->config.outputCfg.bufferProvider.releaseBuffer = NULL;
pContext->config.outputCfg.bufferProvider.cookie = NULL;
pContext->config.outputCfg.mask = EFFECT_CONFIG_ALL;
pContext->pEqualizer = AudioEqualizer::CreateInstance(
NULL,
kNumBands,
AudioBiquadFilter::MAX_CHANNELS,
44100,
gEqualizerPresets,
ARRAY_SIZE(gEqualizerPresets));
for (int i = 0; i < kNumBands; ++i) {
pContext->pEqualizer->setFrequency(i, gFreqs[i]);
pContext->pEqualizer->setBandwidth(i, gBandwidths[i]);
}
pContext->pEqualizer->enable(true);
Equalizer_configure(pContext, &pContext->config);
return 0;
} // end Equalizer_init
//----------------------------------------------------------------------------
// Equalizer_getParameter()
//----------------------------------------------------------------------------
// Purpose:
// Get a Equalizer parameter
//
// Inputs:
// pEqualizer - handle to instance data
// pParam - pointer to parameter
// pValue - pointer to variable to hold retrieved value
// pValueSize - pointer to value size: maximum size as input
//
// Outputs:
// *pValue updated with parameter value
// *pValueSize updated with actual value size
//
//
// Side Effects:
//
//----------------------------------------------------------------------------
int Equalizer_getParameter(AudioEqualizer * pEqualizer, int32_t *pParam, size_t *pValueSize, void *pValue)
{
int status = 0;
int32_t param = *pParam++;
int32_t param2;
char *name;
switch (param) {
case EQ_PARAM_NUM_BANDS:
case EQ_PARAM_CUR_PRESET:
case EQ_PARAM_GET_NUM_OF_PRESETS:
if (*pValueSize < sizeof(int16_t)) {
return -EINVAL;
}
*pValueSize = sizeof(int16_t);
break;
case EQ_PARAM_LEVEL_RANGE:
case EQ_PARAM_BAND_FREQ_RANGE:
if (*pValueSize < 2 * sizeof(int32_t)) {
return -EINVAL;
}
*pValueSize = 2 * sizeof(int32_t);
break;
case EQ_PARAM_BAND_LEVEL:
case EQ_PARAM_GET_BAND:
case EQ_PARAM_CENTER_FREQ:
if (*pValueSize < sizeof(int32_t)) {
return -EINVAL;
}
*pValueSize = sizeof(int32_t);
break;
case EQ_PARAM_GET_PRESET_NAME:
break;
default:
return -EINVAL;
}
switch (param) {
case EQ_PARAM_NUM_BANDS:
*(int16_t *)pValue = kNumBands;
LOGV("Equalizer_getParameter() EQ_PARAM_NUM_BANDS %d", *(int16_t *)pValue);
break;
case EQ_PARAM_LEVEL_RANGE:
*(int32_t *)pValue = -9600;
*((int32_t *)pValue + 1) = 4800;
LOGV("Equalizer_getParameter() EQ_PARAM_LEVEL_RANGE min %d, max %d", *(int32_t *)pValue, *((int32_t *)pValue + 1));
break;
case EQ_PARAM_BAND_LEVEL:
param2 = *pParam;
if (param2 >= kNumBands) {
status = -EINVAL;
break;
}
*(int32_t *)pValue = pEqualizer->getGain(param2);
LOGV("Equalizer_getParameter() EQ_PARAM_BAND_LEVEL band %d, level %d", param2, *(int32_t *)pValue);
break;
case EQ_PARAM_CENTER_FREQ:
param2 = *pParam;
if (param2 >= kNumBands) {
status = -EINVAL;
break;
}
*(int32_t *)pValue = pEqualizer->getFrequency(param2);
LOGV("Equalizer_getParameter() EQ_PARAM_CENTER_FREQ band %d, frequency %d", param2, *(int32_t *)pValue);
break;
case EQ_PARAM_BAND_FREQ_RANGE:
param2 = *pParam;
if (param2 >= kNumBands) {
status = -EINVAL;
break;
}
pEqualizer->getBandRange(param2, *(uint32_t *)pValue, *((uint32_t *)pValue + 1));
LOGV("Equalizer_getParameter() EQ_PARAM_BAND_FREQ_RANGE band %d, min %d, max %d", param2, *(int32_t *)pValue, *((int32_t *)pValue + 1));
break;
case EQ_PARAM_GET_BAND:
param2 = *pParam;
*(int32_t *)pValue = pEqualizer->getMostRelevantBand(param2);
LOGV("Equalizer_getParameter() EQ_PARAM_GET_BAND frequency %d, band %d", param2, *(int32_t *)pValue);
break;
case EQ_PARAM_CUR_PRESET:
*(int16_t *)pValue = pEqualizer->getPreset();
LOGV("Equalizer_getParameter() EQ_PARAM_CUR_PRESET %d", *(int32_t *)pValue);
break;
case EQ_PARAM_GET_NUM_OF_PRESETS:
*(int16_t *)pValue = pEqualizer->getNumPresets();
LOGV("Equalizer_getParameter() EQ_PARAM_GET_NUM_OF_PRESETS %d", *(int16_t *)pValue);
break;
case EQ_PARAM_GET_PRESET_NAME:
param2 = *pParam;
if (param2 >= pEqualizer->getNumPresets()) {
status = -EINVAL;
break;
}
name = (char *)pValue;
strncpy(name, pEqualizer->getPresetName(param2), *pValueSize - 1);
name[*pValueSize - 1] = 0;
*pValueSize = strlen(name) + 1;
LOGV("Equalizer_getParameter() EQ_PARAM_GET_PRESET_NAME preset %d, name %s len %d", param2, gEqualizerPresets[param2].name, *pValueSize);
break;
default:
LOGV("Equalizer_getParameter() invalid param %d", param);
status = -EINVAL;
break;
}
return status;
} // end Equalizer_getParameter
//----------------------------------------------------------------------------
// Equalizer_setParameter()
//----------------------------------------------------------------------------
// Purpose:
// Set a Equalizer parameter
//
// Inputs:
// pEqualizer - handle to instance data
// pParam - pointer to parameter
// pValue - pointer to value
//
// Outputs:
//
//
// Side Effects:
//
//----------------------------------------------------------------------------
int Equalizer_setParameter (AudioEqualizer * pEqualizer, int32_t *pParam, void *pValue)
{
int status = 0;
int32_t preset;
int32_t band;
int32_t level;
int32_t param = *pParam++;
switch (param) {
case EQ_PARAM_CUR_PRESET:
preset = *(int16_t *)pValue;
LOGV("setParameter() EQ_PARAM_CUR_PRESET %d", preset);
if (preset >= pEqualizer->getNumPresets()) {
status = -EINVAL;
break;
}
pEqualizer->setPreset(preset);
pEqualizer->commit(true);
break;
case EQ_PARAM_BAND_LEVEL:
band = *pParam;
level = *(int32_t *)pValue;
LOGV("setParameter() EQ_PARAM_BAND_LEVEL band %d, level %d", band, level);
if (band >= kNumBands) {
status = -EINVAL;
break;
}
pEqualizer->setGain(band, level);
pEqualizer->commit(true);
break;
default:
LOGV("setParameter() invalid param %d", param);
break;
}
return status;
} // end Equalizer_setParameter
} // namespace
} // namespace
//
//--- Effect Control Interface Implementation
//
extern "C" int Equalizer_process(effect_interface_t self, audio_buffer_t *inBuffer, audio_buffer_t *outBuffer)
{
android::EqualizerContext * pContext = (android::EqualizerContext *) self;
if (pContext == NULL) {
return -EINVAL;
}
if (inBuffer == NULL || inBuffer->raw == NULL ||
outBuffer == NULL || outBuffer->raw == NULL ||
inBuffer->frameCount != outBuffer->frameCount) {
return -EINVAL;
}
pContext->adapter.process(inBuffer->raw, outBuffer->raw, outBuffer->frameCount);
return 0;
} // end Equalizer_process
extern "C" int Equalizer_command(effect_interface_t self, int cmdCode, int cmdSize,
void *pCmdData, int *replySize, void *pReplyData) {
android::EqualizerContext * pContext = (android::EqualizerContext *) self;
int retsize;
if (pContext == NULL) {
return -EINVAL;
}
android::AudioEqualizer * pEqualizer = pContext->pEqualizer;
LOGV("Equalizer_command command %d cmdSize %d",cmdCode, cmdSize);
switch (cmdCode) {
case EFFECT_CMD_INIT:
if (pReplyData == NULL || *replySize != sizeof(int)) {
return -EINVAL;
}
*(int *) pReplyData = Equalizer_init(pContext);
break;
case EFFECT_CMD_CONFIGURE:
if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
|| pReplyData == NULL || *replySize != sizeof(int)) {
return -EINVAL;
}
*(int *) pReplyData = Equalizer_configure(pContext,
(effect_config_t *) pCmdData);
break;
case EFFECT_CMD_RESET:
Equalizer_configure(pContext, &pContext->config);
break;
case EFFECT_CMD_GET_PARAM: {
if (pCmdData == NULL || cmdSize < (int)(sizeof(effect_param_t) + sizeof(int32_t)) ||
pReplyData == NULL || *replySize < (int) (sizeof(effect_param_t) + sizeof(int32_t))) {
return -EINVAL;
}
effect_param_t *p = (effect_param_t *)pCmdData;
memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + p->psize);
p = (effect_param_t *)pReplyData;
int voffset = ((p->psize - 1) / sizeof(int32_t) + 1) * sizeof(int32_t);
p->status = android::Equalizer_getParameter(pEqualizer, (int32_t *)p->data, &p->vsize,
p->data + voffset);
*replySize = sizeof(effect_param_t) + voffset + p->vsize;
LOGV("Equalizer_command EFFECT_CMD_GET_PARAM *pCmdData %d, *replySize %d, *pReplyData %08x %08x",
*(int32_t *)((char *)pCmdData + sizeof(effect_param_t)), *replySize,
*(int32_t *)((char *)pReplyData + sizeof(effect_param_t) + voffset),
*(int32_t *)((char *)pReplyData + sizeof(effect_param_t) + voffset + sizeof(int32_t)));
} break;
case EFFECT_CMD_SET_PARAM: {
LOGV("Equalizer_command EFFECT_CMD_SET_PARAM cmdSize %d pCmdData %p, *replySize %d, pReplyData %p", cmdSize, pCmdData, *replySize, pReplyData);
if (pCmdData == NULL || cmdSize < (int)(sizeof(effect_param_t) + sizeof(int32_t)) ||
pReplyData == NULL || *replySize != sizeof(int32_t)) {
return -EINVAL;
}
effect_param_t *p = (effect_param_t *) pCmdData;
*(int *)pReplyData = android::Equalizer_setParameter(pEqualizer, (int32_t *)p->data,
p->data + p->psize);
} break;
default:
LOGW("Equalizer_command invalid command %d",cmdCode);
return -EINVAL;
}
return 0;
}
// effect_interface_t interface implementation for equalizer effect
const struct effect_interface_s gEqualizerInterface = {
Equalizer_process,
Equalizer_command
};
|