最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using tone.js to play a wav file at specific intervals - Stack Overflow

programmeradmin1浏览0评论

When I attempt to use tone.js's Player to repeatedly play a wav file at specific intervals (i.e., at the start, the first measure, and the fourth measure), the first few tones are at the correct intervals but subsequent tones are played at the beginning of every interval.

My html file has a button to start the playing:

<button id="playIt">Play</button>

Here is my javascript/jquery:

$(document).ready(function () {
    $("#playIt").click(function () {
        if (Tone.context.state != "running") {
            Tone.start();
        }

        playNotes();
    });
});

const player = new Tone.Player({
    url: "/tick.mp3",
    loop: true,
    autostart: false
}).toDestination();

Tone.Transport.bpm.value = 140;

// Schedule the player to start at specific times
Tone.Transport.schedule(time => {
    player.start(time);
}, "0:0:0"); // Play at the start

Tone.Transport.schedule(time => {
    player.start(time);
}, "0:1:0"); // Play at 1st measure

Tone.Transport.schedule(time => {
    player.start(time);
}, "0:4:0"); // Play at 4th measure

async function playNotes() {
    Tone.Transport.start();
}

If I turn off looping, it plays once, correctly. When it loops, it plays correctly once and then plays the tone on every measure.

Thanks for your help!

When I attempt to use tone.js's Player to repeatedly play a wav file at specific intervals (i.e., at the start, the first measure, and the fourth measure), the first few tones are at the correct intervals but subsequent tones are played at the beginning of every interval.

My html file has a button to start the playing:

<button id="playIt">Play</button>

Here is my javascript/jquery:

$(document).ready(function () {
    $("#playIt").click(function () {
        if (Tone.context.state != "running") {
            Tone.start();
        }

        playNotes();
    });
});

const player = new Tone.Player({
    url: "/tick.mp3",
    loop: true,
    autostart: false
}).toDestination();

Tone.Transport.bpm.value = 140;

// Schedule the player to start at specific times
Tone.Transport.schedule(time => {
    player.start(time);
}, "0:0:0"); // Play at the start

Tone.Transport.schedule(time => {
    player.start(time);
}, "0:1:0"); // Play at 1st measure

Tone.Transport.schedule(time => {
    player.start(time);
}, "0:4:0"); // Play at 4th measure

async function playNotes() {
    Tone.Transport.start();
}

If I turn off looping, it plays once, correctly. When it loops, it plays correctly once and then plays the tone on every measure.

Thanks for your help!

Share Improve this question asked Feb 7 at 14:38 Steve ASteve A 2,0134 gold badges19 silver badges38 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You need to not loop and let the scheduleRepeat do the playing

Something like this

$(document).ready(function() {
  $("#playIt").click(function() {
    if (Tone.context.state !== "running") {
      Tone.start(); 
    }
    Tone.Transport.start();
  });
});

const player = new Tone.Player({
  url: "/tick.mp3",
  loop: false, // We handle repetition later
  autostart: false
}).toDestination();

Tone.Transport.bpm.value = 140;
const tickDuration = "8n"; // Adjust as needed
const cycleLength = "4m"; // Repeats every 4 measures

// Schedule and repeat tick sounds
["0:0:0", "0:1:0", "0:4:0"].forEach(time => {
  Tone.Transport.scheduleRepeat(t => {
    player.start(t);
    player.stop(t + Tone.Time(tickDuration));
  }, cycleLength, time);
});
发布评论

评论列表(0)

  1. 暂无评论