Csound Csound-dev Csound-tekno Search About

Re: [Cs-dev] Java Wrapper Now Working

Date2005-12-11 22:47
FromVictor Lazzarini
SubjectRe: [Cs-dev] Java Wrapper Now Working
Thanks a lot. The CsoundArgVList class is new as well isn't
it? I hadn't
seen it before. I presume in Python the idea will be more or
less the
same (deriving a class and overriding its methods).

Victor

>
> On Sunday 11 December 2005 20:12, Victor Lazzarini wrote:
>
> > Could you post an example?
>
> Here is a simple Java example that works with the current
> CVS sources (note that this is not implemented for other
> languages yet, and may be changed or removed later). The
> C++ class it uses is also included below.
>
> // CallbackExample.java
>
> import csnd.*;
>
> public class CallbackExample {
>
>   // JavaCallbacks is a subclass of CsoundCallbackWrapper
> that
>   // can override the methods MessageCallback(),
> InputValueCallback(),
>   // OutputValueCallback(), and YieldCallback() for
> defining callbacks
>
>   static class JavaCallbacks extends CsoundCallbackWrapper
> {
>     JavaCallbacks(Csound cs)
>     {
>       super(cs);
>     }
>     public int YieldCallback()
>     {
>       java.lang.System.err.println("Yield called for " +
> GetCsound() + " !");
>       return 1;
>     }
>     public void MessageCallback(int attr, String msg)
>     {
>       if (msg.endsWith("\n"))
>         java.lang.System.err.print("ATTR: " + attr + "
> MSG: " + msg);
>       else
>         java.lang.System.err.println("ATTR: " + attr + "
> MSG: " + msg);
>     }
>   }
>
>   public static void main(String argv[])
>   {
>     Csound cs = new Csound();
>     // create a callback wrapper object for this instance
> of Csound
>     // note that this will use the host data pointer of
> 'cs', so the
>     // host data cannot be used for other purposes
>     JavaCallbacks cb = new JavaCallbacks(cs);
>     // enable the callbacks for which there are
> definitions above
>     cb.SetYieldCallback();
>     cb.SetMessageCallback();
>     // build an argument list from the command line
> options
>     CsoundArgVList args = new CsoundArgVList();
>     args.Append("csound");
>     for (int i = 0; i < argv.length; i++)
>       args.Append(argv[i]);
>     // call Compile() to see if the callbacks work
>     cs.Compile(args.argc(), args.argv());
>     // Reset() must be called before 'cs' or 'cb' is
> possibly
>     // garbage collected, otherwise a crash may occur
>     cs.Reset();
>     // free memory used by the argument list; this is
> optional, and
>     // would be done automatically when 'args' is garbage
> collected
>     args.Clear();
>   }
> };
>
> // end of CallbackExample.java
>
> ----------------------------------------------------------
> -----------
>
> // callback wrapper class declared in
> interfaces/cs_glue.hpp
>
> class CsoundCallbackWrapper {
>  private:
>     CSOUND  *csound_;
>  public:
>     virtual void MessageCallback(int attr, char *msg)
>     {
>     }
>     virtual MYFLT InputValueCallback(const char *chnName)
>     {
>       return (MYFLT) 0;
>     }
>     virtual void OutputValueCallback(const char *chnName,
> MYFLT value)
>     {
>     }
>     virtual int YieldCallback()
>     {
>       return 1;
>     }
>     void SetMessageCallback();
>     void SetInputValueCallback();
>     void SetOutputValueCallback();
>     void SetYieldCallback();
>     CSOUND *GetCsound()
>     {
>       return csound_;
>     }
>     // --------
>     CsoundCallbackWrapper(Csound *csound);
>     CsoundCallbackWrapper(CSOUND *csound);
>     virtual ~CsoundCallbackWrapper()
>     {
>     }
> };
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc. Do you grep
> through log files for problems?  Stop!  Download the new
> AJAX search engine that makes searching your log files as
> easy as surfing the  web.  DOWNLOAD SPLUNK!
> http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
> _______________________________________________
> Csound-devel mailing list
> Csound-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/csound-devel


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Csound-devel mailing list
Csound-devel@lists.sourceforge.net

Date2005-12-11 23:09
FromIstvan Varga
SubjectRe: [Cs-dev] Java Wrapper Now Working
AttachmentsNone  

Date2005-12-11 23:40
FromIstvan Varga
SubjectRe: [Cs-dev] Java Wrapper Now Working
AttachmentsNone  

Date2005-12-12 03:25
FromSteven Yi
SubjectRe: [Cs-dev] Java Wrapper Now Working
AttachmentsNone  

Date2005-12-12 17:29
FromIstvan Varga
SubjectRe: [Cs-dev] Java Wrapper Now Working
AttachmentsNone  

Date2005-12-13 08:46
FromIstvan Varga
SubjectRe: [Cs-dev] Java Wrapper Now Working
AttachmentsNone  

Date2005-12-13 11:09
FromIstvan Varga
SubjectRe: [Cs-dev] Java Wrapper Now Working
AttachmentsNone  

Date2005-12-13 12:08
FromVictor Lazzarini
SubjectRe: [Cs-dev] Java Wrapper Now Working
This is very good; making it possible to access audio channels
as well as control and string channels is a great thing. Great
job, well done.

Victor

At 11:09 13/12/2005, you wrote:
>On Sunday 11 December 2005 23:47, Victor Lazzarini wrote:
>
> > Thanks a lot. The CsoundArgVList class is new as well isn't
> > it? I hadn't seen it before.
>
>Here are a few more examples for these new classes:
>     CsoundChannelList
>     CsoundOpcodeList
>     CsoundUtilityList
>     CsoundMYFLTArray
>     CsoundArgVList
>
>------------------------ chn_list.py ------------------------
>
>#!/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()
>
>------------------------ opcodlst.py ------------------------
>
>#!/usr/bin/python
>
>import csnd
>
>csound = csnd.Csound()
>
># compile something so that we can create an opcode list
>
>csound.Compile('-d', '-m0', '-H0', '-n', 'examples/trapped.csd')
>
># write the opcode list to a file 'opcodelist.txt'
>
>oplst = csnd.CsoundOpcodeList(csound)
>f = open('opcodelist.txt', 'w')
>n = oplst.Count()
>print >> f, n, ' opcodes'
>for i in range(n):
>     print >> f, 
> '-------------------------------------------------------------'
>     print >> f, 'Name:           ', oplst.Name(i)
>     print >> f, 'Output types:   ', oplst.OutTypes(i)
>     print >> f, 'Input types:    ', oplst.InTypes(i)
>f.close()
>
># free memory used by the opcode list; call this before csound.Reset()
>
>oplst.Clear()
>
>csound.Reset()
>
>------------------------ util_lst.py ------------------------
>
>#!/usr/bin/python
>
>import sys
>import csnd
>
>csound = csnd.Csound()
>
># load utility plugins
>
>csound.PreCompile()
>
># create and print the utility list
>
>ulst = csnd.CsoundUtilityList(csound)
>n = ulst.Count()
>print n, ' utilities:'
>for i in range(n):
>     print '  ', ulst.Name(i)
>     print '    ', csound.GetUtilityDescription(ulst.Name(i))
>
># free memory used by the utility list; call this before csound.Reset()
>
>ulst.Clear()
>
># if there are command line arguments, run the sndinfo utility
>
>n = sys.argv.__len__()
>if n > 1:
>     args = csnd.CsoundArgVList()
>     args.Append('sndinfo')
>     print
>     for i in range(1, n):
>         args.Append(sys.argv[i])
>     err = csound.RunUtility('sndinfo', args.argc(), args.argv())
>     print 'sndinfo returns: ', err
>     args.Clear()
>
># clean up
>
>csound.Reset()
>
>
>-------------------------------------------------------
>This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
>for problems?  Stop!  Download the new AJAX search engine that makes
>searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
>http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
>_______________________________________________
>Csound-devel mailing list
>Csound-devel@lists.sourceforge.net
>https://lists.sourceforge.net/lists/listinfo/csound-devel

Victor Lazzarini
Music Technology Laboratory
Music Department
National University of Ireland, Maynooth 



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Csound-devel mailing list
Csound-devel@lists.sourceforge.net