When I use pygame to play mp3s, they play too quickly.
Of course I looked around and found the same problem here. But unfortunately the solution didn't work. I know the mp3s are encoded at 16000 Hz, and I verified this using mutagen. But specifying the frequency didn't help, and in fact changing frequencies didn't change the speed of playback.
I also found this answer, which effectively suggested a different initialization method. But that didn't work for me either.
I'm running a python script as root, and using a USB sound card. I edited my alsa configuration file per this answer to set it as the default audio out for root. Then I'm using this code (or variations per the above SO answers):
pygame.mixer.init(frequency=16000)
pygame.mixer.music.load('myfile.mp3')
pygame.mixer.music.play()
It always plays at what sounds like twice the speed, even when I use different values for the frequency.
~~~~~
Here's the code in more detail:
import boto3
from io import StringIO
import json
import os.path
from contextlib import closing
from time import sleep
import pygame
pygame.mixer.pre_init(16000, -16, 2, 2048)
pygame.init()
#pygame.mixer.init(frequency=16000)
#pygame.mixer.quit()
#pygame.mixer.pre_init(16384, -16, 2, 1024*3)
#pygame.mixer.init()
def play_file(filename):
pygame.mixer.music.load(filename)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
sleep(0.1)
play_file('myfile.mp3')