pygame音频播放速度

时间:2010-01-29 01:46:28

标签: 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
pygame.mixer.init(22100)
pygame.mixer.music.load(sys.argv[1])
pygame.mixer.music.play()
time.sleep(5)
pygame.mixer.quit()

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

感谢。

5 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

我播放的一些mp3音轨慢了下来。我使用mutagen将混音器频率更新为基于mp3采样率,如下所示:

override func viewDidLoad() {
        super.viewDidLoad()

  pageControl.numberOfPages = imageArray.count
        scrollView.frame = self.view.frame
        scrollView.contentSize = CGSize(width: scrollView.frame.size.width * CGFloat(pageControl.numberOfPages) , height: 0)
        scrollView.delegate = self

        for i in 0..<imageArray.count {

            ashvanImage = UIImageView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width - 50 , height: self.view.frame.height - 200))
            ashvanImage.center = scrollView.center
            ashvanImage.contentMode = .scaleAspectFit
            ashvanImage.image = UIImage(named: "\(i).jpg")
            scrollView.addSubview(ashvanImage)
            createPageWith(images: ashvanImage, page: i)

        }
}


  func createPageWith(images:UIImageView, page:Int)  {

        let newView = UIView(frame: CGRect(x: scrollView.frame.size.width * CGFloat(page), y: 0, width: scrollView.frame.size.width, height: scrollView.frame.size.height))
        newView.addSubview(images)
        scrollView.addSubview(newView)
    }



    func scrollViewDidScroll(_ scrollView: UIScrollView) {

        let page = scrollView.contentOffset.x / scrollView.frame.size.width
        pageControl.currentPage = Int(page)

    }

它解决了这个问题。

答案 2 :(得分:1)

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

答案 3 :(得分:1)

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

请注意,如果您要更改frequencypygame.mixer.init中使用的任何其他参数,则必须先调用pygame.mixer.quitPygame documentation

答案 4 :(得分:0)

如果您使用的是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()
相关问题