Skip to content

[DO NOT MERGE] Sim changes for forthcoming beta MicroPython release #113

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 36 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
699a2d7
Work towards building on the audio-recording branch
microbit-matt-hillsdon Mar 20, 2024
202c7fd
The simulator compiles (with my local mpy change)
microbit-matt-hillsdon Mar 21, 2024
41ff6b0
Add pin touches sample
microbit-grace Mar 21, 2024
292addc
Update pin_touches sample to include logo
microbit-grace Mar 21, 2024
b73637d
WIP record audio
microbit-grace Mar 22, 2024
1b9465a
Initial steps towards microphone HAL
microbit-matt-hillsdon Mar 22, 2024
9cc4b2b
Microphone: get as far as reading samples
microbit-matt-hillsdon Mar 22, 2024
c971228
WIP convertToUnit8Array
microbit-grace Mar 25, 2024
3c4a79c
Update to latest from audio-recording branch
microbit-matt-hillsdon Mar 27, 2024
7aaa800
Remove unused HAL method
microbit-matt-hillsdon Mar 27, 2024
c562955
Reinstate clear on display for non-HAL use (reset)
microbit-matt-hillsdon Mar 27, 2024
88e2baf
Safari 13 compatible speech option
microbit-matt-hillsdon Mar 27, 2024
41e7ce3
Merge branch 'beta-updates' of https://github.com/microbit-foundation…
microbit-grace Mar 27, 2024
2916e18
WIP
microbit-matt-hillsdon Mar 27, 2024
62a338e
WIP playing recorded audio
microbit-matt-hillsdon Mar 27, 2024
8eb6fa7
Update
microbit-matt-hillsdon Apr 2, 2024
26a4ee4
Roughly works
microbit-matt-hillsdon Apr 2, 2024
2cda4d2
Sample program
microbit-matt-hillsdon Apr 2, 2024
d232569
Fix set rate in record.py
microbit-grace Apr 2, 2024
19b095b
Remove debug
microbit-matt-hillsdon Apr 2, 2024
90292b6
Merge branch 'beta-updates' of https://github.com/microbit-foundation…
microbit-grace Apr 3, 2024
3c30c59
Give older Safari a chance
microbit-matt-hillsdon Apr 3, 2024
4f1baa3
Tweak Safari workaround
microbit-matt-hillsdon Apr 3, 2024
ef44717
Fix PR feedback
microbit-matt-hillsdon Apr 3, 2024
5907c05
Tweak sample to allow on-the-fly rate change
microbit-matt-hillsdon Apr 3, 2024
4ed8c93
Remove browser tab mic indicator
microbit-grace Apr 11, 2024
aea7937
Activate sim mic light when recording
microbit-grace Apr 11, 2024
8798367
Update simulator micropython lib
microbit-grace May 3, 2024
4da3f33
Add microphone.set_sensitivity example and js hal
microbit-grace May 3, 2024
6042086
Update to latest
microbit-matt-hillsdon May 23, 2024
06b4549
Tweak AUDIO_OUTPUT_BUFFER_SIZE and document
microbit-matt-hillsdon May 23, 2024
b887f22
Audio fixes
microbit-matt-hillsdon May 24, 2024
34e45b3
Update MicroPython to fix silent frames issue
microbit-matt-hillsdon May 28, 2024
7d84c58
Update MicroPython
microbit-matt-hillsdon Aug 19, 2024
677585f
Update MicroPython
microbit-matt-hillsdon Aug 21, 2024
ab07528
Resample via a libsamplerate (#117)
microbit-matt-hillsdon Aug 28, 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
Audio fixes
- Use smaller buffer to avoid too much audio after wait=True
- Don't reset nextStartTime to avoid overlapping audio
- Add indirection to callback so we can clear it properly to avoid
  calling it when audio finishes after MicroPython has terminated
  • Loading branch information
microbit-matt-hillsdon committed May 24, 2024
commit b887f22626622c9bf7ca2ee4630697ddb26f34fa
25 changes: 14 additions & 11 deletions src/board/audio/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ export class BoardAudio {
currentSoundExpressionCallback: undefined | (() => void);
private stopActiveRecording: (() => void) | undefined;

constructor(
private microphoneEl: SVGElement
) {}
constructor(private microphoneEl: SVGElement) {}

initializeCallbacks({
defaultAudioCallback,
Expand Down Expand Up @@ -142,9 +140,9 @@ export class BoardAudio {
setSensitivity(sensitivity: number) {
this.sensitivityNode!.gain.setValueAtTime(
// check if this is correct
sensitivity,
sensitivity,
this.context!.currentTime
)
);
}

setVolume(volume: number) {
Expand Down Expand Up @@ -206,7 +204,7 @@ export class BoardAudio {
this.stopRecording();
return;
}
this.microphoneEl.style.display = "unset"
this.microphoneEl.style.display = "unset";

const source = this.context!.createMediaStreamSource(micStream);
source.connect(this.sensitivityNode!);
Expand Down Expand Up @@ -241,8 +239,8 @@ export class BoardAudio {
recorder.disconnect();
this.sensitivityNode!.disconnect();
source.disconnect();
micStream.getTracks().forEach(track => track.stop())
this.microphoneEl.style.display = "none"
micStream.getTracks().forEach((track) => track.stop());
this.microphoneEl.style.display = "none";
this.stopActiveRecording = undefined;
};
}
Expand Down Expand Up @@ -274,8 +272,9 @@ class BufferedAudio {
) {}

init(sampleRate: number) {
// This is called for each new audio source so don't reset nextStartTime
// or we start to overlap audio
this.sampleRate = sampleRate;
this.nextStartTime = -1;
}

createBuffer(length: number) {
Expand All @@ -291,20 +290,24 @@ class BufferedAudio {
// Use createBufferSource instead of new AudioBufferSourceNode to support Safari 14.0.
const source = this.context.createBufferSource();
source.buffer = buffer;
source.onended = this.callback;
source.onended = this.callCallback;
source.connect(this.destination);
const currentTime = this.context.currentTime;
const first = this.nextStartTime < currentTime;
const startTime = first ? currentTime : this.nextStartTime;
this.nextStartTime = startTime + buffer.length / buffer.sampleRate;
// For audio frames, we're frequently out of data. Speech is smooth.
if (first) {
// We're just getting started so buffer another frame.
this.callback();
}
source.start(startTime);
}

private callCallback = () => {
// Indirect so we can clear callback later
this.callback();
};

dispose() {
// Prevent calls into WASM when the buffer nodes finish.
this.callback = () => {};
Expand Down
6 changes: 2 additions & 4 deletions src/jshal.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,8 @@ 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_sensitivity: function (/** @type {number} */ value) {
Module.board.audio.setSensitivity(value);
},
mp_js_hal_microphone_set_threshold: function (
/** @type {number} */ kind,
Expand Down
11 changes: 10 additions & 1 deletion src/microbithal_js.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,16 @@ void microbit_hal_audio_raw_set_rate(uint32_t sample_rate) {
}

void microbit_hal_audio_raw_write_data(const uint8_t *buf, size_t num_samples) {
mp_js_hal_audio_write_data(buf, num_samples);
bool silence = true;
for (const uint8_t *sample = buf; sample < buf + num_samples; ++sample) {
if (*sample != 128) {
silence = false;
break;
}
}
if (!silence) {
mp_js_hal_audio_write_data(buf, num_samples);
}
}

void microbit_hal_audio_speech_init(uint32_t sample_rate) {
Expand Down
2 changes: 1 addition & 1 deletion src/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,6 @@ extern uint32_t rng_generate_random_word(void);
((mp_raise_NotImplementedError(MP_ERROR_TEXT("simulator limitation: asm_thumb code"))), p)

// The latency of fetching 32 byte audio frames is too much so increase the size
#define AUDIO_OUTPUT_BUFFER_SIZE (128)
#define AUDIO_OUTPUT_BUFFER_SIZE (64)

#endif