是否可以使用Tone.js实时显示MIDI文件中的音符?

问题描述 投票:0回答:1

我想在使用tone.js播放midi文件时实时显示音符,但我所能做的就是在曲目播放之前显示它们。 有没有办法使用tone.js 或任何其他方式将它们显示在曲目旁边?

下面的代码所做的就是在单击播放按钮时记录所有笔记信息。

    async function initiateMidi() {
        const midi = await Midi.fromUrl('src\\static\\saliha-ya-khdoud-ettefeh.mid');
    
        const synth = new Tone.Synth().toDestination();
        synths.push(synth);
    
        const now = Tone.now() + 0.5;
    
        midi.tracks.forEach((track) => {
            track.instrument;
            const notes = track.notes;
            notes.forEach((note) => {
                const noteTime = note.time + now;
                synth.triggerAttackRelease(note.name, note.duration, noteTime, note.velocity);
                console.log(`Note: ${note.name}, Time: ${noteTime}, Duration: ${note.duration}`);
            });
        });
    }

任何有关如何在 note.js 中工作或我应该采取的任何其他方式的帮助将不胜感激,并提前致谢

编辑: 我添加了一个 setTimeout 函数来及时显示笔记,但我仍然有办法使用 note.js 来做到这一点

    async function initiateMidi() {
            const midi = await Midi.fromUrl('src\\static\\saliha-ya-khdoud-ettefeh.mid');
            const synth = new Tone.Synth().toDestination();
            synths.push(synth);
    
            const now = Tone.now() + 0.5;
    
            midi.tracks.forEach((track) => {
                const notes = track.notes;
                notes.forEach((note) => {
                    const noteTime = note.time + now;
                    const noteDuration = note.duration;
    
                    setTimeout(() => {
                        console.log(`Note: ${note.name}, Time: ${noteTime}, Duration: ${noteDuration}`);
                    }, noteTime * 1000);
    
                    synth.triggerAttackRelease(note.name, noteDuration, noteTime, note.velocity);
                });
            });
        }
javascript midi audio-player tone.js
1个回答
0
投票

请参阅 https://tonejs.github.io/docs/14.7.77/Transport.html#schedule。 您可以使用

Transport.schedule()
,其作用几乎与
setTimeout()
一样。一般来说,在 Tone.js 中调度音频事件时,最好使用
schedule()
方法。这可以确保事件在音乐时间线中的正确时间发生,无论节奏如何。

© www.soinside.com 2019 - 2024. All rights reserved.