python pygame 音频播放速度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2159365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:55:58  来源:igfitidea点击:

pygame audio playback speed

pythonpygame

提问by Chris H

quick question.

快速提问。

I'm running pygame under linux just to play some audio files. I've got some .wav files and I'm having problems playing them back at the right speed.

我在 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()

I've ggogle code searched some stuff but everybody seems to be fine calling the init function with its default parameters. Can others try running this script and seeing if they get the same behavior or not? Does anybody know how to speed it up? Or adjust the speed for each file?

我已经用 ggogle 代码搜索了一些东西,但每个人似乎都可以使用默认参数调用 init 函数。其他人可以尝试运行此脚本并查看他们是否得到相同的行为吗?有人知道如何加快速度吗?或者调整每个文件的速度?

Thanks.

谢谢。

采纳答案by Chris H

I figured it out... There is a wave module http://docs.python.org/library/wave.htmland it can read the sample rate for wav files.

我想通了......有一个wave模块http://docs.python.org/library/wave.html,它可以读取wav文件的采样率。

回答by Charles Clayton

I had some mp3 audio tracks playing back slowed down. I updated the mixer frequency to be based on the mp3 sample rate using mutagenlike so:

我有一些 mp3 音轨的播放速度变慢了。我使用诱变剂更新了基于 mp3 采样率的混频器频率,如下所示:

import pygame, mutagen.mp3

song_file = "your_music.mp3"

mp3 = mutagen.mp3.MP3(song_file)
pygame.mixer.init(frequency=mp3.info.sample_rate)

pygame.mixer.music.load(song_file)
pygame.mixer.music.play()

And it fixed the problem.

它解决了这个问题。

回答by alej0

To improve Chris H answer. Here is a example of how to use the wavelibrary.

改进Chris H 的答案。以下是如何使用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()

Remember that if you want to change frequencyor any other parameter used in pygame.mixer.inityou must call pygame.mixer.quitfirst. Pygame documentation

请记住,如果您想更改frequency或使用的任何其他参数,pygame.mixer.init您必须先调用pygame.mixer.quitPygame 文档

回答by Pace

Open your audio file in a free audio tool like Audacity. It will tell you the sampling rate of your media. It will also allow you to convert to a different sampling rate so all your sounds can be the same.

Audacity等免费音频工具中打开您的音频文件。它会告诉您媒体的采样率。它还允许您转换为不同的采样率,以便您的所有声音都相同。

回答by Lost Odinson

If you're using Ogg Vorbis (.ogg) encoding, the same problem of stuttering audio happens. You'll have to read the frequency of what you're trying to play before initializing the mixer object.

如果您使用 Ogg Vorbis (.ogg) 编码,则会发生同样的音频卡顿问题。在初始化混音器对象之前,您必须读取您尝试播放的频率。

Here's how to play .ogg audio with appropriate frequency using pygame.

以下是如何使用 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()