MIDI: Musical Instrument Digital Interface
These include:
Keyboards
Sequencers
Electronic wind instruments
Guitars
Electronic Violins
Learn More about MIDI cables and Electronic Music Hardware: MIDI Instruments
A MIDI file is a sound file that contains all of the instructions to play the sound file on the computer. (File extension: *.mid)
It contains the following instructions of the music file:
notes
volume
sounds
effects
The computer player or DAW(Digital Audio Workstation) will translate the MIDI into a playable format.
Making Music with Python and Pygame.
The following code will only work offline on your computer.
Install Python from Python.org : https://www.python.org/
Install the latest stable version of Python 3 (Version 3.9 is not compatible with Window 7 and lower)
Windows: Open the command prompt (CMD)
Press the windows start and type cmd
Type: python -m pip install pygame
This will install the pygame library with its modules
More about pygame with midi: https://www.pygame.org/docs/ref/midi.html
'''
This program demonstrates controlling midi devices with python and pygame
This program will play a series of notes based on the midi note codes (First part of Twinkle Twinkle Little Star)
List of steps to setup the Midi Program (review the code below):
** Initialize the midi module with pygame.midi.init(). It is alright to call this more than once.
https://www.pygame.org/docs/ref/midi.html#pygame.midi.init
** Create a midi.Output instance for the desired output device port.
** Select instruments with set_instrument() function calls.
** Turn a note on-> note_on()
**Turn off a note --> note_off()
**Call pygame.midi.quit() when finished. This function will uninitialize the midi module
The quit() function ensures the midi module properly shuts down
'''
import pygame
import pygame.midi
from time import sleep
port = 0
PIANO = 1
CLARINENT = 72
VIOLIN = 41
instrument = VIOLIN
pygame.init()
pygame.midi.init()
"""
This function detects all available midi devices and prints out their information
"""
def detectMidiDevice():
for r in range(pygame.midi.get_count()):
print("Midi "+str(r)+" "+str(pygame.midi.get_device_info(r)))
def midiSong1():
port = pygame.midi.get_default_output_id()
print ("Currently using Output ID :%s:" % port)
midi_out = pygame.midi.Output(port, 0)
midi_out.set_instrument(instrument)
midi_out.note_on(60,127) # 60 is middle C, 127 is "how loud" - max is 127
##chords
midi_out.note_on(48,110)
midi_out.note_on(40,110)
midi_out.note_on(43,110)
##
sleep(.5) #pauses the program for 5 milliseconds
midi_out.note_on(60,127)
sleep(.5)
midi_out.note_on(67,127) # G
sleep(.5)
midi_out.note_on(67,127)
sleep(.5)
midi_out.note_on(69,127) # A
##chords
midi_out.note_on(48,110)
midi_out.note_on(41,110)
midi_out.note_on(33,110)
##
sleep(.5)
midi_out.note_on(69,127)
sleep(.5)
del midi_out
pygame.midi.quit()
detectMidiDevice()
midiSong1()
"""
pygame.midi.get_device_info()
https://www.pygame.org/docs/ref/midi.html#pygame.midi.get_device_info
returns information about a midi device
get_device_info(an_id) -> (interf, name, input, output, opened)
get_device_info(an_id) -> None
Gets the device info for a given id.
Parameters: an_id (int) -- id of the midi device being queried
Returns: if the id is out of range None is returned, otherwise a tuple of (interf, name, input, output, opened) is returned.
interf: string describing the device interface (e.g. 'ALSA')
name: string name of the device (e.g. 'Midi Through Port-0')
input: 1 if the device is an input device, otherwise 0
output: 1 if the device is an output device, otherwise 0
opened: 1 if the device is opened, otherwise 0
"""