Site Map - skip to main content

Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes every weekday Monday through Friday.
This page was generated by The HPR Robot at

hpr0991 :: Making a Music Sampler with Midi and Pygame

Using Python Pygame and a Midi controller to make a sampler

<< First, < Previous, , Latest >>

Thumbnail of bgryderclock
Hosted by bgryderclock on 2012-05-20 is flagged as Explicit and is released under a CC-BY-SA license.
Python, Pygame, MIDI, sampler. (Be the first).
The show is available on the Internet Archive at:

Listen in ogg, spx, or mp3 format. Play now:

Duration: 00:09:50


Pygame Midi documentation:

Pygame Mailing List: sample from pygame example folder:

Sampler/Sound Looper made from python, pygame and midi:

Sample values that populate midi_events variable:

Middle C note key press (notice the data1 is 60 and data2 is 127)
<Event(34-Unknown {'status': 144, 'vice_id': 2, 'timestamp': 6701, 'data1': 60, 'data3': 0, 'data2': 127})>

Middle C note key release (notice the data1 is 60 and data2 is 0)
<Event(34-Unknown {'status': 128, 'vice_id': 2, 'timestamp': 6764, 'data1': 60, 'data3': 0, 'data2': 0})>

Middle C# note key press (notice the data1 is now 61)
<Event(34-Unknown {'status': 144, 'vice_id': 2, 'timestamp': 206684, 'data1': 61, 'data3': 0, 'data2': 127})>

Python code snippet that pulls the note number from the midi_events list and appends an "off" string if it is a key release.

    if str(midi_events[0][0][2]) != "0":
        midinote = str(midi_events[0][0][1])
        midinote = str(midi_events[0][0][1]) + "off"

Controlling sounds with if statements and our midinote variable:

    distbassrollloop = pygame.mixer.Sound("7FullCircleDistBassRollLoop.wav")
    distsnarerollloop = pygame.mixer.Sound("7FullCircleDistSnareRollLoop.wav")
    distbass = pygame.mixer.Sound("7FullCircleDistBassPad.wav")
    distsnare = pygame.mixer.Sound("7FullCircleDistSnare.wav")

    if midinote == "48":

    if midinote == "49":

    if midinote == "49off":

    if midinote == "50":

    if midinote == "51":

    if midinote == "51off":

Contact info:

bgryderclock on Google+:

bgryderclock on Twitter:

bgryderclock on Identica:



Subscribe to the comments RSS feed.

Leave Comment

Note to Verbose Commenters
If you can't fit everything you want to say in the comment below then you really should record a response show instead.

Note to Spammers
All comments are moderated. All links are checked by humans. We strip out all html. Feel free to record a show about yourself, or your industry, or any other topic we may find interesting. We also check shows for spam :).

Provide feedback
Your Name/Handle:
Anti Spam Question: What does the letter P in HPR stand for?
Are you a spammer?
What is the HOST_ID for the host of this show?
What does HPR mean to you?