Mididef.noteOn e MIDIdef.noteOff

Hi, I would need to understand how to trigger soundfiles using a midi keyboard (to be precise, they are 7) that keep the sound as long as I hold down the note and then fade out when I release it. I tried to create a miidef.noteon and a miidef.noteoff respectively but it only works if the signal is a synth and I would like to be able to do it using a playbuf instead. Thank you!

Could you post a little of your code because what you have described should work.

Here is an example of what the synth might look like.

SynthDef(\example, {
	var snd = PlayBuf.ar(2, \bufnum.kr);
	var env = Env([0, 1, 1, 0], 0.5, releaseNode: 2).kr(doneAction: 2, gate: \gate.kr(1));
	Out.ar(\bus.kr, snd * env)
});

this is my code

Buffer.freeAll;
(
b = Buffer.readChannel(s,“files”.resolveRelative;
);
)

~paths = ( “files”.resolveRelative ++ “/*.wav”).pathMatch;
~bufs = ~paths.collect({arg i; Buffer.read(s, i)});
~bufs.size;

(
SynthDef.new(\playbuf,{
arg amp=1, out=0, buf, gate=0;
var sig, env;
sig = PlayBuf.ar(2,buf);
env = EnvGen.kr(Env.adsr, gate);
sig = sig * env * amp;
Out.ar(out,sig);
}).add;
)

~notes = Array.newClear(128);

(
MIDIdef.noteOn(\noteOnTest,{
arg vel, nn;
~notes[nn] = Synth.new(\playbuf,
[
\freq,nn.midicps,
\buf,~bufs[i].bufnum,
\amp,vel.linexp(1,127,0.01,0.3),
\gate,1,

	     ]
);

});

MIDIdef.noteOff(\noteOffTest, {
arg vel, nn;
~notes[nn].set(\gate,0);
~notes[nn] = nil;
});
)

Does ~bufs[0].play produce a sound?

Where is i set?

A couple of other points (probably not related).

You are missing the doneAction: 2 to free the synth once the envelope has stopped.

Usually,

the gate should default to 1.

now this is my code

Buffer.freeAll;
(
b = Buffer.readChannel(s,“files”.resolveRelative;
);
)

~paths = ( “files”.resolveRelative ++ “/*.wav”).pathMatch;
~bufs = ~paths.collect({arg i; Buffer.read(s, i)});
~bufs.size;

(
SynthDef.new(\playbuf,{
arg amp=1, out=0, buf, gate=1;
var sig, env;
sig = PlayBuf.ar(2,buf);
env = EnvGen.kr(Env.adsr, gate, doneAction:2);
sig = sig * env * amp;
Out.ar(out,sig);
}).add;
)

~notes = Array.newClear(128);

(
MIDIdef.noteOn(\noteOnTest,{
arg vel, nn;
~notes[nn] = Synth.new(\playbuf,
[
\freq,nn.midicps,
\buf,~bufs[i].bufnum,
\amp,vel.linexp(1,127,0.01,0.3),
\gate,1,

	     ]
);

});

MIDIdef.noteOff(\noteOffTest, {
arg vel, nn;
~notes[nn].set(\gate,0);
~notes[nn] = nil;
});
)

Welcome to the forum, Leoo.

If you have a minute, would you mind editing your post to enclose the code in backticks? Like this:

```
your code here
```

The forum’s text processing mangles code, e.g.

~paths = ( “files”.resolveRelative ++ “/*.wav”).pathMatch;
--> ERROR: syntax error, unexpected end of file

~paths = ( “files”.resolveRelative +/+ “*.wav”).pathMatch;
--> ERROR: syntax error, unexpected '*' (because the quotes are wrong)

Thanks.
hjh