bastibe / SoundCard

A Pure-Python Real-Time Audio Library
https://soundcard.readthedocs.io
BSD 3-Clause "New" or "Revised" License
680 stars 69 forks source link

Is there a way to play sound at different speeds while maintaining pitch? #66

Closed Pololot64 closed 4 years ago

bastibe commented 4 years ago

Not within SoundCard, no. SoundCard does not change the audio in any way.

Pololot64 commented 4 years ago

Do you have any suggestions on how to do this in realtime? (Sorry for marking it as an issue. I don't want to clutter up your github)

bastibe commented 4 years ago

For example: https://librosa.github.io/librosa/generated/librosa.effects.time_stretch.html

A simple way to do this on your own is to cut the signal into short, overlapping blocks. For example, hann-windowed blocks of 20 ms, with 10 ms overlap, so they add back to the original signal if simply added at their original offsets. Then remove every third block to speed up by 30%. If you want to be fancy, remove the block with the lowest crest factor of every three. This will invariably be a vowel, not a consonant, and will lessen the impact on speech. This works surprisingly well, and is far simpler than FFT-based time stretching algorithms. Doesn't work particularly well for music, though, where you'll need something more complicated.

If you need that, FFT every block, then truncate the spectra, then IFFT with a shorter FFT length.

Pololot64 commented 4 years ago

Awesome! Thank you so much!