Skip to content

Commit b73637d

Browse files
WIP record audio
1 parent 292addc commit b73637d

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

src/board/audio/index.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface AudioOptions {
1313
speechAudioCallback: () => void;
1414
}
1515

16-
export class Audio {
16+
export class BoardAudio {
1717
private frequency: number = 440;
1818
// You can mute the sim before it's running so we can't immediately write to the muteNode.
1919
private muted: boolean = false;
@@ -66,6 +66,46 @@ export class Audio {
6666
);
6767
}
6868

69+
async record() {
70+
let mediaRecorder: MediaRecorder | undefined;
71+
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
72+
try {
73+
const stream = await navigator.mediaDevices.getUserMedia({ video: false, audio: true });
74+
mediaRecorder = new MediaRecorder(stream);
75+
mediaRecorder.start();
76+
77+
setTimeout(() => {
78+
if (mediaRecorder) {
79+
mediaRecorder.stop();
80+
}
81+
}, 5000)
82+
83+
const chunks:Blob[] = []
84+
mediaRecorder.ondataavailable = (e: BlobEvent) => {
85+
chunks.push(e.data);
86+
}
87+
88+
mediaRecorder.onstop = async () => {
89+
if (chunks.length > 0) {
90+
const recordingType = 'audio/wav; codecs=MS_PCM'
91+
const blob = new Blob(chunks, { type: recordingType });
92+
const audioURL = window.URL.createObjectURL(blob);
93+
const recording = new Audio(audioURL);
94+
await recording.play()
95+
}
96+
}
97+
98+
} catch (error) {
99+
console.log("An error occurred, could not get microphone access");
100+
if (mediaRecorder) {
101+
mediaRecorder.stop();
102+
}
103+
}
104+
} else {
105+
console.log("getUserMedia not supported on your browser!");
106+
}
107+
}
108+
69109
async createAudioContextFromUserInteraction(): Promise<void> {
70110
this.context =
71111
this.context ??

src/board/index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import svgText from "../microbit-drawing.svg";
22
import { Accelerometer } from "./accelerometer";
3-
import { Audio } from "./audio";
3+
import { BoardAudio } from "./audio";
44
import { Button } from "./buttons";
55
import { Compass } from "./compass";
66
import {
@@ -92,7 +92,7 @@ export class Board {
9292
display: Display;
9393
buttons: Button[];
9494
pins: Pin[];
95-
audio: Audio;
95+
audio: BoardAudio;
9696
temperature: RangeSensor;
9797
microphone: Microphone;
9898
accelerometer: Accelerometer;
@@ -202,7 +202,7 @@ export class Board {
202202
this.pins[MICROBIT_HAL_PIN_P19] = new StubPin("pin19");
203203
this.pins[MICROBIT_HAL_PIN_P20] = new StubPin("pin20");
204204

205-
this.audio = new Audio();
205+
this.audio = new BoardAudio();
206206
this.temperature = new RangeSensor("temperature", -5, 50, 21, "°C");
207207
this.accelerometer = new Accelerometer(onChange);
208208
this.compass = new Compass();
@@ -513,6 +513,10 @@ export class Board {
513513
return this.runningPromise;
514514
}
515515

516+
async record(): Promise<void> {
517+
await this.audio.record()
518+
}
519+
516520
/**
517521
* An external reset.
518522
*/
@@ -782,6 +786,10 @@ export const createMessageListener = (board: Board) => (e: MessageEvent) => {
782786
board.stop();
783787
break;
784788
}
789+
case "record": {
790+
board.record();
791+
break;
792+
}
785793
case "reset": {
786794
board.reset();
787795
break;

src/demo.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ <h1>MicroPython-micro:bit simulator example embedding</h1>
115115
<div class="actions">
116116
<button id="stop">Stop</button>
117117
<button id="reset">Reset</button>
118+
<button id="record">Record</button>
118119
<button id="mute">Mute</button>
119120
<button id="unmute">Unmute</button>
120121
</div>
@@ -241,6 +242,15 @@ <h1>MicroPython-micro:bit simulator example embedding</h1>
241242
);
242243
});
243244

245+
document.querySelector("#record").addEventListener("click", async () => {
246+
simulator.postMessage(
247+
{
248+
kind: "record",
249+
},
250+
"*"
251+
);
252+
});
253+
244254
document.querySelector("#reset").addEventListener("click", async () => {
245255
simulator.postMessage(
246256
{

0 commit comments

Comments
 (0)