pytorch / audio

Data manipulation and transformation for audio signal processing, powered by PyTorch
https://pytorch.org/audio
BSD 2-Clause "Simplified" License
2.43k stars 635 forks source link

Real time synthesis with oscillator_bank #3788

Open peastman opened 1 month ago

peastman commented 1 month ago

🚀 The feature

I suggest adding another input and another output to oscillator_bank(). The new argument would contain the initial phases of the oscillators. The new output would contain the final phases at the end of the generated waveform.

To avoid roundoff error that grows with time, the returned phases should always be wrapped to the interval $[0, 2\pi]$.

Motivation, pitch

I want to use TorchAudio for real time synthesis. In this context, you generate just a small amount of audio at a time, perhaps a few milliseconds. You do this repeatedly, varying the synthesis parameters based on actions by the user, to create a live performance.

This requires the phases of the oscillators to be continuous across multiple calls to oscillator_bank(). Otherwise you get a discontinuity in the waveform.

Code to use it would look something like this.

phases = torch.zeros(num_oscillators)
while still_playing():
    waveform, phases = oscillator_bank(get_frequencies(), get_amplitudes(), sample_rate, phases)
    play_audio(waveform)

Alternatives

An alternative would be to create an OscillatorBank object that would internally store the phases, guaranteeing continuity across multiple calls. In that case the code might look like

oscillators = OscillatorBank(num_oscillators, sample_rate)
while still_playing():
    waveform = oscillators(get_frequencies(), get_amplitudes())
    play_audio(waveform)

Additional context

No response