CC Messages during a NoteOn Message

I would like to be able to change the parameters of a synth while a note is holding - amount of modulation, etc. In the code below, I am trying something simple by trying to change the amplitude while holding a note. I am using a control bus so that I can change the volume while the note is holding, but it’s not working.

Something is going wrong between the bus and MIDIdef.

Thank you for any help!

MIDIIn.connectAll;
// MIDIClient.init;

(

// ~myControl = 0.5;

// A SynthDef with ADSR envelope
SynthDef(“quick2”, {arg freq = 440, amp = 0.1, gate = 1;
var snd, env;
env = Env.adsr(0.01, 0.1, 0.3, 2, amp).kr(2, gate);
snd = Saw.ar(freq: [freq, freq*1.5], mul: env);
Out.ar(0, snd)
}).add;
)

// Play it with a MIDI keyboard
(
var noteArray = Array.newClear(128); // array has one slot per possible MIDI note

MIDIdef.noteOn(\myKeyDown, {arg vel, note;
noteArray[note] = Synth(“quick2”, [\freq, note.midicps, \amp, vel.linlin(0, 127, 0, 1)]);
[“NOTE ON”, note].postln;
});

MIDIdef.noteOff(\myKeyUp, {arg vel, note;
noteArray[note].set(\gate, 0);
[“NOTE OFF”, note].postln;
});
)

~myControl = Bus.control(s,1);

x = Synth(\quick2)
x.free;
x.map(1, ~myControl.index);
x.map([\amp, ~myControl.index]);

// Synth(\quick2, [\amp, In.kr(~myControl).range(0.1, 0.7)]);

(

MIDIdef.cc(\myAmp, {
arg val, ccNum, channel;

val.postln;

~myControl = val.linlin(0, 127, 0.1, 0.7);

}, 2

);
)

One thing here is that you’re controlling \amp by velocity and by MIDI CC. That isn’t going to work – initially you set amplitude according to velocity, but then map to the control bus, at which point the velocity will be forgotten.

So if you want the two things to control volume, then you need two control inputs in the SynthDef.

~myControl = val.linlin(0, 127, 0.1, 0.7);

Here you need ~myControl.set(val.linlin(0, 127, 0.1, 0.7));. You’re reassigning ~myControl to a number, losing the reference to the bus. ~myControl is the bus object, not the value.

x.map(1, ~myControl.index); should be OK. x.map([\amp, ~myControl.index]); isn’t – remove the array brackets.

Synth(\quick2, [\amp, In.kr(~myControl).range(0.1, 0.7)]); – you can’t put a UGen in a synth argument list. You can do Synth(\quick2, [amp: ~myControl.asMap]) but that won’t handle the range.

hjh