Python pygame音频播放速度

Python pygame音频播放速度,python,pygame,Python,Pygame,快速提问 我在linux下运行pygame只是为了播放一些音频文件。 我有一些.wav文件,在以正确的速度播放时遇到问题 import pygame.mixer, sys, time #plays too fast pygame.mixer.init(44100) pygame.mixer.music.load(sys.argv[1]) pygame.mixer.music.play() time.sleep(5) pygame.mixer.quit() #plays too slow py

快速提问

我在linux下运行pygame只是为了播放一些音频文件。 我有一些.wav文件,在以正确的速度播放时遇到问题

import pygame.mixer, sys, time

#plays too fast
pygame.mixer.init(44100)
pygame.mixer.music.load(sys.argv[1])
pygame.mixer.music.play()
time.sleep(5)
pygame.mixer.quit()

#plays too slow
pygame.mixer.init(22100)
pygame.mixer.music.load(sys.argv[1])
pygame.mixer.music.play()
time.sleep(5)
pygame.mixer.quit()
我已经在ggogle代码中搜索了一些东西,但似乎每个人都可以使用默认参数调用init函数。其他人是否可以尝试运行此脚本并查看他们是否获得相同的行为?有人知道如何加速吗?或者调整每个文件的速度


谢谢。

使用免费音频工具打开音频文件,如。它将告诉您媒体的采样率。它还允许您转换为不同的采样率,这样所有声音都可以相同。

我想出来了。。。
有一个wave模块,它可以读取wav文件的采样率。

我有一些mp3音轨播放速度减慢。我根据mp3采样率更新了混音器频率,如下所示:

它解决了这个问题。

需要改进。下面是一个如何使用
wave
库的示例

import wave
import pygame

file_path = '/path/to/sound.wav'
file_wav = wave.open(file_path)
frequency = file_wav.getframerate()
pygame.mixer.init(frequency=frequency)
pygame.mixer.music.load(file_path)
pygame.mixer.music.play()

请记住,如果要更改
频率
pygame.mixer.init
中使用的任何其他参数,必须先调用
pygame.mixer.quit

如果使用Ogg Vorbis(.Ogg)编码,同样的音频口吃问题也会发生。在初始化混音器对象之前,您必须读取要播放的内容的频率

下面是如何使用pygame以适当的频率播放.ogg音频

from pyogg import VorbisFile
from pygame import mixer

# path to your audio
path = "./file.ogg"
# an object representing the audio, see https://github.com/Zuzu-Typ/PyOgg
sound = VorbisFile(path)
# pull the frequency out of the Vorbis abstraction
frequency = sound.frequency
# initialize the mixer
mixer.init(frequency=frequency)
# add the audio to the mixer's music channel
mixer.music.load(path)
# mixer.music.set_volume(1.0)
# mixer.music.fadeout(15)
# play
mixer.music.play()

不一定。我相信您可以找到某种方法来计算Python中输入媒体的采样率,然后对媒体进行适当的重新采样。这将允许您正确处理用户提供的外来媒体。但是,pygame必须以一致的速率播放媒体,并且该速率必须与媒体本身的采样速率相匹配。这是无法回避的事实。
from pyogg import VorbisFile
from pygame import mixer

# path to your audio
path = "./file.ogg"
# an object representing the audio, see https://github.com/Zuzu-Typ/PyOgg
sound = VorbisFile(path)
# pull the frequency out of the Vorbis abstraction
frequency = sound.frequency
# initialize the mixer
mixer.init(frequency=frequency)
# add the audio to the mixer's music channel
mixer.music.load(path)
# mixer.music.set_volume(1.0)
# mixer.music.fadeout(15)
# play
mixer.music.play()