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

How to fade in or fade out using GrainPlayer? #1091

Closed rtwalz closed 2 years ago

rtwalz commented 2 years ago

Hello, new to Tone.js and it seems really powerful so far.

I'm trying to fade in an mp3 sample I'm loading using a GrainPlayer. According to the docs, fadeIn or fadeOut properties don't exist on GrainPlayer. However, the regular Player class does have fadeIn and fadeOut properties. I need to use GrainPlayer because I want to control the playback rate while preserving pitch. How would I go about doing a nice fade-in with GrainPlayer? My initial solution is to slowly raise the volume manually every couple fractions of a second or so, but maybe someone else has a better idea.

marcelblum commented 2 years ago

The web audio api has native methods for doing this super smoothly which Tone exposes, check out GrainPlayer.volume.rampTo(value, rampTime, startTime) and more broadly https://developer.mozilla.org/en-US/docs/Web/API/AudioParam

rtwalz commented 2 years ago

Thank you! This is exactly what I was looking for.