danigb / smplr

A web audio sampler instrument
https://danigb.github.io/smplr/
188 stars 19 forks source link

onEnded has stopped working with version 0.10 #38

Closed drscottlobo closed 1 year ago

drscottlobo commented 1 year ago

Hi, anyone else having issues with onEnded no longer firing? Plays back fine, then no console.log("I ended") after the note completes.

Here's a quick functional component showing the issue:


"use client";

import { Soundfont } from "smplr";
import { useState, useRef, useEffect } from "react";

export default function MIDIPlayer() {
    const [player, setPlayer] = useState(null);
    const [isLoading, setIsLoading] = useState(true);
    const ctx = useRef();

    async function loadInstrument() {
        const newPlayer = await new Soundfont(ctx.current, {
            instrument: "marimba",
        }).load;
        setPlayer(newPlayer);
        setIsLoading(false);
    }

    useEffect(() => {
        ctx.current = new AudioContext();
        async function init() {
            await loadInstrument();
        }
        init();
    }, []);

    function handlePlay() {
        player.start({
            note: "C4",
            duration: 1,
            onEnded: () => {
                console.log("I ended!");
                // will be called after 1 second
            },
        });
    }

    if (isLoading) {
        return "loading...";
    }

    return (
        <div>
            <button onClick={handlePlay}>Click me</button>
        </div>
    );
}
danigb commented 1 year ago

Yes, you're right. Fixed (with tests) in latest version

drscottlobo commented 1 year ago

Thanks! I’ll check it!___Scott Wolf, DMAOxnard College - Professor of MusicOn Sep 26, 2023, at 12:35 PM, danigb @.***> wrote: Yes, you're right. Fixed (with tests) in latest version

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>