Skip to content

Commit 4da3f33

Browse files
Add microphone.set_sensitivity example and js hal
1 parent 8798367 commit 4da3f33

File tree

6 files changed

+62
-5
lines changed

6 files changed

+62
-5
lines changed

src/board/audio/index.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export class BoardAudio {
2222
private oscillator: OscillatorNode | undefined;
2323
private volumeNode: GainNode | undefined;
2424
private muteNode: GainNode | undefined;
25+
private sensitivityNode: GainNode | undefined;
2526

2627
default: BufferedAudio | undefined;
2728
speech: BufferedAudio | undefined;
@@ -46,6 +47,11 @@ export class BoardAudio {
4647
this.context.currentTime
4748
);
4849
this.muteNode.connect(this.context.destination);
50+
this.sensitivityNode = this.context.createGain();
51+
this.sensitivityNode.gain.setValueAtTime(
52+
0.2, // sensitivity medium level
53+
this.context.currentTime
54+
);
4955
this.volumeNode = this.context.createGain();
5056
this.volumeNode.connect(this.muteNode);
5157

@@ -133,6 +139,14 @@ export class BoardAudio {
133139
}
134140
}
135141

142+
setSensitivity(sensitivity: number) {
143+
this.sensitivityNode!.gain.setValueAtTime(
144+
// check if this is correct
145+
sensitivity,
146+
this.context!.currentTime
147+
)
148+
}
149+
136150
setVolume(volume: number) {
137151
this.volumeNode!.gain.setValueAtTime(
138152
volume / 255,
@@ -195,9 +209,7 @@ export class BoardAudio {
195209
this.microphoneEl.style.display = "unset"
196210

197211
const source = this.context!.createMediaStreamSource(micStream);
198-
// TODO: wire up microphone sensitivity to this gain node
199-
const gain = this.context!.createGain();
200-
source.connect(gain);
212+
source.connect(this.sensitivityNode!);
201213
// TODO: consider AudioWorklet - worth it? Browser support?
202214
// consider alternative resampling approaches
203215
// what sample rates are actually supported this way?
@@ -222,12 +234,12 @@ export class BoardAudio {
222234
});
223235
offlineContext.startRendering();
224236
};
225-
gain.connect(recorder);
237+
this.sensitivityNode!.connect(recorder);
226238
recorder.connect(this.context!.destination);
227239

228240
this.stopActiveRecording = () => {
229241
recorder.disconnect();
230-
gain.disconnect();
242+
this.sensitivityNode!.disconnect();
231243
source.disconnect();
232244
micStream.getTracks().forEach(track => track.stop())
233245
this.microphoneEl.style.display = "none"

src/demo.html

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ <h1>MicroPython-micro:bit simulator example embedding</h1>
9090
<option value="display">Display</option>
9191
<option value="inline_assembler">Inline assembler</option>
9292
<option value="microphone">Microphone</option>
93+
<option value="microphone_sensitivity">Microphone sensitivity</option>
9394
<option value="music">Music</option>
9495
<option value="pin_logo">Pin logo</option>
9596
<option value="pin_touches">Pin touches</option>
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from microbit import microphone, audio, button_a, button_b, pin_logo
2+
3+
sensitivities = [
4+
microphone.SENSITIVITY_LOW,
5+
microphone.SENSITIVITY_MEDIUM,
6+
microphone.SENSITIVITY_HIGH
7+
]
8+
sensitivity_index = 0
9+
10+
print("Recording...")
11+
frame = microphone.record(3000)
12+
print("Button A to play")
13+
print("Button B to record")
14+
print("Logo to change sensitivity")
15+
16+
while True:
17+
if button_a.was_pressed():
18+
print("Start playing")
19+
audio.play(frame, wait=True)
20+
print("Playing ended")
21+
22+
if button_b.was_pressed():
23+
print("Recording...")
24+
frame = microphone.record(3000)
25+
26+
if pin_logo.was_touched():
27+
sensitivity_index = (sensitivity_index + 1) % len(sensitivities)
28+
print("Sensitivity change to", sensitivities[sensitivity_index])
29+
microphone.set_sensitivity(sensitivities[sensitivity_index])
30+

src/jshal.h

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ void mp_js_hal_audio_stop_expression(void);
8383
bool mp_js_hal_audio_is_expression_active(void);
8484

8585
void mp_js_hal_microphone_init(void);
86+
void mp_js_hal_microphone_set_sensitivity(float value);
8687
void mp_js_hal_microphone_set_threshold(int kind, int value);
8788
int mp_js_hal_microphone_get_level(void);
8889
void mp_js_hal_microphone_start_recording(uint8_t *buf, size_t max_len, size_t *cur_len, int rate);

src/jshal.js

+5
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,11 @@ mergeInto(LibraryManager.library, {
278278
Module.board.microphone.microphoneOn();
279279
},
280280

281+
mp_js_hal_microphone_set_sensitivity: function (
282+
/** @type {number} */ value
283+
) {
284+
Module.board.audio.setSensitivity(value)
285+
},
281286
mp_js_hal_microphone_set_threshold: function (
282287
/** @type {number} */ kind,
283288
/** @type {number} */ value

src/microbithal_js.c

+8
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,14 @@ void microbit_hal_microphone_init(void) {
484484
*/
485485
}
486486

487+
void microbit_hal_microphone_set_sensitivity(float value)
488+
{
489+
mp_js_hal_microphone_set_sensitivity(value);
490+
/*
491+
uBit.audio.processor->setGain(value);
492+
*/
493+
}
494+
487495
void microbit_hal_microphone_set_threshold(int kind, int value) {
488496
mp_js_hal_microphone_set_threshold(kind, value);
489497
/*

0 commit comments

Comments
 (0)