From 58f75d38dcfd113c90e4e27e55468d06412c653e Mon Sep 17 00:00:00 2001 From: Lionel Han <47760119+IGoByJoe@users.noreply.github.com> Date: Sat, 1 Aug 2020 16:34:48 -0700 Subject: fixed bugs and added waveform (need fix) --- src/client/documents/Documents.ts | 2 +- src/client/views/nodes/AudioBox.scss | 10 +++- src/client/views/nodes/AudioBox.tsx | 91 +++++++++++++++++++++++++++++++++++- src/typings/index.d.ts | 1 + 4 files changed, 101 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/client/documents/Documents.ts b/src/client/documents/Documents.ts index 122383744..88f470576 100644 --- a/src/client/documents/Documents.ts +++ b/src/client/documents/Documents.ts @@ -628,7 +628,7 @@ export namespace Docs { } export function AudioDocument(url: string, options: DocumentOptions = {}) { - const instance = InstanceFromProto(Prototypes.get(DocumentType.AUDIO), new AudioField(new URL(url)), { hideLinkButton: true, useLinkSmallAnchor: true, ...options }); + const instance = InstanceFromProto(Prototypes.get(DocumentType.AUDIO), new AudioField(new URL(url)), { ...options }); // hideLinkButton: false, useLinkSmallAnchor: false, Doc.GetProto(instance).backgroundColor = ComputedField.MakeFunction("this._audioState === 'playing' ? 'green':'gray'"); return instance; } diff --git a/src/client/views/nodes/AudioBox.scss b/src/client/views/nodes/AudioBox.scss index 6d52988bb..35ab5da8b 100644 --- a/src/client/views/nodes/AudioBox.scss +++ b/src/client/views/nodes/AudioBox.scss @@ -140,6 +140,14 @@ height: 100%; background-color: red; position: absolute; + top: 0px; + } + + .waveform { + position: relative; + width: 100%; + height: 100%; + overflow: hidden; } .audiobox-linker, @@ -259,7 +267,7 @@ .audiobox-marker-container1:hover, .audiobox-marker-minicontainer:hover { - opacity: 1; + opacity: 0.8; } .audiobox-marker-minicontainer { diff --git a/src/client/views/nodes/AudioBox.tsx b/src/client/views/nodes/AudioBox.tsx index 34f87fc10..3d1932883 100644 --- a/src/client/views/nodes/AudioBox.tsx +++ b/src/client/views/nodes/AudioBox.tsx @@ -29,7 +29,8 @@ import { LabelBox } from "./LabelBox"; import { Transform } from "../../util/Transform"; import { Scripting } from "../../util/Scripting"; import { ColorBox } from "./ColorBox"; - +import Waveform from "react-audio-waveform" +import axios from "axios" // testing testing @@ -73,6 +74,7 @@ export class AudioBox extends ViewBoxAnnotatableComponent = []; _first: boolean = false; + _buckets: Array = new Array(); private _isPointerDown = false; private _currMarker: any; @@ -80,6 +82,7 @@ export class AudioBox extends ViewBoxAnnotatableComponent = []; + // @observable private _buckets: Array = new Array(); @observable private _paused: boolean = false; @observable private static _scrubTime = 0; @@ -299,6 +302,81 @@ export class AudioBox extends ViewBoxAnnotatableComponent { + let audioCtx = new (window.AudioContext)(); + const buckets: number[] = []; + + axios({ url: this.path, responseType: "arraybuffer" }) + .then(async response => { + let audioData = response.data; + + await 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); + 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(); + } + @computed get audio() { const interactive = this.active() ? "-interactive" : ""; return