Skip to content

Commit

Permalink
Added feature - Command Line Interpreter for interactive music compos…
Browse files Browse the repository at this point in the history
…ition with PySynth.
  • Loading branch information
pranavrc committed Jul 21, 2012
1 parent 4c911e2 commit 57a58d5
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 3 deletions.
107 changes: 107 additions & 0 deletions menv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import pysynth
from pyaudio import pyaudio
import wave
import sys
import os
import string

helpContent = "PySynth musical note interpreter.\nUsage: <Duration><Note> <Duration2><Note2> .... <DurationN><NoteN>\nOptional arguments:\n\t--bpm=Beats per minute [Default:120]\n\t--repeat=Number of bars [Default:1]\nSample: 1d 2c 4f --bpm=150 --repeat=2\nCommands: 'exit' and 'help'"
usageHelp = "Notes are 'a' through 'g' of course,\noptionally with '#' or 'b' appended for sharps or flats.\nFinally the octave number (defaults to octave 4 if not given).\nAn asterisk at the end makes the note a little louder (useful for the beat).\n'r' is a rest.\n\nNote value is a number:\n1=Whole Note; 2=Half Note; 4=Quarter Note, etc.\nDotted notes can be written in two ways:\n1.33 = -2 = dotted half\n2.66 = -4 = dotted quarter\n5.33 = -8 = dotted eighth"

class mEnv:
cliInput = ''
synthParam = []
bpmVal = 0
repeatVal = 0
inputEntered = False
def __init__(self):
cliInput = raw_input(">>> ")

self.parse(cliInput)

try:
if self.bpmVal and self.repeatVal:
pysynth.make_wav(self.synthParam, fn = 'temp.wav', silent = True, bpm = self.bpmVal, repeat = self.repeatVal)
elif self.bpmVal:
pysynth.make_wav(self.synthParam, fn = 'temp.wav', silent = True, bpm = self.bpmVal)
elif self.repeatVal:
pysynth.make_wav(self.synthParam, fn = 'temp.wav', silent = True, repeat = self.repeatVal)
else:
pysynth.make_wav(self.synthParam, fn = 'temp.wav', silent = True)
except KeyError:
print "Improper Syntax - Type 'help' to see usage."
mEnv()

def parse(self, cliInput):
if cliInput == 'exit':
sys.exit()
if cliInput == 'help':
print '\n' + helpContent + '\n-------------------------------------------\n' + usageHelp + '\n'
mEnv()

cliInput = cliInput.split()
self.synthParam = []
for comp in cliInput:
if comp.startswith('--'):
comp = comp.strip('-')
comp = comp.split('=')
if comp[0] == 'bpm':
try:
self.bpmVal = int(comp[1])
except IndexError:
print "Improper Syntax - Type 'help' to see usage."
mEnv()
if comp[0] == 'repeat':
try:
self.repeatVal = int(comp[1]) - 1
except IndexError:
print "Improper Syntax - Type 'help' to see usage."
mEnv()
continue

i = 0
for alphanum in comp:
if alphanum.isalpha():
try:
self.synthParam.append((comp[i:], int(comp[:i])))
except ValueError:
print "Non-existential Command."
mEnv()

break
i += 1

def play(self):
chunk = 1024
wf = wave.open('temp.wav', 'rb')
p = pyaudio.PyAudio()

# open stream
stream = p.open(format =
p.get_format_from_width(wf.getsampwidth()),
channels = wf.getnchannels(),
rate = wf.getframerate(),
output = True)

# read data
data = wf.readframes(chunk)

# play stream
while data != '':
stream.write(data)
data = wf.readframes(chunk)

stream.stop_stream()
stream.close()

p.terminate()

def removeFile(self):
os.remove('temp.wav')

if __name__ == "__main__":
print helpContent
while True:
a = mEnv()
a.play()
a.removeFile()
1 change: 1 addition & 0 deletions pyaudio
Submodule pyaudio added at 775aac
7 changes: 4 additions & 3 deletions pysynth.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@

import wave, math, struct

def make_wav(song,bpm=120,transpose=0,pause=.05,boost=1.1,repeat=0,fn="out.wav"):
def make_wav(song,bpm=120,transpose=0,pause=.05,boost=1.1,repeat=0,fn="out.wav", silent=False):
f=wave.open(fn,'w')

f.setnchannels(1)
Expand Down Expand Up @@ -192,12 +192,13 @@ def render2(a,b,vol):
# Write to output file (in WAV format)
##########################################################################

print "Writing to file", fn
if silent == False:
print "Writing to file", fn
curpos = 0
ex_pos = 0.
for rp in range(repeat+1):
for nn, x in enumerate(song):
if not nn % 4:
if not nn % 4 and silent == False:
print "[%u/%u]\t" % (nn+1,len(song))
if x[0]!='r':
if x[0][-1] == '*':
Expand Down

0 comments on commit 57a58d5

Please sign in to comment.