Access Your PC Soundcard (Python)

vegaseat 1 Tallied Votes 667 Views Share

The lowly soundcard in your PC has fascinating capabilities. All you need is a module that can handle the Musical Instrument Digital Interface (MIDI). Tell the soundcard which instrument to play, specify the musical note, the duration, the beat and other things. The sound chip makes 127 instruments available and a whole set of drums and things. You can play a number of different instruments at the same time. You will be surprised about the soundeffects you can create! The Python module is called PMIDI, written by the great sound expert Peter Parente (UNC Assistive Technology), a free download from

# PMIDI is another excellent sound module by Peter Parente, author of pyTTS
# download (self-installer): PMIDI-1.1.win32-py2.4.exe
# free from:
# named instruments and drums are in ..\PMIDI\
# this test plays up and down the scales with optional drums and a sexy ending
# the notes in this example are given as a string then turned into a list
# tested with Python24    vegaseat   18nov2005

from PMIDI import *
from time import sleep

# setup ...
seq = Sequencer()
song = seq.NewSong()
t = song.NewVoice()
t.SetInstrumentByName('French Horn')
m = t.NewMeasure()

# contains note/octave pairs
str1 = "C4D4E4F4G4A4B4C5C5B4A4G4F4E4D4C4"
noteList = list(str1)
print noteList   # test the list

# NewNote(beat,duration,note,octave)  # pitch is note, octave
# NewMeasure() by default numbers itself each time it is called, starts with 0

dur  = 6   # duration of that note in beats
beat = 0   # beat 0 to 63, can not exceed 64 beats per measure here
diff = 8   # use 1 to 9 (fast to slow)
k = 1
for val in noteList:
    k += 1
    # pick note and octave alternately
    if k % 2 == 0:
        note = val
        # print note  # test
        octave = int(val)
        # print beat, dur, note, octave  # test
        m.NewNote(beat, dur, note, octave)
        beat += diff
    if beat > 63:
        m = t.NewMeasure()  # next measure
        beat = 0

# optional, first part accompanied with drums ...
t = song.NewVoice(is_drum = True)
m = t.NewMeasure()  # would start with drum measure 0
# NewHit(beat,duration,drum_type)
m.NewHit(0, 8, 'Bass Drum 1')
m.NewHit(8, 8, 'Pedal Hi-Hat')
m.NewHit(16, 8, 'Bass Drum 1')
m.NewHit(24, 8, 'Pedal Hi-Hat')
m.NewHit(32, 8, 'Bass Drum 1')
m.NewHit(40, 8, 'Pedal Hi-Hat')
m.NewHit(48, 8, 'Bass Drum 1')
m.NewHit(56, 8, 'Pedal Hi-Hat')

# optional closing sound at end of measure 1 ...
t = song.NewVoice()
t.SetInstrumentByName('Tinkle bell')
m = t.NewMeasure(1)
for k in range(8):
    down = ['C', 'B', 'A#', 'A', 'G#', 'G', 'F#', 'F']  # go down the scale
    m.NewNote(56 + k, 1, down[k], 8)

sleep(7)    # give it enough time to play out
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Have fun experimenting with this snippet!

caribedude 0 Newbie Poster

Is this supposed to play on my computer? I can't get the PMIDI module to work. I can write a code and it runs(I've tried this code), but I can't hear anything. Does anyone know why this happens? I'm using Windows XP and yes, my speakers are on and they work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.