2022-11-27 14:53:22 -07:00
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
|
|
|
|
import * as Tone from "tone";
|
|
|
|
|
|
|
|
import { InferenceResult } from "../types";
|
|
|
|
|
|
|
|
interface AudioPlayerProps {
|
|
|
|
paused: boolean;
|
|
|
|
inferenceResults: InferenceResult[];
|
2022-11-27 17:46:10 -07:00
|
|
|
nowPlayingCallback: (result: InferenceResult, playerTime: number) => void;
|
2022-12-13 23:29:49 -07:00
|
|
|
playerIsBehindCallback: (isBehind: boolean) => void;
|
2022-11-27 14:53:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Audio player with Tone.js
|
|
|
|
*
|
|
|
|
* TODO(hayk): Look into https://github.com/E-Kuerschner/useAudioPlayer as an alternative.
|
|
|
|
*/
|
|
|
|
export default function AudioPlayer({
|
|
|
|
paused,
|
|
|
|
inferenceResults,
|
2022-11-27 17:46:10 -07:00
|
|
|
nowPlayingCallback,
|
2022-12-13 23:29:49 -07:00
|
|
|
playerIsBehindCallback,
|
2022-11-27 14:53:22 -07:00
|
|
|
}: AudioPlayerProps) {
|
|
|
|
const [tonePlayer, setTonePlayer] = useState<Tone.Player>(null);
|
|
|
|
|
|
|
|
const [numClipsPlayed, setNumClipsPlayed] = useState(0);
|
|
|
|
const [prevNumClipsPlayed, setPrevNumClipsPlayed] = useState(0);
|
|
|
|
|
|
|
|
const [resultCounter, setResultCounter] = useState(0);
|
|
|
|
|
|
|
|
// On load, create a player synced to the tone transport
|
|
|
|
useEffect(() => {
|
|
|
|
if (tonePlayer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inferenceResults.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-27 17:46:10 -07:00
|
|
|
const result = inferenceResults[0];
|
2022-11-27 14:53:22 -07:00
|
|
|
|
2022-11-27 17:46:10 -07:00
|
|
|
const player = new Tone.Player(result.audio, () => {
|
2022-11-27 14:53:22 -07:00
|
|
|
player.loop = true;
|
|
|
|
player.sync().start(0);
|
|
|
|
|
|
|
|
// Set up a callback to increment numClipsPlayed at the edge of each clip
|
|
|
|
const bufferLength = player.sampleTime * player.buffer.length;
|
2022-11-27 15:11:20 -07:00
|
|
|
|
|
|
|
// TODO(hayk): Set this callback up to vary each time using duration_s
|
2022-11-27 14:53:22 -07:00
|
|
|
Tone.Transport.scheduleRepeat((time) => {
|
|
|
|
setNumClipsPlayed((n) => n + 1);
|
|
|
|
}, bufferLength);
|
|
|
|
|
|
|
|
setTonePlayer(player);
|
|
|
|
|
|
|
|
// Make further load callbacks do nothing.
|
|
|
|
player.buffer.onload = () => {};
|
|
|
|
}).toDestination();
|
|
|
|
}, [tonePlayer, inferenceResults]);
|
|
|
|
|
|
|
|
// On play/pause button, play/pause the audio with the tone transport
|
|
|
|
useEffect(() => {
|
|
|
|
if (!paused) {
|
|
|
|
if (Tone.context.state == "suspended") {
|
|
|
|
Tone.context.resume();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tonePlayer) {
|
|
|
|
Tone.Transport.start();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (tonePlayer) {
|
|
|
|
Tone.Transport.pause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [paused, tonePlayer]);
|
|
|
|
|
|
|
|
// If there is a new clip, switch to it
|
|
|
|
useEffect(() => {
|
|
|
|
if (numClipsPlayed == prevNumClipsPlayed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const maxResultCounter = Math.max(
|
|
|
|
...inferenceResults.map((r) => r.counter)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (maxResultCounter < resultCounter) {
|
|
|
|
console.info(
|
2022-12-13 23:29:49 -07:00
|
|
|
"No new result available, looping previous clip",
|
2022-11-27 14:53:22 -07:00
|
|
|
resultCounter,
|
|
|
|
maxResultCounter
|
|
|
|
);
|
2022-12-13 23:29:49 -07:00
|
|
|
playerIsBehindCallback(true);
|
2022-11-27 14:53:22 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-13 23:29:49 -07:00
|
|
|
playerIsBehindCallback(false);
|
|
|
|
|
2022-11-27 14:53:22 -07:00
|
|
|
const result = inferenceResults.find(
|
|
|
|
(r: InferenceResult) => r.counter == resultCounter
|
|
|
|
);
|
|
|
|
|
|
|
|
setResultCounter((c) => c + 1);
|
|
|
|
|
|
|
|
tonePlayer.load(result.audio).then(() => {
|
|
|
|
// Re-jigger the transport so it stops playing old buffers. It seems like this doesn't
|
|
|
|
// introduce a gap, but watch out for that.
|
|
|
|
Tone.Transport.pause();
|
|
|
|
if (!paused) {
|
|
|
|
Tone.Transport.start();
|
|
|
|
}
|
2022-11-27 17:46:10 -07:00
|
|
|
|
|
|
|
const playerTime = Tone.Transport.seconds;
|
|
|
|
nowPlayingCallback(result, playerTime);
|
2022-11-27 14:53:22 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
setPrevNumClipsPlayed(numClipsPlayed);
|
|
|
|
}, [
|
|
|
|
numClipsPlayed,
|
|
|
|
prevNumClipsPlayed,
|
|
|
|
resultCounter,
|
|
|
|
inferenceResults,
|
|
|
|
paused,
|
2022-11-27 17:59:00 -07:00
|
|
|
nowPlayingCallback,
|
2022-12-14 02:14:25 -07:00
|
|
|
playerIsBehindCallback,
|
2022-11-27 14:53:22 -07:00
|
|
|
tonePlayer,
|
|
|
|
]);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|