Csound Csound-dev Csound-tekno Search About

Re: [Cs-dev] A student attempting to use the API (Francois Pinot)

Date2006-07-18 18:15
FromFrancois Pinot
SubjectRe: [Cs-dev] A student attempting to use the API (Francois Pinot)
Hello,

I wrote an example to read audio data from the software bus from the 
Java wrapper to the CsounD API. Maybe this can help.
Cheers

Francois Pinot


TestCsoundAudioBuffer.java :

/**
 * TestCsoundAudioBuffer.java
 *
 * An example showing how to read audio channels data
 * from the Csound bus
 *
 * Assuming that this file, the csnd.jar file and
 * the testAudioBuffer.csd file are in the
 * same directory, one can compile this example:
 *   javac -cp csnd.jar;. TestCsoundAudioBuffer.java
 *
 * and run it:
 *   java -cp csnd.jar;. TestCsoundAudioBuffer
 */

import csnd.*;

/**
 * @author Francois Pinot
 *
 */
public class TestCsoundAudioBuffer implements csndConstants {

  public static void main(String[] args) {
    // Get a Csound object. This object encapsulates an instance of Csound
    Csound csound = new Csound();
    // Compile our example
    csound.Compile("testAudioBuffer.csd");
    // Get ksmps (audio buffer length on each control cycle)
    int ksmps = csound.GetKsmps();
    // Get a list of allocated channels of the bus
    CsoundChannelList chnList = new CsoundChannelList(csound);
    // Print some trace info
    System.out.println("Allocated channels: " + chnList.Count());
   
    if (chnList.Count() > 0) {
      // Print some more trace info
      System.out.print("1st channel name: " + chnList.Name(0));
      System.out.println(" - is audio channel? " + 
chnList.IsAudioChannel(0));
      System.out.print("2nd channel name: " + chnList.Name(1));
      System.out.println(" - is audio channel? " + 
chnList.IsAudioChannel(1));
     
      // Create two CsoundMYFLTArray objects. Those objects don't actually
      // point to any array because the constructor has no param.
      // We shall make their pointer field point to the arrays
      // created by Csound later.
      CsoundMYFLTArray data1 = new CsoundMYFLTArray();
      CsoundMYFLTArray data2 = new CsoundMYFLTArray();
      // Get two references to uninitialized pointers of type MYFLT**
      // from our two CsoundMYFLTArray objects.
      // Notice that MYFLT is supposed to be of type double.
      SWIGTYPE_p_p_double pdata1 = data1.GetPtr();
      SWIGTYPE_p_p_double pdata2 = data2.GetPtr();
      // The type of channel we're attempting to get from the bus
      int flags = CSOUND_AUDIO_CHANNEL | CSOUND_OUTPUT_CHANNEL;
      // Those calls will write the correct pointer values in  our two 
references
      csound.GetChannelPtr(pdata1, "ach1", flags);
      csound.GetChannelPtr(pdata2, "ach2", flags);
     
      // Now, we can use the GetValue method of the CsoundMYFLTArray
      // objects to read the channels values.
      // On each cycle value, we can read exactly ksmps audio values
      // from each audio channel
      while (csound.PerformKsmps() == 0) {
        for (int i = 0; i < ksmps; i++) {
          double x1 = data1.GetValue(i);
          double x2 = data2.GetValue(i);
          System.out.printf("%4d: %9f\t%9f\t%9f\n", i, x1, x2, x1+x2);
        }
      }
      System.out.println("=========================================");
    }
    csound.Reset();
  }
}


testAudioBuffer.csd :


  ; testAudioBuffer.csd
  ; author Francois Pinot
  ;

  -Wdo dac



  sr      =        44100
  kr      =        441
  ksmps   =        100
  nchnls  =        2

          chn_a    "ach1", 2      ; declare an audio channel named "ach1"
                                  ; for output on the software bus
          chn_a    "ach2", 2      ; declare an audio channel named "ach2"
                                  ; for output on the software bus
          instr 1
  idur    =        p3

  a1      oscil    1, 441, 1
  a2      oscil    1, 441, 2
          chnset   a1, "ach1"     ; writes data from a1 to "ach1" channel
          chnset   a2, "ach2"     ; writes data from a1 to "ach1" channel
          outs     a1*10000, a2*10000
          endin



  f1 0 16384 9 1 1 0     ; one period of a sine wave
  f2 0 16384 9 1 1 180   ; one period of an opposite phase sine wave

  i1 0 0.005             ; play two periods of the sine waves
  e




-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Csound-devel mailing list
Csound-devel@lists.sourceforge.net