[Csnd] lazy request for simple Python scripting example
Date | 2012-11-13 11:47 |
From | Richard Dobson |
Subject | [Csnd] lazy request for simple Python scripting example |
Hello everyone, actually this is a ~very~ lazy request, to be pointed to a blushingly simple and short example of scripting Csound (note events) in Python, that I can maybe even put on a presentation slide for a talk to some school teachers tomorrow, just to show what it would look like. I have as usual got too much to sort out in not enough time to be relaxed about it! Richard Dobson |
Date | 2012-11-13 12:21 |
From | Oeyvind Brandtsegg |
Subject | Re: [Csnd] lazy request for simple Python scripting example |
Hi Richard, do you want to let Python write the score text file to be used by Csound, or generate events in realtime? Here's my simples score write script that I used to use in a class earlier #!/usr/bin/python # -*- coding: latin-1 -*- """ A Csound score writer @author: Øyvind Brandtsegg @contact: obrandts@gmail.com @license: GPL """ # create a file object outfilename = 'my_pythongenerated_score.inc' f = open(outfilename, 'w') # iteration, 13 times over, the value i changes with each iteration for i in range(0,13): # just a progress indicator print 'processing', i # write instrument events to file instrument = 31 start = i duration = 0.8 amp = -10 note = i+60 pan = 0.5 reverb = 0 # write a csound score line as text to the file f.write('i %i %f %f %f %f %f %f \n' %(instrument, start, duration, amp, note, pan, reverb)) # set score length variable to match the actual score length, and write it to the csound score file scorelength = i+2 f.write('#define scorelength # %i #'%scorelength) # close the file f.close print 'Csound score written to', outfilename Oeyvind 2012/11/13 Richard Dobson <richarddobson@blueyonder.co.uk> Hello everyone, -- Oeyvind Brandtsegg Professor of Music Technology NTNU 7491 Trondheim Norway Cell: +47 92 203 205 http://flyndresang.no/ http://www.partikkelaudio.com/ http://soundcloud.com/brandtsegg http://soundcloud.com/t-emp |
Date | 2012-11-13 12:24 |
From | Oeyvind Brandtsegg |
Subject | Re: [Csnd] lazy request for simple Python scripting example |
.... or if you want to do it in realtime, below is a csd that calls a python function (provided in a separate file, pasted at the far botton here). Just random note generation, but shows the general principle. *** simplest.csd*** <CsoundSynthesizer> <CsOptions> -odac1 -iadc1 -b2048 -B4096 -+rtaudio=pa </CsOptions> <CsInstruments> sr = 44100 ksmps = 256 nchnls = 2 0dbfs = 1 giSine ftgen 0, 0, 65537, 10, 1 pyinit pyruni "import sys" pyruni "import os" pyruni "sys.path.append(os.getcwd())" ; make sure that python can find our modules pyruni "import simplest" ; import our own module ;********************************************************************* ; generate a melody by calling python to get (random) note data instr 2 iLow = p4 ; lowest note for random selection iHigh = p5 ; highest note for random selection kbpm = 240 ; tempo in beats per minute ktempo = kbpm/60 ; tempo in ticks per second kTrig metro ktempo ; generate metronome pulse kNote pycall1t kTrig, "simplest.getNextNote", iLow, iHigh if kTrig == 0 kgoto done kDur = (1/ktempo) * 0.9 ; here, note duration is 90% of the time until the next note kAmp = -15 ; amp in decibel instrNum = 31 ; instrument to be triggered event "i", instrNum, 0, kDur, kAmp, kNote done: endin ;*************************************************** ; sine wave instrument instr 31 ; input parameters iamp = ampdbfs(p4) ; amp in dB, relative to 0dbfs iNote = p5 ; p5 used as "midi note number" print iNote icps = cpsmidinn(iNote) ; get cps from midi note number ; envelope iAttack = 0.005 iDecay = 0.3 iSustain = 0.3 iRelease = 0.1 amp madsr iAttack, iDecay, iSustain, iRelease ; audio generator a1 oscil3 amp*iamp, icps, giSine outch 1, a1, 2, a1 endin </CsInstruments> <CsScore> ; start dur low high i2 0 4 60 72 i2 6 4 84 86 i2 8 4 48 55 e </CsScore> </CsoundSynthesizer> *** simplest.py *** #!/usr/bin/python # -*- coding: latin-1 -*- """ Some functions to be called from Csound. @author: Øyvind Brandtsegg @contact: obrandts@gmail.com @license: GPL """ import random def getNextNote(low, high): """ Get data for the next note. Return a random note within a set range. @param low: The lowest note allowed. @param high: The highest note allowed. @return: Midi note number """ # pycall expects a float, but we want only whole (note) numbers, # so we use float(int()) return float(int((random.random()*(high-low))+low)) # test if __name__ == '__main__' : print getNextNote(60, 72) print getNextNote(60, 72) print getNextNote(60, 72) best Oeyvind 2012/11/13 Oeyvind Brandtsegg <oyvind.brandtsegg@ntnu.no> Hi Richard, -- Oeyvind Brandtsegg Professor of Music Technology NTNU 7491 Trondheim Norway Cell: +47 92 203 205 http://flyndresang.no/ http://www.partikkelaudio.com/ http://soundcloud.com/brandtsegg http://soundcloud.com/t-emp |
Date | 2012-11-13 12:45 |
From | Richard Dobson |
Subject | Re: [Csnd] lazy request for simple Python scripting example |
I was looking for something to show Python being used to send note event to a running Csound (I assume that's an available thing to do!). Or in other words, showing Csound as in a sense an extension module for Python. But the first example you give, below, is useful as I can fit the for loop into a PP slide. Ideally, I would like to have an equivalent that sends the note event directly to Csound. I assumed Python could do that, buy maybe I am wrong there? Persuading schools to install (a non-trivial exercise in their heavily locked down networks) and use/teach Csound is one thing, and maybe quite a big thing, but as Python is already established for CS in schools, this might offer a relatively smooth "way in". In the more general case and longer term, I am looking for real-time audio and MIDI modules for Python that would meet school's needs for simplicity, flexibility, etc... Richard Dobson On 13/11/2012 12:21, Oeyvind Brandtsegg wrote: > Hi Richard, > do you want to let Python write the score text file to be used by > Csound, or generate events in realtime? > > Here's my simples score write script that I used to use in a class earlier > .. > # iteration, 13 times over, the value i changes with each iteration > for i in range(0,13): > # just a progress indicator > print 'processing', i > # write instrument events to file > instrument = 31 > start = i > duration = 0.8 > amp = -10 > note = i+60 > pan = 0.5 > reverb = 0 > # write a csound score line as text to the file > f.write('i %i %f %f %f %f %f %f \n' %(instrument, start, duration, > amp, note, pan, reverb)) > ... |
Date | 2012-11-13 13:27 |
From | Oeyvind Brandtsegg |
Subject | Re: [Csnd] lazy request for simple Python scripting example |
Ah, yes, It is of course also possible to run Csound as a python module (import csnd) I do have some examples of that buried somewhere, but not as simplified as those two. Someone else probably has better educational examples for that approach. Or, take the cb.py example in Csound/examples/python and use something like cs.InputMessage('i %i 0 %i %i' %(p1, p3, p4)) to send score events to the loaded csound instance. The reason why I don't use that approach as much is that it is rather cumbersome to create a sample accurate clock in python, so I started doing it the other way around, letting Csound be the master app responsible for time keeping, hosting Python for compositional logic. best Oeyvind 2012/11/13 Richard Dobson <richarddobson@blueyonder.co.uk> I was looking for something to show Python being used to send note event to a running Csound (I assume that's an available thing to do!). Or in other words, showing Csound as in a sense an extension module for Python. -- Oeyvind Brandtsegg Professor of Music Technology NTNU 7491 Trondheim Norway Cell: +47 92 203 205 http://flyndresang.no/ http://www.partikkelaudio.com/ http://soundcloud.com/brandtsegg http://soundcloud.com/t-emp |
Date | 2012-11-13 14:09 |
From | Richard Dobson |
Subject | Re: [Csnd] lazy request for simple Python scripting example |
Ah, thank you for that pointer. I just discovered I had managed to install Csound with the python option unchecked. The issue about timing in Python is very important; I see you say "cumbersome" rather than "impossible", which leave some hope! Richard Dobson On 13/11/2012 13:27, Oeyvind Brandtsegg wrote: > Ah, yes, > It is of course also possible to run Csound as a python module (import csnd) > I do have some examples of that buried somewhere, but not as simplified > as those two. > Someone else probably has better educational examples for that approach. > Or, take the cb.py example in Csound/examples/python and use something like > cs.InputMessage('i %i 0 %i %i' %(p1, p3, p4)) > to send score events to the loaded csound instance. > > The reason why I don't use that approach as much is that it is rather > cumbersome to create a sample accurate clock in python, so I started > doing it the other way around, letting Csound be the master app > responsible for time keeping, hosting Python for compositional logic. > |
Date | 2012-11-13 14:16 |
From | Oeyvind Brandtsegg |
Subject | Re: [Csnd] lazy request for simple Python scripting example |
hehe, you can find precise clocks in for example the pygame module (never actually used it), or if you want to synchronize tightly with Csound, you can construct your own timekeeper and let it be incremented eack ksmps cycle (cumbersome) Oeyvind 2012/11/13 Richard Dobson <richarddobson@blueyonder.co.uk> Ah, thank you for that pointer. I just discovered I had managed to install Csound with the python option unchecked. The issue about timing in Python is very important; I see you say "cumbersome" rather than "impossible", which leave some hope! -- Oeyvind Brandtsegg Professor of Music Technology NTNU 7491 Trondheim Norway Cell: +47 92 203 205 http://flyndresang.no/ http://www.partikkelaudio.com/ http://soundcloud.com/brandtsegg http://soundcloud.com/t-emp |
Date | 2012-11-13 14:50 |
From | Richard Dobson |
Subject | Re: [Csnd] lazy request for simple Python scripting example |
That's fine; pygame is a standard installation in schools already. It is strictly Csound that will be the uncharted territory. Richard Dobson On 13/11/2012 14:16, Oeyvind Brandtsegg wrote: > hehe, > you can find precise clocks in for example the pygame module (never > actually used it), > or if you want to synchronize tightly with Csound, you can construct > your own timekeeper and let it be incremented eack ksmps cycle (cumbersome) > Oeyvind > |
Date | 2012-11-13 18:45 |
From | Andres Cabrera |
Subject | Re: [Csnd] lazy request for simple Python scripting example |
Hi, If you manage to get the latest version of CsoundQt installed there, I just made an adaptation of a python script by you: http://qutecsound.git.sourceforge.net/git/gitweb.cgi?p=qutecsound/qutecsound;a=tree;f=src/Scripts/LiveCoding;h=54a552cf0bf55cbee369a9c073d370476edabf4b;hb=HEAD I took the liberty of including it in the examples (although they are not in the latest release). Please let me know if you prefer that I remove them. Cheers, Andrés On Tue, Nov 13, 2012 at 3:47 AM, Richard Dobson |
Date | 2012-11-13 19:31 |
From | Richard Dobson |
Subject | Re: [Csnd] lazy request for simple Python scripting example |
Thanks! How do I run it? I am presenting on windows, and only have QuteCsound 6 at the moment. Fine to include it in the examples; all this work is for outreach one way or another. Richard Dobson On 13/11/2012 18:45, Andres Cabrera wrote: > Hi, > > If you manage to get the latest version of CsoundQt installed there, I > just made an adaptation of a python script by you: > > http://qutecsound.git.sourceforge.net/git/gitweb.cgi?p=qutecsound/qutecsound;a=tree;f=src/Scripts/LiveCoding;h=54a552cf0bf55cbee369a9c073d370476edabf4b;hb=HEAD > > I took the liberty of including it in the examples (although they are > not in the latest release). Please let me know if you prefer that I > remove them. > |
Date | 2012-11-13 19:39 |
From | Andres Cabrera |
Subject | Re: [Csnd] lazy request for simple Python scripting example |
Hi Richard, You will need the latest version of CsoundQt which includes Python scripting support. There is a builtin object called q which can be used to interact with CsoundQt and running Csound instances. Andy will hopefully have a Windows version for you to try soon. Cheers, Andrés On Tue, Nov 13, 2012 at 11:31 AM, Richard Dobson |
Date | 2012-11-13 20:24 |
From | Victor |
Subject | Re: [Csnd] lazy request for simple Python scripting example |
Richard, did any of the examples in the csound sources help? I can write a simple example if you need one. Sorry I did not respond earlier, but it was a full teaching day. Victor On 13 Nov 2012, at 19:31, Richard Dobson |
Date | 2012-11-13 21:34 |
From | Richard Dobson |
Subject | Re: [Csnd] lazy request for simple Python scripting example |
On 13/11/2012 20:24, Victor wrote: > Richard, > > did any of the examples in the csound sources help? I can write a > simple example if you need one. Sorry I did not respond earlier, but > it was a full teaching day. > the "keys.py" example looks good; but I don't have "ah.wav" to go with it. I could do with something like that but with very simple synth sounds - it's just to show the possibilities. John sent me something like that, using MIDI input, that works very well (a bit too hot for the Raspberry Pi though!); a similar one driven by Python would be great. However, the more I look at it, I have a lot to cover in less than an hour, so actually demoing Csound may be a step too far at this stage. Richard Dobson |
Date | 2012-11-13 21:39 |
From | peiman khosravi |
Subject | Re: [Csnd] lazy request for simple Python scripting example |
> > The reason why I don't use that approach as much is that it is rather > cumbersome to create a sample accurate clock in python, so I started doing > it the other way around, letting Csound be the master app responsible for > time keeping, hosting Python for compositional logic. > Ah that's a nice pointer. Thanks! p |
Date | 2012-11-15 00:28 |
From | Andres Cabrera |
Subject | Re: [Csnd] lazy request for simple Python scripting example |
Hi, Another interesting way I bumped into today: http://codehop.com/introducing-python-score-for-csound/ Cheers, Andrés On Tue, Nov 13, 2012 at 1:39 PM, peiman khosravi |
Date | 2012-11-15 02:55 |
From | Jacob Joaquin |
Subject | Re: [Csnd] lazy request for simple Python scripting example |
This is near the top of my list of projects to get back to. It's just that I took an exciting detour into the world of Arduino and microcontrollers, and plan on being here for a little while longer. As for Python, I'll just say I've been writing Csound scores since 1995 and Python makes everything far easier and is magnitudes faster to create scores with. The difference is quite insane and in no way am I exaggerating this. Best, Jake On Wed, Nov 14, 2012 at 4:28 PM, Andres Cabrera <mantaraya36@gmail.com> wrote: Hi, codehop.com | #code #art #music |
Date | 2012-11-15 09:15 |
From | Richard Dobson |
Subject | Re: [Csnd] lazy request for simple Python scripting example |
I can report that what I was able to show teachers did appear to be received well, with some encouraging responses and comments (Scratch has all manner of timing issues, but can still be got to do some funky/cool things sound-wise), so the mission to get Sound and Music Computing into schools is hopefully up and running. So the immediate requirement has now passed, and because of the almost inevitable late start and overruns, there was in the end no time to demo anything Csound-related, or any Python for that matter. I can now eliminate the "lazy", and I will look at putting some examples of my own together. Nevertheless, I will always be interested to see/receive any reasonably compact examples of Python-driven Csound, that may for example demonstrate some teaching point, coding technique or topic. If people think of kids age 10 onwards, mostly just starting serious study of programming, perhaps being encouraged to create original sounds for games etc, that will give the general idea of level. So nothing too heavily mathematical or conceptually abstruse! I suppose the ultimate goal is a (smaller!) counterpart to the Audio Programming Book, based on Python. Richard Dobson On 15/11/2012 02:55, Jacob Joaquin wrote: > This is near the top of my list of projects to get back to. It's just > that I took an exciting detour into the world of Arduino and > microcontrollers, and plan on being here for a little while longer. As > for Python, I'll just say I've been writing Csound scores since 1995 and > Python makes everything far easier and is magnitudes faster to create > scores with. The difference is quite insane and in no way am I > exaggerating this. > |
Date | 2012-11-15 09:22 |
From | zappfinger |
Subject | [Csnd] Re: lazy request for simple Python scripting example |
Have you also looked at Pyo? It is a sound engine that you can directly use from Python.... Richard -- View this message in context: http://csound.1045644.n5.nabble.com/lazy-request-for-simple-Python-scripting-example-tp5717857p5717970.html Sent from the Csound - General mailing list archive at Nabble.com. |
Date | 2012-11-15 09:57 |
From | Richard Dobson |
Subject | Re: [Csnd] Re: lazy request for simple Python scripting example |
It's on the list. there are two interconnected objectives - find the most suitable sound engine(s) for teachers and students to use, within Python (which schools already use), and advocacy for Csound. Richard Dobson On 15/11/2012 09:22, zappfinger wrote: > Have you also looked at Pyo? It is a sound engine that you can directly use > from Python.... > > Richard > > > |
Date | 2012-11-15 14:28 |
From | Jacob Joaquin |
Subject | Re: [Csnd] lazy request for simple Python scripting example |
It's possible to have a Python-driven Csound system that hides most all of the details of Csound, and provides only a simplified interface for students to interact with. Here's a very simple example that provides students a single trumpet function with four parameters: trumpet(start_time, duration, amplitude, note) And then all they'd have to do is write a score with this single function: trumpet(0.0, 0.5, -3, 'D5')
trumpet(0.5, 0.5, -3, 'G5') trumpet(1.0, 0.5, -3, 'A5') trumpet(1.5, 0.5, -3, 'B5') etc... All the actual Csound is hidden, and students could just focus on creating note events. And then as course continues, more and more functionality could be provided, slowly building the students computer music repertoire. This includes more instruments, computer programming paradigms such as conditional statements and loops, introducing concepts like random, sound design, etc.
Here's a list of examples I've put together that combines Csound and Python:
The top six audio examples here are from this Python library: Best, Jake
On Thu, Nov 15, 2012 at 1:15 AM, Richard Dobson <richarddobson@blueyonder.co.uk> wrote: I can report that what I was able to show teachers did appear to be received well, with some encouraging responses and comments (Scratch has all manner of timing issues, but can still be got to do some funky/cool things sound-wise), so the mission to get Sound and Music Computing into schools is hopefully up and running. codehop.com | #code #art #music |