Code - Home

Music Generator

A simple python program I wrote sometime in ~2015-2017 that creates a random 'tune' with bleeps and bloops. It doesn't really follow any music theory.

			
import winsound
import random

# A list of notes
#        A    Bb   B   C     Db   D    Eb   E    F    Gb   G    Ab
notes = [440, 466, 493, 523, 554, 587, 622, 659, 698, 739, 783, 830]

# Each note has an array with other notes that can be chosen next.
# Some notes appear more than once to adjust the probabilities.
A  = [659, 659, 659, 587, 587, 830]
Bb = [698, 698, 622]
B  = [587, 587, 587, 659, 659, 440]
C  = [587, 587, 587, 587, 783, 783, 783, 440, 440, 493, 466, 698]
Db = [830, 830]
D  = [440, 659]
Eb = [466, 830]
E  = [440, 587]
F  = [783, 783, 783, 783, 440, 440, 440, 659, 659, 587]
Gb = [554, 493]
G  = [587, 587, 440, 440, 659]
Ab = [554, 554, 554, 554, 440, 440, 440, 783, 783, 587]

# Put the transition lists in the same order as "notes"
next_notes = [A, Bb, B, C, Db, D, Eb, E, F, Gb, G, Ab]

# A list of note durations
times = [150, 150, 150, 300, 300, 600]

# Arrays for the notes and times
notelist = []
timelist = []

# Create a tune
newnote = random.choice(notes)
notelist.append(newnote)
timelist.append(random.choice(times))

for x in range(7):
    # Find which note we currently have
    for i in range(len(notes)):
        if newnote == notes[i]:
            # Pick the next note from the corresponding list
            newnote = random.choice(next_notes[i])
            break
    notelist.append(newnote)
    timelist.append(random.choice(times))

# Show the notes and times
print(notelist)
print(timelist)

# Play the tune forever
a = 0
b = 1

while True:
    print(notelist[a], timelist[a])
    winsound.Beep(notelist[a], timelist[a])
    a += 1
    if a >= len(notelist):
        a = 0
        b += 1
        print("bar", b)