Created
April 19, 2015 19:15
-
-
Save jacobjoaquin/cdde5d11e7e9f077a5ca to your computer and use it in GitHub Desktop.
Csound Python Score Real-time Performance Prototype
This file contains hidden or 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/python | |
import csnd6 | |
import mido | |
import sys | |
from csd.pysco import PythonScore | |
from random import random | |
def note_to_freq(n): | |
'''Convert MIDI note to frequency''' | |
return 440.0 * 2 ** ((n - 69) / 12.0) | |
def flush_score(): | |
'''Send PySco data to Csound, and reset data''' | |
output = '\n'.join(score._score_data) | |
cs.ReadScore(output) | |
print(output) | |
score._score_data = [] | |
def play_arp(amp, note): | |
'''Arpeggiate note''' | |
arp_notes = (0, 4, 7, 12) | |
t = 0 | |
for a in arp_notes: | |
score.i(1, t, 0.25, amp, note_to_freq(note + a)) | |
t += 0.25 | |
# Setup PySco | |
score = PythonScore() | |
cue = score.cue | |
# Setup Csound API | |
cs = csnd6.Csound() | |
csPerf = csnd6.CsoundPerformanceThread(cs) | |
cs.SetOption('-odac') | |
cs.CompileOrc(''' | |
sr = 44100 | |
kr = 4410 | |
ksmps = 10 | |
nchnls = 1 | |
0dbfs = 1.0 | |
''') | |
cs.Start() | |
csPerf.Play() | |
cs.CompileOrc(''' | |
instr 1 | |
idur = p3 | |
iamp = p4 | |
ifreq = p5 | |
k1 line 1, idur, 0 | |
a1 oscils iamp, ifreq, 1 | |
out a1 * k1 | |
endin | |
''') | |
# Play something in the beginning for maintain sanity | |
score.i('1 0 0.125 0.25 440'); | |
flush_score() | |
# The rest is modified from mido receive.py example | |
if len(sys.argv) > 1: | |
portname = sys.argv[1] | |
else: | |
portname = None # Use default port | |
try: | |
with mido.open_input(portname) as port: | |
print('Using {}'.format(port)) | |
print('Waiting for messages...') | |
for m in port: | |
print('Received {}'.format(m)) | |
if m.type is 'note_on' and m.velocity and m.note: | |
play_arp(m.velocity / 127.0 * 0.5, m.note) | |
flush_score() | |
except KeyboardInterrupt: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment