WebAudio / web-audio-api

The Web Audio API v1.0, developed by the W3C Audio WG
https://webaudio.github.io/web-audio-api/
Other
1.04k stars 165 forks source link

Attack + crossfaded loop + release #2537

Closed josephernest closed 1 year ago

josephernest commented 1 year ago

Is there a way with Web Audio API to play an attack .wav, then a (crossfaded) loop for 10 seconds, then a release sample, in a seamless way?

We might do:

const source = audioContext.createBufferSource();
source.buffer = attack_buffer;
source.detune.value = detune_value;
source.connect(gainNode);
source.start();

and then another buffer for the loop, but it probably won't be precisely in sync / it won't be seamless.

Is there a documented way to do this properly?

GeorgeTailor commented 1 year ago

it will be in sync but it will not be seamless. If it's a one shot then you'd be better off with stitching the files together manually and then apply ADSR envelope by any means available (either some editor or some JS code to manipulate samples).

Problem with seamlessness is that playing a lot of short source buffers may cause "clicking" sound, not sure what exactly might be causing this, but to overcome this I did a very small attack envelope to minimize this effect, which might introduce lack of seamlessness in your case as this sound track should be treated as one despite being broken down into several parts.

apart from that this really seems like a stackoverflow question or something that belongs to discussions rather than an issues

josephernest commented 1 year ago

Thanks for your answer @GeorgeTailor.

What I need is basically play a sound "note.wav" from position/offset 0. Then after a few seconds, when it reaches the playback position loop_end it should seamlessly go to loop_start (which is not 0) and continue the playback in loop forerver between loop_start and loop_end. Would you have an example of how to do this?

Note: this approach won't work here because we don't loop the full sample, rather after reaching loop_end, we start again at loop_start.

This is needed:

0                 loop_start                loop_end
------------------|-------------------------|

playback:
* 0 to loop_start
* loop_start to loop_end
* loop_start to loop_end
* loop_start to loop_end
* ... forever
josephernest commented 1 year ago

Do you have any idea @GeorgeTailor?

Here is a linked SO question: https://stackoverflow.com/questions/75458803/loop-an-audio-file-between-2-looping-points-with-web-audio-api-seamless

padenot commented 1 year ago

This repository is home to the Web Audio API specification.

Questions on how to do something with the Web Audio API are better suited to either Stack Overflow or the Web Audio API slack.

Cross faded looping isn't something the Web Audio API supports. The rest can be implemented to a certain degree with looping, but detuning will resample the audio, and this might cause subtle time shifts (although implementations try to not do that).