spotify / pedalboard

🎛 🔊 A Python library for audio.

Home Page:https://spotify.github.io/pedalboard

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using Pitch shift generates empty audio file

AWAS666 opened this issue · comments

So I have been trying to use this to change the pitch of a tts voice on the fly.
I basically just wanna apply the pitch shift to a wav with with 22050 sample rate.

If I just use any other effect, it works perfectly fine, but if I use the pitch shift I get an empty audio file.

It seems to not write anything do it as the size changes from 5mb to 1kb

Other effects like "Chorus" work just fine though

Hi @AWAS666!

I'm not sure I can help unless you have a code example to reproduce the issue.

The PitchShift plugin does buffer audio within itself depending on how extreme the pitch shift is; as the documentation says, you may need to call process again to flush its internal buffers and receive the remaining audio:

The returned array may contain up to (but not more than) the same number of samples as were provided. If fewer samples were returned than expected, the plugin has likely buffered audio inside itself. To receive the remaining audio, pass another audio buffer into process with reset set to False.

I did just use the the tutorial and switch it up with pitchshift

 board = Pedalboard([PitchShift(semitones=7.0)])
    # Open an audio file for reading, just like a regular file:
    with AudioFile('test2.wav') as f:    
        # Open an audio file to write to:
        with AudioFile('test.wav', 'w', f.samplerate, f.num_channels) as o:        
            # Read one second of audio at a time, until the file is empty:
            while f.tell() < f.frames:
                chunk = f.read(f.samplerate)
                
                # Run the audio through our pedalboard:
                effected = board(chunk, f.samplerate, reset=False)
                
                # Write the output to our output file:
                o.write(effected)

Hmm, this does appear to be a more complicated bug than I thought - while the pitch shifter does buffer audio, it should still output something. This might require a bugfix on my end. Thanks for the report!

In the meantime, if you're only modifying fairly small audio files that have fixed durations, you can use the time_stretch function instead. Note that time_stretch does not operate in a streaming fashion and requires the entire audio file to be loaded into memory first, which can use a huge amount of memory for longer audio files.

with AudioFile('test2.wav') as f:
    with AudioFile('test.wav', 'w', f.samplerate, f.num_channels) as o:        
        o.write(pedalboard.time_stretch(f.read(f.frames), f.samplerate, pitch_shift_in_semitones=7))

I set reset to True and that seems to make the example work