Tuesday, June 12, 2012

KIns progress: 06/12/12

Some significant progress on the KIns has been made! Here's a summary of my process so far:

I started using keyinmulti2.ck (http://smelt.cs.princeton.edu/code/keyboard/keyinmulti2.ck) from the S.M.E.L.T. website as my base. It already supported polyphony (playing more than one note at once) and variable note length, so those things can be crossed off the KIns project summary (I've updated that post too to reflect the work done so far). The way the program implements polyphony is very elegant--the making of actual noise is handled by one function, and when the program receives a message that a key has been pressed on the keyboard, it sporks a new shred of the keysound function, and when a key release message is received, it unsporks the proper shred to stop the sound. Therefore, pressing three keys at once will cause three shreds to get sporked, which will sit there playing their respective notes until they are unsporked. The number of notes you can have playing at one time seems to be limited only by the number of simultaneous key presses the computer is capable of registering.

I've made two specific modifications to keyinmulti2.ck.
1. I rearranged the key mappings to make a more linear progression. They were originally arranged in "frets" like on a guitar, so that there were a lot of overlapping notes between the key rows. This arrangement actually makes somewhat more sense for real music making, but for the beginner level user that the KIns is aimed at, I think it just makes it a lot more confusing. I feel like there's probably a somewhat optimal arrangement for the key mapping that makes more sense the way I've got it arranged now, but I think that will be figured out through experimentation much later on.
2. I added the capability to switch between different instrument sounds by pressing the number keys. In this version, 1 switches to a basic sine wave, 2 is a saw wave, and 3 is a "Rhodey" instrument (from the STK instrument kit that's built into ChucK). It's extremely easy to edit the instruments/add new instruments in the code, but I'm hoping eventually to be able to do this via a GUI and to also have slider controls on the GUI for the individual properties of the instruments. For example, a sine wave has very few built-in controls, but the STK instruments have lots of different controls that vary with the instrument. Having a dynamically changing GUI would be great for giving more control over these instruments.

One thing that I think would be good to work on next would be capability to sustain notes after the key is released, so that they fade out rather than being cut off abruptly by the shred being unsporked right as the key is released. Another thing is of course the GUI, which could be executed in MAUI (miniAudicle's built in, but very simple GUI). Processing also seems a promising choice for the GUI though. Julia found some demos that show two-way interaction between Processing and ChucK, I think via OSC, so I might look into those to see if that would be possible.

Here is the code for the current iteration of the KIns. It's very heavily commented, probably more so than is actually necessary.


/*----------------------------------------------------------------------------
S.M.E.L.T. : Small Musically Expressive Laptop Toolkit

Copyright (c) 2008 Dan Trueman.  All rights reserved.
http://smelt.cs.princeton.edu/
http://soundlab.cs.princeton.edu/

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
U.S.A.
-----------------------------------------------------------------------------*/

//-----------------------------------------------------------------------------
// name: keyinmulti2.ck
// desc: this program creates an array filled with the keyboard code for
// each key on the keyboard, indexed by the keyboard row and column. Then,
// it treats each row as a string, which is "tuned" in the code. Pressing 
// a key will play the note.
//
// This version supports polyphony, and the note ends when you release the key!
// Warning: Due to hardware (not our fault), you may not be able to play all chords.
//
// to run (in command line chuck):
//     %> chuck keyinmulti2.ck
//
// to run (in miniAudicle):
//     (make sure VM is started, add the thing)

//
//-----------------------------------------------------------------------------

//Hid = human input device. It's a variable to hold whatever HID the program
//needs to use
Hid hi;
//Hidmsg contains data about what the HID is doing at any given moment
HidMsg msg;
//sound determines what sound the keyboard is making at any given time
0 => int sound;

//initializes the HID as the keyboard, and exits if there's no keyboard available
0 => int deviceNum;
hi.openKeyboard( deviceNum ) => int deviceAvailable;
if ( deviceAvailable == 0 ) me.exit();
<<< "keyboard '", hi.name(), "' ready" >>>;


//array with key codes, for MacBook anyhow

[30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 45, 46, 42], //1234... row
[20, 26, 8, 21, 23, 28, 24, 12, 18, 19, 47, 48, 49], //qwer... row
[4, 22, 7, 9, 10, 11, 13, 14, 15, 51, 52], //asdf... row
[29, 27, 6, 25, 5, 17, 16, 54, 55, 56]   //zxcv... row

]   @=> int row[][];

//our big array of pitch values, indexed by ASCII value
int keyToPitch_table[256];

//this function takes each row and tunes it in half steps, based
//on whatever fundamental pitch note specified
fun void tuneString(int whichString, int basepitch) {
    
    for (0 => int i; i < row[whichString].cap(); i++) {
        
        basepitch + i => keyToPitch_table[row[whichString][i]];
        
        <<<row[whichString][i], keyToPitch_table[row[whichString][i]]>>>;
        
    }
    
}

//tune the strings!! This starts at (I think) A1, and then continues up by halftones
//each key. To hear the progression, go from left to right z->?, then up to a->",
//then q->]

tuneString(3, 55);
tuneString(2, 65);
tuneString(1, 76);
//tuneString(0, 185);


//makes the key sounds!
//currently configured to let the top row (number keys) control the type of sound
//the KIns makes. The code below should be pretty self-explanatory.
fun void keysound(float freq, Event noteOff) {
    
    if(sound==0){
    
    SinOsc sine => ADSR envelope => dac;
    envelope.set(80::ms, 25::ms, 0.1, 150::ms);
    
    freq => sine.freq;
    
    envelope.keyOn();
    noteOff => now;
    envelope.keyOff();
    150::ms => now;
    
    envelope =< dac;
}
else if(sound==1){
    
    SawOsc saw => ADSR envelope => dac;
    envelope.set(10::ms, 25::ms, 0.1, 150::ms);
    
    freq => saw.freq;
    
    envelope.keyOn();
    noteOff => now;
    envelope.keyOff();
    150::ms => now;
    
    envelope =< dac;
}
else if(sound ==2){
    Rhodey voc=> JCRev r =>dac;
    freq => voc.freq;
0.8 => voc.gain;
.8 => r.gain;
.2 => r.mix;

voc.noteOn(1);
    noteOff => now;
    voc.noteOff(1);
    150::ms => now;
}
}

Event noteOffs[256];

//infinite time loop
while( true )
{
    
    hi => now;
    
    //only does things when there's a message coming in from the HID
    while( hi.recv( msg ) )
    {
        //only if the message from the HID is that a button was pressed...
        if( msg.isButtonDown() )
        {
            //the following if/elseif statements check to see if the button press
            //should cause the type of sound to change. Only the value
            //of sound needs to change, as the keysound function
            //handles actually producing the appropriate sound.
            if(msg.which==30){
                0 => sound;
            }
            else if(msg.which==31){
                1=>sound;
            }
            else if(msg.which==32){
                2=>sound;
            }
            else{
            keyToPitch_table[ msg.which ] => Std.mtof => float freq;
            spork ~ keysound(freq, noteOffs[ msg.which] );
        }
            
        }
        //if the message was not that a button was pressed...
        else
        {   
            noteOffs[ msg.which ].signal();
        }
    }
}


1 comment:

  1. Freda: This is great! So much fun to play with. I too was thinking it would be nice to be able to have the keys sustain a bit longer rather than cut out. Really great work! Jan

    ReplyDelete