From b827bdfecc5646b479c7e5654ed98243de91cf9a Mon Sep 17 00:00:00 2001 From: Lionel Han <47760119+IGoByJoe@users.noreply.github.com> Date: Thu, 6 Aug 2020 17:24:56 -0700 Subject: cleaned up code and fixed bugs --- src/client/views/nodes/AudioBox.tsx | 415 ++++++--------------- src/client/views/nodes/AudioResizer.scss | 11 - src/client/views/nodes/AudioResizer.tsx | 48 --- .../views/nodes/formattedText/FormattedTextBox.tsx | 18 +- 4 files changed, 114 insertions(+), 378 deletions(-) delete mode 100644 src/client/views/nodes/AudioResizer.scss delete mode 100644 src/client/views/nodes/AudioResizer.tsx (limited to 'src') diff --git a/src/client/views/nodes/AudioBox.tsx b/src/client/views/nodes/AudioBox.tsx index 1d060120e..11dfe5fe6 100644 --- a/src/client/views/nodes/AudioBox.tsx +++ b/src/client/views/nodes/AudioBox.tsx @@ -60,7 +60,6 @@ export class AudioBox extends ViewBoxAnnotatableComponent = []; _first: boolean = false; _dragging = false; @@ -77,7 +76,6 @@ export class AudioBox extends ViewBoxAnnotatableComponent { runInAction(() => AudioBox._scrubTime = 0); runInAction(() => AudioBox._scrubTime = timeInMillisFrom1970); }; @@ -126,36 +124,38 @@ export class AudioBox extends ViewBoxAnnotatableComponent { + // for determining if the link is created after recording (since it will use linkTime rather than creation date) + DocListCast(this.dataDoc.links).map((l, i) => { let la1 = l.anchor1 as Doc; let la2 = l.anchor2 as Doc; - let linkTime = NumCast(l.anchor2_timecode); - let endTime; - if (Doc.AreProtosEqual(la1, this.dataDoc)) { - la1 = l.anchor2 as Doc; - la2 = l.anchor1 as Doc; - linkTime = NumCast(l.anchor1_timecode); - } - if (la2.audioStart) { - linkTime = NumCast(la2.audioStart); - } + if (la1 === sel || la2 === sel) { // if the selected document is linked to this audio + let linkTime = NumCast(l.anchor2_timecode); + let endTime; + if (Doc.AreProtosEqual(la1, this.dataDoc)) { + la1 = l.anchor2 as Doc; + la2 = l.anchor1 as Doc; + linkTime = NumCast(l.anchor1_timecode); + } + if (la2.audioStart) { + linkTime = NumCast(la2.audioStart); + } - if (la1.audioStart) { - linkTime = NumCast(la1.audioStart); - } + if (la1.audioStart) { + linkTime = NumCast(la1.audioStart); + } - if (la1.audioEnd) { - endTime = NumCast(la1.audioEnd); - } + if (la1.audioEnd) { + endTime = NumCast(la1.audioEnd); + } - if (la2.audioEnd) { - endTime = NumCast(la2.audioEnd); - } + if (la2.audioEnd) { + endTime = NumCast(la2.audioEnd); + } - if (linkTime) { - link = true; - this.layoutDoc.playOnSelect && this.recordingStart && sel && !Doc.AreProtosEqual(sel, this.props.Document) && endTime ? this.playFrom(linkTime, endTime) : this.playFrom(linkTime); + if (linkTime) { + link = true; + this.layoutDoc.playOnSelect && this.recordingStart && sel && !Doc.AreProtosEqual(sel, this.props.Document) && (endTime ? this.playFrom(linkTime, endTime) : this.playFrom(linkTime)); + } } }); } @@ -189,19 +189,19 @@ export class AudioBox extends ViewBoxAnnotatableComponent { - if (this._repeat) { - this.playFrom(0); - } else { - this._ele!.pause(); - this.audioState = "paused"; - } + console.log("pause"); + this._ele!.pause(); + this.audioState = "paused"; }); + // play audio for documents created during recording playFromTime = (absoluteTime: number) => { this.recordingStart && this.playFrom((absoluteTime - this.recordingStart) / 1000); } + // play back the audio from time @action playFrom = (seekTimeInSeconds: number, endTime: number = this.dataDoc.duration) => { let play; @@ -220,15 +220,15 @@ export class AudioBox extends ViewBoxAnnotatableComponent this.audioState = "playing"); if (endTime !== this.dataDoc.duration) { - play = setTimeout(() => this.pause(), (this._duration) * 1000); + play = setTimeout(() => this.pause(), (this._duration) * 1000); // use setTimeout to play a specific duration } - } else { // this is getting called because time is greater than duration + } else { this.pause(); } } } - + // update the recording time updateRecordTime = () => { if (this.audioState === "recording") { if (this._paused) { @@ -241,6 +241,7 @@ export class AudioBox extends ViewBoxAnnotatableComponent { this._stream = await navigator.mediaDevices.getUserMedia({ audio: true }); this._recorder = new MediaRecorder(this._stream); @@ -259,6 +260,7 @@ export class AudioBox extends ViewBoxAnnotatableComponent this._recorder && this.stopRecording(), 60 * 60 * 1000); // stop after an hour } + // context menu specificContextMenu = (e: React.MouseEvent): void => { const funcs: ContextMenuProps[] = []; funcs.push({ description: (this.layoutDoc.playOnSelect ? "Don't play" : "Play") + " when link is selected", event: () => this.layoutDoc.playOnSelect = !this.layoutDoc.playOnSelect, icon: "expand-arrows-alt" }); @@ -268,6 +270,7 @@ export class AudioBox extends ViewBoxAnnotatableComponent { this._recorder.stop(); this._recorder = undefined; @@ -278,6 +281,7 @@ export class AudioBox extends ViewBoxAnnotatableComponent { if (e.button === 0 && !e.ctrlKey) { this._recorder ? this.stopRecording() : this.recordAudioAnnotation(); @@ -285,14 +289,13 @@ export class AudioBox extends ViewBoxAnnotatableComponent { this.playFrom(this._ele!.paused ? this._ele!.currentTime : -1); e.stopPropagation(); } - onStop = (e: any) => { - this.layoutDoc.playOnSelect = !this.layoutDoc.playOnSelect; - e.stopPropagation(); - } + + // creates a text document for dictation onFile = (e: any) => { const newDoc = Docs.Create.TextDocument("", { title: "", _chromeStatus: "disabled", @@ -306,125 +309,21 @@ export class AudioBox extends ViewBoxAnnotatableComponent { e?.addEventListener("timeupdate", this.timecodeChanged); e?.addEventListener("ended", this.pause); this._ele = e; } + // returns the path of the audio file @computed get path() { const field = Cast(this.props.Document[this.props.fieldKey], AudioField); const path = (field instanceof AudioField) ? field.url.href : ""; return path === nullAudio ? "" : path; } - // @action - // buckets = () => { - // let audioCtx = new (window.AudioContext)(); - // const buckets: number[] = []; - - // axios({ url: this.path, responseType: "arraybuffer" }) - // .then(response => runInAction(() => { - // let audioData = response.data; - - // audioCtx.decodeAudioData(audioData, buffer => { - // let decodedAudioData = buffer.getChannelData(0); - // const NUMBER_OF_BUCKETS = 100; - // let bucketDataSize = Math.floor(decodedAudioData.length / NUMBER_OF_BUCKETS); - - // for (let i = 0; i < NUMBER_OF_BUCKETS; i++) { - // let startingPoint = i * bucketDataSize; - // let endingPoint = i * bucketDataSize + bucketDataSize; - // let max = 0; - // for (let j = startingPoint; j < endingPoint; j++) { - // if (decodedAudioData[j] > max) { - // max = decodedAudioData[j]; - // } - // } - // let size = Math.abs(max); - // buckets.push(size / 2); - // } - - // }); - // return buckets - // })); - // } - - @action - buckets = async () => { - let audioCtx = new (window.AudioContext)(); - const buckets: number[] = []; - - axios({ url: this.path, responseType: "arraybuffer" }) - .then(response => { - let audioData = response.data; - - audioCtx.decodeAudioData(audioData, action(buffer => { - let decodedAudioData = buffer.getChannelData(0); - const NUMBER_OF_BUCKETS = 100; - let bucketDataSize = Math.floor(decodedAudioData.length / NUMBER_OF_BUCKETS); - - for (let i = 0; i < NUMBER_OF_BUCKETS; i++) { - let startingPoint = i * bucketDataSize; - let endingPoint = i * bucketDataSize + bucketDataSize; - let max = 0; - for (let j = startingPoint; j < endingPoint; j++) { - if (decodedAudioData[j] > max) { - max = decodedAudioData[j]; - } - } - let size = Math.abs(max); - buckets.push(size / 2); - this._buckets.push(size / 2); - } - - })); - return buckets; - }); - } - - @computed get peaks() { - // let audioCtx = new (window.AudioContext)(); - // let buckets: number[] = []; - - // return (async () => { - // await axios({ url: this.path, responseType: "arraybuffer" }) - // .then(response => { - // let audioData = response.data; - - // audioCtx.decodeAudioData(audioData, buffer => { - // let decodedAudioData = buffer.getChannelData(0); - // const NUMBER_OF_BUCKETS = 100; - // let bucketDataSize = Math.floor(decodedAudioData.length / NUMBER_OF_BUCKETS); - - - - // for (let i = 0; i < NUMBER_OF_BUCKETS; i++) { - // let startingPoint = i * bucketDataSize; - // let endingPoint = i * bucketDataSize + bucketDataSize; - // let max = 0; - // for (let j = startingPoint; j < endingPoint; j++) { - // if (decodedAudioData[j] > max) { - // max = decodedAudioData[j]; - // } - // } - // let size = Math.abs(max); - // console.log(size); - // buckets.push(size / 2); - // console.log(buckets); - // } - // }); - // console.log(buckets); - // return buckets; - // }); - // console.log(buckets.length); - // return buckets; - // })(); - - - return this.buckets(); - } - + // returns the html audio element @computed get audio() { const interactive = this.active() ? "-interactive" : ""; return