-
Notifications
You must be signed in to change notification settings - Fork 3
/
dsAudio.c
615 lines (535 loc) · 18.1 KB
/
dsAudio.c
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
607
608
609
610
611
612
613
614
615
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2017 RDK Management
*
* 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 <sys/types.h>
#include "dsAudio.h"
#include <stdint.h>
#include <math.h>
#include "dsError.h"
#include "dsUtl.h"
#include "dshalUtils.h"
#include <alsa/asoundlib.h>
#define ALSA_CARD_NAME "hw:0"
#if (SND_LIB_MAJOR >= 1) && (SND_LIB_MINOR >= 2)
#define ALSA_ELEMENT_NAME "HDMI"
#else
#define ALSA_ELEMENT_NAME "PCM"
#endif
#define MAX_LINEAR_DB_SCALE 24
typedef struct _AOPHandle_t {
dsAudioPortType_t m_vType;
int m_index;
int m_nativeHandle;
bool m_IsEnabled;
} AOPHandle_t;
static AOPHandle_t _handles[dsAUDIOPORT_TYPE_MAX][2] = {
};
float dBmin;
float dBmax;
static dsAudioEncoding_t _encoding = dsAUDIO_ENC_PCM;
static bool _isms11Enabled = false;
static dsAudioStereoMode_t _stereoModeHDMI = dsAUDIO_STEREO_STEREO;
static void dsGetdBRange();
bool dsIsValidHandle(intptr_t uHandle)
{
size_t index ;
bool retValue = false;
for (index = 0; index < dsAUDIOPORT_TYPE_MAX; index++) {
if ((intptr_t)&_handles[index][0] == uHandle) {
retValue = true;
break;
}
}
return retValue;
}
static int8_t initAlsa(const char *selemname, const char *s_card, snd_mixer_elem_t **element)
{
int ret = 0;
snd_mixer_t *smixer = NULL;
snd_mixer_selem_id_t *sid;
if ((ret = snd_mixer_open(&smixer, 0)) < 0) {
printf("Cannot open sound mixer %s", snd_strerror(ret));
snd_mixer_close(smixer);
return ret;
}
if ((ret = snd_mixer_attach(smixer, s_card)) < 0) {
printf("sound mixer attach Failed %s", snd_strerror(ret));
snd_mixer_close(smixer);
return ret;
}
if ((ret = snd_mixer_selem_register(smixer, NULL, NULL)) < 0) {
printf("Cannot register sound mixer element %s", snd_strerror(ret));
snd_mixer_close(smixer);
return ret;
}
ret = snd_mixer_load(smixer);
if (ret < 0) {
printf("Sound mixer load %s error: %s", s_card, snd_strerror(ret));
snd_mixer_close(smixer);
return ret;
}
ret = snd_mixer_selem_id_malloc(&sid);
if (ret < 0) {
printf("Sound mixer: id allocation failed. %s: error: %s", s_card, snd_strerror(ret));
snd_mixer_close(smixer);
return ret;
}
snd_mixer_selem_id_set_index(sid, 0);
snd_mixer_selem_id_set_name(sid, selemname);
*element = snd_mixer_find_selem(smixer, sid);
if (NULL == *element) {
printf("Unable to find simple control '%s',%i\n", snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid));
snd_mixer_close(smixer);
ret = -1;
}
return ret;
}
//###################################################################################################
dsError_t dsAudioPortInit()
{
dsError_t ret = dsERR_NONE;
_handles[dsAUDIOPORT_TYPE_HDMI][0].m_vType = dsAUDIOPORT_TYPE_HDMI;
_handles[dsAUDIOPORT_TYPE_HDMI][0].m_nativeHandle = dsAUDIOPORT_TYPE_HDMI;
_handles[dsAUDIOPORT_TYPE_HDMI][0].m_index = 0;
_handles[dsAUDIOPORT_TYPE_HDMI][0].m_IsEnabled = true;
_handles[dsAUDIOPORT_TYPE_SPDIF][0].m_vType = dsAUDIOPORT_TYPE_SPDIF;
_handles[dsAUDIOPORT_TYPE_SPDIF][0].m_nativeHandle = dsAUDIOPORT_TYPE_SPDIF;
_handles[dsAUDIOPORT_TYPE_SPDIF][0].m_index = 0;
_handles[dsAUDIOPORT_TYPE_SPDIF][0].m_IsEnabled = true;
dsGetdBRange();
return ret;
}
static void dsGetdBRange()
{
#ifdef ALSA_AUDIO_MASTER_CONTROL_ENABLE
long min_dB_value, max_dB_value;
const char *s_card = ALSA_CARD_NAME;
const char *element_name = ALSA_ELEMENT_NAME;
snd_mixer_elem_t *mixer_elem = NULL;
initAlsa(element_name,s_card,&mixer_elem);
if(mixer_elem == NULL) {
printf("failed to initialize alsa!\n");
return;
}
if(!snd_mixer_selem_get_playback_dB_range(mixer_elem, &min_dB_value, &max_dB_value)) {
dBmax = (float) max_dB_value/100;
dBmin = (float) min_dB_value/100;
}
#endif
}
dsError_t dsGetAudioPort(dsAudioPortType_t type, int index, intptr_t *handle)
{
dsError_t ret = dsERR_NONE;
if (dsAudioType_isValid(type))
{
*handle = (intptr_t)&_handles[type][index];
}
else
{
ret = dsERR_NONE;
}
return ret;
}
dsError_t dsGetAudioEncoding(intptr_t handle, dsAudioEncoding_t *encoding)
{
dsError_t ret = dsERR_NONE;
*encoding = _encoding;
return ret;
}
dsError_t dsGetAudioCompression(intptr_t handle, dsAudioCompression_t *compression)
{
dsError_t ret = dsERR_NONE;
if (dsIsValidHandle(handle)) {
ret = dsERR_NONE;
}
else {
ret = dsERR_NONE;
}
return ret;
}
dsError_t dsGetStereoMode(intptr_t handle, dsAudioStereoMode_t *stereoMode)
{
dsError_t ret = dsERR_NONE;
*stereoMode = _stereoModeHDMI;
return ret;
}
dsError_t dsGetPersistedStereoMode (intptr_t handle, dsAudioStereoMode_t *stereoMode)
{
return dsERR_NONE;
}
dsError_t dsGetStereoAuto (intptr_t handle, int *autoMode)
{
return dsERR_NONE;
}
dsError_t dsIsAudioMute (intptr_t handle, bool *muted)
{
#ifdef ALSA_AUDIO_MASTER_CONTROL_ENABLE
printf("Inside %s :%d\n",__FUNCTION__,__LINE__);
dsError_t ret = dsERR_NONE;
if( ! dsIsValidHandle(handle) || NULL == muted ){
ret = dsERR_INVALID_PARAM;
}
const char *s_card = ALSA_CARD_NAME;
const char *element_name = ALSA_ELEMENT_NAME;
snd_mixer_elem_t *mixer_elem = NULL;
initAlsa(element_name,s_card,&mixer_elem);
if(mixer_elem == NULL) {
printf("failed to initialize alsa!\n");
return dsERR_GENERAL;
}
int mute_status;
if (snd_mixer_selem_has_playback_switch(mixer_elem)) {
snd_mixer_selem_get_playback_switch(mixer_elem, SND_MIXER_SCHN_FRONT_LEFT, &mute_status);
if (!mute_status) {
*muted = true;
} else {
*muted = false;
}
}
else {
ret = dsERR_GENERAL;
}
return ret;
#else
return dsERR_NONE;
#endif
}
dsError_t dsSetAudioMute(intptr_t handle, bool mute)
{
#ifdef ALSA_AUDIO_MASTER_CONTROL_ENABLE
printf("Inside %s :%d\n",__FUNCTION__,__LINE__);
dsError_t ret = dsERR_NONE;
if( ! dsIsValidHandle(handle)){
ret = dsERR_INVALID_PARAM;
}
const char *s_card = ALSA_CARD_NAME;
const char *element_name = ALSA_ELEMENT_NAME;
snd_mixer_elem_t *mixer_elem = NULL;
initAlsa(element_name,s_card,&mixer_elem);
if(mixer_elem == NULL) {
printf("failed to initialize alsa!\n");
return dsERR_GENERAL;
}
if (snd_mixer_selem_has_playback_switch(mixer_elem)) {
snd_mixer_selem_set_playback_switch_all(mixer_elem, !mute);
if (mute) {
printf("Audio Mute success\n");
} else {
printf("Audio Unmute success.\n");
}
}
return ret;
#else
return dsERR_NONE;
#endif
}
dsError_t dsIsAudioPortEnabled(intptr_t handle, bool *enabled)
{
printf("Inside %s :%d\n",__FUNCTION__,__LINE__);
dsError_t ret = dsERR_NONE;
bool audioEnabled = true;
ret = dsIsAudioMute(handle, &audioEnabled);
if (ret == dsERR_NONE) {
*enabled = !audioEnabled;
}
return ret;
}
dsError_t dsEnableAudioPort(intptr_t handle, bool enabled)
{
printf("Inside %s :%d\n",__FUNCTION__,__LINE__);
return dsSetAudioMute ( handle, !enabled );
}
dsError_t dsGetAudioGain(intptr_t handle, float *gain)
{
#ifdef ALSA_AUDIO_MASTER_CONTROL_ENABLE
dsError_t ret = dsERR_NONE;
if( ! dsIsValidHandle(handle) || gain == NULL) {
ret = dsERR_INVALID_PARAM;
}
if ( dsERR_NONE == ret ) {
long value_got;
const char *s_card = ALSA_CARD_NAME;
const char *element_name = ALSA_ELEMENT_NAME;
long vol_min = 0, vol_max = 0;
double normalized= 0, min_norm = 0;
snd_mixer_elem_t *mixer_elem;
initAlsa(element_name,s_card,&mixer_elem);
if(mixer_elem == NULL) {
printf("failed to initialize alsa!\n");
return dsERR_GENERAL;
}
snd_mixer_selem_get_playback_dB_range(mixer_elem, &vol_min, &vol_max);
if(!snd_mixer_selem_get_playback_dB(mixer_elem, SND_MIXER_SCHN_FRONT_LEFT, &value_got))
{
printf("dsGetAudioGain: Gain in dB %.2f\n", value_got/100.0);
if ((vol_max - vol_min) <= MAX_LINEAR_DB_SCALE * 100)
{
*gain = (value_got - vol_min) / (double)(vol_max - vol_min);
}
else
{
normalized = pow(10, (value_got - vol_max) / 6000.0);
if (vol_min != SND_CTL_TLV_DB_GAIN_MUTE)
{
min_norm = pow(10, (vol_min - vol_max) / 6000.0);
normalized = (normalized - min_norm) / (1 - min_norm);
}
*gain = (float)(((int)(100.0f * normalized + 0.5f))/1.0f);
printf("dsGetAudioGain: Rounded Gain in linear scale %.2f\n", *gain);
}
}
}
return ret;
#else
return dsERR_NONE;
#endif
}
dsError_t dsGetAudioDB(intptr_t handle, float *db)
{
#ifdef ALSA_AUDIO_MASTER_CONTROL_ENABLE
dsError_t ret = dsERR_NONE;
if( ! dsIsValidHandle(handle) || db == NULL) {
ret = dsERR_INVALID_PARAM;
}
if ( dsERR_NONE == ret ) {
long db_value;
const char *s_card = ALSA_CARD_NAME;
const char *element_name = ALSA_ELEMENT_NAME;
snd_mixer_elem_t *mixer_elem = NULL;
initAlsa(element_name,s_card,&mixer_elem);
if(mixer_elem == NULL) {
printf("failed to initialize alsa!\n");
return dsERR_GENERAL;
}
if(!snd_mixer_selem_get_playback_dB(mixer_elem, SND_MIXER_SCHN_FRONT_LEFT, &db_value)) {
*db = (float) db_value/100;
}
}
return ret;
#else
return dsERR_NONE;
#endif
}
dsError_t dsGetAudioLevel(intptr_t handle, float *level)
{
#ifdef ALSA_AUDIO_MASTER_CONTROL_ENABLE
dsError_t ret = dsERR_NONE;
if( ! dsIsValidHandle(handle) ) {
ret = dsERR_INVALID_PARAM;
}
if ( dsERR_NONE == ret ) {
long vol_value, min, max;
const char *s_card = "default";
const char *element_name = ALSA_ELEMENT_NAME;
snd_mixer_elem_t *mixer_elem = NULL;
initAlsa(element_name,s_card,&mixer_elem);
if(mixer_elem == NULL) {
printf("failed to initialize alsa!\n");
return dsERR_GENERAL;
}
if(!snd_mixer_selem_get_playback_volume(mixer_elem, SND_MIXER_SCHN_FRONT_LEFT, &vol_value)) {
snd_mixer_selem_get_playback_volume_range(mixer_elem, &min, &max);
*level = (float)((vol_value - min)*100/(max - min));
}
}
return ret;
#else
return dsERR_NONE;
#endif
}
dsError_t dsGetAudioMaxDB(intptr_t handle, float *maxDb)
{
*maxDb = dBmax;
return dsERR_NONE;
}
dsError_t dsGetAudioMinDB(intptr_t handle, float *minDb)
{
*minDb = dBmin;
return dsERR_NONE;
}
dsError_t dsGetAudioOptimalLevel(intptr_t handle, float *optimalLevel)
{
dsError_t ret = dsERR_NONE;
return ret;
}
dsError_t dsIsAudioLoopThru(intptr_t handle, bool *loopThru)
{
dsError_t ret = dsERR_NONE;
return ret;
}
dsError_t dsSetAudioEncoding(intptr_t handle, dsAudioEncoding_t encoding)
{
dsError_t ret = dsERR_NONE;
_encoding = encoding;
return ret;
}
dsError_t dsSetAudioCompression(intptr_t handle, dsAudioCompression_t compression)
{
dsError_t ret = dsERR_NONE;
return ret;
}
dsError_t dsIsAudioMSDecode(intptr_t handle, bool *ms11Enabled)
{
dsError_t ret = dsERR_NONE;
*ms11Enabled = _isms11Enabled;
return ret;
}
dsError_t dsSetStereoMode(intptr_t handle, dsAudioStereoMode_t mode) {
dsError_t ret = dsERR_NONE;
return ret;
}
dsError_t dsSetStereoAuto (intptr_t handle, int autoMode)
{
return dsERR_NONE;
}
dsError_t dsSetAudioGain(intptr_t handle, float gain)
{
#ifdef ALSA_AUDIO_MASTER_CONTROL_ENABLE
dsError_t ret = dsERR_NONE;
if( ! dsIsValidHandle(handle) ) {
ret = dsERR_INVALID_PARAM;
}
if ( dsERR_NONE == ret ) {
const char *s_card = ALSA_CARD_NAME;
const char *element_name = ALSA_ELEMENT_NAME;
bool enabled = false;
ret = dsERR_GENERAL;
snd_mixer_elem_t *mixer_elem;
initAlsa(element_name,s_card,&mixer_elem);
if(mixer_elem == NULL) {
printf("failed to initialize alsa!\n");
return dsERR_GENERAL;
}
dsIsAudioMute(handle, &enabled);
if (true == enabled)
{
printf("dsSetAudioGain: Mute is enabled. \n");
return ret;
}
long vol_min, vol_max;
double min_norm;
gain = gain / 100.0f;
snd_mixer_selem_get_playback_dB_range(mixer_elem, &vol_min, &vol_max);
if (vol_max - vol_min <= MAX_LINEAR_DB_SCALE * 100)
{
long floatval = lrint(gain * (vol_max - vol_min)) + vol_min;
snd_mixer_selem_set_playback_dB_all(mixer_elem, floatval, 0);
ret = dsERR_NONE;
}
else
{
if (vol_min != SND_CTL_TLV_DB_GAIN_MUTE)
{
min_norm = pow(10, (vol_min - vol_max) / 6000.0);
gain = gain * (1 - min_norm) + min_norm;
}
long floatval = lrint(6000.0 * log10(gain)) + vol_max;
snd_mixer_selem_set_playback_dB_all(mixer_elem, floatval, 0);
printf("dsSetAudioGain: Setting gain in dB: %.2f \n", floatval/100.0);
ret = dsERR_NONE;
}
}
return ret;
#else
return dsERR_NONE;
#endif
}
dsError_t dsSetAudioDB(intptr_t handle, float db)
{
#ifdef ALSA_AUDIO_MASTER_CONTROL_ENABLE
dsError_t ret = dsERR_NONE;
if( ! dsIsValidHandle(handle) ) {
ret = dsERR_INVALID_PARAM;
}
if ( dsERR_NONE == ret ) {
const char *s_card = ALSA_CARD_NAME;
const char *element_name = ALSA_ELEMENT_NAME;
snd_mixer_elem_t *mixer_elem = NULL;
initAlsa(element_name,s_card,&mixer_elem);
if(mixer_elem == NULL) {
printf("failed to initialize alsa!\n");
return dsERR_GENERAL;
}
if(db < dBmin) {
db = dBmin;
}
if(db > dBmax) {
db = dBmax;
}
if(!snd_mixer_selem_set_playback_dB_all(mixer_elem, (long) db * 100, 0)) {
ret = dsERR_NONE;
}
else {
ret = dsERR_GENERAL;
}
}
return ret;
#else
return dsERR_NONE;
#endif
}
dsError_t dsSetAudioLevel(intptr_t handle, float level)
{
#ifdef ALSA_AUDIO_MASTER_CONTROL_ENABLE
dsError_t ret = dsERR_NONE;
if( ! dsIsValidHandle(handle)) {
ret = dsERR_INVALID_PARAM;
}
if ( dsERR_NONE == ret ) {
long vol_value, min, max;
const char *s_card = ALSA_CARD_NAME;
const char *element_name = ALSA_ELEMENT_NAME;
snd_mixer_elem_t *mixer_elem = NULL;
initAlsa(element_name,s_card,&mixer_elem);
if(mixer_elem == NULL) {
printf("failed to initialize alsa!\n");
return dsERR_GENERAL;
}
snd_mixer_selem_get_playback_volume_range(mixer_elem, &min, &max);
vol_value = (long)(((level / 100.0) * (max - min)) + min);
if(snd_mixer_selem_set_playback_volume_all(mixer_elem, vol_value)) {
printf("Failed to set Audio level\n");
}
}
return ret;
#else
return dsERR_NONE;
#endif
}
dsError_t dsEnableLoopThru(intptr_t handle, bool loopThru)
{
dsError_t ret = dsERR_NONE;
return ret;
}
dsError_t dsAudioPortTerm()
{
dsError_t ret = dsERR_NONE;
return ret;
}
bool dsCheckSurroundSupport()
{
bool status = false;
int num_channels = 0;
for (int i=1; i<=8; i++) {
if (vc_tv_hdmi_audio_supported(EDID_AudioFormat_eAC3, i, EDID_AudioSampleRate_e44KHz, EDID_AudioSampleSize_16bit ) == 0)
num_channels = i;
}
if (num_channels)
status = true;
return status;
}