Skip to content

Commit

Permalink
minor fix: set record gain to .98, refactor auto stop recording logic
Browse files Browse the repository at this point in the history
  • Loading branch information
terryzfeng committed May 4, 2024
1 parent f64bbad commit 1abd978
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
13 changes: 11 additions & 2 deletions src/components/chuckBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { theChuck, startChuck, connectMic } from "@/host";
import Editor from "@/components/monaco/editor";
import VmMonitor from "@/components/vmMonitor";
import Recorder from "@/components/recorder";
import Recorder, { RecordState } from "@/components/recorder";

// detect operating system
const isWindows = navigator.userAgent.includes("Windows");
Expand Down Expand Up @@ -100,7 +100,16 @@ export default class ChuckBar {
static removeCode() {
theChuck?.removeLastCode().then(
// Success
(shredID: number) => VmMonitor.removeShredRow(shredID),
(shredID: number) => {
VmMonitor.removeShredRow(shredID);
// Also stop recording if no shreds
if (
Recorder.state === RecordState.recording &&
VmMonitor.getNumShreds() === 0
) {
Recorder.stopRecording();
}
},
() => {} // Failure, do nothing
);
}
Expand Down
21 changes: 7 additions & 14 deletions src/components/recorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ import Console from "./console";
import ProjectSystem from "./fileExplorer/projectSystem";
import VmMonitor from "./vmMonitor";

enum RecordState {
export enum RecordState {
stopped = 0,
recording = 1,
armed = 2,
}

enum RecordButtonImage {
record = "img/record-button.svg",
stop = "img/stop-button.svg",
record = "img/record-button.svg",
armed = "img/armed-button.svg",
}

export default class Recorder {
private static state: RecordState = RecordState.stopped;
public static state: RecordState = RecordState.stopped;
public static recordButton: HTMLButtonElement;
public static recordImage: HTMLImageElement;
public static playButton: HTMLButtonElement;
Expand All @@ -54,15 +54,6 @@ export default class Recorder {
Recorder.removeButton = document.getElementById(
"removeButton"
)! as HTMLButtonElement;
// Add special event listener to automatically stop recording when only 1 shred is left
Recorder.removeButton.addEventListener("click", () => {
if (
Recorder.state === RecordState.recording &&
VmMonitor.getNumShreds() === 1
) {
Recorder.stopRecording();
}
});
}

static configureRecorder(audioContext: AudioContext, gainNode: GainNode) {
Expand All @@ -72,10 +63,12 @@ export default class Recorder {
gainNode.connect(Recorder.stream);
Recorder.buffer = [];

// Write record data to buffer
Recorder.recorder.ondataavailable = (e) => {
Recorder.buffer.push(e.data);
};

// When stop recording, convert buffer to wav blob
Recorder.recorder.onstop = async () => {
// Convert buffer to wav blob
const blob = new Blob(Recorder.buffer, {
Expand Down Expand Up @@ -177,7 +170,7 @@ export default class Recorder {
* @param audioBuffer audio buffer to convert
* @returns Promise that resolves to a Blob
*/
export async function convertAudioBufferToWavBlob(
async function convertAudioBufferToWavBlob(
audioBuffer: AudioBuffer
): Promise<Blob> {
return new Promise<Blob>((resolve) => {
Expand Down Expand Up @@ -207,7 +200,7 @@ export async function convertAudioBufferToWavBlob(
* @param name name of blob
* @returns url to the blob
*/
export function getBlobLink(blob: Blob, name: string): string {
function getBlobLink(blob: Blob, name: string): string {
const blobUrl = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = blobUrl;
Expand Down
3 changes: 1 addition & 2 deletions src/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ export async function startChuck() {
theChuck.getParamString("VERSION").then((value: string) => {
Console.print("system version: " + value);
});
// .finally(() => Console.print("WebChucK is running!"));
Console.print(
"number of channels: " + audioContext.destination.maxChannelCount
);
Expand All @@ -116,7 +115,7 @@ export async function startChuck() {

// Configure Recorder
recordGain = audioContext.createGain();
recordGain.gain.value = 0.96; // so it doesn't clip
recordGain.gain.value = 0.98; // Prevents weird clipping artifacts
theChuck.connect(recordGain);
Recorder.configureRecorder(audioContext, recordGain);

Expand Down

0 comments on commit 1abd978

Please sign in to comment.