Today I cleaned up my one-button instrument…

Admittedly, I added one more button to switch between modes.

It can be played by any HID device with two buttons.

Just change the HID.open() statement to your device.

`
(
HID.findAvailable;
c = HID.open( 6447, 2326 ); // my external mouse, use any other if you wish
// c.debug_(true);
// c.debug_(false);
// c.postElements;
// HIDFunc.trace( false );
HIDdef.usage( \onebut, { |v| v.postln; }, \b1 );
);</p>

s.boot;
// synth to play back the waveform:
(
s.waitForBoot({

(
// synth to write the waveform to a buffer:
SynthDef( \wavewriteb2, { |input=0,buffer=0,dur=5,speed=1,offset=0.5,mul=1,gate=0|
EnvGen.kr( Env.sine, gate, timeScale: dur ) * // why an envelope?
BufWr.kr(
In.kr( input, 1 ) * mul - offset, buffer, Phasor.kr( gate, speed, 0, BufFrames.kr(buffer)), 0 );
}).send(s);

// synth to play back the waveform:
SynthDef( \waveplayb, { |buffer=0, speed=0.5, out=0, amp=1, vol = 0.5|
Out.ar( out,
vol * amp * BufRd.ar( 1, buffer, LFSaw.ar(BufDur.kr(buffer).reciprocal * speed * 2).range(0, BufFrames.kr(buffer)) )
);
}).send(s);

SynthDef( \waveplaybEnv, { |buffer=0, speed=0.5, out=0, amp=1, vol = 0.5|
Out.ar( out,
vol * amp * BufRd.ar( 1, buffer, LFSaw.ar(BufDur.kr(buffer).reciprocal * speed * 2).range(0, BufFrames.kr(buffer)) ) * EnvGen.kr( Env.sine, timeScale: \dur.kr(0.5 ), doneAction: 2 );
);
}).send(s);
);

s.sync;

// s.sampleRate /
// s.options.blockSize / s.sampleRate

b = Buffer.alloc( s, 256, 1 );
a = Bus.control;

s.sync;

x = Synth.new( \wavewriteb2, [ \buffer, b, \input, a.index ] );
y = Synth.new( \waveplayb, [ \buffer, b ] );
z = Synth.new( \waveplayb, [ \buffer, b ] );

(
Tdef( \fourBit ).set( \delta, 0.025, \bitVal, 0, \bitSum, 0 );
Tdef( \fourBit, { |ev|
loop{
4.do{ |i|
ev[ \bitSum ] = ev[ \bitSum ] + ( 2**i * ev[ \bitVal ] );
ev[ \delta ].wait;
};
a.set( ev[\bitSum] / 16 );
Synth.new( \waveplaybEnv, [ \buffer, b, \speed, 2 * ( ev[ \bitSum ] + 1 ) / 16, \dur, 1, \amp, 0.4 ] );
ev[ \bitSum ] = 0;
}
});
);

(
Tdef( \sevenBit ).set( \delta, 0.15, \bitVal, 0, \bitSum, 0 );
Tdef( \sevenBit, { |ev|
loop{
7.do{ |i|
ev[ \bitSum ] = ev[ \bitSum ] + ( 2**i * ev[ \bitVal ] );
ev[ \delta ].wait;
};
a.set( ev[\bitSum] / (2**7) );
Synth.new( \waveplaybEnv, [ \buffer, b, \speed, 2 *( ev[ \bitSum ] + 1 ) / (2**7), \dur, 7*( 1 - ((ev[ \bitSum ]) / (2**7)) ), \amp, ((( ev[ \bitSum ] + 1 ) / (2**7)).pow(1/3)), \bitsum, ev[\bitSum] ].postln );
ev[ \bitSum ] = 0;
}
});
);

(
~switchMode = { |v|
Tdef( \fourBit ).stop;
Tdef( \sevenBit ).stop;

switch( v,
0, {
HIDdef.usage( \onebut, { |v| a.set( v.postln; ) }, \b1 );
},
1, {
Tdef( \fourBit ).play;
HIDdef.usage( \onebut, { |v| Tdef( \fourBit ).set( \bitVal, v.postln; ); }, \b1 );
},
2, {
HIDdef.usage( \onebut, { |v| Tdef( \sevenBit ).set( \bitVal, v.postln; ); }, \b1 );
Tdef( \sevenBit ).play;
}
);
};
);
s.scope;
~currentMode = 0;

HIDdef.usage( \but2, { |v| ~currentMode = (~currentMode + 1)%3; ~switchMode.value( ~currentMode.value ); }, \b3 );

x.set( \speed, 0.01 );

});

TdefAllGui.new;
);

/*
//playing fun:

x.set( \speed, 0.01 );
x.set( \speed, 0.05 );
x.set( \mul, 1, \offset, 0.5 );
x.set( \mul, 1, \offset, 0 );

y.set( \speed, 1/4 );
z.set( \speed, 2 );

*/
`