electro-music.org   Dedicated to experimental electro-acoustic
and electronic music
 
 Front Page  |  Radio  |  Forum  |  Media  |  Playlists  |  Wiki
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
count backwards
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [10 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Author Message
jirko



Joined: Dec 15, 2009
Posts: 59

PostPosted: Wed Apr 20, 2011 12:56 am    Post subject: count backwards Reply with quote  Mark this post and the followings unread

Hi,

I need a counter with a kind of variable to decide the count direction.

assuming whe are counting inside a range:

for(int count; count < 32; count++) etc

something like:

1,2,3.. and if the variable changes then we start to count backwards, but always inside the given range "32".

Every help will be much appreciated!!
Back to top
View user's profile Send private message
Antimon



Joined: Jan 18, 2005
Posts: 4145
Location: Sweden
Audio files: 373
G2 patch files: 100

PostPosted: Wed Apr 20, 2011 1:52 am    Post subject: Reply with quote  Mark this post and the followings unread

Code:
1 => int direction;

for (0 => int counter; counter >= 0 && counter < 32; direction +=> counter) {

   (...)

   -1 => int direction; // start counting backwards
}

_________________
Antimon's Window
@soundcloud @Flattr home - you can't explain music
Back to top
View user's profile Send private message Visit poster's website
jirko



Joined: Dec 15, 2009
Posts: 59

PostPosted: Wed Apr 20, 2011 2:49 am    Post subject: Reply with quote  Mark this post and the followings unread

Thanks a lot Stefan!

This works until the count reaches "0", then it will repeat "0" all the time:

Here is a small example:

Code:

1 => int direction;


fun void sq()
{
    while(1)
    {
        for (0 => int counter; counter >= 0 && counter <32> counter)
        {
            <<<counter>>>;
            200::ms=>now;
        }
       
    }
}

spork~sq();

3::second=>now;

<<<"start to count backwards">>>;
-1 => direction;

week=>now;
Back to top
View user's profile Send private message
Antimon



Joined: Jan 18, 2005
Posts: 4145
Location: Sweden
Audio files: 373
G2 patch files: 100

PostPosted: Wed Apr 20, 2011 3:11 am    Post subject: Reply with quote  Mark this post and the followings unread

NP :)

Remember to check "Disable HTML" and "Disable smilies" when you post code here - your code in the for statement got mangled a bit.

I don't know what you want to use the loop for, when you want it to stop etc - I didn't know that you had encased the for loop in an endless while loop for example. If you want more help maybe you can describe the full problem you need to solve?

Here's another kind of loop that will iterate endlessly and keep counter within 0 through 31. I haven't tested this in ChucK's compiler - hopefully you see what I mean anyway:

Code:
1 => int direction;
0 => int counter;

while (1) {

   (...)

   if ( <insert condition to change direction here> ) {
      -direction => direction; // change direction
   }
   
   direction +=> counter
   counter % 32 => counter; // Modulo 32 keeps 0 <= counter < 32
   
   // alternative to % 32 line -
   // since 32 is an exponent of two, we can mask out bits to
   // accomplish the same thing:
   // counter & 0x3f => counter
}

_________________
Antimon's Window
@soundcloud @Flattr home - you can't explain music
Back to top
View user's profile Send private message Visit poster's website
jirko



Joined: Dec 15, 2009
Posts: 59

PostPosted: Wed Apr 20, 2011 3:26 am    Post subject: Reply with quote  Mark this post and the followings unread

"I don't know what you want to use the loop for"

This loop is for a step sequencer (drums) that I'm working.

"hopefully you see what I mean anyway"

yes I see it, I though that modulo is the way to go, but testing your code and changing the condition we get a count between 0 and -1:


Code:

1 => int direction;
0 => int counter;

while (1) {
   
    if ( true ) {
        -direction => direction; // change direction
    }
   
    direction +=> counter;
    counter % 32 => counter; // Modulo 32 keeps 0 <= counter < 32
    <<<counter>>>;
   
    200::ms=>now;
}



Thanks!
Back to top
View user's profile Send private message
Antimon



Joined: Jan 18, 2005
Posts: 4145
Location: Sweden
Audio files: 373
G2 patch files: 100

PostPosted: Wed Apr 20, 2011 3:30 am    Post subject: Reply with quote  Mark this post and the followings unread

That's because you're changing the direction every step. Put something other than "true" on the line that now says "if ( true ) {". When do you want the sequencer to start playing backwards?
_________________
Antimon's Window
@soundcloud @Flattr home - you can't explain music
Back to top
View user's profile Send private message Visit poster's website
jirko



Joined: Dec 15, 2009
Posts: 59

PostPosted: Wed Apr 20, 2011 3:54 am    Post subject: Reply with quote  Mark this post and the followings unread

uuups! :)

I have tried with if(counter==8) and this works one time, then we get negative values and the if sentence will not match anymore.

"When do you want the sequencer to start playing backwards?"

The best way for my setup is to have a variable that decides the count direction.

f.e.

0=>countDirection; // counting back
1=>countDirection; // counting forward

and thanks again for your patience!
Back to top
View user's profile Send private message
Antimon



Joined: Jan 18, 2005
Posts: 4145
Location: Sweden
Audio files: 373
G2 patch files: 100

PostPosted: Wed Apr 20, 2011 5:07 am    Post subject: Reply with quote  Mark this post and the followings unread

OK, modulo seems to retain the sign of the variable for some reason. Do the bitmask instead then:

Code:
1 => int direction;
29 => int counter;

fun void stepper() {
  while (1) {
    direction +=> counter;
    counter & 0x1f => counter; // Modulo 32 keeps 0 <= counter < 32
    <<<counter>>>;

    200::ms=>now;
  }
}
spork ~ stepper();

fun void changeDirection() {
        -direction => direction; // change direction
}


while (1) {
  1500::ms => now;
  changeDirection();
}


I've also extracted the direction changing in a separate function, and put the step thing in a shred, so you can change it from the outside. How is that?

Oh and I had the bitmask wrong before, it should be 0x1f. A downside here is that this will only work for 2 exponent sequence lenghts (8, 16, 32, 64 etc). If you want uneven lenghts you'll have to put an if statement in there and check the boundaries explicitly.

/Stefan

_________________
Antimon's Window
@soundcloud @Flattr home - you can't explain music
Back to top
View user's profile Send private message Visit poster's website
jirko



Joined: Dec 15, 2009
Posts: 59

PostPosted: Wed Apr 20, 2011 5:44 am    Post subject: Reply with quote  Mark this post and the followings unread

Thanks Stefan! This works nicely!
And yes, I need uneven lenghts, but I'm not sure how to check the boundaries..

Can you give me a hint?
Back to top
View user's profile Send private message
Antimon



Joined: Jan 18, 2005
Posts: 4145
Location: Sweden
Audio files: 373
G2 patch files: 100

PostPosted: Wed Apr 20, 2011 6:24 am    Post subject: Reply with quote  Mark this post and the followings unread

I mean like brute force, instead of the bitmask line do:

Code:
if (counter < 0) {
   SEQUENCE_LENGTH => counter;
} else if (counter >= SEQUENCE_LENGTH) {
   0 => counter;
}



And set 32 => int SEQUENCE_LENGTH; somewhere at the top.

/Stefan

_________________
Antimon's Window
@soundcloud @Flattr home - you can't explain music
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [10 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
Log in
Username:     Password:      Log me on automatically each visit    


Forum with support of Syndicator RSS
Powered by phpBB © 2001, 2005 phpBB Group
Copyright © 2003 through 2026 by electro-music.org