Skip to main content

Featured

Python programming 8 Music with mouse-drawn lines by PyGame

I revised my previous program "Small interactive music maker" and now melody is made from mouse-drawn lines. Actually, change is not big. Instead of my Fourier series function, two key functions are included as - draw lines with mouse by pygame - from line to fx Drawing lines from below.   https://www.dreamincode.net/forums/topic/401541-buttons-and-sliders-in-pygame/ This must be some pretty fun for kids!   ---------------------------------------------------------------------------------------------- blog 8 Some versions of pygame may have some bug. Need " pip install pygame==2.0.0.dev6 " import pygame, math, sys, time import pygame.midi # use pygame.midi (not Mido) from random import random, choice pygame.init() # initialize Pygame pygame.midi.init() # initialize Pygame MIDI port = pygame.midi.Output(0) # port open port.set_instrument(53, channel = 0) # Voice port.set_instrument(0, channel = 1) # piano port.set_instrument(33, channel = 2) # bass X = 900 ...

How to create music

This blog is for someone interested in composing music automatically using python.

Python has a library 'Mido' which can handle Midi messages. Using Mido library, with input of basic structure of a song, such as key, scale and chord progression, a program make a music score.

Basic concept is as below,

1. Structure of music
Input basic structure of a music, such as key, scale and chord progression as python lists. Play whole or a part of chord progressions by python to review and refine.

  e.g. 1st phrase. key is C major. chord progression is ["I", "VI", "V", "V7"]

2. Create melody
For each small part (e.g. 16 bar), melody is made by some rules. Some rules I tried are

   - Pitch moves up or down mostly by one or two within a scale randomly. 

   - Use some Fourier series to develop pitch movements. Then fit to closest scale or chord pitches.  

   - Rhythm (length of notes) randomly selected.

3. Bass and Drum
Add some drum and bass patterns.

4. Set instruments within DAW
Output MIDI file of a score to DAW. Set some VST instruments and output audio file by DAW.

I'll show the details later.


Comments

Popular Posts