Skip to content

Instantly share code, notes, and snippets.

@JA1TYE
Created December 18, 2022 11:41
Show Gist options
  • Save JA1TYE/96f8812272e96a3e80bf3820a5836ef6 to your computer and use it in GitHub Desktop.
Save JA1TYE/96f8812272e96a3e80bf3820a5836ef6 to your computer and use it in GitHub Desktop.
Resample type Pitch Shift Effect without crossfading
from pydub import AudioSegment
import numpy as np
from pydub.playback import play
buflen = 2000
rate = 2.0
buf = np.zeros(buflen)
sound = AudioSegment.from_file("input.wav", "wav")
samples = np.array(sound.get_array_of_samples())
sample = samples[::sound.channels]
rpf = 0
rp1 = 0
wp = 0
result = np.zeros(len(sample))
gain1 = 1.0
#Processing loop
for i in range(len(sample)):
buf[wp] = sample[i] #Write sample to the ring buffer
out1 = buf[rp1] #Read sample from the ring buffer
result[i] = out1
#Update write pointer and read pointer
wp = (wp + 1) % buflen
rpf = rpf + rate
rp1 = int(round(rpf)) % buflen
test = AudioSegment(result.real.astype("int16").tobytes(), sample_width=sound.sample_width, frame_rate=sound.frame_rate, channels = 1)
test.export("output.wav", format="wav")
play(test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment