Tonejs / Tone.js

A Web Audio framework for making interactive music in the browser.
https://tonejs.github.io
MIT License
13.4k stars 976 forks source link

Playing large files in player doesnt start when scheduled (e.g 10mb) #984

Closed cordial closed 2 years ago

cordial commented 2 years ago

Sorry, this is a bit how to, but I'm struggling getting large files to play at the right time. Admittedly, I kinda expected that might be the case, but I'm wondering what the best way around it is.

My scenario is as follows - A musical journey that can play two samples during each section of music (sometimes on loop and sometimes not), then the next section will play a new section of music, but if the previous section is still playing it should end gracefully i.e. get to the end and not cut off. So with a total of 18 sections - that gives 36 samples.

Currently I have two players, which were being reused for each section, but if i have samples that need to carry on for longer than their section, so that they finish properly, I'm going to need a pool of players I think. But looking at the Players object, you seem to have to load all the URLs at the start? If I have 36 samples, is that going to work? My Java head is thinking I have an array of Player objects (say 10), and every time I get to a new section I .find() a player that is currently 'started'. Would that be a performant way to proceed?

Also, when the section sample is large, it isn't starting in time to play at the start of the section (when using the transport scheduling). Should I be preloading using buffers i.e. does that actually load the whole files in memory? That could get rather large with 18 x 2 samples (36).

Thanks for the great library, it has really helped loads. And sorry this is just asking for advice (again).

dirkk0 commented 2 years ago

Preload all samples with the Players object, then play the samples. Your scenario shouldn't be a problem for ToneJS.

tambien commented 2 years ago

Thanks @dirkk0 I agree it seems like the delay is caused by waiting for the sample to load and not anything Tone.js related. Please send a test-case if it does seem like a Tone.js issue.

If you have many samples, i would recommend loading them just-in-time. Knowing exactly when to load them to avoid loading delays while not keeping too many audio files in memory is a complex balance.