electro-music.com   Dedicated to experimental electro-acoustic
and electronic music
 
    Front Page  |  Radio
 |  Media  |  Forum  |  Wiki  |  Links
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
Step through array conditionally?
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [3 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Author Message
jimmysword



Joined: Jan 11, 2008
Posts: 13
Location: Leeds, W Yorks

PostPosted: Mon Mar 22, 2010 1:43 pm    Post subject:  Step through array conditionally?
Subject description: ...at least I think that's what I want to do
Reply with quote  Mark this post and the followings unread

Hello all, long time since I've been on here but I feel like chucking again! I've had an idea to play simple sequences from an array as follows.

I have a counter that goes up in increments of 1 until it reaches 16 then it resets.

While the count is looping I want to check against an array and play a sample if the number in the array is the same as the number of the count. So the array below would play a SndBuf from position 0 on the 1st, 5th, 9th and 13th count. The count would get to 16 and then it would start again.

Code:
[1,5,9,13] @=> int pattern[];


It seems like it should be simple...

I hope someone can understand what I've tried to explain and give me some much needed and hugely appreciated guidance.

edit: I think I've almost cracked it -

Code:
240.0 => float tempo; // tempo in Beat Per Minute
minute / tempo => dur beat; // with Tempo, we can calculate how long a beat should be

[1,5,9,13] @=> int kickpat[];

int count;
1 => count;


// construct the patch
SndBuf kick => Gain g => dac;
"kick.wav" => kick.read;
kick.samples() => kick.pos;
.5 => g.gain;
while(true)
{
  //loop over the entire array
  for( 0 => int i; i < kickpat.cap(); i++ )
  {
    <<<count>>>;

    if ( kickpat[i] == count )
    {
    0 => kick.pos;
    }
 
    1::beat=> now;

    if ( count > 15 )
    {
    0 => count;
    }
 
    count++;
  }
}

However the sample only plays on count 1, probably something to do with the order I'm doing things... any ideas?
Back to top
View user's profile Send private message
Kassen
Janitor
Janitor


Joined: Jul 06, 2004
Posts: 7678
Location: The Hague, NL
G2 patch files: 3

PostPosted: Mon Mar 22, 2010 5:47 pm    Post subject: Reply with quote  Mark this post and the followings unread

can't verify the code now as my soundcard is busy with some internet radio, but I'd always advance time right above or below the line incrementing the count and do the modulo right after the incrementing. To me those things belong together conceptually so I always keep them together in the code as well. Sometimes I even use something like this;

++count%16 => count;

It's a bit dense but at least it makes sure the operations will always occur in one place.

_________________
Kassen
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Antimon



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

PostPosted: Wed Mar 24, 2010 12:31 am    Post subject: Reply with quote  Mark this post and the followings unread

I think it looks like it should work... anyway, I feel I want to rearrange a couple of things, to make it more readable. For starters, what you're doing with the count variable is essentially what a for loop does, so I'd nest the for loops:

Code:

(...)
while(true)
{
  //loop over 16 counts
  for( 0 => int count; count < 16; count++ )
  {
    //loop over the entire array
    for( 0 => int i; i < kickpat.cap(); i++ )
    {
      <<<count>>>;
 
      if ( kickpat[i] == count )
      {
        0 => kick.pos;
      }
 
      1::beat=> now;
    }
  }
}


here it's easier perhaps to see that 1::beat => now is inside the wrong loop. It should be moved outside one level - exchange its line with line below with the curly brace.

second, it seems you have the kickpat array sorted. If you can count on it staying that way (if you dynamically want to add elements here you need to move the higher stuff to the right), you don't need to loop through kickpat each time you check:

Code:

(...)
while(true)
{
  0 => int kickIndex;
  //loop over 16 counts
  for( 0 => int count; count < 16; count++ )
  {
    <<<count>>>;
 
    if ( kickpat[kickIndex] == count )
    {
      0 => kick.pos;
      kickIndex++;
      if (kickIndex >= kickpath.cap())
      {
        0 => kickIndex;
      }
    }
 
    1::beat=> now;
  }
}


/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 [3 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


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