pxt-microbit 6.0.10 → 6.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/built/sim.d.ts CHANGED
@@ -573,6 +573,7 @@ declare namespace pxsim {
573
573
  recording: HTMLAudioElement;
574
574
  audioPlaying: boolean;
575
575
  recordTimeoutID: any;
576
+ currentlyErasing: boolean;
576
577
  handleAudioPlaying: () => void;
577
578
  handleAudioStopped: () => void;
578
579
  initListeners: () => void;
package/built/sim.js CHANGED
@@ -2314,6 +2314,13 @@ var pxsim;
2314
2314
  (function (pxsim) {
2315
2315
  var record;
2316
2316
  (function (record_1) {
2317
+ let _initialized = false;
2318
+ function init() {
2319
+ if (!_initialized) {
2320
+ registerSimStop();
2321
+ _initialized = true;
2322
+ }
2323
+ }
2317
2324
  function stopRecorder(b) {
2318
2325
  b.recordingState.recorder.stop();
2319
2326
  b.recordingState.currentlyRecording = false;
@@ -2325,17 +2332,25 @@ var pxsim;
2325
2332
  });
2326
2333
  }
2327
2334
  }
2328
- function populateRecording(b) {
2329
- const blob = new Blob(b.recordingState.chunks, { type: "audio/ogg; codecs=opus" });
2330
- b.recordingState.audioURL = window.URL.createObjectURL(blob);
2331
- b.recordingState.recording = new Audio(b.recordingState.audioURL);
2332
- b.recordingState.initListeners();
2335
+ async function populateRecording(b) {
2336
+ if (b.recordingState.currentlyErasing) {
2337
+ await erasingAsync(b);
2338
+ }
2339
+ if (b.recordingState.chunks[0].size > 0) {
2340
+ b.recordingState.audioURL = null;
2341
+ const recordingType = pxsim.isSafari() ? "audio/mp4" : "audio/ogg; codecs=opus";
2342
+ const blob = new Blob(b.recordingState.chunks, { type: recordingType });
2343
+ b.recordingState.audioURL = window.URL.createObjectURL(blob);
2344
+ b.recordingState.recording = new Audio(b.recordingState.audioURL);
2345
+ b.recordingState.initListeners();
2346
+ }
2333
2347
  b.recordingState.currentlyRecording = false;
2334
2348
  b.recordingState.recorder = null;
2335
2349
  b.recordingState.chunks = [];
2336
2350
  }
2337
2351
  async function record() {
2338
2352
  let b = pxsim.board();
2353
+ init();
2339
2354
  if (b.recordingState.recorder) {
2340
2355
  b.recordingState.recorder.stop();
2341
2356
  clearTimeout(b.recordingState.recordTimeoutID);
@@ -2353,9 +2368,8 @@ var pxsim;
2353
2368
  b.recordingState.recorder.ondataavailable = (e) => {
2354
2369
  b.recordingState.chunks.push(e.data);
2355
2370
  };
2356
- b.recordingState.recorder.onstop = () => {
2357
- populateRecording(b);
2358
- registerSimStop(b);
2371
+ b.recordingState.recorder.onstop = async () => {
2372
+ await populateRecording(b);
2359
2373
  };
2360
2374
  }
2361
2375
  catch (error) {
@@ -2378,17 +2392,19 @@ var pxsim;
2378
2392
  return;
2379
2393
  if (b.recordingState.currentlyRecording && b.recordingState.recordTimeoutID) {
2380
2394
  clearTimeout(b.recordingState.recordTimeoutID);
2381
- stopRecorder(b);
2395
+ if (b.recordingState.recorder) {
2396
+ stopRecorder(b);
2397
+ }
2382
2398
  }
2383
2399
  else if (b.recordingState.recording && b.recordingState.audioPlaying) {
2384
2400
  b.recordingState.handleAudioStopped();
2385
- b.recordingState.recording.pause();
2386
- b.recordingState.recording.currentTime = 0;
2401
+ stopPlayback();
2387
2402
  }
2388
2403
  }
2389
- function registerSimStop(b) {
2404
+ function registerSimStop() {
2390
2405
  pxsim.AudioContextManager.onStopAll(() => {
2391
- if (b.recordingState.recording) {
2406
+ const b = pxsim.board();
2407
+ if (b && b.recordingState && b.recordingState.recording) {
2392
2408
  stopAudio();
2393
2409
  b.recordingState.recording.removeEventListener("play", b.recordingState.handleAudioPlaying);
2394
2410
  b.recordingState.recording.removeEventListener("ended", b.recordingState.handleAudioStopped);
@@ -2399,11 +2415,22 @@ var pxsim;
2399
2415
  const b = pxsim.board();
2400
2416
  if (!b)
2401
2417
  return;
2418
+ init();
2402
2419
  stopAudio();
2403
2420
  b.recordingState.audioPlaying = true;
2404
- setTimeout(() => {
2405
- if (b.recordingState.recording) {
2406
- b.recordingState.recording.play();
2421
+ setTimeout(async () => {
2422
+ if (!b.recordingState.currentlyErasing && b.recordingState.recording) {
2423
+ try {
2424
+ await b.recordingState.recording.play();
2425
+ }
2426
+ catch (e) {
2427
+ if (!(e instanceof DOMException)) {
2428
+ throw e;
2429
+ }
2430
+ }
2431
+ }
2432
+ else {
2433
+ b.recordingState.audioPlaying = false;
2407
2434
  }
2408
2435
  }, 10);
2409
2436
  }
@@ -2412,18 +2439,35 @@ var pxsim;
2412
2439
  stopAudio();
2413
2440
  }
2414
2441
  record_1.stop = stop;
2442
+ function stopPlayback() {
2443
+ const b = pxsim.board();
2444
+ if (!b)
2445
+ return;
2446
+ b.recordingState.recording.pause();
2447
+ b.recordingState.recording.currentTime = 0;
2448
+ b.recordingState.recording.removeEventListener("play", b.recordingState.handleAudioPlaying);
2449
+ b.recordingState.recording.removeEventListener("ended", b.recordingState.handleAudioStopped);
2450
+ }
2451
+ function erasingAsync(b) {
2452
+ return new Promise((resolve, reject) => {
2453
+ if (b.recordingState.recording && b.recordingState.audioPlaying) {
2454
+ stopPlayback();
2455
+ }
2456
+ if (b.recordingState.audioURL) {
2457
+ window.URL.revokeObjectURL(b.recordingState.audioURL);
2458
+ b.recordingState.recording = null;
2459
+ }
2460
+ b.recordingState.audioPlaying = false;
2461
+ resolve(null);
2462
+ b.recordingState.currentlyErasing = false;
2463
+ });
2464
+ }
2415
2465
  function erase() {
2416
2466
  const b = pxsim.board();
2417
2467
  if (!b)
2418
2468
  return;
2419
2469
  b.recordingState.chunks = [];
2420
- if (b.recordingState.audioPlaying) {
2421
- b.recordingState.recording.pause();
2422
- b.recordingState.audioPlaying = false;
2423
- b.recordingState.recording.currentTime = 0;
2424
- }
2425
- window.URL.revokeObjectURL(b.recordingState.audioURL);
2426
- b.recordingState.recording = null;
2470
+ b.recordingState.currentlyErasing = true;
2427
2471
  }
2428
2472
  record_1.erase = erase;
2429
2473
  function setMicrophoneGain(gain) {