Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

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();
        }
    }
}


Wednesday, June 6, 2012

Recording Sounds

 Recording Sounds: Always Great When You Start Thinking Like a Researcher

So, today I wanted to be able to record voices and other sounds using ChucK. I did not really want to record ChucK compositions because I had done that before, but I wanted to be able to use an external microphone, or just an inbuilt microphone to make the recordings. I knew I had to use adc, but to what was a supposed to chuck it to? After some deep thinking, I thought I should chuck it to dac, but then how would it record the sound? I then thought doing the following: adc => SinOsc s => dac. I gave the

Tuesday, June 5, 2012

ChucK reactions and cool discoveries

It has been really great learning to use ChucK, and making cool discoveries with each new day before me. ChucK to me is not a challenging language, but the challenge comes in understanding what you are doing. Understanding how to use the unit generators (I overheard the professor saying "unit generators" seems to be an old phrase, but I don't know what else to call them), and understanding the many STK

Changing the Face of Music: making your keyboard a musical instrument


Changing the Face of Music: Making your keyboard a musical instrument

             I view research as a process of examining society’s problems, and then look for ways to provide solutions for those problems. Research to me is never purposeless, but it always has an intrinsic goal to serve. After working on ChucK for the past few days and discovering how one can turn a computer keyboard into a musical instrument, I realized how this technique could be used to meet societal needs. Being an international student from a developing nation living in a developed nation now, I have a deep understanding of problems that could be in both developing and developed nations, and I continually realize that financial problems are not just a curse for developing nations—they exist in developed nations as well. Because many families and institutions barely make ends meet, musical instruments become luxuries to many. Many kids may even grow up without much knowledge of music, yet they are gifted and could do great in the field. As a way to curb problems of access to musical instruments, I thought it would be great if we could turn our keyboards into musical instruments. Instead of complaining about lack of money, we can enjoy the best of both worlds—our computers and the music. Even if the keyboard instruments may be so different from the real instruments, sparking an interest in any person about something may make it easier for that person to learn the real thing when exposed to it. Besides, this is a generation where people are doing things they have never done before, and breaking new ground, so this could be a dawn of a new era in music.
            There are also a bunch of other people who might not have garage band and other applications that play musical instruments on their computers, yet they love music, but they just cannot get off their computers to play an instrument, or the do not own an instrument. Such people could also benefit from the musical instrument models in ChucK.

What you need to turn your keyboard into a musical instrument

Monday, June 4, 2012

Beat Experimentation

I was reading about beats and interference, so I thought I'd try it out.

I created this infinite time loop program, and it plays around with beats. Try it out, it's pretty cool, and doesn't completely give you a headache.

Disclaimer: I owe most of the coolness of the output to the JCRev...


May someone explain this to me

So, I was working on my organ, so that it is easier to play, and I was working with if else statements. After a couple of if else statements, the code stopped working--I mean the program would not execute what was in the bottom if else statements. I then put an if on one of the statements where it was failing to execute, and maintained the other if else statements as if else statements, and band, the code worked. This is beyond my understanding really? Does anyone know why? Is it because ChucK can only accept a certain amount  of if-else statements? Is that even feasible? Is there a limit to cases you can have?

Confused

Friday, June 1, 2012

Songs for the keyboard piano

Twinkle Twinkle:

nnhhkkhffddaanhhhhffddahhffddannhhkkhffddaan

Ode To Joy:

ddfhhfdannaddaaddfhhfdannadann

Harry Potter:


dhgd;kgdhgagc

Thursday, May 31, 2012

Hardcoded class of musical notes

Here is a class I've written using the chart located on this page: http://www.zytrax.com/tech/audio/audio.html#notes. The explanation in the comments should hopefully be sufficient to understand how to use it. It should be helpful for anyone wanting to work with creating music in ChucK. I may in the future add some scales to it, but since there is such a wide variety
of scales available in music it would probably be more efficient for those to be added by the individual. For scales, using an array would probably be the best way to go. The code is below the jump.


Wednesday, May 30, 2012

Lab Idea 1: Intro to ChucK for Non-Programmers

Here's an idea for a lab for a class to do. It doesn't assume anything beyond a basic knowledge of how computers work and attempts to explain the basics of ChucK to someone who's never programmed before, while hopefully also giving some ideas for more advanced students to explore. Content is below the jump.