-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio.c
More file actions
391 lines (303 loc) · 10.5 KB
/
audio.c
File metadata and controls
391 lines (303 loc) · 10.5 KB
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <semaphore.h>
#include "bcm_host.h"
#include "ilclient.h"
#define N_WAVE 1024 /* dimension of Sinewave[] */
#define PI (1<<16>>1)
#define SIN(x) Sinewave[((x)>>6) & (N_WAVE-1)]
#define COS(x) SIN((x)+(PI>>1))
extern short Sinewave[];
#ifndef countof
#define countof(arr) (sizeof(arr) / sizeof(arr[0]))
#endif
#define BUFFER_SIZE_SAMPLES 1024
typedef int int32_t;
typedef struct
{
sem_t sema;
ILCLIENT_T *client;
COMPONENT_T *audio_render;
COMPONENT_T *list[2];
OMX_BUFFERHEADERTYPE *user_buffer_list; // buffers owned by the client
uint32_t num_buffers;
uint32_t bytes_per_sample;
} AUDIOPLAY_STATE_T;
static void input_buffer_callback(void *data, COMPONENT_T *comp)
{
// do nothing - could add a callback to the user
// to indicate more buffers may be available.
}
int32_t audioplay_create(AUDIOPLAY_STATE_T **handle, uint32_t sample_rate, uint32_t num_channels, uint32_t bit_depth, uint32_t num_buffers, uint32_t buffer_size)
{
uint32_t bytes_per_sample = (bit_depth * num_channels) >> 3;
int32_t ret = -1;
*handle = NULL;
// basic sanity check on arguments
if(sample_rate >= 8000 && sample_rate <= 96000 &&
(num_channels == 1 || num_channels == 2 || num_channels == 4 || num_channels == 8) &&
(bit_depth == 16 || bit_depth == 32) &&
num_buffers > 0 &&
buffer_size >= bytes_per_sample)
{
// buffer lengths must be 16 byte aligned for VCHI
int size = (buffer_size + 15) & ~15;
AUDIOPLAY_STATE_T *st;
// buffer offsets must also be 16 byte aligned for VCHI
st = calloc(1, sizeof(AUDIOPLAY_STATE_T));
if(st)
{
OMX_ERRORTYPE error;
OMX_PARAM_PORTDEFINITIONTYPE param;
OMX_AUDIO_PARAM_PCMMODETYPE pcm;
int32_t s;
ret = 0;
*handle = st;
// create and start up everything
s = sem_init(&st->sema, 0, 1);
assert(s == 0);
st->bytes_per_sample = bytes_per_sample;
st->num_buffers = num_buffers;
st->client = ilclient_init();
assert(st->client != NULL);
ilclient_set_empty_buffer_done_callback(st->client, input_buffer_callback, st);
error = OMX_Init();
assert(error == OMX_ErrorNone);
ilclient_create_component(st->client, &st->audio_render, "audio_render", ILCLIENT_ENABLE_INPUT_BUFFERS | ILCLIENT_DISABLE_ALL_PORTS);
assert(st->audio_render != NULL);
st->list[0] = st->audio_render;
// set up the number/size of buffers
memset(¶m, 0, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
param.nSize = sizeof(OMX_PARAM_PORTDEFINITIONTYPE);
param.nVersion.nVersion = OMX_VERSION;
param.nPortIndex = 100;
error = OMX_GetParameter(ILC_GET_HANDLE(st->audio_render), OMX_IndexParamPortDefinition, ¶m);
assert(error == OMX_ErrorNone);
param.nBufferSize = size;
param.nBufferCountActual = num_buffers;
error = OMX_SetParameter(ILC_GET_HANDLE(st->audio_render), OMX_IndexParamPortDefinition, ¶m);
assert(error == OMX_ErrorNone);
// set the pcm parameters
memset(&pcm, 0, sizeof(OMX_AUDIO_PARAM_PCMMODETYPE));
pcm.nSize = sizeof(OMX_AUDIO_PARAM_PCMMODETYPE);
pcm.nVersion.nVersion = OMX_VERSION;
pcm.nPortIndex = 100;
pcm.nChannels = num_channels;
pcm.eNumData = OMX_NumericalDataSigned;
pcm.eEndian = OMX_EndianLittle;
pcm.nSamplingRate = sample_rate;
pcm.bInterleaved = OMX_TRUE;
pcm.nBitPerSample = bit_depth;
pcm.ePCMMode = OMX_AUDIO_PCMModeLinear;
switch(num_channels) {
case 1:
pcm.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
break;
case 8:
pcm.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
pcm.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
pcm.eChannelMapping[2] = OMX_AUDIO_ChannelCF;
pcm.eChannelMapping[3] = OMX_AUDIO_ChannelLFE;
pcm.eChannelMapping[4] = OMX_AUDIO_ChannelLR;
pcm.eChannelMapping[5] = OMX_AUDIO_ChannelRR;
pcm.eChannelMapping[6] = OMX_AUDIO_ChannelLS;
pcm.eChannelMapping[7] = OMX_AUDIO_ChannelRS;
break;
case 4:
pcm.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
pcm.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
pcm.eChannelMapping[2] = OMX_AUDIO_ChannelLR;
pcm.eChannelMapping[3] = OMX_AUDIO_ChannelRR;
break;
case 2:
pcm.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
pcm.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
break;
}
error = OMX_SetParameter(ILC_GET_HANDLE(st->audio_render), OMX_IndexParamAudioPcm, &pcm);
assert(error == OMX_ErrorNone);
ilclient_change_component_state(st->audio_render, OMX_StateIdle);
if(ilclient_enable_port_buffers(st->audio_render, 100, NULL, NULL, NULL) < 0)
{
// error
ilclient_change_component_state(st->audio_render, OMX_StateLoaded);
ilclient_cleanup_components(st->list);
error = OMX_Deinit();
assert(error == OMX_ErrorNone);
ilclient_destroy(st->client);
sem_destroy(&st->sema);
free(st);
*handle = NULL;
return -1;
}
ilclient_change_component_state(st->audio_render, OMX_StateExecuting);
}
}
return ret;
}
int32_t audioplay_delete(AUDIOPLAY_STATE_T *st)
{
OMX_ERRORTYPE error;
ilclient_change_component_state(st->audio_render, OMX_StateIdle);
error = OMX_SendCommand(ILC_GET_HANDLE(st->audio_render), OMX_CommandStateSet, OMX_StateLoaded, NULL);
assert(error == OMX_ErrorNone);
ilclient_disable_port_buffers(st->audio_render, 100, st->user_buffer_list, NULL, NULL);
ilclient_change_component_state(st->audio_render, OMX_StateLoaded);
ilclient_cleanup_components(st->list);
error = OMX_Deinit();
assert(error == OMX_ErrorNone);
ilclient_destroy(st->client);
sem_destroy(&st->sema);
free(st);
return 0;
}
uint8_t *audioplay_get_buffer(AUDIOPLAY_STATE_T *st)
{
OMX_BUFFERHEADERTYPE *hdr = NULL;
hdr = ilclient_get_input_buffer(st->audio_render, 100, 0);
if(hdr)
{
// put on the user list
sem_wait(&st->sema);
hdr->pAppPrivate = st->user_buffer_list;
st->user_buffer_list = hdr;
sem_post(&st->sema);
}
return hdr ? hdr->pBuffer : NULL;
}
int32_t audioplay_play_buffer(AUDIOPLAY_STATE_T *st,
uint8_t *buffer,
uint32_t length)
{
OMX_BUFFERHEADERTYPE *hdr = NULL, *prev = NULL;
int32_t ret = -1;
if(length % st->bytes_per_sample)
return ret;
sem_wait(&st->sema);
// search through user list for the right buffer header
hdr = st->user_buffer_list;
while(hdr != NULL && hdr->pBuffer != buffer && hdr->nAllocLen < length)
{
prev = hdr;
hdr = hdr->pAppPrivate;
}
if(hdr) // we found it, remove from list
{
ret = 0;
if(prev)
prev->pAppPrivate = hdr->pAppPrivate;
else
st->user_buffer_list = hdr->pAppPrivate;
}
sem_post(&st->sema);
if(hdr)
{
OMX_ERRORTYPE error;
hdr->pAppPrivate = NULL;
hdr->nOffset = 0;
hdr->nFilledLen = length;
error = OMX_EmptyThisBuffer(ILC_GET_HANDLE(st->audio_render), hdr);
assert(error == OMX_ErrorNone);
}
return ret;
}
int32_t audioplay_set_dest(AUDIOPLAY_STATE_T *st, const char *name)
{
int32_t success = -1;
OMX_CONFIG_BRCMAUDIODESTINATIONTYPE ar_dest;
if (name && strlen(name) < sizeof(ar_dest.sName))
{
OMX_ERRORTYPE error;
memset(&ar_dest, 0, sizeof(ar_dest));
ar_dest.nSize = sizeof(OMX_CONFIG_BRCMAUDIODESTINATIONTYPE);
ar_dest.nVersion.nVersion = OMX_VERSION;
strcpy((char *)ar_dest.sName, name);
error = OMX_SetConfig(ILC_GET_HANDLE(st->audio_render), OMX_IndexConfigBrcmAudioDestination, &ar_dest);
assert(error == OMX_ErrorNone);
success = 0;
}
return success;
}
uint32_t audioplay_get_latency(AUDIOPLAY_STATE_T *st)
{
OMX_PARAM_U32TYPE param;
OMX_ERRORTYPE error;
memset(¶m, 0, sizeof(OMX_PARAM_U32TYPE));
param.nSize = sizeof(OMX_PARAM_U32TYPE);
param.nVersion.nVersion = OMX_VERSION;
param.nPortIndex = 100;
error = OMX_GetConfig(ILC_GET_HANDLE(st->audio_render), OMX_IndexConfigAudioRenderingLatency, ¶m);
assert(error == OMX_ErrorNone);
return param.nU32;
}
#define CTTW_SLEEP_TIME 10
#define MIN_LATENCY_TIME 20
const int SAMPLERATE[] = {8000, 11025, 44100, 96000};
const int BITDEPTH[] = {16, 32};
const int CHANNELS[] = {1, 2, 4, 8};
static const char *audio_dest[] = {"local", "hdmi"};
void play_api_test(int samplerate, int bitdepth, int nchannels, int dest)
{
AUDIOPLAY_STATE_T *st;
int32_t ret;
unsigned int i, j, n;
int phase = 0;
int inc = 256<<16;
int dinc = 0;
int buffer_size = (BUFFER_SIZE_SAMPLES * bitdepth * nchannels)>>3;
assert(dest == 0 || dest == 1);
ret = audioplay_create(&st, samplerate, nchannels, bitdepth, 10, buffer_size);
assert(ret == 0);
ret = audioplay_set_dest(st, audio_dest[dest]);
assert(ret == 0);
// iterate for 5 seconds worth of packets
for (n=0; n<((samplerate * 5)/ BUFFER_SIZE_SAMPLES); n++)
{
uint8_t *buf;
int16_t *p;
uint32_t latency;
while((buf = audioplay_get_buffer(st)) == NULL)
usleep(10*1000);
p = (int16_t *) buf;
// fill the buffer
for (i=0; i<BUFFER_SIZE_SAMPLES; i++)
{
int16_t val = SIN(phase);
phase += inc>>16;
inc += dinc;
if (inc>>16 < 512)
dinc++;
else
dinc--;
for(j=0; j<nchannels; j++)
{
if (bitdepth == 32)
*p++ = 0;
*p++ = val;
}
}
// try and wait for a minimum latency time (in ms) before
// sending the next packet
while((latency = audioplay_get_latency(st)) > (samplerate * (MIN_LATENCY_TIME + CTTW_SLEEP_TIME) / 1000))
usleep(CTTW_SLEEP_TIME*1000);
ret = audioplay_play_buffer(st, buf, buffer_size);
assert(ret == 0);
}
audioplay_delete(st);
}
int main (int argc, char **argv)
{
int audio_dest = 0; // 0=headphones, 1=hdmi
int samplerate = 48000; // audio sample rate in Hz
int channels = 2; // numnber of audio channels
int bitdepth = 16; // number of bits per sample
bcm_host_init();
if(argc > 1)
audio_dest = atoi(argv[1]);
printf("Outputting audio to %s\n", audio_dest == 0 ? "analogue" : "hdmi");
play_api_test(samplerate, bitdepth, channels, audio_dest);
return 0;
}