Skip to content

Commit

Permalink
temporarily link midi display and audio
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe-serna committed Oct 22, 2024
1 parent 9624bbe commit edeab7d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
19 changes: 14 additions & 5 deletions app/audio/preview/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import MinimapPlugin from "wavesurfer.js/dist/plugins/minimap";

export default function Preview() {
const containerRef = useRef<HTMLDivElement | null>(null);
const [isPlaying, setIsPlaying] = useState(false);
const [midiFile, setMidiFile] = useState<File | null>(null);

const { wavesurfer, isReady, isPlaying, currentTime } = useWavesurfer({
const { wavesurfer, isReady, currentTime } = useWavesurfer({
container: containerRef,
url: "/audio/tsubasa_bass.mp3",
waveColor: "#eab308",
Expand Down Expand Up @@ -47,15 +48,17 @@ export default function Preview() {
}
}, [isReady, wavesurfer]);

const onPlayPause = () => {
useEffect(() => {
wavesurfer && wavesurfer.playPause();
};
}, [isPlaying]);

return (
<div className="flex h-full w-[1000px] flex-col justify-center">
<div ref={containerRef} />

<button onClick={onPlayPause}>{isPlaying ? "Pause" : "Play"}</button>
<button onClick={() => setIsPlaying(!isPlaying)}>
{isPlaying ? "Pause" : "Play"}
</button>
<div>
<input
type="file"
Expand All @@ -65,7 +68,13 @@ export default function Preview() {
setMidiFile(e.target.files[0]);
}}
/>
{midiFile && <PianoRoll midiFile={midiFile} />}{" "}
{midiFile && (
<PianoRoll
midiFile={midiFile}
isPlaying={isPlaying}
setIsPlaying={setIsPlaying}
/>
)}
</div>
</div>
);
Expand Down
9 changes: 7 additions & 2 deletions components/PianoRoll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { Midi } from "@tonejs/midi";

interface PianoRollProps {
midiFile: Blob; // Accepting the MIDI file as a Blob prop
isPlaying: boolean;
setIsPlaying: (isPlaying: boolean) => void;
}

interface MidiNote {
Expand All @@ -14,10 +16,13 @@ interface MidiNote {
duration: number;
}

const PianoRoll: React.FC<PianoRollProps> = ({ midiFile }) => {
const PianoRoll: React.FC<PianoRollProps> = ({
midiFile,
isPlaying,
setIsPlaying,
}) => {
const canvasRef = useRef<HTMLCanvasElement | null>(null);
const [midiData, setMidiData] = useState<MidiNote[]>([]);
const [isPlaying, setIsPlaying] = useState(false);
const [progress, setProgress] = useState(0); // Track progress of MIDI playback

useEffect(() => {
Expand Down

0 comments on commit edeab7d

Please sign in to comment.