Midi works! However....making a class for using midi

Thanks @jon

I’ve updated the class to something like this:

class midiPlayer(object):
    status = NOT_STARTED
    tStart = 0
    frameNStart = 0
    setVolume = 1
    def __init__(self, Sound):
        self.Sound = Sound 
    def play(self):
        freq = 44100    # audio CD quality
        bitsize = -16   # unsigned 16 bit
        channels = 2    # 1 is mono, 2 is stereo
        buffer = 1024    # number of samples
        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() 

As you say, I don’t actually need tStart etc. I’ve just left them in anyway but I’m not using them. midiPlayer.busy() checks if the music is still playing.

Currently I’m needing to explicitly set audio.status to FINISHED once it’s finished playing. As such I currently have a bit of code that looks like this:

if audio_feedback.status == STARTED and audio_feedback.busy() == 0:
    audio_feedback.status = FINISHED

Although I’m worried that running the audio_feedback.busy() function like this on every frame might be a strain on the system? If that’s the case, is there a smarter way in which I can do this?

The memory usage does indeed seem to be steadily rising again (Memory leak - what could _LogEntry be?) now that I’ve made these changes to use midi (first time around in turned out that I was making a new text stimulus on every frame… :see_no_evil:), although this isn’t as steep a rise as it was first time. Do you reckon this might be because I keep running the audio_feedback.busy() function, or would you predict some other reason?

Thanks :slight_smile: