%pylab inline
from __future__ import print_function
from __future__ import division
Populating the interactive namespace from numpy and matplotlib
To use icsound create an icsound instance:
import icsound
cs = icsound.icsound()
Then start the engine:
cs.start_engine()
Csound server started.
You can set the properties of the Csound engine with parameters to the start_engine() function.
help(cs.start_engine)
Help on method start_engine in module icsound: start_engine(self, sr=44100, ksmps=64, nchnls=2, zerodbfs=1.0, dacout='dac', adcin=None, port=None, buffersize=None) method of icsound.icsound instance Start the csound engine on a separate thread
The engine runs in a separate thread, so it doesn't block execution of python.
cs.start_engine()
icsound: Csound already running
Use the %%csound magic command to directly type csound language code in the cell and send it to the current engine.
%%csound
gkinstr init 1
%%csound
print i(gkinstr)
So where did it print?
cs.print_log()
rtaudio: ALSA module enabled rtmidi: ALSA Raw MIDI module enabled WARNING: STK opcodes not available: define environment variable RAWWAVE_PATH (points to rawwaves directory) to use STK opcodes. --Csound version 6.07 (double samples) Apr 12 2016 graphics suppressed, ascii substituted 0dBFS level = 1.0 orch now loaded audio buffered in 256 sample-frame blocks ALSA output: total buffer size: 1024, period size: 256 writing 512 sample blks of 64-bit floats to dac SECTION 1: instr 0: #i0 = 1.000
By default, messages from Csound are not shown, but they are stored in an internal buffer. You can view them with the print_log() function. If the log is getting too long and confusing, use the clear_log() function.
You can create csound f-tables directly from python lists or numpy arrays:
cs.fill_table(1, [4,5,7,0,8,7,9])
cs.fill_table(2, array([8,7,9, 1, 1, 1]))
Tables can be plotted in the usual matplotlib way, but icsound provides a plot_table function which styles the graphs.
cs.plot_table(1)
cs.plot_table(2, reuse=True)
grid()
You can get the function table values from the csound instance:
cs.get_table_data(1)
array([ 4., 5., 7., 0., 8., 7.], dtype=float32)
cs.make_table(2, 1024, 10, 1)
cs.make_table(3, 1024, -10, 0.5, 1)
cs.plot_table(2)
cs.plot_table(3, reuse=True)
#ylim((-1.1,1.1))
cs.get_table_data(2)[100: 105]
array([ 0.57580817, 0.58081394, 0.58579785, 0.59075969, 0.59569931], dtype=float32)
If ctypes is available in your system, icsound will use it to significantly speed up data transfer to and from Csound f-tables. The following will create 320 tables with 720 points each:
randsig = random.random((320, 720))
i = 0
for i,row in enumerate(randsig):
cs.fill_table(50 + i, row)
print(i, '..', end='' )
0 ..1 ..2 ..3 ..4 ..5 ..6 ..7 ..8 ..9 ..10 ..11 ..12 ..13 ..14 ..15 ..16 ..17 ..18 ..19 ..20 ..21 ..22 ..23 ..24 ..25 ..26 ..27 ..28 ..29 ..30 ..31 ..32 ..33 ..34 ..35 ..36 ..37 ..38 ..39 ..40 ..41 ..42 ..43 ..44 ..45 ..46 ..47 ..48 ..49 ..50 ..51 ..52 ..53 ..54 ..55 ..56 ..57 ..58 ..59 ..60 ..61 ..62 ..63 ..64 ..65 ..66 ..67 ..68 ..69 ..70 ..71 ..72 ..73 ..74 ..75 ..76 ..77 ..78 ..79 ..80 ..81 ..82 ..83 ..84 ..85 ..86 ..87 ..88 ..89 ..90 ..91 ..92 ..93 ..94 ..95 ..96 ..97 ..98 ..99 ..100 ..101 ..102 ..103 ..104 ..105 ..106 ..107 ..108 ..109 ..110 ..111 ..112 ..113 ..114 ..115 ..116 ..117 ..118 ..119 ..120 ..121 ..122 ..123 ..124 ..125 ..126 ..127 ..128 ..129 ..130 ..131 ..132 ..133 ..134 ..135 ..136 ..137 ..138 ..139 ..140 ..141 ..142 ..143 ..144 ..145 ..146 ..147 ..148 ..149 ..150 ..151 ..152 ..153 ..154 ..155 ..156 ..157 ..158 ..159 ..160 ..161 ..162 ..163 ..164 ..165 ..166 ..167 ..168 ..169 ..170 ..171 ..172 ..173 ..174 ..175 ..176 ..177 ..178 ..179 ..180 ..181 ..182 ..183 ..184 ..185 ..186 ..187 ..188 ..189 ..190 ..191 ..192 ..193 ..194 ..195 ..196 ..197 ..198 ..199 ..200 ..201 ..202 ..203 ..204 ..205 ..206 ..207 ..208 ..209 ..210 ..211 ..212 ..213 ..214 ..215 ..216 ..217 ..218 ..219 ..220 ..221 ..222 ..223 ..224 ..225 ..226 ..227 ..228 ..229 ..230 ..231 ..232 ..233 ..234 ..235 ..236 ..237 ..238 ..239 ..240 ..241 ..242 ..243 ..244 ..245 ..246 ..247 ..248 ..249 ..250 ..251 ..252 ..253 ..254 ..255 ..256 ..257 ..258 ..259 ..260 ..261 ..262 ..263 ..264 ..265 ..266 ..267 ..268 ..269 ..270 ..271 ..272 ..273 ..274 ..275 ..276 ..277 ..278 ..279 ..280 ..281 ..282 ..283 ..284 ..285 ..286 ..287 ..288 ..289 ..290 ..291 ..292 ..293 ..294 ..295 ..296 ..297 ..298 ..299 ..300 ..301 ..302 ..303 ..304 ..305 ..306 ..307 ..308 ..309 ..310 ..311 ..312 ..313 ..314 ..315 ..316 ..317 ..318 ..319 ..
cs.plot_table(104)
You can send instruments to the running csound engine with the %%csound magic. Any syntax errors will be displayed inline.
%%csound
instr 1
asig asds
error: syntax error, unexpected T_IDENT (token "asds") line 3: >>>asig asds <<< Unexpected untyped word asig when expecting a variable Parsing failed due to invalid input! Stopping on parser failure
%%csound
instr 1
asig oscil 0.5, 440
outs asig, asig
error: syntax error, unexpected $end (token "") line 4: >>> <<< Parsing failed due to invalid input! Stopping on parser failure
%%csound
instr 1
asig oscil 0.5, 440
outs asig, asig
endin
Csound channels can be used to send values to Csound. They can affect running instances of instruments by using the invalue/chnget opcodes:
cs.set_channel("val", 10)
You can also read the channels from Csound. These channels can be set from icsound or within instruments with the outvalue/chnset opcodes:
cs.get_channel("val")
10.0
You can record the realtime output from csound: (This requires Csound 6.04)
cs.start_record("out.wav")
cs.send_score("i 1 0 1")
import time
time.sleep(1)
cs.stop_record()
!aplay out.wav
Playing WAVE 'out.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo
cs.stop_engine()
cs
<icsound.icsound instance at 0x7f067ad6a0e0>
cs.stop_engine()
Engine is not running.
Found this:
request_url = 'https://spreadsheets.google.com/feeds/list/0AhySzEddwIC1dEtpWF9hQUhCWURZNEViUmpUeVgwdGc/1/public/basic?alt=json'
import urllib2
f = urllib2.urlopen(request_url)
import json
ftse_data = json.load(f)
ftse_data.keys()
[u'feed', u'version', u'encoding']
symbol_list = ftse_data['feed']['entry']
len(symbol_list)
101
symbol_names = [symbol['title']['$t'] for symbol in symbol_list]
import datetime
start_date = datetime.date(2005,1, 1)
end_date = datetime.date(2010, 12, 31)
symbol = symbol_names[0]
date_string = '&a=%02i&b=%02i&c=%i&d=%02i&e=%02i&f=%i&g=d'%(start_date.day, start_date.month, start_date.year,
end_date.day, end_date.month, end_date.year)
settings = 's=%s&ignore=.csv'%symbol
url = 'http://ichart.finance.yahoo.com/table.csv?' + settings + date_string
#url = 'http://ichart.finance.yahoo.com/table.csv?s=^GSPC&ignore=.csv'
f = urllib2.urlopen(url)
data = f.read()
import csv
parsed_csv = csv.reader(data.split('\n'))
parsed_csv.next() # This removes the "header"
['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Close']
highs = []
for row in parsed_csv:
if len(row) > 0:
highs.append(float(row[2]))
plot(highs)
[<matplotlib.lines.Line2D at 0x7f065016dd10>]
Now let's get us some data
symbol_highs = {}
for symbol in symbol_names:
date_string = '&a=%02i&b=%02i&c=%i&d=%02i&e=%02i&f=%i&g=d'%(start_date.day, start_date.month, start_date.year,
end_date.day, end_date.month, end_date.year)
settings = 's=%s&ignore=.csv'%symbol
url = 'http://ichart.finance.yahoo.com/table.csv?' + settings + date_string
try:
f = urllib2.urlopen(url)
data = f.read()
parsed_csv = csv.reader(data.split('\n'))
parsed_csv.next() # This removes the "header"
highs = []
for row in parsed_csv:
if len(row) > 0:
highs.append(float(row[2]))
symbol_highs[symbol] = array(highs)
except:
print("Symbol %s not found"%symbol)
Symbol AMEC.L not found Symbol BSY.L not found Symbol CSCG.L not found Symbol IPR.L not found Symbol RSL.L not found
len(symbol_highs)
96
for highs in symbol_highs.itervalues():
plot(highs)
plot(highs)
[<matplotlib.lines.Line2D at 0x7f065011bb90>]
def normalize_array(input_array):
data_range = input_array.max() - input_array.min()
out_array = (input_array - input_array.min())/data_range
return out_array
for highs in symbol_highs.itervalues():
plot(normalize_array(highs))
cs = icsound.icsound()
cs.start_engine(ksmps=128)
Csound server started.
%%csound
instr 1
itable = p4
ilow = p5
ihigh = p6
iamp = p7 ; mean of the data
ipan = p8
imean = p9
kread poscil 1, 1/p3, itable
kfreq = ilow + (ihigh - ilow) * kread
aout oscili iamp * (1 - abs(iamp - kread)), kfreq
;;aL,aR pan2 aout, ipan
outs aout, aout
endin
cs.fill_table(1, normalize_array(highs))
cs.send_score('i 1 0 10 %i %f %f %f 0.5'%(1, 220, 2200, 0.2))
for i, highs in enumerate(symbol_highs.itervalues()):
cs.fill_table(i + 1, normalize_array(highs))
cs.send_score('i 1 0 10 %i %f %f %f 0.5'%(10, 220, 2200, 0.2))
score = ''
amp = 0.8/len(symbol_highs)
for i in range(len(symbol_highs)):
score += 'i 1 0 20 %i %f %f %f 0.5\n'%(i + 1, 55, 6000, amp)
cs.send_score(score)
len(symbol_highs)
96
score = ''
amp = 0.8/len(symbol_highs)
for i in range(32):
score += 'i 1 0 20 %i %f %f %f 0 %f\n'%(i + 1, 50, 300, amp, i/96.0)
for i in range(32):
score += 'i 1 0 20 %i %f %f %f 0.5 %f\n'%(i + 1 + 32, 900, 1200, amp, (i+ 32)/96.0)
for i in range(32):
score += 'i 1 0 20 %i %f %f %f 1 %f\n'%(i + 1 + 64, 3000, 6000, amp, (i+64)/96.0)
cs.send_score(score)
Panning, and relative level
By: Andrés Cabrera mantaraya36@gmail.com
For Course MAT 240F at UCSB
This ipython notebook is licensed under the CC-BY-NC-SA license: http://creativecommons.org/licenses/by-nc-sa/4.0/