Skip to content

Commit bf67483

Browse files
committed
implement live waveform feature
1 parent 5db4a28 commit bf67483

File tree

2 files changed

+45
-23
lines changed

2 files changed

+45
-23
lines changed

js/utils/synthutils.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,25 +2006,18 @@ function Synth() {
20062006
* @memberof Synth
20072007
*/
20082008
this.startRecording = async () => {
2009-
20102009
await Tone.start();
2011-
20122010
this.mic = new Tone.UserMedia();
20132011
this.recorder = new Tone.Recorder();
2014-
20152012
await this.mic.open()
20162013
.then(() => {
20172014
console.log("Mic opened");
2018-
20192015
this.mic.connect(this.recorder);
2020-
20212016
this.recorder.start();
2022-
20232017
})
20242018
.catch((error) => {
20252019
console.log(error);
20262020
});
2027-
20282021
}
20292022

20302023
/**
@@ -2033,12 +2026,10 @@ function Synth() {
20332026
* @memberof Synth
20342027
*/
20352028
this.stopRecording = async () => {
2036-
2037-
const recording = await this.recorder.stop();
2029+
this.recording = await this.recorder.stop();
20382030
this.mic.close();
2039-
this.audioURL = URL.createObjectURL(recording);
2031+
this.audioURL = URL.createObjectURL(this.recording);
20402032
return this.audioURL;
2041-
20422033
}
20432034

20442035
/**
@@ -2047,12 +2038,30 @@ function Synth() {
20472038
* @memberof Synth
20482039
*/
20492040
this.playRecording = async () => {
2050-
20512041
const player = new Tone.Player().toDestination();
20522042
await player.load(this.audioURL)
20532043
player.start();
2054-
20552044
}
20562045

2046+
/**
2047+
* Analyzing the audio
2048+
* @function
2049+
* @memberof Synth
2050+
*/
2051+
this.LiveWaveForm = () => {
2052+
this.analyser = new Tone.Analyser('waveform', 8192);
2053+
this.mic.connect(this.analyser);
2054+
}
2055+
2056+
/**
2057+
* Gets real-time waveform values
2058+
* @function
2059+
* @memberof Synth
2060+
*/
2061+
this.getValues = () => {
2062+
const values = this.analyser.getValue();
2063+
return values;
2064+
};
2065+
20572066
return this;
20582067
}

js/widgets/sampler.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -458,17 +458,20 @@ function SampleWidget() {
458458
this._playbackBtn.id="playbackBtn";
459459
this._playbackBtn.classList.add("disabled");
460460

461-
let is_recording = false;
461+
this.is_recording = false;
462462

463463
this._recordBtn.onclick = async () => {
464-
if (!is_recording) {
464+
if (!this.is_recording) {
465465
await this.activity.logo.synth.startRecording();
466-
is_recording = true;
466+
this.is_recording = true;
467467
this._recordBtn.getElementsByTagName('img')[0].src = "header-icons/record.svg";
468468
this.displayRecordingStartMessage();
469+
this.activity.logo.synth.LiveWaveForm();
470+
471+
// Display Live Waveform
469472
} else {
470473
this.recordingURL = await this.activity.logo.synth.stopRecording();
471-
is_recording = false;
474+
this.is_recording = false;
472475
this._recordBtn.getElementsByTagName('img')[0].src = "header-icons/mic.svg";
473476
this.displayRecordingStopMessage();
474477
this._playbackBtn.classList.remove("disabled");
@@ -480,6 +483,7 @@ function SampleWidget() {
480483
this.sampleName = `Recorded Audio ${this.recordingURL}`;
481484
this._addSample();
482485
this.activity.logo.synth.playRecording();
486+
// Display Recorded Waveform
483487
};
484488

485489
widgetWindow.sendToCenter();
@@ -910,13 +914,13 @@ function SampleWidget() {
910914

911915
const draw = () => {
912916
this.drawVisualIDs[turtleIdx] = requestAnimationFrame(draw);
913-
if (this.pitchAnalysers[turtleIdx] && (this.running || resized)) {
917+
if (this.is_recording || (this.pitchAnalysers[turtleIdx] && (this.running || resized))) {
914918
canvasCtx.fillStyle = "#FFFFFF";
915919
canvasCtx.font = "10px Verdana";
916920
this.verticalOffset = -canvas.height / 4;
917921
this.zoomFactor = 40.0;
918922
canvasCtx.fillRect(0, 0, width, height);
919-
923+
920924
let oscText;
921925
if (turtleIdx >= 0) {
922926
//.TRANS: The sound sample that the user uploads.
@@ -926,18 +930,27 @@ function SampleWidget() {
926930
//.TRANS: The reference tone is a sound used for comparison.
927931
canvasCtx.fillText(_("reference tone"), 10, 10);
928932
canvasCtx.fillText(oscText, 10, canvas.height / 2 + 10);
929-
933+
930934
for (let turtleIdx = 0; turtleIdx < 2; turtleIdx += 1) {
931-
const dataArray = this.pitchAnalysers[turtleIdx].getValue();
935+
let dataArray;
936+
if (this.is_recording) {
937+
dataArray = turtleIdx === 0
938+
? this.pitchAnalysers[0].getValue()
939+
: this.activity.logo.synth.getValues();
940+
console.log(dataArray);
941+
} else {
942+
dataArray = this.pitchAnalysers[turtleIdx].getValue();
943+
}
944+
932945
const bufferLength = dataArray.length;
933946
const rbga = SAMPLEOSCCOLORS[turtleIdx];
934947
const sliceWidth = (width * this.zoomFactor) / bufferLength;
935948
canvasCtx.lineWidth = 2;
936949
canvasCtx.strokeStyle = rbga;
937950
canvasCtx.beginPath();
938-
951+
939952
let x = 0;
940-
953+
941954
for (let i = 0; i < bufferLength; i++) {
942955
const y = (height / 2) * (1 - dataArray[i]) + this.verticalOffset;
943956
if (i === 0) {

0 commit comments

Comments
 (0)