Skip to content

Instantly share code, notes, and snippets.

@dgnsrekt
Last active October 21, 2019 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgnsrekt/8b922dd33ae31656e1506fbc60af8bd5 to your computer and use it in GitHub Desktop.
Save dgnsrekt/8b922dd33ae31656e1506fbc60af8bd5 to your computer and use it in GitHub Desktop.
from subprocess import Popen, PIPE, STDOUT, run, check_output
from pathlib import Path
from shlex import split
from random import randint, choice
import sys
PITCH = {
"A": 27.500,
"Bb": 29.135,
"A#": 29.135,
"B": 30.868,
"C": 16.352,
"C#": 17.324,
"Db": 17.324,
"D": 18.354,
"Eb": 19.445,
"D#": 19.445,
"E": 20.602,
"F": 21.827,
"F#": 46.249,
"Gb": 46.249,
"G": 24.500,
"Ab": 25.957,
"G#": 25.957,
}
C_MAJOR_SCALE = ["C", "D", "E", "F", "G", "A", "B"]
G_MINOR_SCALE = ["G", "A", "Bb", "C", "D", "Eb", "F"]
G_DORIAN_SCALE = ["G", "A", "Bb", "C", "D", "E", "F"]
C_MAJOR_CHORD = ["C", "E", "G"]
G_MINOR_CHORD = ["G", "Bb", "D"]
octave = 3
scale = G_DORIAN_SCALE
note_length = 2
channels = 2
sequence_length = 12
repeat = 3
add_random_octaves = True
random_melody = True
repeat_bars = 4
if octave:
for key, value in PITCH.items():
PITCH[key] = value * (octave * octave)
command = Path("/usr/bin/alsabat")
assert command.exists()
if random_melody:
melody = [PITCH[choice(scale)] for _ in range(sequence_length)] * repeat
else:
scale = scale + [scale[0]]
melody = [PITCH[key] for key in scale]
rev_melody = [PITCH[key] for key in scale[::-1]]
melody = melody + rev_melody
melody * repeat
print(melody)
def rand_pitch(pitch):
x = randint(0, 4)
if x:
return pitch * x
else:
return pitch
if add_random_octaves:
melody = list(map(lambda x: rand_pitch(x), melody))
if repeat_bars:
melody2 = list(map(lambda x: x * 2, melody))
melody = melody + melody2
melody = melody * repeat_bars
for frequency in melody:
cmd = f"{str(command)} -F {frequency} -n {note_length * 1000} -P plughw"
cmd = split(cmd)
process = check_output(cmd, shell=False)
@dgnsrekt
Copy link
Author

dgnsrekt commented Oct 21, 2019

Ideas
Class melody

Zip(pitch_sequence, note_length seq) => note sequence

@dgnsrekt
Copy link
Author

add rests

@dgnsrekt
Copy link
Author

dgnsrekt commented Oct 21, 2019

for beat in bar => note len seq

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment