msp vector question


    Jun 25 2015 | 6:11 pm
    Dear all,
    I'm very new Java so please forgive my ignorance. I think I've pretty much figured out how max externals are written in java and have written a couple of test externals that work. I've also got the basics of writing msp externals but I cannot see any examples of what I'm trying to do. I'm not even sure if it is possible.
    In short, I'm using the java wrapper of csound api to make a java-based msp external that can run csound. I know there is csound~ but it's kind of broken and also I'd really like to learn to do it myself.
    So far I've figured out the hard bit, which is the methods of the csound api. I even have a working example that runs inside a java object in max.
    The problem is that I cannot figure out how to pass csound output (which is being received as an array of N floating point samples) to msp. I'm running csound in a different thread and writing its output to an array in a loop. Then I'm using the perform() method of msp to access this array iteratively. But what I'm getting out of the object is a weird somewhat periodic glitchy sound that doesn't resemble what's going in. How does one deal with a situation like this?
    Here's my code. I would really really appreciate any pointers.
    Many Thanks Peiman
    import com.cycling74.max.*; import com.cycling74.msp.*; import csnd6.*;
    public class csound extends MSPPerformer {
    Csound c = new Csound(); String filename = null; double[] csoundOutput; int vec_size; double sampleValue; int ksmps;
    public csound(Atom[] csd) { filename = csd[0].toString(); declareInlets(new int[]{SIGNAL}); declareOutlets(new int[]{SIGNAL}); }
    public void dspsetup(MSPSignal[] in, MSPSignal[] out) { post("dspsetup was called."); float[] output = out[0].vec; vec_size = output.length; csoundOutput = new double[vec_size]; post(Integer.toString(vec_size)); }
    public void compile() { Thread t = new Thread(){ public void run() {
    csnd6.csoundInitialize( csnd6.CSOUNDINIT_NO_ATEXIT | csnd6.CSOUNDINIT_NO_SIGNAL_HANDLER);
    c.SetHostImplementedAudioIO(1, 512); c.SetOption("-odac"); c.SetOption("-b 512" ); int result = c.Compile(filename); int ksmps = c.GetKsmps();
    post("csound compiled"); post("ksmps is " + Integer.toString(ksmps));
    CsoundMYFLTArray myBuffer = new CsoundMYFLTArray(); myBuffer.SetPtr(c.GetSpout());
    int i; int j;
    j = 0; while (c.PerformKsmps() == 0) { for(i = 0; i < vec_size; i++) { sampleValue = myBuffer.GetValue(j); csoundOutput[i] = sampleValue; j++; if (j >= ksmps) { j = 0; break; // exiting the for loop to stay synchronized } } }
    c.Cleanup(); c.Reset(); } };
    t.start(); }
    public void stop() { c.Stop(); }
    public void perform(MSPSignal[] in, MSPSignal[] out) { float[] o = out[0].vec; int i;
    for (i=0; i < vec_size; i++) { o[i] = (float) csoundOutput[i]; } } }