| Author |
Message |
twist3d

Joined: Apr 27, 2010 Posts: 6 Location: uk
|
Posted: Wed Apr 28, 2010 4:16 am Post subject:
key assignment to notes |
 |
|
hi all,
i've just started using chuck, and obviously a complete noob. would it be possible to assign different keys on a keyboard so each key plays a different note, and each row plays a different oscillator?
sorry if my explanation is hard to follow, but this is what i want to do:
osc1 = w, e, r, t, y, u, i, o
osct2 = s, d, f, g, h, j, k, l
osc3 = ~, z, x, c, v, b, n, m
can i set up an array so that the notes are known to chuck, so osc1 plays notes at that frequency selected, then if i press a key for osc2, it plays that note?
many thanks in advance, and once again sorry for the lame explanation! |
|
|
Back to top
|
|
 |
twist3d

Joined: Apr 27, 2010 Posts: 6 Location: uk
|
Posted: Wed Apr 28, 2010 7:31 am Post subject:
|
 |
|
below is the code i've written so far. I've used the examples for mouse and keyboard, but i've hit a bit of a wall.
essentially, i think i need to link each key press of the keyboard separately, so that when a key is pressed (ex. 'w' pressed causes SinOsc to generate 261.63Hz).
am i on the right track with this?
again, thanks for any help offered!
| Code: |
//take mouse and keyboard messages
Hid mouse;
HidMsg message;
Hid keyboard;
HidMsg kbmessage;
0 => int kbdevice;
0 => int mdevice;
//show the devices are ready for input
if (me.args() ) me.arg(0) => Std.atoi => kbdevice;
if ( !keyboard.openKeyboard (kbdevice) ) me.exit();
<<< "keyboard" + keyboard.name() + "ready" >>>;
if (me.args() ) me.arg(0) => Std.atoi => mdevice;
if( !mouse.openMouse( mdevice ) ) me.exit();
<<< "mouse '" + mouse.name() + "' ready", "" >>>;
//note numbers for an octave
// [261.63,293.66,329.63,349.23,392.00,440.00,493.88,523.25] @=> float notes1[];
// c4 d4 e4 f4 g4 a5 b5 c5
//assign kb keys to notes
//[ 87, 69, 82, 84, 89, 85, 73, 79 ] @=> int kbmessage.ascii;
//specify time
1::second => dur T;
T - (now % T) => now;
//the oscillators defined with their gains
SinOsc s1 => BPF bpf1 => dac;
0.5 => s1.gain;
TriOsc t1 => BPF bpf2 => dac;
0.5 => t1.gain;
PulseOsc p1 => BPF bpf3 => dac;
0.5 => p1.gain;
//define the center frequency for each filter
500. => float middle_freq1;
500. => float middle_freq2;
500. => float middle_freq3;
//launch control for each filter
//spork~ kb_control_notes();
while (true)
{
keyboard => now;
while (keyboard.recv ( kbmessage ) )
{
if (kbmessage.isButtonDown() )
{
<<< "down", kbmessage.which, "(code)", kbmessage.ascii(), "(ascii)" >>>;
}
}
}
while( true )
{
// wait for mouse input
mouse => now;
//recieve the data from the mouse
while( mouse.recv( message ) )
{
if( message.isMouseMotion() )
{
if( message.deltaX )
{
<<< "mouse motion:", message.deltaX, "on x-axis" >>>;
}
if( message.deltaY )
{
<<< "mouse motion:", message.deltaY, "on y-axis" >>>;
}
}
else if( message.isButtonDown() )
{
<<< "mouse button", message.which, "down" >>>;
}
else if( message.isButtonUp() )
{
<<< "mouse button", message.which, "up" >>>;
}
}
}
fun void kb_control_notes();
{
while (true)
{
keyboard => now;
{
while (keyboard.recv ( kbmessage ) )
{
if (kbmessage.isButtonDown() )
{
if (87 => kbmessage.ascii);
{
261.63 => s1.freq;
}
}
}
}
}
}
|
|
|
|
Back to top
|
|
 |
vrachnasormora

Joined: Mar 22, 2007 Posts: 42 Location: Preveza,Greece
|
Posted: Wed Apr 28, 2010 9:42 am Post subject:
|
 |
|
Hi, perhaps a way to do this:
| Code: | Hid keyboard;
HidMsg kbmessage;
0 => int kbdevice;
//show the devices are ready for input
if (me.args() ) me.arg(0) => Std.atoi => kbdevice;
if ( !keyboard.openKeyboard (kbdevice) ) me.exit();
<<< "keyboard" + keyboard.name() + "ready" >>>;
//note numbers for an octave
[ 261.63, 293.66, 329.63, 349.23, 392.00, 440.00, 493.88, 523.25 ] @=> float notes1[];
// c4 d4 e4 f4 g4 a5 b5 c5
//assign kb keys to notes
[ [ 87, 69, 82, 84, 89, 85, 73, 79 ],
[ 83, 68, 70, 71, 72, 74, 75, 76 ],
[ 96, 90, 88, 67, 86, 66, 78, 77 ] ] @=> int kbank[][];
//the oscillators defined with their gains
SinOsc s1 => dac;
0.33 => s1.gain;
TriOsc t1 => dac;
0.33 => t1.gain;
PulseOsc p1 => dac;
0.33 => p1.gain;
while (true)
{
keyboard => now;
{
while (keyboard.recv( kbmessage ) )
{
if ( kbmessage.isButtonDown() )
{
for ( int i; i < kbank.size(); i++ )
{
for ( int j; j < kbank[i].size(); j++ )
{
if ( kbmessage.ascii == kbank[i][j] )
{
if ( i == 0 )
{
notes1[j] => s1.freq;
}
if ( i == 1 )
{
notes1[j] => t1.freq;
}
if ( i == 2 )
{
notes1[j] => p1.freq;
}
}
}
}
}
}
}
} |
|
|
|
Back to top
|
|
 |
twist3d

Joined: Apr 27, 2010 Posts: 6 Location: uk
|
Posted: Wed Apr 28, 2010 10:16 am Post subject:
|
 |
|
wow, thank you for helping me with this
i can see what you've done to get that working.
i'd like to implement note-on, note off as well, so would that simple need to have something like this added to it?
| Code: |
if( msg.isButtonDown() )
{
if ( i == 0 )
{
notes1[j] => s1.freq;
}
}
|
i'd presume i'd need to do this for each 'row' |
|
|
Back to top
|
|
 |
vrachnasormora

Joined: Mar 22, 2007 Posts: 42 Location: Preveza,Greece
|
Posted: Wed Apr 28, 2010 10:46 am Post subject:
|
 |
|
Add some envelopes after the oscillators:
| Code: | SinOsc s1 => Envelope e1 => dac;
0.33 => s1.gain;
TriOsc t1 => Envelope e2 => dac;
0.33 => t1.gain;
PulseOsc p1 => Envelope e3 => dac;
0.33 => p1.gain;
fun void play_note( Envelope e, dur duration )
{
e.keyOn();
duration => now;
e.keyOff();
}
while (true)
{
keyboard => now;
{
while (keyboard.recv( kbmessage ) )
{
if ( kbmessage.isButtonDown() )
{
for ( int i; i < kbank.size(); i++ )
{
for ( int j; j < kbank[i].size(); j++ )
{
if ( kbmessage.ascii == kbank[i][j] )
{
if ( i == 0 )
{
notes1[j] => s1.freq;
spork~ play_note( e1, 250::ms );
}
if ( i == 1 )
{
notes1[j] => t1.freq;
spork~ play_note( e2, 250::ms );
}
if ( i == 2 )
{
notes1[j] => p1.freq;
spork~ play_note( e3, 250::ms );
}
}
}
}
}
}
}
} |
|
|
|
Back to top
|
|
 |
twist3d

Joined: Apr 27, 2010 Posts: 6 Location: uk
|
Posted: Wed Apr 28, 2010 11:20 am Post subject:
|
 |
|
wow, you're a genius!
thank you so much for your help
once i get the rest of my code sorted, i'll post in here to see what people think.
once again, many thanks for your help!! |
|
|
Back to top
|
|
 |
|