Pygame延迟问题

2024-10-02 20:32:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我为我正在进行的一个实验上了一堂midiPlayer课。看起来是这样的:

class midiPlayer(object):
    status = NOT_STARTED
    setVolume = 1
    def __init__(self, Sound):
        self.Sound = Sound 
    def play(self):
        freq = 44100    
        bitsize = -16   
        channels = 2    
        buffer = 1024   
        pygame.mixer.init(freq, bitsize, channels, buffer)
        pygame.mixer.music.set_volume(self.setVolume)
        pygame.mixer.music.load(self.Sound)
        pygame.mixer.music.play()
        self.status = STARTED 
    def stop(self):
        pygame.mixer.music.stop()
        self.status = FINISHED
    def setSound(self, music_file):
        self.Sound = music_file
    def busy(self):
        return pygame.mixer.music.get_busy()

在大多数人的计算机上,这运行得非常好,但在一名参与者的计算机上(使用Windows 10),大约有0.6秒的延迟。我自己的机器(MacOS Sierra)上没有问题

他能做些什么来减少延迟?他试图减少缓冲区大小,但这似乎没有任何效果

以下是在最小可复制示例中使用的类的示例:

import pygame
from psychopy import core

class midiPlayer(object):
    setVolume = 1
    def __init__(self, Sound):
        self.Sound = Sound 
    def play(self):
        freq = 44100    
        bitsize = -16   
        channels = 2    
        buffer = 1024   
        pygame.mixer.init(freq, bitsize, channels, buffer)
        pygame.mixer.music.set_volume(self.setVolume)
        pygame.mixer.music.load(self.Sound)
        pygame.mixer.music.play()
    def stop(self):
        pygame.mixer.music.stop()
    def setSound(self, music_file):
        self.Sound = music_file
    def busy(self):
        return pygame.mixer.music.get_busy()

# You can download the MIDI file at https://wetransfer.com/downloads/868799aa1aacf7361de9a47d3218d2ee20200818095737/c3ccee ; obviously you'll need to update the file location below to wherever the file is saved on your own computer: 
MIDIFILE = midiPlayer("/Users/sam/Downloads/sounds/A4_version_2_gentlemen_normal.mid")
trialClock = core.Clock()
started_playing = 0
text_printed = 0 
continueRoutine = True

trialClock.reset()
while continueRoutine: 
    t = trialClock.getTime()
    if started_playing == 0:
        MIDIFILE.play()
        started_playing = 1
        
    if t >= 4.2 and text_printed ==0:
        print 'now'
        text_printed = 1
        # this should appear on the 8th note of the tune

    if t >= 6:
        MIDIFILE.stop()
        continueRoutine = False

Tags: theselfplayinitdefmusicpygamefile