Skip to content
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

feat: add live waveform visualization to sampler #4040

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
54 changes: 54 additions & 0 deletions js/utils/synthutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2000,5 +2000,59 @@ function Synth() {
Tone.Destination.volume.rampTo(db, 0.01);
};

/**
* Starts Recording
* @function
* @memberof Synth
*/
this.startRecording = async () => {

await Tone.start();

this.mic = new Tone.UserMedia();
this.recorder = new Tone.Recorder();

await this.mic.open()
.then(() => {
console.log("Mic opened");

this.mic.connect(this.recorder);

this.recorder.start();

})
.catch((error) => {
console.log(error);
});

}

/**
* Stops Recording
* @function
* @memberof Synth
*/
this.stopRecording = async () => {

const recording = await this.recorder.stop();
this.mic.close();
this.audioURL = URL.createObjectURL(recording);
return this.audioURL;

}

/**
* Plays Recording
* @function
* @memberof Synth
*/
this.playRecording = async () => {

const player = new Tone.Player().toDestination();
await player.load(this.audioURL)
player.start();

}

return this;
}
94 changes: 21 additions & 73 deletions js/widgets/sampler.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,15 @@ function SampleWidget() {
* Displays a message indicating that recording has started.
* @returns {void}
*/
function displayRecordingStartMessage() {
this.displayRecordingStartMessage = function () {
this.activity.textMsg(_("Recording started..."));
}

/**
* Displays a message indicating that recording has stopped.
* @returns {void}
*/
function displayRecordingStopMessage() {
this.displayRecordingStopMessage = function () {
this.activity.textMsg(_("Recording complete..."));
}

Expand Down Expand Up @@ -448,92 +448,40 @@ function SampleWidget() {
_("Toggle Mic"),
""
);
this._recordBtn.onclick = function() {
ToggleMic(this);
}.bind(this._recordBtn);


this._playbackBtn= widgetWindow.addButton(
"playback.svg",
ICONSIZE,
_("Playback"),
"");
this._playbackBtn.id="playbackBtn"
this._playbackBtn.classList.add("disabled");

this._playbackBtn.id="playbackBtn";
this._playbackBtn.classList.add("disabled");

let is_recording = false;

const togglePlaybackButtonState = () => {
if (!audioURL) {
this._playbackBtn.classList.add("disabled");
this._recordBtn.onclick = async () => {
if (!is_recording) {
await this.activity.logo.synth.startRecording();
is_recording = true;
this._recordBtn.getElementsByTagName('img')[0].src = "header-icons/record.svg";
this.displayRecordingStartMessage();
} else {
this.recordingURL = await this.activity.logo.synth.stopRecording();
is_recording = false;
this._recordBtn.getElementsByTagName('img')[0].src = "header-icons/mic.svg";
this.displayRecordingStopMessage();
this._playbackBtn.classList.remove("disabled");
}
};

this._playbackBtn.onclick = () => {
playAudio();
this.sampleData = this.recordingURL;
this.sampleName = `Recorded Audio ${this.recordingURL}`;
this._addSample();
this.activity.logo.synth.playRecording();
};

let can_record = false;
let is_recording = false;
let recorder = null;
let chunks = [];
let audioURL = null;

async function setUpAudio() {
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
recorder = new MediaRecorder(stream);
recorder.ondataavailable = e => {
chunks.push(e.data);
};
recorder.onstop = async e => {
let blob = new Blob(chunks, { type: 'audio/webm' });
chunks = [];
audioURL = URL.createObjectURL(blob);
displayRecordingStopMessage.call(that);
togglePlaybackButtonState();

const module = await import("https://cdn.jsdelivr.net/npm/[email protected]/+esm");
const getWaveBlob = module.getWaveBlob;
const wavBlob = await getWaveBlob(blob);
const wavAudioURL = URL.createObjectURL(wavBlob);
that.sampleData = wavAudioURL;
that.sampleName = `Recorded Audio ${audioURL}`;
that._addSample();
};
can_record = true;
} catch (err) {
console.log("The following error occurred: " + err);
}
}
}
function ToggleMic(buttonElement) {
if (!can_record) return;

is_recording = !is_recording;
if (is_recording) {
recorder.start();
buttonElement.getElementsByTagName('img')[0].src = "header-icons/record.svg";
displayRecordingStartMessage.call(that);
} else {
recorder.stop();
buttonElement.getElementsByTagName('img')[0].src = "header-icons/mic.svg";
}
}

function playAudio() {
if (audioURL) {
const audio = new Audio(audioURL);
audio.play();
console.log("Playing audio.");
} else {
console.error("No recorded audio available.");
}
}

setUpAudio();

widgetWindow.sendToCenter();
this.widgetWindow = widgetWindow;

Expand Down
Loading