mirror of
https://github.com/element-hq/element-web.git
synced 2024-12-16 22:41:48 +03:00
c2d37af1cb
This all started with a bug where the clock wouldn't update appropriately, and ended with a whole refactoring to support later playback in the timeline. Playback and recording instances are now independent, and this applies to the <Playback* /> components as well. Instead of those playback components taking a recording, they take a playback instance which has all the information the components need. The clock was incredibly difficult to do because of the audio context's time tracking and the source's inability to say where it is at in the buffer/in time. This means we have to track when we started playing the clip so we can capture the audio context's current time, which may be a few seconds by the first time the user hits play. We also track stops so we know when to reset that flag. Waveform calculations have also been moved into the base component, deduplicating the math a bit.
78 lines
2.3 KiB
TypeScript
78 lines
2.3 KiB
TypeScript
/*
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
import {SimpleObservable} from "matrix-widget-api";
|
|
import {IDestroyable} from "../utils/IDestroyable";
|
|
|
|
// Because keeping track of time is sufficiently complicated...
|
|
export class PlaybackClock implements IDestroyable {
|
|
private clipStart = 0;
|
|
private stopped = true;
|
|
private lastCheck = 0;
|
|
private observable = new SimpleObservable<number[]>();
|
|
private timerId: number;
|
|
private clipDuration = 0;
|
|
|
|
public constructor(private context: AudioContext) {
|
|
}
|
|
|
|
public get durationSeconds(): number {
|
|
return this.clipDuration;
|
|
}
|
|
|
|
public set durationSeconds(val: number) {
|
|
this.clipDuration = val;
|
|
this.observable.update([this.timeSeconds, this.clipDuration]);
|
|
}
|
|
|
|
public get timeSeconds(): number {
|
|
return (this.context.currentTime - this.clipStart) % this.clipDuration;
|
|
}
|
|
|
|
public get liveData(): SimpleObservable<number[]> {
|
|
return this.observable;
|
|
}
|
|
|
|
private checkTime = () => {
|
|
const now = this.timeSeconds;
|
|
if (this.lastCheck !== now) {
|
|
this.observable.update([now, this.durationSeconds]);
|
|
this.lastCheck = now;
|
|
}
|
|
};
|
|
|
|
public flagStart() {
|
|
if (this.stopped) {
|
|
this.clipStart = this.context.currentTime;
|
|
this.stopped = false;
|
|
}
|
|
|
|
if (!this.timerId) {
|
|
// case to number because the types are wrong
|
|
// 100ms interval to make sure the time is as accurate as possible
|
|
this.timerId = <number><any>setInterval(this.checkTime, 100);
|
|
}
|
|
}
|
|
|
|
public flagStop() {
|
|
this.stopped = true;
|
|
}
|
|
|
|
public destroy() {
|
|
this.observable.close();
|
|
if (this.timerId) clearInterval(this.timerId);
|
|
}
|
|
}
|