[Csnd] Handling realtime events with python/csound...
Date | 2010-01-04 20:08 |
From | Anthony Palomba |
Subject | [Csnd] Handling realtime events with python/csound... |
I am using the csound/python API to load a csd file. I would like to be able to send events to an instrument in this csd in real time via python. I have been analyzing the example drone.py, it looks like you can send messages and set values via python: self.csound.SetChannel("gkDistortFactor", float(self.gkDistortFactor.get())) # Apply initial control channel values before actually starting synthesis. f = self.configuration.gkHarmonicTableFactor message = 'f 1 0 65536 10 3 0 %f 0 0 %f\n' % (f * 1.0, f * 2.0) self.csound.inputMessage(message) Would I be able to send an i-statement using inputMessage? Also, is there some documentation that lists all the functions supported by the csound/python interface? Does it support all the functions in the csound API reference manual? Thanks, Anthony |
Date | 2010-01-04 20:23 |
From | Michael Gogins |
Subject | [Csnd] Re: Handling realtime events with python/csound... |
Yes, i statements work the same as f statements in inputMessage. Yes, use the C/C++ documentation for Python as well. In addition, you can in python do this: import csnd import CsoundAC print help(csnd) print help(CsoundAC) Hope this helps, Mike On 1/4/10, Anthony Palomba |
Date | 2010-01-04 20:44 |
From | Anthony Palomba |
Subject | [Csnd] Re: Re: Handling realtime events with python/csound... |
Thanks Micheal, I will give that a try. Another quick question... I want to be able to play gestures in real time. The gesture would be created by some python function that takes as its parameters a time value. Is there a way you can define a timer in python that would allow me to call my gesture function at a constant rate? Ultimately, I want to develop a system like Common Music but python based. Anthony On Mon, Jan 4, 2010 at 2:23 PM, Michael Gogins <michael.gogins@gmail.com> wrote: Yes, i statements work the same as f statements in inputMessage. |
Date | 2010-01-04 20:54 |
From | Michael Gogins |
Subject | [Csnd] Re: Re: Re: Handling realtime events with python/csound... |
Your program is ambitious, and also one that I have thought about myself. First of all, CsoundAC does a lot of what Common Music does, plus other things that Common Music does not know how to do. Second, Christopher Ariza has developed athenaCL, a Python module that also does a lot of stuff that Common Music and CsoundAC do, plus other things that neither of them do. He is working on a new version that should be easier to use by scripting Python (currently athenaCL is supposed to be used in an interactive command line mode). Third, if I were to take this on, I would use Lua not Python for various reasons (you can build Lua in, don't need to install it; LuaJIT is faster than Python; etc.). Feel free to talk about this at length with me. Do you have a specification document or notes on what you want to achieve? P.S. yes, you can use a timer to call in events. What you might want is to set up a processKsmps function in Python that dequeues events and dispatches them to Csound before calling csoundProcessKsmps proper. Then your timer callbacks can enqueue events on your Csound event queue. There are lots of facilities for doing this kind of work -- have you looked at Max/MSP or Pure Data? Or have you used Common Music itself? Hope this helps, Mike On 1/4/10, Anthony Palomba |
Date | 2010-01-04 22:04 |
From | Anthony Palomba |
Subject | [Csnd] Re: Re: Re: Re: Handling realtime events with python/csound... |
Yes indeed I use Common Music extensively, it is really great. Scheme is a pretty straight forward language and Taube's examples make it very easy to pick up. But CMs csound integration is pretty minimal. And there is no inter process synchronization which limits things when you want to create a complex interdependent gesture. Max/MSP is great I use it every day just about. I do all my interactive things with it and load csound csd in it with csound~. One objective I have in mind here is code consolidation. It would be ideal to have a library of algorithmic functions that I could call from any environment. Since Max and Csound both understand python, it would make sense that this library could be a composite of csoundAC and AthenaCL. From a python environment I could also interact with Ableton Live, I could even call this library from something like Blender, which also understands python. I have been doing some reading on your Music Graph system too. I like the "music space" node paradigm but it seems to be geared towards non-real time rendering of a score. Having real time control of gestures and musical structures would allow me to play and experiment with the composition in real-time. So I think you are saying that python might not be fast enough to handle things? Perhaps it would be best to leave the timing to something more reliable like Max? I do not have any design spec for this yet. I am trying to wrap my brain around all the parts and how they might fit together. Your feedback is greatly appreciated. - Anthony On Mon, Jan 4, 2010 at 2:54 PM, Michael Gogins <michael.gogins@gmail.com> wrote: Your program is ambitious, and also one that I have thought about myself. |
Date | 2010-01-05 08:32 |
From | Oeyvind Brandtsegg |
Subject | [Csnd] Re: Re: Re: Re: Re: Handling realtime events with python/csound... |
Just my 2 cents on this. Yes, I think Python should be fast enough for most compositional algorithms. There's also optimization methods (psyco, Cpython, etc) that you could use to gain efficiency, these are very effective. What I do in ImproSculpt is run Csound in a ksmps loop, and then increment the clock inside this loop. I use a separate module for timing and queing, where each event is time stamped, and dispersed when it's time is come. The timer starts at zero when the application starts, and is incremented in 1/kr seconds once every ksmps loop iteration (if you want to keep time in seconds). This is not a perfect solution, as any event calls that takes more then 1/kr seconds to complete will leads to Csound dropouts. But, it ensures k-rate sync between Python and Csound though. I would not use Max for timing functions, at least not the standard metro objects etc. These may deviate in timing precision depending on system load (the same will happen to Python time.time() btw.) If Max has a sample precice clock, you might consider using it. best Oeyvind 2010/1/4 Anthony Palomba |
Date | 2010-01-05 13:02 |
From | Michael Gogins |
Subject | [Csnd] Re: Re: Re: Re: Re: Re: Handling realtime events with python/csound... |
Sounds the same as what I was saying. MKG from cell phone On Jan 5, 2010 3:33 AM, "Oeyvind Brandtsegg" <obrandts@gmail.com> wrote: |
Date | 2010-01-05 23:37 |
From | thorin kerr |
Subject | [Csnd] Re: Re: Re: Re: Re: Re: Re: Handling realtime events with python/csound... |
Actually, I've got a question on this... Is there any difference in explicitly writing a python loop with performKsmps and some function... i.e. while True: csound.performKsmps(True) somefunc() As opposed to simply using using the SetProcessCallback method on the CsoundPerformanceThread class? Thorin On Tue, Jan 5, 2010 at 11:02 PM, Michael Gogins |
Date | 2010-01-05 23:55 |
From | Peiman Khosravi |
Subject | [Csnd] Re: Re: Re: Re: Re: Re: Re: Re: Handling realtime events with python/csound... |
Yes I agree but you can very easily make a signal-based MSP clock and then convert its output to max messages. That way you are not relying on max for timing so everything is sample-accurate with no fluctuations. Best, Peiman |
Date | 2010-01-25 23:21 |
From | Anthony Palomba |
Subject | [Csnd] Re: Re: Re: Re: Re: Re: Re: Re: Re: Handling realtime events with python/csound... |
Attachments | pytest3.csd pytest3.py |
So I have made some progress in investigating the possibility of sending realtime procedural events from python to csound, but I seem to have hit a snag... The goal is to load a csd then send a message to it to play a note. I found a nice threading class that manages timed operations. It forms the basis of my timing mechanism for sending events to csound. I created two events, one that starts the csound performance and one that plays a note every second. What I am seeing is that nothing gets played. Perhaps I am sending the wrong message type? What is the proper way to send a note event to csound? I have attached my .py and .csd files. Any feedback would be greatly appreciated. Thanks, Anthony On Tue, Jan 5, 2010 at 5:55 PM, Peiman Khosravi <peimankhosravi@gmail.com> wrote:
|
Date | 2010-01-25 23:34 |
From | Victor Lazzarini |
Subject | [Csnd] Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Handling realtime events with python/csound... |
You can use cs = Csound() ... cs.InputMessage("i1 0 1 0.1 440") cs.PerformKsmps() Just make sure your code is thread-safe, ie. that InputMessage() does not collide with PerformKsmps(). You can use GetScoreTime() (or something) to get the current score time, if you want to sync the timing of your events. Victor On 25 Jan 2010, at 23:21, Anthony Palomba wrote: So I have made some progress in investigating the possibility |
Date | 2010-01-25 23:51 |
From | Michael Gogins |
Subject | [Csnd] Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Handling realtime events with python/csound... |
..Looking at your code, I'm guessing the reason it doesn't sound is because you invoke a complete Csound performance in the same thread from which you are sending events. You either need to send the events from a different thread, or else not use threads at all, but just handle events in between calls to csoundPerformKsmps, in a loop. The CsoundPerformanceThread class in the examples/python/shapes.py program does the former, and the examples/python/drone.py program does the latter. Both of these approaches work well enough. The loop approach in drone.py is simpler, and has worked very well for me, without dropouts or glitches. Note that in drone.py, the events are function table statements ('f' statements), and they do cause dropouts because it takes time to recalculate the function tables. If the events were note statements ('i' statements), they would not cause dropouts; I have other code that demonstrates this (the examples/python/PythonDemoApp.py program is one, but you need wxPython to run it). Note that if you use threads, you have to keep Csound from running at the instant you are sending events to Csound, otherwise Csound could become corrupted and crash. The CsoundPerformanceThread class has code internally that takes care of this. Hope this helps, Mike On Mon, Jan 25, 2010 at 6:21 PM, Anthony Palomba |
Date | 2010-01-26 16:50 |
From | Anthony Palomba |
Subject | [Csnd] Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Handling realtime events with python/csound... |
Attachments | pytest3.py |
You will have to forgive me, I am relatively new to python. I discovered that my event scheduler was not running the csound performance. I have fixed the problem, but am now seeing a different problem... If you look at the source, you will see that each processes is started as its own thread. When I create the process that starts the csound performance, it looks like csound.perform() blocks and does not let anything else run. I am not sure why it is doing that, because the csound performance should be running in a separate thread. I have attached my source file for reference. -ap On Mon, Jan 25, 2010 at 5:51 PM, Michael Gogins <michael.gogins@gmail.com> wrote: ..Looking at your code, I'm guessing the reason it doesn't sound is |
Date | 2010-01-26 18:04 |
From | Victor Lazzarini |
Subject | [Csnd] Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Handling realtime events with python/csound... |
Why don't you use the CsoundPerformanceThread class? It's there exactly for this purpose. Victor On 26 Jan 2010, at 16:50, Anthony Palomba wrote: You will have to forgive me, I am relatively new to python. I discovered |
Date | 2010-01-28 17:40 |
From | Anthony Palomba |
Subject | [Csnd] Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Handling realtime events with python/csound... |
Attachments | pytest3-2.py |
Okay, I took a look at shapes.py and have a better idea of what is going on. But I am still having trouble. The lack of documentation on the python interface makes it very hard to figure things out. I now use the CsoundPerformanceThread, like so... csound = csnd.Csound() res = csound.Compile("pytest3.csd") perf = csnd.CsoundPerformanceThread(csound) perf.Play() This should create a thread that will run the performance, yes? But when I know start my message process, which is in another thread, I get an exception... <Process(Thread-1, initial daemon)> playing note! Unhandled exception in thread started by <bound method Process.run of <Process(Thread-1, initial daemon)>> Is it possible to send messages to a csound thread from another thread? I have attached my source file for reference. Thanks, Anthony On Tue, Jan 26, 2010 at 12:04 PM, Victor Lazzarini <Victor.Lazzarini@nuim.ie> wrote:
|
Date | 2010-01-28 17:52 |
From | "ilterzouomo" |
Subject | [Csnd] Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Handling realtime events with python/csound... |
Hi Mr. Anthony,
this work to me:
import csnd
cs = csnd.Csound()
p = csnd.CsoundPerformanceThread(cs) cs.Compile("piping01.csd")
p.Play() # now csd is running (with a dummy ftable) and i
can send messages like ...
p.InputMessage("i1 0 1")
p.InputMessage("i1 2
1\
i1 3 1 ") a mini help,
ciao,
fran.
|
Date | 2010-01-28 18:02 |
From | "ilterzouomo" |
Subject | [Csnd] Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Handling realtime events with python/csound... |
I forget to say that if You run the file from some
editor (i use scite) You need to import time and add at the end of file
time.sleep(5), that is wait for 5 sec.
ciao,
fran.
|
Date | 2010-01-28 21:23 |
From | Anthony Palomba |
Subject | [Csnd] Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Handling realtime events with python/csound... |
Hey Fran, thanks for the examples. It looks like you are sending messages from the python executing process to the csound performance thread. In my example, I create a second thread which then sends the message to the csound performance thread. My intention is to be able to have algorithmic processes that run on their own, that send messages or update parameters of a csound performance. These processes could expire after a given period of time or play infinitely. Does anyone know if it is possible to send messages to a csound thread from another thread? My example code was posted previously. -ap On Thu, Jan 28, 2010 at 12:02 PM, ilterzouomo <ilterzouomo@fastwebnet.it> wrote:
|
Date | 2010-01-28 21:59 |
From | Oeyvind Brandtsegg |
Subject | [Csnd] Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Handling realtime events with python/csound... |
I did test it quickly, and here's a few things you can change: - Use capitalized "I" on InputMessage, not inputMessage. It's an easy error to make - don't allow your main thread to finish, as all the child threads will have nowhere to go... Add an import time and time.sleep(5) to keep it running for a short time - Stop and Join the csound perf thread before exiting the main thread, this will clean up nicely before stopping. Add these to the end of the python script: perf.Stop() perf.Join() best Oeyvind 2010/1/28 Anthony Palomba |
Date | 2010-01-29 00:06 |
From | Anthony Palomba |
Subject | [Csnd] Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Handling realtime events with python/csound... |
Attachments | pytest3-2.py |
YAY! It works! Thanks Oeyvind for your help. Another question, under what circumstances would I need to call csound.PerformKsmps()? I have attached the working version of my py file for everyone's reference. Thanks again, Anthony On Thu, Jan 28, 2010 at 3:59 PM, Oeyvind Brandtsegg <obrandts@gmail.com> wrote: I did test it quickly, and here's a few things you can change: |
Date | 2010-01-31 01:16 |
From | Oeyvind Brandtsegg |
Subject | [Csnd] Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Handling realtime events with python/csound... |
You only need to call csound.PerformKsmps() if you're not using the performanceThread. The performanceThread is a separate C thread for running Csound. The other (older) method is requires Python to call PerformKsmps once for each k-rate pass in Csound (csound performs one k-rate pass and produces ksmps number of samples each time PerformKsmsps is called). best Oeyvind 2010/1/29 Anthony Palomba |