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?