(Python wave module) Can't change audio sampling rate without affecting playback speed
Asked Answered
T

2

0

I'm currently working on a project which includes using pygame to play sounds on a button press. Since I didn't find a good way to record sound from the application (I tried PyAudio with Portaudio repeatedly, but couldn't make it work) I was forced to use external program to record audio.

The sounds I'm importing in pyaudio mixer have sampling rate of 44,1 kHz, while the sound the program is recording is 48 kHz.

Here comes the problem: When I record the audio everything is fine, but if I want to import the recorded file for further use it plays slower than usually.

I figured out it was because of the sampling rate and since I can only set one sampling rate with pyaudio mixer, I decided to try modifying the sample rate of the new file back to 44,1 kHz with this code:

import wave
spf = wave.open('C:\Users\mavri\Desktop\My Recordings\zvuk.wav', 'rb')                  
CHANNELS = spf.getnchannels()
swidth = spf.getsampwidth()
RATE=spf.getframerate()
signal = spf.readframes(-1)
spf.close()
wf = wave.open('C:\Users\mavri\Desktop\My Recordings\zvuk.wav', 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(swidth)
wf.setframerate(44100)
wf.writeframes(signal)
wf.close()

Now the problem is the same, but this time it affects the audio file, not pygame. The audio file is slower than usual. I tried playing with sample width, by multiplying it with new sample rate and dividing by 44100, but what I got was just a lot of hissing noise which reminded of the sound, but was nowhere near it.

My question is: How can I modify the code provided so the new file created has sampling rate of 44,1 kHz, but playback speed remains unchanged?

Thermotherapy answered 15/3, 2017 at 12:27 Comment(1)
I'd like to add I found an option which allows me to modify sample rate while recording, but I would still like to know the answer considering the possibility of downloading sounds with different sample rates etc.Thermotherapy
T
1

I found the aswer here: Python - downsampling wav audio file

It needed a little tweaking to get it work, just use converted[0] instead of converted when writing frames since method doesn't accept tuples.

Thermotherapy answered 16/3, 2017 at 12:20 Comment(0)
T
1

You can change the sample rate using librosa However, librosa usually runs into issues while installing

INSTALLATION

using conda environment

python --> 3.6.8
conda install ffmpeg=4.3.1
conda install numba=0.48
pip install librosa==0.6.0

CODE

import librosa

audio_file = "Original.wav" #48KHz

#SAME PLAYBACK SPEED
x, sr = librosa.load(audio_file, sr=44100)
librosa.output.write_wav("Test1.wav", x, sr=22050, norm=False)

#SAME PLAYBACK SPEED
x, sr = librosa.load(audio_file, sr=48000)
y = librosa.resample(x, 48000, 44100)
librosa.output.write_wav("Test3.wav", y, sr=44100, norm=False)

#SLOW PLAYBACK SPEED
x, sr = librosa.load(audio_file, sr=48000)
librosa.output.write_wav("Test2.wav", x, sr=44100, norm=False)

UPDATE

libroa when saving the output changes the datatype to 32bit float by default. Hence, to save the array, use soundwrite and specify the datatype while saving the audio

import soundfile as sf
data, samplerate = soundfile.read('old.wav')
sf.write("Test4.wav", x, 22050, subtype='PCM_16')
Taxaceous answered 8/4, 2021 at 7:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.