electro-music.com   Dedicated to experimental electro-acoustic
and electronic music
 
    Front Page  |  Articles  |  Radio
 |  Media  |  Forum  |  Links  |  Store
Forum with support of Syndicator RSS
 FAQFAQ   CalendarCalendar   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   LinksLinks
 RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in  Chat RoomChat Room 
 Forum index » DIY Hardware and Software » ChucK programming language
Stepping through an array on key press
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [5 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Author Message
yoshima



Joined: Nov 28, 2009
Posts: 15
Location: UK

PostPosted: Mon Apr 26, 2010 3:08 pm    Post subject: Stepping through an array on key press
Subject description: How do I make it loop?
Reply with quote  Mark this post and the followings unread

Hello,

I'm trying to get ChucK to step through an array each time a key is pressed on the keyboard. It works but it only goes through the array once....

How can I get it to keep looping through?

Thank for any help....

Heres my code so far.....

Code:
// HID
Hid hi;
HidMsg msg;

// which keyboard
0 => int device;
// get from command line
if( me.args() ) me.arg(0) => Std.atoi => device;

// open keyboard (get device number from command line)
if( !hi.openKeyboard( device ) ) me.exit();
<<< "keyboard '" + hi.name() + "' ready", "" >>>;

// Patch
FMVoices v => JCRev r => dac;

0 => v.gain;

[ 0, 0, 3, 5, 5, 6, 9, 3, 4, 2, 1, 5, 7 ] @=> int song [];

int i;


// infinite event loop
while( true )
{
    // wait for event
    hi => now;
   
    // get message
    while( hi.recv( msg ) )
    {


{
   
    if (msg.isButtonDown())
    {           
        Std.mtof(60+song[i++]) => float freq;
        freq => v.freq;
        <<<song>>>;
        1 => v.noteOn;
        0.5 => v.gain;   
        0.2::second => now;
    }
   
    else
    {
        0 => v.noteOff;
    }     
}
}
}
Back to top
View user's profile Send private message
vrachnasormora



Joined: Mar 22, 2007
Posts: 42
Location: Preveza,Greece

PostPosted: Mon Apr 26, 2010 3:32 pm    Post subject: Reply with quote  Mark this post and the followings unread

Hi, try changing this line:
Code:
Std.mtof(60+song[i++]) => float freq;

to:
Code:
Std.mtof(60+song[++i%song.size()]) => float freq;
Back to top
View user's profile Send private message
yoshima



Joined: Nov 28, 2009
Posts: 15
Location: UK

PostPosted: Mon Apr 26, 2010 3:49 pm    Post subject: Reply with quote  Mark this post and the followings unread

It works!! Thanks for the help...

But I have to remove the line:

Code:
<<<song>>>;


What code do I need for it to print the number in the array?

Thanks,
Back to top
View user's profile Send private message
vrachnasormora



Joined: Mar 22, 2007
Posts: 42
Location: Preveza,Greece

PostPosted: Mon Apr 26, 2010 4:16 pm    Post subject: Reply with quote  Mark this post and the followings unread

You could again use:
Code:
<<< song[i%song.size()] >>>;

but because the modulo operation is quite expensive CPU-wise to re-express as you have already compute it, I would suggest:
Code:
   if (msg.isButtonDown())
            {           
                Std.mtof(60+song[i]) => float freq;
                freq => v.freq;
                <<<song[i]>>>;
                ++i%song.size() => i;
                1 => v.noteOn;
                0.5 => v.gain;   
                0.2::second => now;
            }

plus now you don't lose the first element of the array at the start of the program.Bye
Back to top
View user's profile Send private message
yoshima



Joined: Nov 28, 2009
Posts: 15
Location: UK

PostPosted: Mon Apr 26, 2010 4:33 pm    Post subject: Reply with quote  Mark this post and the followings unread

wow... thanks...

you have been really helpful...

cheers... Very Happy
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [5 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
 Forum index » DIY Hardware and Software » ChucK programming language
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum
Niio2

Please support our site. If you click through and buy from
our affiliate partners, we earn a small commission.


Forum with support of Syndicator RSS
Powered by phpBB © 2001, 2005 phpBB Group
Copyright © 2003 through 2009 by electro-music.com - Conditions Of Use