How to avoid Pygame changing female voice into male

758 views Asked by At

I am trying to play an audio file of a female, speaking something in hindi using pygame library in python. When i manually click on audio file and listen to it, it is a female voice, but when but I play it via below script, I get a male voice. I guess it is converting female frequency into male. Why is that and how to avoid it?

Note: I am using Raspbian on Raspberry Pi.

This is the link to the audio file: https://drive.google.com/open?id=18pLBoCMxWZzB-RO3qqVmi0zREgJckb3M

My coding:

import pygame.time
from pygame.mixer import *
pre_init()
init()
filename = 'speech.wav'
music.load(filename)
music.play()
while pygame.mixer.music.get_busy():
    pygame.time.Clock().tick(10)
1

There are 1 answers

2
Torxed On
import pygame.time
from pygame.mixer import *
pre_init()
init(frequency=32000)
filename = 'speech.wav'
music.load(filename)
music.play()
while pygame.mixer.music.get_busy():
    pygame.time.Clock().tick(10)

pygame.mixer.init() takes a frequency parameter. And since the player defaults to 22050, the playback will be in slowm-otion - making it sound different. You can circumvent this by changing the speed manually, or you can probably get the actual speed from music.load(), the meta-data should be in the file.

Otherwise, just do ffmpeg -i speech.wav and you'll see the correct frequency at the bottom.

To change the frequency of audio-files to match the same frequencies, you could use ffmpeg to re-encode the files. Now, I'm no magician with ffmpeg - but the just goes something along the lines of:

ffmpeg -i speech.wav -af asetrate="32000*1.38125,atempo=1/1.38125" output.wav

Or use Audacity or something others recommend: https://superuser.com/questions/292833/how-to-change-audio-frequency

A second option to change frequency on the player instead, is to call pygame.mixer.quit() after each media file and re-initiate it with a new frequency matching your new file. Or lastly, read the docs and see if it's possible to change frequency playback on a already initialized instance of the mixer. This is beyond my knowledge tho. I just know what your original root problem is :)