| Author |
Message |
jimmysword

Joined: Jan 11, 2008 Posts: 13 Location: Leeds, W Yorks
|
Posted: 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 |
 |
|
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
|
|
 |
Kassen
Janitor


Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Mon Mar 22, 2010 5:47 pm Post subject:
|
 |
|
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
|
|
 |
Antimon
Joined: Jan 18, 2005 Posts: 4145 Location: Sweden
Audio files: 371
G2 patch files: 100
|
Posted: Wed Mar 24, 2010 12:31 am Post subject:
|
 |
|
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
|
|
 |
|