On Monday 09 January 2006 20:57, Oeyvind Brandtsegg wrote:
> It's hard for me to find my way through the csoundAPI documentation,
> what is the command for accessing the software bus from a host application ?
> e.g. in the example from the manual that defines
> chn_k "cutoff", 1, 3, 1000, 500, 2000
> ... What command would I use to write to this channel from my host app ?
>
> I use
> csound = csnd.CppSound()
> and then
> csound.InputMessage("i 1 0 1 1")
> to send events.
For simple access to the bus (limited to setting and getting control
channels, and setting string channels), you can use Csound::SetChannel()
and Csound::GetChannel(). The first example shows using this interface.
A more advanced example is also included, in which the channels
exported in the orchestra header are listed after calling
Csound::Compile(), and pointers to the channel data are used
later during performance (you may want to read the cs_glue.hpp
header included with the RC2 release for documentation on the
various classes used in this example).
------------------------------------------------------------------------
#!/usr/bin/python
import csnd
csound = csnd.Csound()
f = open('chn_simple_1.csd', 'w')
print >> f, '''
-d -m135 -H0 -s -h -o dac
sr = 44100
ksmps = 100
nchnls = 2
gkpwd chnexport "PulseWidth", 1
gkpan chnexport "Pan", 1
instr 1
a1 vco2 p5, p4, 2, gkpwd
apanL interp cos(gkpan * 1.57), 0, 1
apanR interp sin(gkpan * 1.57), 0, 1
outs a1 * apanL, a1 * apanR
endin
i 1 0 5 220 16000
e
'''
f.close()
if csound.Compile('chn_simple_1.csd') == 0:
csound.SetChannel('PulseWidth', 0.95)
csound.SetChannel('Pan', 1.0)
while csound.PerformKsmps() == 0:
curTime = (csound.GetScoreTime() / 5.0) % 1.0
csound.SetChannel('Pan', 1.0 - curTime)
csound.SetChannel('PulseWidth', 0.05 + abs(curTime * 1.8 - 0.9))
csound.Reset()
------------------------------------------------------------------------
#!/usr/bin/python
import csnd
import wave
import array
import math
csound = csnd.Csound()
# create and compile a CSD file
f = open('chntest.csd', 'w')
print >> f, '''
sr = 48000
ksmps = 32
nchnls = 1
gkfco init 1000
gkfco chnexport "FilterFreq", 1, 3, 1000, 200, 4000
gSfname chnexport "FileName", 1
gaout chnexport "AudioOutput", 2
instr 1
printf_i "The file name is: '%s'\\n", 1, gSfname
a1 vco2 20 / sqrt(gkfco), 220
a2 pareq a1, gkfco, 0, gkfco / 220, 2
gaout = taninv(a2) * 0.6
endin
i 1 0 5
e
'''
f.close()
err = csound.Compile('-d', '-n', 'chntest.csd')
if (err != 0):
raise(SystemExit(err))
# create and print a channel list
controlInput = ''
audioOutput = ''
stringInput = ''
chnlst = csnd.CsoundChannelList(csound)
n = chnlst.Count()
if n > 0:
print '============================================================'
print n, ' channels'
print '------------------------------------------------------------'
for i in range(n):
print 'Name: ', chnlst.Name(i)
tmp = 'unknown'
if chnlst.IsControlChannel(i):
tmp = 'control'
elif chnlst.IsAudioChannel(i):
tmp = 'audio'
elif chnlst.IsStringChannel(i):
tmp = 'string'
print 'Type: ', tmp
print 'Input: ', ['no', 'yes'][chnlst.IsInputChannel(i)]
print 'Output: ', ['no', 'yes'][chnlst.IsOutputChannel(i)]
if chnlst.SubType(i) > 0:
tmp = ['integer', 'linear', 'exponential']
print 'Subtype: ', tmp[chnlst.SubType(i) - 1]
print 'Default value: ', chnlst.DefaultValue(i)
print 'Minimum value: ', chnlst.MinValue(i)
print 'Maximum value: ', chnlst.MaxValue(i)
print '------------------------------------------------------------'
if chnlst.IsControlChannel(i) and chnlst.IsInputChannel(i):
if chnlst.SubType(i) == csnd.CSOUND_CONTROL_CHANNEL_EXP:
controlInput = chnlst.Name(i)
if chnlst.IsAudioChannel(i) and chnlst.IsOutputChannel(i):
audioOutput = chnlst.Name(i)
if chnlst.IsStringChannel(i) and chnlst.IsInputChannel(i):
stringInput = chnlst.Name(i)
# free memory used by the channel list; call this before csound.Reset()
chnlst.Clear()
if controlInput == '' or audioOutput == '':
csound.Reset()
raise(SystemExit(0))
# if found an exponential control input channel and an audio output channel,
# perform the orchestra
# get pointer to the control input channel
ptr1 = csnd.CsoundMYFLTArray()
csound.GetChannelPtr(ptr1.GetPtr(), controlInput,
csnd.CSOUND_CONTROL_CHANNEL + csnd.CSOUND_INPUT_CHANNEL)
# get pointer to the audio output channel
ptr2 = csnd.CsoundMYFLTArray()
csound.GetChannelPtr(ptr2.GetPtr(), audioOutput,
csnd.CSOUND_AUDIO_CHANNEL + csnd.CSOUND_OUTPUT_CHANNEL)
# if there is a string input channel, write the file name to it
if stringInput != '':
ptr3 = csnd.CsoundMYFLTArray()
csound.GetChannelPtr(ptr3.GetPtr(), stringInput,
csnd.CSOUND_STRING_CHANNEL + csnd.CSOUND_INPUT_CHANNEL)
ptr3.SetStringValue('chntest.wav', csound.GetStrVarMaxLen())
ptr3 = None
# get the range of the control channel
tmp, dflt, minVal, maxVal = csound.GetControlChannelParams(controlInput)
ksmps = csound.GetKsmps()
# open a 16 bit mono WAV file for storing the audio output
f = wave.open('chntest.wav', 'wb')
f.setframerate(int(csound.GetSr()))
f.setnchannels(1)
f.setsampwidth(2)
buf = array.array('B')
# writes a floating point sample (-1.0 to 1.0) to the buffer
def writeSample(x):
nn = int(x * 32767.0)
if nn < 0:
if nn > -32768:
nn = 65536 + nn
else:
nn = 32768
elif nn > 32767:
nn = 32767
buf.append(nn & 255)
buf.append(nn >> 8)
x = maxVal
c1 = math.pow(0.5, (1.0 / csound.GetKr()))
while 1:
# set control input
ptr1.SetValue(0, x)
x = minVal + ((x - minVal) * c1)
# perform one control period
if csound.PerformKsmps() != 0:
break
# get audio output
for i in range(ksmps):
writeSample(ptr2.GetValue(i))
# write sound file
f.writeframes(buf)
f.close()
# clean up
csound.Reset()
------------------------------------------------------------------------