1

I did find the Python code snippet on PMIDI module here. It works fine and I have made some interesting sounds with it. Is there way to save this as sound file?

2
Contributors
1
Reply
4
Views
11 Years
Discussion Span
Last Post by vegaseat
1

The beauty of the PMIDI module is that you can produce sound, be it music or soundeffects, within your program with a few lines of code. This way you don't have to load a number of wave files (.wav) that are typically between 5k and 250k in size. Here is an example ...

# some sound effects using PMIDI
# NewNote(beat,duration,note,octave)

from PMIDI import *
from time import sleep

def bird():
    seq = Sequencer()
    song = seq.NewSong()
    voice = song.NewVoice(0)
    voice.SetInstrumentByName('Bird Tweet')
    m = voice.NewMeasure()
    m.NewNote(0, 8, 'A', 5)
    m.NewNote(8, 8, 'D', 5)
    m.NewNote(16, 8, 'A', 5)
    seq.Play()
    sleep(1.2)   # enough seconds to play
    seq.Close()

def gun():
    seq = Sequencer()
    song = seq.NewSong()
    voice = song.NewVoice()
    voice.SetInstrumentByName('Gunshot')
    m = voice.NewMeasure()
    m.NewNote(0, 12, 'F', 4)
    seq.Play()
    sleep(0.8)   # enough seconds to play
    seq.Close()

def wood():
    seq = Sequencer()
    song = seq.NewSong()
    voice = song.NewVoice()
    voice.SetInstrumentByName('Woodblock')
    m = voice.NewMeasure()
    m.NewNote(0, 24, 'G', 3)
    seq.Play()
    sleep(1)   # enough seconds to play
    seq.Close()


bird()    # bird chirps
sleep(1)  # wait a second
gun()     # idiot shoots it
sleep(1)
wood()    # bird hits ground
Votes + Comments
Very helpful!
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.