Sc3/processing communication problems

bearing in mind the following piece of code

// Define the SynthDef object first
(
SynthDef(\example, {
    |freq = 440, amp = 0.5, numVoices = 5|
    var sig = Mix.fill(numVoices, { |i|
        var freqOffset = Rand(-10, 10).exprange;
        var ampOffset = Rand(-0.2, 0.2).exprange;
        var gate = Impulse.ar(freq * (1 + freqOffset), 0, numVoices - i);
        var env = EnvGen.kr(Env.perc(0.01, 0.1), gate, doneAction: Done.freeSelf);
        var snd = SinOsc.ar(freq * (1 + freqOffset), 0, amp * (1 + ampOffset)) * env;
        snd;
    });
    Out.ar(0, sig);
}).add;
)

// Define the OSC function that uses the SynthDef object
(
OSCFunc({ |msg|
    SynthDef(\example).clear; // clear all existing instances of the synth
    Synth.new(\example, [\freq, msg[1].asFloat, \amp, msg[2].asFloat, \numVoices, msg[3].asInteger]);
}, '/sound');
)

and this

// Processing code
import oscP5.*;
import netP5.*;

OscP5 oscP5;
NetAddress scAddr;
float freq = 440;
float amp = 0.5;

void setup() {
  size(800, 600);
  smooth();
  oscP5 = new OscP5(this, 12000);
  scAddr = new NetAddress("127.0.0.1", 57120);
}

void draw() {
  background(0);
  noStroke();
  fill(255);
  ellipse(width/2, height/2, map(freq, 0, 1000, 0, width), map(amp, 0, 1, 0, height));
}

void mouseMoved() {
  freq = map(mouseX, 0, width, 0, 1000);
  amp = map(mouseY, 0, height, 1, 0);
  OscMessage msg = new OscMessage("/sound");
  msg.add(freq);
  msg.add(amp);
  oscP5.send(msg, scAddr);
}

what am i theoretically doing wrong?

i am getting the following error while evaluating the supercollider code:

ERROR: Primitive '_BasicNew' failed.
Index not an Integer
RECEIVER:
class Array (0000021B98D32D40) {
  instance variables [19]
    name : Symbol 'Array'
    nextclass : instance of Meta_Array2D (0000021BA141E840, size=19, set=5)
    superclass : Symbol 'ArrayedCollection'
    subclasses : nil
    methods : instance of Array (0000021BA14BB400, size=50, set=6)
    instVarNames : nil
    classVarNames : nil
    iprototype : nil
    cprototype : nil
    constNames : nil
    constValues : nil
    instanceFormat : Integer 1
    instanceFlags : Integer 0
    classIndex : Integer 1128
    classFlags : Integer 3
    maxSubclassIndex : Integer 1128
    filenameSymbol : Symbol 'C:\Program Files\SuperCollider-3.13.0\SCClassLibrary\Common\Collections\Array.sc'
    charPos : Integer 0
    classVarIndex : Integer 5
}
PATH: D:/_CS/TMM_SUPERCOLLIDER/2/1.scd

PROTECTED CALL STACK:
	Meta_MethodError:new	0000021BA01A2180
		arg this = PrimitiveFailedError
		arg what = Index not an Integer
		arg receiver = Array
	Meta_PrimitiveFailedError:new	0000021BA01A9F40
		arg this = PrimitiveFailedError
		arg receiver = Array
	Object:primitiveFailed	0000021B9FD7C300
		arg this = Array
	Meta_Collection:fill	0000021BA14248C0
		arg this = Array
		arg size = an OutputProxy
		arg function = a Function
		var obj = nil
	Meta_Mix:fill	0000021B9FF66480
		arg this = Mix
		arg n = an OutputProxy
		arg function = a Function
		var array = nil
	a FunctionDef	0000021BA3ADDE68
		sourceCode = "{
    |freq = 440, amp = 0.5, numVoices = 5|
    var sig = Mix.fill(numVoices, { |i|
        var freqOffset = Rand(-10, 10).exprange;
        var ampOffset = Rand(-0.2, 0.2).exprange;
        var gate = Impulse.ar(freq * (1 + freqOffset), 0, numVoices - i);
        var env = EnvGen.kr(Env.perc(0.01, 0.1), gate, doneAction: Done.freeSelf);
        var snd = SinOsc.ar(freq * (1 + freqOffset), 0, amp * (1 + ampOffset)) * env;
        snd;
    });
    Out.ar(0, sig);
}"
		arg freq = an OutputProxy
		arg amp = an OutputProxy
		arg numVoices = an OutputProxy
		var sig = nil
	SynthDef:buildUgenGraph	0000021BA1919F00
		arg this = a SynthDef
		arg func = a Function
		arg rates = nil
		arg prependArgs = [  ]
		var result = nil
		var saveControlNames = nil
	a FunctionDef	0000021BA18D5B40
		sourceCode = "<an open Function>"
	Function:prTry	0000021BA0420D00
		arg this = a Function
		var result = nil
		var thread = a Thread
		var next = nil
		var wasInProtectedFunc = false
	
CALL STACK:
	MethodError:reportError
		arg this = <instance of PrimitiveFailedError>
	Nil:handleError
		arg this = nil
		arg error = <instance of PrimitiveFailedError>
	Thread:handleError
		arg this = <instance of Thread>
		arg error = <instance of PrimitiveFailedError>
	Object:throw
		arg this = <instance of PrimitiveFailedError>
	Function:protect
		arg this = <instance of Function>
		arg handler = <instance of Function>
		var result = <instance of PrimitiveFailedError>
	SynthDef:build
		arg this = <instance of SynthDef>
		arg ugenGraphFunc = <instance of Function>
		arg rates = nil
		arg prependArgs = nil
	< closed FunctionDef >  (no arguments or variables)
	Interpreter:interpretPrintCmdLine
		arg this = <instance of Interpreter>
		var res = nil
		var func = <instance of Function>
		var code = "(
SynthDef(\example, {
    |..."
		var doc = nil
		var ideClass = <instance of Meta_ScIDE>
	Process:interpretPrintCmdLine
		arg this = <instance of Main>
^^ The preceding error dump is for ERROR: Primitive '_BasicNew' failed.
Index not an Integer
RECEIVER: Array

vielen danke!!!
T.

A note:

This may not do what you have written in the comment, unless you have some extension that defines aSynthDef.clear.

[\freq, msg[1].asFloat, This asFloat is a bit strange – are you sure oscP5 is not sending a number? I think it should. Btw. Synth doesn’t distinguish between float and int, everything is interpreted as a float in the end.

About your error:

numVoices is not a number, but an argument of the SynthDef, which is passed into the function as a Control, and therefore a continuous signal. Mix.fill(n, { ... }) needs a number once, you can’t dynamically change the size of your voices like this, in a single SynthDef.

One solution is to write the SynthDef for one voice, and spawn numVoices Synths in your OSCFunc, e.g. by

numVoices.do { Synth(\example, [\freq, msg[1]]);  }

ok. can someone provide me an example of the full code completely fixed? and not just tips on how to fix it?

vielen danke
love under wilt
T.

Sorry @tmm881! This usually isn’t a space where you’ll find too many people up for doing full rewrites of other peoples non-working code - I think most of us have enough trouble getting our own code running and working :slight_smile: - so you might be stuck with tips.

5 Likes

This is how i decode the error message. The receiver is an Array type, but has received a basicNew instruction. Maybe some assumption of the return type is wrong somewhere.

It is this bit:

You can reproduce this with:

{ |n| Array.fill(n, { 1 }) }.asSynthDef
{ |n| Array.new(n) }.asSynthDef