Tonejs / Tone.js

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

fadeIn and fadeOut Based on Player Loop Size #1111

Closed abswiz closed 1 year ago

abswiz commented 2 years ago

The feature you'd like Tone.Player appears to fadeIn and fadeOut based on the buffer size. A logical assumption would be that the fadeIn and fadeOut should work for a set loop... i.e. fadeIn at the start of the loop and fadeOut at the end of the loop. If no loop is set then the fadeIn and fadeOut working on the overall buffer makes sense.

Any alternatives you've considered We have achieved by slicing the buffer to the loop size and resampling using an offline context where performance is not an issue. In realtime scenarios, we have used linearRampTo to automate the player volume.

tambien commented 1 year ago

The best way to do this would be to actually modify the buffer. You could apply the fade in/out directly to the buffer using something like Tone.Offline and then use that buffer in your player.

const fadedAudioBuffer = await Tone.Offline(() => {
  const player = new Tone.Player(originalBuffer).toDestination();
  player.fadeIn = 0.1;
  player.fadeOut = 0.1;
  player.start(0);
}, originalBuffer.duration)

// fadedAudioBuffer is the same as originalBuffer but with fadeIn/Out baked in
const player = new Tone.Player(fadedAudioBuffer)
player.loop = true;

The reason that i suggest doing it this way is that the functionality is not easily achievable in the Tone.js code code. There is no callback at the loop points so there's no way to apply a gain envelope at those points.

abswiz commented 1 year ago

Thanks Yotam ... fully understand :) ... but thought I'd ask. The offline approach is the one we've used in many places. We have also used a real-time approach using automation. A good example of this is where you may be trying to alter the part of a buffer that is being played as a drum hit for example and you want any changes to the buffer length to take effect straightaway. This cannot be done offline and thought I'd include for completeness in case anyone else needs something similar. An interesting extension of this scenario is to figure out the next drum step and fade out the previous one. Very interesting problem, which we did manage to solve using some intricate automation... Thanks again for considering the request.