Skip to content

Commit fda93f4

Browse files
Fix convertAudioBuffer.
1 parent a8fde49 commit fda93f4

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

src/board/conversions.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ import {
1111
MICROBIT_HAL_ACCELEROMETER_EVT_TILT_DOWN,
1212
MICROBIT_HAL_ACCELEROMETER_EVT_TILT_LEFT,
1313
MICROBIT_HAL_ACCELEROMETER_EVT_TILT_RIGHT,
14-
MICROBIT_HAL_ACCELEROMETER_EVT_TILT_UP, MICROBIT_HAL_MICROPHONE_SET_THRESHOLD_HIGH,
15-
MICROBIT_HAL_MICROPHONE_SET_THRESHOLD_LOW
14+
MICROBIT_HAL_ACCELEROMETER_EVT_TILT_UP,
15+
MICROBIT_HAL_MICROPHONE_SET_THRESHOLD_HIGH,
16+
MICROBIT_HAL_MICROPHONE_SET_THRESHOLD_LOW,
1617
} from "./constants";
1718

18-
19-
2019
export function convertSoundThresholdNumberToString(
2120
value: number
2221
): "low" | "high" {
@@ -95,3 +94,16 @@ export function convertAccelerometerNumberToString(value: number): string {
9594
throw new Error(`Invalid value ${value}`);
9695
}
9796
}
97+
98+
export const convertAudioBuffer = (
99+
heap: Uint8Array,
100+
source: number,
101+
target: AudioBuffer
102+
) => {
103+
const channel = target.getChannelData(0);
104+
for (let i = 0; i < channel.length; ++i) {
105+
// Convert from uint8 to -1..+1 float.
106+
channel[i] = (heap[source + i] / 255) * 2 - 1;
107+
}
108+
return target;
109+
};

src/board/wasm.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,4 @@ export class ModuleWrapper {
5353
this.module!.HEAPU8.set(packet, buf);
5454
return buf;
5555
}
56-
57-
convertAudioBuffer(source: number, target: AudioBuffer) {
58-
const channel = target.getChannelData(0);
59-
const heap = this.module.HEAPU8;
60-
for (let i = 0; i < channel.length; ++i) {
61-
// Convert from uint8 to -1..+1 float.
62-
channel[i] = (heap[source + i] / 255) * 2 - 1;
63-
}
64-
return target;
65-
}
6656
}

src/jshal.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ mergeInto(LibraryManager.library, {
174174

175175
mp_js_hal_audio_write_data: function (buf, num_samples) {
176176
Module.board.audio.default.writeData(
177-
Module.board.module.convertAudioBuffer(
177+
Module.conversions.convertAudioBuffer(
178+
Module.HEAPU8,
178179
buf,
179180
Module.board.audio.default.createBuffer(num_samples)
180181
)
@@ -187,7 +188,8 @@ mergeInto(LibraryManager.library, {
187188

188189
mp_js_hal_audio_speech_write_data: function (buf, num_samples) {
189190
Module.board.audio.speech.writeData(
190-
Module.board.module.convertAudioBuffer(
191+
Module.conversions.convertAudioBuffer(
192+
Module.HEAPU8,
191193
buf,
192194
Module.board.audio.speech.createBuffer(num_samples)
193195
)

0 commit comments

Comments
 (0)