I made a midiPlayer class for an experiment I'm running. It looks like this:
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()
On most people's computers, this is running absolutely fine, but on one participant's computer (using Windows 10), there is about 0.6s latency. I don't have the problem on my own machine (MacOS Sierra).
Is there anything he can do to lose the latency? He's tried reducing the buffer size, but this doesn't seem to have any effect.
Here is an example of the class being used in a minimal reproducible example.:
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