Skip to content

Commit

Permalink
Add microphone.set_sensitivity example and js hal
Browse files Browse the repository at this point in the history
  • Loading branch information
microbit-grace committed May 3, 2024
1 parent 8798367 commit 4da3f33
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/board/audio/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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?
Expand All @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ <h1>MicroPython-micro:bit simulator example embedding</h1>
<option value="display">Display</option>
<option value="inline_assembler">Inline assembler</option>
<option value="microphone">Microphone</option>
<option value="microphone_sensitivity">Microphone sensitivity</option>
<option value="music">Music</option>
<option value="pin_logo">Pin logo</option>
<option value="pin_touches">Pin touches</option>
Expand Down
30 changes: 30 additions & 0 deletions src/examples/microphone_sensitivity.py
Original file line number Diff line number Diff line change
@@ -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])

1 change: 1 addition & 0 deletions src/jshal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/jshal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/microbithal_js.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
/*
Expand Down

0 comments on commit 4da3f33

Please sign in to comment.