Skip to content

WIP: Audio recording and playback #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 50 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
cf08a1d
codal_port/modaudio: Extend AudioFrame constructor to take opt size.
dpgeorge Nov 14, 2023
2119cd9
codal_port/modaudio: Rework audio playing to play a long AudioFrame.
dpgeorge Nov 14, 2023
1dc4c12
codal_port/modaudio: Remove buffer expansion.
dpgeorge Nov 14, 2023
07190d5
codal_app: Add microphone recording interface.
dpgeorge Nov 14, 2023
edfa023
codal_port/microbit_microphone: Add methods to record.
dpgeorge Nov 14, 2023
7e3b102
codal_port/modaudio: Add audio.sound_level() method.
dpgeorge Nov 14, 2023
c487a38
src: Add test recording program.
dpgeorge Nov 14, 2023
30ef534
codal_port/modaudio: Rename "size" field to "alloc_size".
dpgeorge Jan 15, 2024
affdb0a
codal_port/modaudio: Make AudioFrame constructor public.
dpgeorge Jan 15, 2024
8ae75da
codal_app/microbithal_microphone: Store the current recording length.
dpgeorge Jan 15, 2024
1e4ae5b
codal_port/modaudio: Add a "used_size" field to AudioFrame.
dpgeorge Jan 15, 2024
0cf7f04
codal_port/modaudio: Add rate to AudioFrame.
dpgeorge Jan 15, 2024
ab6ea69
codal_port/microbit_microphone: Use rate from AudioFrame, or update it.
dpgeorge Jan 15, 2024
bd7905d
codal_port/modaudio: Use AudioFrame rate as rate when playing it.
dpgeorge Jan 15, 2024
2756929
src: Update test_record.py.
dpgeorge Jan 15, 2024
19dbe4b
codal_app/main: Set microphone gain to 0.2.
dpgeorge Jan 18, 2024
f1b03ee
codal_port/modaudio: Take the sqrt of sound_level.
dpgeorge Jan 18, 2024
4e3ac1d
codal_port/modaudio: Mostly use "alloc_size" instead of "used_size".
dpgeorge Jan 18, 2024
40979ac
codal_port/modaudio: Round up AudioFrame size to nearest 32.
dpgeorge Jan 18, 2024
4878157
codal_port/modaudio: Increas "used_size" when data is written.
dpgeorge Jan 18, 2024
5fe6307
src: Update test_record.py.
dpgeorge Jan 18, 2024
a528b1e
codal_port/modaudio: Update to build with latest micropython.
dpgeorge Jan 18, 2024
71d9430
codal_app/microbithal_microphone: Make input streaming use pullInto.
dpgeorge Feb 26, 2024
8528e89
codal_port/microbit_microphone: Implement wait argument to record_into.
dpgeorge Mar 25, 2024
5d1b3b5
codal_app/microbithal_microphone: Remove CODAL workaround.
dpgeorge Mar 25, 2024
7ba6c03
codal_app/microbithal_microphone: Add func to set the sensitivity.
dpgeorge Apr 22, 2024
b19fbde
codal_port/microbit_microphone: Add set_sensitivity method and consts.
dpgeorge Apr 22, 2024
8041d86
codal_port/modaudio: Ensure AudioFrame size is non-zero.
dpgeorge Apr 22, 2024
80cfac7
codal_port/microbit_microphone: Validate duration and rate args.
dpgeorge Apr 22, 2024
61aaf2a
codal_port/modaudio: Allow AudioFrame to be arbitrary length.
dpgeorge Apr 29, 2024
e8a5d61
codal_port/modaudio: Make None the default to AudioFrame().
dpgeorge Apr 29, 2024
0441aa2
codal_port/microbit_microphone: Require rate to be positive.
dpgeorge Apr 29, 2024
c7f64c1
codal_port/modaudio: Fix typecode of AudioFrame buffer to be unsigned.
dpgeorge Apr 30, 2024
c741736
codal_port/modaudio: Use mp_sched_schedule_node for audio fetcher.
dpgeorge May 20, 2024
26d077a
codal_port/modaudio: Separate default AudioFrame and output buffer size.
dpgeorge May 20, 2024
a4fb09b
codal_port/modaudio: Allow output buffer to be larger than 32 bytes.
dpgeorge May 20, 2024
a280e25
codal_port/modaudio: Stop streaming audio when data is exhausted.
dpgeorge May 27, 2024
9819ff8
codal_port: Implement AudioTrack and AudioRecording.
dpgeorge Aug 1, 2024
3fba8c9
src: Update test_record.py.
dpgeorge Aug 2, 2024
96a24ff
codal_port/modaudio: Remove used_size entry from AudioFrame type.
dpgeorge Aug 5, 2024
02e5289
codal_port/modaudio: Make AudioFrame add and mult helpers public.
dpgeorge Aug 21, 2024
30251c7
codal_port/microbit_audiotrack: Implement +,+=,-,-=,*,*= on AudioTrack.
dpgeorge Aug 21, 2024
ceb13c8
src: Update test_record.py to use mult for volume.
dpgeorge Aug 21, 2024
e337b94
src: In test_record.py, make sure my_track is defined.
dpgeorge Aug 22, 2024
d4c7b67
codal_port/microbit_audiorecording: Make track() args positional.
dpgeorge Aug 26, 2024
5cc0cb0
src: Use was_pressed in test_record.py.
dpgeorge Sep 9, 2024
5b36b01
codal_port/modaudio: Improve feeding of audio pipeline.
dpgeorge Sep 9, 2024
311dece
codal_app/microbithal_audio: Add microbit_hal_audio_is_playing() func.
dpgeorge Sep 9, 2024
4c957c4
codal_port/modaudio: Make audio waiting wait for audio to be silent.
dpgeorge Sep 9, 2024
df4cb05
codal_port/modaudio: Make sure final partial audio data is sent out.
dpgeorge Sep 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
codal_port/modaudio: Remove buffer expansion.
Playing generated samples (eg from v1 code) now sounds better without
buffer expansion.

Signed-off-by: Damien George <damien@micropython.org>
  • Loading branch information
dpgeorge committed Sep 18, 2024
commit 1dc4c1243674ff3d4539ceed320c54a13d618023
19 changes: 5 additions & 14 deletions src/codal_port/modaudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,14 @@
#define LOG_AUDIO_CHUNK_SIZE (5)
#define AUDIO_CHUNK_SIZE (1 << LOG_AUDIO_CHUNK_SIZE)
#define DEFAULT_SAMPLE_RATE (7812)
#define BUFFER_EXPANSION (4) // smooth out the samples via linear interpolation
#define OUT_CHUNK_SIZE (BUFFER_EXPANSION * AUDIO_CHUNK_SIZE)

typedef enum {
AUDIO_OUTPUT_STATE_IDLE,
AUDIO_OUTPUT_STATE_DATA_READY,
AUDIO_OUTPUT_STATE_DATA_WRITTEN,
} audio_output_state_t;

static uint8_t audio_output_buffer[OUT_CHUNK_SIZE];
static uint8_t audio_output_buffer[AUDIO_CHUNK_SIZE];
static volatile audio_output_state_t audio_output_state;
static volatile bool audio_fetcher_scheduled;
static size_t audio_raw_offset;
Expand Down Expand Up @@ -128,16 +126,9 @@ static void audio_data_fetcher(void) {
audio_raw_offset += AUDIO_CHUNK_SIZE;

uint8_t *dest = &audio_output_buffer[0];
uint32_t last = dest[OUT_CHUNK_SIZE - 1];
for (int i = 0; i < AUDIO_CHUNK_SIZE; ++i) {
uint32_t cur = src[i];
for (int j = 0; j < BUFFER_EXPANSION; ++j) {
// Get next sample with linear interpolation.
uint32_t sample = ((BUFFER_EXPANSION - 1 - j) * last + (j + 1) * cur) / BUFFER_EXPANSION;
// Write sample to the buffer.
*dest++ = sample;
}
last = cur;
// Copy sample to the buffer.
*dest++ = src[i];
}

audio_buffer_ready();
Expand All @@ -152,7 +143,7 @@ static MP_DEFINE_CONST_FUN_OBJ_1(audio_data_fetcher_wrapper_obj, audio_data_fetc
void microbit_hal_audio_raw_ready_callback(void) {
if (audio_output_state == AUDIO_OUTPUT_STATE_DATA_READY) {
// there is data ready to send out to the audio pipeline, so send it
microbit_hal_audio_raw_write_data(&audio_output_buffer[0], OUT_CHUNK_SIZE);
microbit_hal_audio_raw_write_data(&audio_output_buffer[0], AUDIO_CHUNK_SIZE);
audio_output_state = AUDIO_OUTPUT_STATE_DATA_WRITTEN;
} else {
// no data ready, need to call this function later when data is ready
Expand All @@ -167,7 +158,7 @@ void microbit_hal_audio_raw_ready_callback(void) {
static void audio_init(uint32_t sample_rate) {
audio_fetcher_scheduled = false;
audio_output_state = AUDIO_OUTPUT_STATE_IDLE;
microbit_hal_audio_raw_init(BUFFER_EXPANSION * sample_rate);
microbit_hal_audio_raw_init(sample_rate);
}

void microbit_audio_play_source(mp_obj_t src, mp_obj_t pin_select, bool wait, uint32_t sample_rate) {
Expand Down