2021-04-28 05:27:36 +03:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2021-06-22 01:18:39 +03:00
|
|
|
import { SimpleObservable } from "matrix-widget-api";
|
|
|
|
import { IDestroyable } from "../utils/IDestroyable";
|
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
2021-04-28 05:27:36 +03:00
|
|
|
|
|
|
|
// 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;
|
2021-06-22 01:18:39 +03:00
|
|
|
private placeholderDuration = 0;
|
2021-04-28 05:27:36 +03:00
|
|
|
|
|
|
|
public constructor(private context: AudioContext) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public get durationSeconds(): number {
|
2021-06-22 01:18:39 +03:00
|
|
|
return this.clipDuration || this.placeholderDuration;
|
2021-04-28 05:27:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-22 01:18:39 +03:00
|
|
|
/**
|
|
|
|
* Populates default information about the audio clip from the event body.
|
|
|
|
* The placeholders will be overridden once known.
|
|
|
|
* @param {MatrixEvent} event The event to use for placeholders.
|
|
|
|
*/
|
|
|
|
public populatePlaceholdersFrom(event: MatrixEvent) {
|
|
|
|
const durationSeconds = Number(event.getContent()['info']?.['duration']);
|
|
|
|
if (Number.isFinite(durationSeconds)) this.placeholderDuration = durationSeconds;
|
|
|
|
}
|
|
|
|
|
2021-05-14 07:35:43 +03:00
|
|
|
/**
|
|
|
|
* Mark the time in the audio context where the clip starts/has been loaded.
|
|
|
|
* This is to ensure the clock isn't skewed into thinking it is ~0.5s into
|
|
|
|
* a clip when the duration is set.
|
|
|
|
*/
|
|
|
|
public flagLoadTime() {
|
|
|
|
this.clipStart = this.context.currentTime;
|
|
|
|
}
|
|
|
|
|
2021-04-28 05:27:36 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|