Last active
November 22, 2016 19:58
-
-
Save eliasdorneles/dc6cfaffda0a5fc63a82 to your computer and use it in GitHub Desktop.
Challenge: guess the song by just reading the code (before playing it)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import subprocess | |
import time | |
VERBOSE = False | |
def wait(seconds): | |
time.sleep(seconds) | |
def play_note(note='C', duration=1, delay=0, vol=1): | |
# requires sox to be installed: http://sox.sf.net | |
command = ( | |
"play -qn synth {duration} pluck {note}" | |
" fade l 0 {duration} 2 reverb vol {vol}" | |
).format(note=note, duration=duration, vol=vol) | |
if VERBOSE: | |
print(command) | |
subprocess.Popen(command.split()) | |
if delay: | |
wait(delay) | |
def play_chord(notes, duration=4, delay=0, vol=1): | |
for note in notes: | |
play_note(note, duration, delay=0.01, vol=vol) | |
if delay: | |
wait(delay) | |
def play_song(): | |
for a in range(2): | |
play_note('C2', duration=5) | |
for b in range(4): | |
play_chord(['E3', 'G3', 'C4'], delay=0.4) | |
play_note('C3', delay=0.4) | |
play_note('F2', duration=5) | |
for b in range(3): | |
play_chord(['F3', 'A3', 'C4'], delay=0.4) | |
play_note('C3', delay=0.4) | |
play_chord(['F3', 'A3', 'C4'], delay=0.2) | |
play_note('A#3', duration=0.5, delay=0.2) | |
play_note('B3', duration=0.5) | |
play_note('C3', delay=0.4) | |
for a in range(4): | |
play_note('C2', duration=4) | |
for b in range(3): | |
play_chord(['E3', 'G3', 'C4'], delay=0.4) | |
play_note('C3', delay=0.4) | |
play_chord(['E3', 'G3', 'B3'], delay=0.4) | |
play_note('C3', delay=0.4) | |
play_note('F2', duration=4) | |
for b in range(3): | |
play_chord(['F3', 'A3', 'C4'], delay=0.4) | |
play_note('C3', delay=0.4) | |
if a % 2 == 0: | |
play_chord(['F3', 'A3', 'C4'], delay=0.2) | |
play_note('A#3', duration=0.5, delay=0.2) | |
play_note('B3', duration=0.5) | |
play_note('C3', delay=0.4) | |
else: | |
play_chord(['F3', 'A3', 'C4'], delay=0.4) | |
if a == 3: | |
play_note('C2', duration=0.5) | |
play_note('C3', delay=0.4) | |
play_note('F2', duration=3) | |
play_chord(['F3', 'A3', 'C4'], delay=0.4) | |
play_note('C3', delay=0.4) | |
play_chord(['F3', 'A3', 'C4'], delay=0.4) | |
play_note('F2', duration=0.5) | |
play_note('C3', delay=0.4) | |
play_note('E2', duration=3) | |
play_chord(['E3', 'A3', 'C4'], delay=0.4) | |
play_note('C3', delay=0.4) | |
play_chord(['E3', 'A3', 'C4', 'E4'], delay=0.4) | |
play_note('E2', duration=0.5) | |
play_chord(['C3', 'E4'], delay=0.4) | |
play_note('D4', duration=3) | |
play_note('D2', duration=3) | |
play_chord(['F3', 'A3', 'C4'], delay=0.4) | |
play_note('D3', delay=0.4) | |
play_chord(['F3', 'A3', 'C4'], delay=0.4) | |
play_note('D2', duration=0.5) | |
play_note('D3', delay=0.4) | |
play_note('C2', duration=3) | |
play_chord(['F3', 'A3', 'C4'], delay=0.4) | |
play_note('D3', delay=0.4) | |
play_chord(['F3', 'A3', 'C4'], delay=0.4) | |
play_note('D3', delay=0.4) | |
play_note('G1', duration=4) | |
play_chord(['D3', 'F3', 'G3', 'B3'], delay=0.4) | |
play_chord(['D3', 'F3', 'G3', 'B3'], delay=0.4) | |
wait(0.8) | |
play_chord(['D3', 'F3', 'G3', 'B3'], delay=0.8) | |
play_note('C4', duration=1, delay=0.4) | |
play_note('D4', duration=4, delay=0.4) | |
play_chord(['G2', 'D3', 'F3', 'G3', 'B3'], delay=1.6, duration=4, vol=0.5) | |
play_note('E4', delay=0.4) | |
play_note('G4', delay=0.4) | |
play_note('E4', delay=0.2) | |
play_note('D4', delay=0.2) | |
play_note('C4', delay=0.4) | |
for i in range(3): | |
play_note('F2', duration=3) | |
play_chord(['C3', 'F3'], delay=0.4) | |
play_note('C3', delay=0.4) | |
play_chord(['F3', 'A3'], delay=0.4) | |
play_chord(['C3', 'C4'], duration=0.5, delay=0.4) | |
play_note('G2', duration=3) | |
play_chord(['D3', 'G3', 'B3'], delay=0.4) | |
play_note('D3', delay=0.4) | |
if i == 1: | |
play_chord(['D3', 'G3', 'A3'], delay=0.2) | |
play_note('B3', delay=0.2) | |
play_note('G3', delay=0.4) | |
else: | |
play_chord(['D3', 'G3', 'A3'], delay=0.4) | |
play_note('B3', delay=0.2) | |
play_note('C4', delay=0.2) | |
play_note('C2', duration=3) | |
for b in range(2): | |
play_chord(['E3', 'G3', 'C4'], delay=0.4) | |
play_note('C3', duration=0.5, delay=0.4) | |
play_note('E2', duration=3) | |
for b in range(2): | |
play_chord(['D3', 'G#3', 'B3'], delay=0.4) | |
play_note('D3', duration=0.5, delay=0.4) | |
play_note('F2', duration=3) | |
play_chord(['C3', 'F3'], delay=0.4) | |
play_note('C3', delay=0.4) | |
play_chord(['F3', 'A3'], delay=0.4) | |
play_chord(['C3', 'C4'], delay=0.4) | |
play_note('G2', duration=3) | |
play_chord(['F3', 'A3', 'D4'], delay=0.8) | |
play_note('C4', delay=0.4) | |
play_note('D4', delay=0.4) | |
play_note('C2', duration=8) | |
play_chord(['E3', 'G3', 'E4'], delay=0.2, duration=6) | |
play_note('C4', delay=0.2) | |
play_note('C4', duration=7) | |
if __name__ == '__main__': | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--verbose', action='store_true', | |
help='Enable verbose mode') | |
parser.add_argument('--test', action='store_true', help='Run test') | |
args = parser.parse_args() | |
if args.verbose: | |
VERBOSE = True | |
play_song() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No Ubuntu: