[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Sound Queuing/Looping Program with MIDI Input.



Hi,

I wrote a pygame program that play, loop and queue sounds files. My goal was to keep it short, simple and only one file. The installation and operation instructions are on the bottom of the file. Please see the code below and let me know if you have any suggestions for improvement. Thank you Lenard Lindstrom and Ian Mallett for your assistance! :)

# ------------start badpenni.py file ------------
# The Installation and Operation instructions are at the bottom of this file.
import sys
import os
import pygame
import pygame.midi
from pygame.locals import *
pygame.mixer.pre_init(44100,-16,2,2048)
pygame.init()
pygame.midi.init()

sound1 = pygame.mixer.Sound("beat1.wav")
sound2 = pygame.mixer.Sound("beat2.wav")
sound3 = pygame.mixer.Sound("beat3.wav")
sound4 = pygame.mixer.Sound("beat4.wav")

def assignmidi():
        global nextsound
        if mymidinote == "48":
            nextsound = sound1
            updateScreenAndQueueNextSound()
        if mymidinote == "50":
            nextsound = sound2
            updateScreenAndQueueNextSound()
        if mymidinote == "52":
            nextsound = sound3
            updateScreenAndQueueNextSound()
        if mymidinote == "53":
            nextsound = sound4
            updateScreenAndQueueNextSound()

def updateScreenAndQueueNextSound():
    global nextnotethatwillplay
    nextnotethatwillplay = mymidinote
    reserved_channel_0.queue(nextsound)
    if notethatwasplayed == nextnotethatwillplay:
        screen.fill(Color("LawnGreen"))
        screen.blit(my_font.render("Same Queued",True,Color("Black")),(0,0) )
    if  notethatwasplayed != nextnotethatwillplay:
        screen.fill(Color("CornflowerBlue"))
        screen.blit(my_font.render("Change Queued",True,Color("White")),(0,0) )
    pygame.display.update()

notethatwasplayed = ""

for i in range( pygame.midi.get_count() ):
    r = pygame.midi.get_device_info(i)
    (interf, name, input, output, opened) = r
    in_out = ""
    if input:
        in_out = "(input)"
    if output:
        in_out = "(output)"

    print ("%2i: interface :%s:, name :%s:, opened :%s:  %s" % (i, interf, name, opened, in_out))

mydevicenumber=raw_input('See list above. Please enter the number for your midi input device: ')

print "you chose " + mydevicenumber + ''

reserved_channel_0 = pygame.mixer.Channel(0)

my_font = pygame.font.SysFont("Arial",104)

pygame.fastevent.init()
event_get = pygame.fastevent.get
event_post = pygame.fastevent.post

input_id = int(mydevicenumber)
i = pygame.midi.Input( input_id )

TRACK_END = USEREVENT + 1
reserved_channel_0.set_endevent(TRACK_END)

pygame.display.set_caption("BadPenni Loop Sequencer")
screen = pygame.display.set_mode((800, 600), RESIZABLE, 32)

print "starting"

going = True

while going:

    events = event_get()
    for e in events:
        if e.type in [QUIT]:
            going = False
        if e.type == TRACK_END:
            reserved_channel_0.queue(nextsound)
            notethatwasplayed = nextnotethatwillplay
            screen.fill(Color("LawnGreen"))
            screen.blit(my_font.render("Looping",True,Color("Black")),(0,0) )
            pygame.display.update()

    if i.poll():
        midi_events = i.read(10)
        print "full midi_events " + str(midi_events)
        print "my midi note is " + str(midi_events[0][0][1])

        if str(midi_events[0][0][2]) != "0":
            mymidinote = str(midi_events[0][0][1])
            print "on event"

        if str(midi_events[0][0][2]) == "0":
            mymidinote = "0"
            print "off event"

        assignmidi()

        #convert them into pygame events.
        midi_evs = pygame.midi.midis2events(midi_events, i.device_id)

        for m_e in midi_evs:
            event_post( m_e )
del i
exit()

"""
---------------------------------------------------------------------
Installing the Program:

    1) Install Pygame. (pygame.org)
    2) Copy this file into a folder with sound files.
    3) Edit the file to assign the sound files to simples variable at line 11 like this:

sound1 = pygame.mixer.Sound("beat1.wav")
sound2 = pygame.mixer.Sound("beat2.wav")
sound3 = pygame.mixer.Sound("beat3.wav")
sound4 = pygame.mixer.Sound("beat4.wav")
(and so on..)

    4) Edit the file to assign the sound files to MIDI note numbers (C=48, C#=49, D=50 etc) at line 18 like this:

        if mymidinote == "48":
            nextsound = sound1
            updateScreenAndQueueNextSound()
        if mymidinote == "50":
            nextsound = sound2
            updateScreenAndQueueNextSound()
        if mymidinote == "52":
            nextsound = sound3
            updateScreenAndQueueNextSound()
        if mymidinote == "53":
            nextsound = sound4
            updateScreenAndQueueNextSound()

(*Hint watch the spaces in front of the lines, they are important)

---------------------------------------------------------------------
Running the Program:

    1) Open a terminal window
    2) Change to the folder with the program and sound files
    3) Enter this command: python -u badpenni.py
    4) This prompt will be displayed:

 0: interface :ALSA:, name :Midi Through Port-0:, opened :0:  (output)
 1: interface :ALSA:, name :Midi Through Port-0:, opened :0:  (input)
 2: interface :ALSA:, name :Virtual Keyboard:, opened :0:  (input)
See list above. Please enter the number for your midi input device:

    6) Enter the number from the list for your MIDI device.

    7) Play the assigned notes with your MIDI device. The assigned sounds will play and loop. Newly triggered sounds will be queued to play when the current loop finishes.
"""

# -------------end badpenni.py file -------------