This repository has been archived by the owner on Dec 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaudio.h
328 lines (273 loc) · 9.59 KB
/
audio.h
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
/*
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
// Modified by Elehobica, 2021
#ifndef _PICO_AUDIO_H
#define _PICO_AUDIO_H
#include "pico.h"
#include "pico/util/buffer.h"
#include "hardware/sync.h"
#ifdef __cplusplus
extern "C" {
#endif
/** \file audio.h
* \defgroup pico_audio pico_audio
*
* Common API for audio output
*
*/
// PICO_CONFIG: SPINLOCK_ID_AUDIO_FREE_LIST_LOCK, Spinlock number for the audio free list, min=0, max=31, default=6, group=audio
#ifndef SPINLOCK_ID_AUDIO_FREE_LIST_LOCK
#define SPINLOCK_ID_AUDIO_FREE_LIST_LOCK 6
#endif
// PICO_CONFIG: SPINLOCK_ID_AUDIO_PREPARED_LISTS_LOCK, Spinlock number for the audio prepared list, min=0, max=31, default=7, group=audio
#ifndef SPINLOCK_ID_AUDIO_PREPARED_LISTS_LOCK
#define SPINLOCK_ID_AUDIO_PREPARED_LISTS_LOCK 7
#endif
// PICO_CONFIG: PICO_AUDIO_NOOP, Enable/disable audio by forcing NOOPS, type=bool, default=0, group=audio
#ifndef PICO_AUDIO_NOOP
#define PICO_AUDIO_NOOP 0
#endif
typedef enum {
AUDIO_PCM_FORMAT_S32 = 0, ///< signed 32bit PCM
AUDIO_PCM_FORMAT_S16, ///< signed 16bit PCM
AUDIO_PCM_FORMAT_S8, ///< signed 8bit PCM
AUDIO_PCM_FORMAT_U32, ///< unsigned 16bit PCM
AUDIO_PCM_FORMAT_U16, ///< unsigned 16bit PCM
AUDIO_PCM_FORMAT_U8 ///< unsigned 16bit PCM
} audio_pcm_format_t;
typedef enum {
AUDIO_CHANNEL_MONO = 1,
AUDIO_CHANNEL_STEREO = 2
} audio_channel_t;
/** \brief Audio format definition
*/
typedef struct audio_format {
uint32_t sample_freq; ///< Sample frequency in Hz
audio_pcm_format_t pcm_format; ///< Audio format \ref audio_formats
audio_channel_t channel_count; ///< Number of channels
} audio_format_t;
/** \brief Audio buffer format definition
*/
typedef struct audio_buffer_format {
const audio_format_t *format; ///< Audio format
uint16_t sample_stride; ///< Sample stride
} audio_buffer_format_t;
/** \brief Audio buffer definition
*/
typedef struct audio_buffer {
mem_buffer_t *buffer;
const audio_buffer_format_t *format;
uint32_t sample_count;
uint32_t max_sample_count;
uint32_t user_data; // only valid while the user has the buffer
// private - todo make an internal version
struct audio_buffer *next;
} audio_buffer_t;
typedef struct audio_connection audio_connection_t;
typedef struct audio_buffer_pool {
enum {
ac_producer, ac_consumer
} type;
const audio_format_t *format;
// private
audio_connection_t *connection;
spin_lock_t *free_list_spin_lock;
// ----- begin protected by free_list_spin_lock -----
audio_buffer_t *free_list;
spin_lock_t *prepared_list_spin_lock;
audio_buffer_t *prepared_list;
audio_buffer_t *prepared_list_tail;
} audio_buffer_pool_t;
typedef struct audio_connection audio_connection_t;
struct audio_connection {
audio_buffer_t *(*producer_pool_take)(audio_connection_t *connection, bool block);
void (*producer_pool_give)(audio_connection_t *connection, audio_buffer_t *buffer);
audio_buffer_t *(*consumer_pool_take)(audio_connection_t *connection, bool block);
void (*consumer_pool_give)(audio_connection_t *connection, audio_buffer_t *buffer);
audio_buffer_pool_t *producer_pool;
audio_buffer_pool_t *consumer_pool;
};
/*! \brief Allocate and initialise an audio producer pool
* \ingroup pico_audio
*
* \param format Format of the audio buffer
* \param buffer_count \todo
* \param buffer_sample_count \todo
* \return Pointer to an audio_buffer_pool
*/
audio_buffer_pool_t *audio_new_producer_pool(audio_buffer_format_t *format, int buffer_count,
int buffer_sample_count);
/*! \brief Allocate and initialise an audio consumer pool
* \ingroup pico_audio
*
* \param format Format of the audio buffer
* \param buffer_count
* \param buffer_sample_count
* \return Pointer to an audio_buffer_pool
*/
audio_buffer_pool_t *audio_new_consumer_pool(audio_buffer_format_t *format, int buffer_count,
int buffer_sample_count);
/*! \brief Allocate and initialise an audio wrapping buffer
* \ingroup pico_audio
*
* \param format Format of the audio buffer
* \param buffer \todo
* \return Pointer to an audio_buffer
*/
audio_buffer_t *audio_new_wrapping_buffer(audio_buffer_format_t *format, mem_buffer_t *buffer);
/*! \brief Allocate and initialise an new audio buffer
* \ingroup pico_audio
*
* \param format Format of the audio buffer
* \param buffer_sample_count \todo
* \return Pointer to an audio_buffer
*/
audio_buffer_t *audio_new_buffer(audio_buffer_format_t *format, int buffer_sample_count);
/*! \brief Initialise an audio buffer
* \ingroup pico_audio
*
* \param audio_buffer Pointer to an audio_buffer
* \param format Format of the audio buffer
* \param buffer_sample_count \todo
*/
void audio_init_buffer(audio_buffer_t *audio_buffer, audio_buffer_format_t *format, int buffer_sample_count);
/*! \brief \todo
* \ingroup pico_audio
*
* \param ac \todo
* \param buffer \todo
* \return Pointer to an audio_buffer
*/
void give_audio_buffer(audio_buffer_pool_t *ac, audio_buffer_t *buffer);
/*! \brief \todo
* \ingroup pico_audio
*
* \return Pointer to an audio_buffer
*/
audio_buffer_t *take_audio_buffer(audio_buffer_pool_t *ac, bool block);
/*! \brief \todo
* \ingroup pico_audio
*
*/
static inline void release_audio_buffer(audio_buffer_pool_t *ac, audio_buffer_t *buffer) {
buffer->sample_count = 0;
give_audio_buffer(ac, buffer);
}
/*! \brief \todo
* \ingroup pico_audio
*
* todo we are currently limited to 4095+1 input samples
* step is fraction of an input sample per output sample * 0x1000 and should be < 0x1000 i.e. we we are up-sampling (otherwise results are undefined)
*/
void audio_upsample(int16_t *input, int16_t *output, uint output_count, uint32_t step);
/*! \brief \todo
* \ingroup pico_audio
* similar but the output buffer is word aligned, and we output an even number of samples.. this is slightly faster than the above
* todo we are currently limited to 4095+1 input samples
* step is fraction of an input sample per output sample * 0x1000 and should be < 0x1000 i.e. we we are up-sampling (otherwise results are undefined)
*/
void audio_upsample_words(int16_t *input, int16_t *output_aligned, uint output_word_count, uint32_t step);
/*! \brief \todo
* \ingroup pico_audio
*/
void audio_upsample_double(int16_t *input, int16_t *output, uint output_count, uint32_t step);
/*! \brief \todo
* \ingroup pico_audio
*/
void audio_complete_connection(audio_connection_t *connection, audio_buffer_pool_t *producer,
audio_buffer_pool_t *consumer);
/*! \brief \todo
* \ingroup pico_audio
*/
audio_buffer_t *get_free_audio_buffer(audio_buffer_pool_t *context, bool block);
/*! \brief \todo
* \ingroup pico_audio
*/
void queue_free_audio_buffer(audio_buffer_pool_t *context, audio_buffer_t *ab);
/*! \brief \todo
* \ingroup pico_audio
*/
audio_buffer_t *get_full_audio_buffer(audio_buffer_pool_t *context, bool block);
/*! \brief \todo
* \ingroup pico_audio
*/
void queue_full_audio_buffer(audio_buffer_pool_t *context, audio_buffer_t *ab);
/*! \brief \todo
* \ingroup pico_audio
*
* generally an pico_audio connection uses 3 of the defaults and does the hard work in one of them
*/
void consumer_pool_give_buffer_default(audio_connection_t *connection, audio_buffer_t *buffer);
/*! \brief \todo
* \ingroup pico_audio
*/
audio_buffer_t *consumer_pool_take_buffer_default(audio_connection_t *connection, bool block);
/*! \brief \todo
* \ingroup pico_audio
*/
void producer_pool_give_buffer_default(audio_connection_t *connection, audio_buffer_t *buffer);
/*! \brief \todo
* \ingroup pico_audio
*/
audio_buffer_t *producer_pool_take_buffer_default(audio_connection_t *connection, bool block);
enum audio_correction_mode {
none,
fixed_dither,
dither,
noise_shaped_dither,
};
struct buffer_copying_on_consumer_take_connection {
struct audio_connection core;
audio_buffer_t *current_producer_buffer;
uint32_t current_producer_buffer_pos;
};
struct producer_pool_blocking_give_connection {
audio_connection_t core;
audio_buffer_t *current_consumer_buffer;
uint32_t current_consumer_buffer_pos;
};
/*! \brief \todo
* \ingroup pico_audio
*/
audio_buffer_t *mono_to_mono_consumer_take(audio_connection_t *connection, bool block);
/*! \brief \todo
* \ingroup pico_audio
*/
audio_buffer_t *mono_s8_to_mono_consumer_take(audio_connection_t *connection, bool block);
/*! \brief \todo
* \ingroup pico_audio
*/
audio_buffer_t *stereo_s16_to_stereo_s16_consumer_take(audio_connection_t *connection, bool block);
/*! \brief \todo
* \ingroup pico_audio
*/
audio_buffer_t *stereo_s32_to_stereo_s32_consumer_take(audio_connection_t *connection, bool block);
/*! \brief \todo
* \ingroup pico_audio
*/
audio_buffer_t *mono_to_stereo_consumer_take(audio_connection_t *connection, bool block);
/*! \brief \todo
* \ingroup pico_audio
*/
audio_buffer_t *mono_s8_to_stereo_consumer_take(audio_connection_t *connection, bool block);
/*! \brief \todo
* \ingroup pico_audio
*/
void stereo_s16_to_stereo_s16_producer_give(audio_connection_t *connection, audio_buffer_t *buffer);
/*! \brief \todo
* \ingroup pico_audio
*/
void stereo_s32_to_stereo_s32_producer_give(audio_connection_t *connection, audio_buffer_t *buffer);
// not worth a separate header for now
typedef struct __packed pio_audio_channel_config {
uint8_t base_pin;
uint8_t dma_channel;
uint8_t pio_sm;
} pio_audio_channel_config_t;
#ifdef __cplusplus
}
#endif
#endif //_AUDIO_H