To use icsound create an icsound instance:
In [1]:
import icsound
cs = icsound.icsound()
Then start the engine:
In [2]:
cs.start_engine()
You can set the properties of the Csound engine with parameters to the start_engine() function.
In [3]:
help(cs.start_engine)
The engine runs in a separate thread, so it doesn't block execution of python.
In [4]:
cs.start_engine()
Use the %%csound magic command to directly type csound language code in the cell and send it to the current engine.
In [5]:
%%csound
gkinstr init 1
In [6]:
%%csound
print i(gkinstr)
So where did it print?
In [7]:
cs.print_log()
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 cinfusing, use the clear_log() function.
You can create csound f-tables directly from python lists or numpy arrays:
In [8]:
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.
In [9]:
cs.plot_table(1)
cs.plot_table(2, reuse=True)
grid()
You can get the function table values from the csound instance:
In [10]:
cs.get_table_data(1)
Out[10]:
In [11]:
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))
In [12]:
cs.get_table_data(2)[100: 105]
Out[12]:
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:
In [13]:
randsig = random.random((320, 720))
i = 0
for i,row in enumerate(randsig):
cs.fill_table(50 + i, row)
print i, '..',
In [14]:
cs.plot_table(104)
You can send instruments to the running csound engine with the %%csound magic. Any syntax errors will be displayed inline.
In [15]:
%%csound
instr 1
asig asds
In [16]:
%%csound
instr 1
asig oscil 0.5, 440
outs asig, asig
In [17]:
%%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:
In [18]:
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:
In [19]:
cs.get_channel("val")
Out[19]:
You can record the realtime output from csound: (This requires Csound 6.04 - unrealeased as of this writing...)
In [25]:
cs.start_record("out.wav")
In [26]:
cs.send_score("i 1 0 1")
import time
time.sleep(1)
In [27]:
cs.stop_record()
In [28]:
!aplay out.wav
You can also interact with engines through UDP. Note that not all operations are available, notably reading f-tables, but you can send instruments and note events to the remote engine.
In [31]:
cs_client = icsound.icsound()
cs_client.start_client()
In [32]:
cs.clear_log()
Now send notes and instruments from the client:
In [33]:
cs_client.send_score("i 1 0 1")
cs_client.send_code("print i(gkinstr)")
And show the log in the server:
In [34]:
cs.print_log()
In [ ]:
cs.stop_engine()
In [ ]:
cs
In [ ]:
cs.stop_engine()
In [ ]:
Another engine:
In [9]:
ics = icsound.icsound()
In [10]:
ics.start_engine(buffersize=64)
In [11]:
ics.list_interfaces()
In [12]:
%%csound
instr 1
asig oscil 0.5, 440
outs asig, asig
endin
In [13]:
ics.send_score("i 1 0 0.5")
In [ ]:
ics.stop_engine()
In [8]:
del ics
In [ ]: