From 4da3f33b88394e170592bc700bca5e525b85cfe0 Mon Sep 17 00:00:00 2001 From: Grace Date: Fri, 3 May 2024 15:07:36 +0100 Subject: [PATCH] Add microphone.set_sensitivity example and js hal --- src/board/audio/index.ts | 22 ++++++++++++++----- src/demo.html | 1 + src/examples/microphone_sensitivity.py | 30 ++++++++++++++++++++++++++ src/jshal.h | 1 + src/jshal.js | 5 +++++ src/microbithal_js.c | 8 +++++++ 6 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 src/examples/microphone_sensitivity.py diff --git a/src/board/audio/index.ts b/src/board/audio/index.ts index bf128487..14c938cb 100644 --- a/src/board/audio/index.ts +++ b/src/board/audio/index.ts @@ -22,6 +22,7 @@ export class BoardAudio { private oscillator: OscillatorNode | undefined; private volumeNode: GainNode | undefined; private muteNode: GainNode | undefined; + private sensitivityNode: GainNode | undefined; default: BufferedAudio | undefined; speech: BufferedAudio | undefined; @@ -46,6 +47,11 @@ export class BoardAudio { this.context.currentTime ); this.muteNode.connect(this.context.destination); + this.sensitivityNode = this.context.createGain(); + this.sensitivityNode.gain.setValueAtTime( + 0.2, // sensitivity medium level + this.context.currentTime + ); this.volumeNode = this.context.createGain(); this.volumeNode.connect(this.muteNode); @@ -133,6 +139,14 @@ export class BoardAudio { } } + setSensitivity(sensitivity: number) { + this.sensitivityNode!.gain.setValueAtTime( + // check if this is correct + sensitivity, + this.context!.currentTime + ) + } + setVolume(volume: number) { this.volumeNode!.gain.setValueAtTime( volume / 255, @@ -195,9 +209,7 @@ export class BoardAudio { this.microphoneEl.style.display = "unset" const source = this.context!.createMediaStreamSource(micStream); - // TODO: wire up microphone sensitivity to this gain node - const gain = this.context!.createGain(); - source.connect(gain); + source.connect(this.sensitivityNode!); // TODO: consider AudioWorklet - worth it? Browser support? // consider alternative resampling approaches // what sample rates are actually supported this way? @@ -222,12 +234,12 @@ export class BoardAudio { }); offlineContext.startRendering(); }; - gain.connect(recorder); + this.sensitivityNode!.connect(recorder); recorder.connect(this.context!.destination); this.stopActiveRecording = () => { recorder.disconnect(); - gain.disconnect(); + this.sensitivityNode!.disconnect(); source.disconnect(); micStream.getTracks().forEach(track => track.stop()) this.microphoneEl.style.display = "none" diff --git a/src/demo.html b/src/demo.html index f293bc29..90bcbbc5 100644 --- a/src/demo.html +++ b/src/demo.html @@ -90,6 +90,7 @@

MicroPython-micro:bit simulator example embedding

+ diff --git a/src/examples/microphone_sensitivity.py b/src/examples/microphone_sensitivity.py new file mode 100644 index 00000000..75eab189 --- /dev/null +++ b/src/examples/microphone_sensitivity.py @@ -0,0 +1,30 @@ +from microbit import microphone, audio, button_a, button_b, pin_logo + +sensitivities = [ + microphone.SENSITIVITY_LOW, + microphone.SENSITIVITY_MEDIUM, + microphone.SENSITIVITY_HIGH +] +sensitivity_index = 0 + +print("Recording...") +frame = microphone.record(3000) +print("Button A to play") +print("Button B to record") +print("Logo to change sensitivity") + +while True: + if button_a.was_pressed(): + print("Start playing") + audio.play(frame, wait=True) + print("Playing ended") + + if button_b.was_pressed(): + print("Recording...") + frame = microphone.record(3000) + + if pin_logo.was_touched(): + sensitivity_index = (sensitivity_index + 1) % len(sensitivities) + print("Sensitivity change to", sensitivities[sensitivity_index]) + microphone.set_sensitivity(sensitivities[sensitivity_index]) + diff --git a/src/jshal.h b/src/jshal.h index bad3e076..19a9d1a1 100644 --- a/src/jshal.h +++ b/src/jshal.h @@ -83,6 +83,7 @@ void mp_js_hal_audio_stop_expression(void); bool mp_js_hal_audio_is_expression_active(void); void mp_js_hal_microphone_init(void); +void mp_js_hal_microphone_set_sensitivity(float value); void mp_js_hal_microphone_set_threshold(int kind, int value); int mp_js_hal_microphone_get_level(void); void mp_js_hal_microphone_start_recording(uint8_t *buf, size_t max_len, size_t *cur_len, int rate); diff --git a/src/jshal.js b/src/jshal.js index b20fdf97..bcb72080 100644 --- a/src/jshal.js +++ b/src/jshal.js @@ -278,6 +278,11 @@ mergeInto(LibraryManager.library, { Module.board.microphone.microphoneOn(); }, + mp_js_hal_microphone_set_sensitivity: function ( + /** @type {number} */ value + ) { + Module.board.audio.setSensitivity(value) + }, mp_js_hal_microphone_set_threshold: function ( /** @type {number} */ kind, /** @type {number} */ value diff --git a/src/microbithal_js.c b/src/microbithal_js.c index fc6563d6..a2ebdc1f 100644 --- a/src/microbithal_js.c +++ b/src/microbithal_js.c @@ -484,6 +484,14 @@ void microbit_hal_microphone_init(void) { */ } +void microbit_hal_microphone_set_sensitivity(float value) +{ + mp_js_hal_microphone_set_sensitivity(value); + /* + uBit.audio.processor->setGain(value); + */ +} + void microbit_hal_microphone_set_threshold(int kind, int value) { mp_js_hal_microphone_set_threshold(kind, value); /*