Skip to content

Commit 2cd80d1

Browse files
committed
wip, with debugging printf's
1 parent fd71d92 commit 2cd80d1

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

locale/circuitpython.pot

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,10 @@ msgstr ""
489489
msgid "Attempted heap allocation when VM not running."
490490
msgstr ""
491491

492+
#: ports/raspberrypi/audio_dma.c
493+
msgid "Audio conversion not implemented"
494+
msgstr ""
495+
492496
#: shared-bindings/wifi/Radio.c
493497
msgid "AuthMode.OPEN is not used with password"
494498
msgstr ""
@@ -1213,6 +1217,10 @@ msgstr ""
12131217
msgid "Insufficient encryption"
12141218
msgstr ""
12151219

1220+
#: ports/raspberrypi/audio_dma.c
1221+
msgid "Internal audio buffer too small"
1222+
msgstr ""
1223+
12161224
#: ports/stm/common-hal/busio/UART.c
12171225
msgid "Internal define error"
12181226
msgstr ""

ports/raspberrypi/audio_dma.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,16 @@ void audio_dma_reset(void) {
5151

5252
void audio_dma_convert_signed(audio_dma_t *dma, uint8_t *buffer, uint32_t buffer_length,
5353
uint8_t **output_buffer, uint32_t *output_buffer_length) {
54+
55+
size_t output_buffer_max_length;
5456
if (dma->first_buffer_free) {
5557
*output_buffer = dma->first_buffer;
58+
output_buffer_max_length = dma->first_buffer_length;
5659
} else {
5760
*output_buffer = dma->second_buffer;
61+
output_buffer_max_length = dma->second_buffer_length;
5862
}
63+
5964
#pragma GCC diagnostic push
6065
#pragma GCC diagnostic ignored "-Wcast-align"
6166
if (dma->signed_to_unsigned ||
@@ -65,6 +70,12 @@ void audio_dma_convert_signed(audio_dma_t *dma, uint8_t *buffer, uint32_t buffer
6570
*output_buffer_length = buffer_length / dma->sample_spacing;
6671
uint32_t out_i = 0;
6772
if (dma->sample_resolution <= 8 && dma->output_resolution > 8) {
73+
// reading bytes, writing 16-bit samples
74+
*output_buffer_length = *output_buffer_length * 2;
75+
if (*output_buffer_length > output_buffer_max_length) {
76+
mp_raise_RuntimeError(translate("Internal audio buffer too small"));
77+
}
78+
6879
size_t shift = dma->output_resolution - dma->sample_resolution;
6980

7081
for (uint32_t i = 0; i < buffer_length; i += dma->sample_spacing) {
@@ -107,6 +118,10 @@ void audio_dma_convert_signed(audio_dma_t *dma, uint8_t *buffer, uint32_t buffer
107118
}
108119
out_i += 1;
109120
}
121+
} else {
122+
// (dma->sample_resolution > 8 && dma->output_resolution <= 8)
123+
// Not currently used, but might be in the future.
124+
mp_raise_RuntimeError(translate("Audio conversion not implemented"));
110125
}
111126
} else {
112127
*output_buffer = buffer;
@@ -149,7 +164,6 @@ void audio_dma_load_next_block(audio_dma_t *dma) {
149164
// If we don't have an output buffer, save the pointer to first_buffer for use in the single
150165
// buffer special case.
151166
if (dma->first_buffer == NULL) {
152-
mp_printf(&mp_plat_print,"no first buffer\n");
153167
dma->first_buffer = output_buffer;
154168
}
155169

@@ -210,8 +224,11 @@ audio_dma_result audio_dma_setup_playback(audio_dma_t *dma,
210224
uint32_t max_buffer_length;
211225
audiosample_get_buffer_structure(sample, single_channel_output, &single_buffer, &samples_signed,
212226
&max_buffer_length, &dma->sample_spacing);
213-
227+
mp_printf(&mp_plat_print, "single_buffer: %d, samples_signed: %d, max_buffer_length: %d, dma->sample_spacing: %d\n",
228+
single_buffer, samples_signed, max_buffer_length, dma->sample_spacing); ////
214229
// Check to see if we have to scale the resolution up.
230+
mp_printf(&mp_plat_print, "dma->sample_resolution: %d, dma->output_resolution: %d, output_signed: %d, single_channel_output: %d\n",
231+
dma->sample_resolution, dma->output_resolution, output_signed, single_channel_output);
215232
if (dma->sample_resolution <= 8 && dma->output_resolution > 8) {
216233
max_buffer_length *= 2;
217234
}
@@ -222,13 +239,15 @@ audio_dma_result audio_dma_setup_playback(audio_dma_t *dma,
222239
}
223240

224241
dma->first_buffer = (uint8_t *)m_realloc(dma->first_buffer, max_buffer_length);
242+
dma->first_buffer_length = max_buffer_length;
225243
if (dma->first_buffer == NULL) {
226244
return AUDIO_DMA_MEMORY_ERROR;
227245
}
228246

229247
dma->first_buffer_free = true;
230248
if (!single_buffer) {
231249
dma->second_buffer = (uint8_t *)m_realloc(dma->second_buffer, max_buffer_length);
250+
dma->second_buffer_length = max_buffer_length;
232251
if (dma->second_buffer == NULL) {
233252
return AUDIO_DMA_MEMORY_ERROR;
234253
}

ports/raspberrypi/audio_dma.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ typedef struct {
4848
uint8_t output_resolution; // in bits
4949
uint8_t sample_resolution; // in bits
5050
uint8_t *first_buffer;
51+
size_t first_buffer_length;
5152
uint8_t *second_buffer;
53+
size_t second_buffer_length;
5254
background_callback_t callback;
5355
} audio_dma_t;
5456

0 commit comments

Comments
 (0)