Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Pitch shift generates empty audio file #267

Open
AWAS666 opened this issue Oct 20, 2023 · 4 comments
Open

Using Pitch shift generates empty audio file #267

AWAS666 opened this issue Oct 20, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@AWAS666
Copy link

AWAS666 commented Oct 20, 2023

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

@psobot
Copy link
Member

psobot commented Oct 20, 2023

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.

@AWAS666
Copy link
Author

AWAS666 commented Oct 20, 2023

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)

@psobot
Copy link
Member

psobot commented Oct 20, 2023

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))

@psobot psobot added the bug Something isn't working label Nov 14, 2023
@Seshpenguin
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants