| Author |
Message |
conexionismo

Joined: Dec 29, 2007 Posts: 11 Location: spain
|
Posted: Sat Dec 29, 2007 10:18 pm Post subject:
Up and Down |
 |
|
Hello.
I am new in this great language.
I have one simple thing:
A loop counter counts from 0 to 100, to 99 to 0, from 0 to 100, etc.
Surely it is very easy, but not achievement.
for? while?
Excuse my ignorance and my spanish english.
Thanks |
|
|
Back to top
|
|
 |
Kassen
Janitor


Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Sun Dec 30, 2007 4:12 am Post subject:
|
 |
|
Hi, conexionismo! Welcome!
Your English is better then my Spanish but we can speak ChucK together.
See if you can understand this;
| Code: | 1 => int direction;
int counter;
100 => int max;
0 => int min;
while (true)
{
<<<counter>>>;
//your code goes here
0.25::second => now;
if (counter >= max) -1 => direction;
else if (counter <= min) 1 => direction;
direction +=> counter;
} |
The trick is that every time we loop we add 1 or -1 to the counter and we switch the direction if we hit the minimum or the maximum.
TIP; if you don't know what kind of loop to use take "while", you can always write any loop as a while loop.
Happy ChucKing! _________________ Kassen |
|
|
Back to top
|
|
 |
kijjaz

Joined: Sep 20, 2004 Posts: 765 Location: bangkok, thailand
Audio files: 4
|
Posted: Sun Dec 30, 2007 1:04 pm Post subject:
|
 |
|
Yes. Kassen's method is really easy to understand. Thanks.  |
|
|
Back to top
|
|
 |
kijjaz

Joined: Sep 20, 2004 Posts: 765 Location: bangkok, thailand
Audio files: 4
|
Posted: Sun Dec 30, 2007 1:41 pm Post subject:
|
 |
|
ahhh i did tried to do that in a shorter expression..
I thought I should use "if" for doing that
but after playing around with the idea,
I come up with this nasty expression..
I'm not sure if I can simplify it more
but it works now:
| Code: | 100 => int max;
int i, j;
while(true)
{
<<< j + 1 >>>;
i++;
1 - 2 * (i % (max * 2) - i % max) / max +=> j;
40::ms => now;
} |
the expression's quite a mess now hahahha -_-
- - -
update: after I let my brain float around for a while..
I came up with a simplified one.. with only one variable! oh yay..
| Code: |
100 => int max;
int i;
while(true)
{
<<< max - Std.abs(i % (2 * max) - max) >>>;
i++;
40::ms => now;
} |
|
|
|
Back to top
|
|
 |
conexionismo

Joined: Dec 29, 2007 Posts: 11 Location: spain
|
Posted: Sun Dec 30, 2007 3:22 pm Post subject:
great! |
 |
|
Thanks!
The kassen and the kijjaz code works fine.
I thought not in 'else if' and >=.
Knew that I had to be an expression like that of kijjaz, but my knowledge is limited.
Chuck is powerful and a new and exciting world!! |
|
|
Back to top
|
|
 |
Kassen
Janitor


Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Mon Dec 31, 2007 11:13 am Post subject:
|
 |
|
| kijjaz wrote: | ahhh i did tried to do that in a shorter expression..
| Code: |
100 => int max;
int i;
while(true)
{
<<< max - Std.abs(i % (2 * max) - max) >>>;
i++;
40::ms => now;
} |
|
I love it!
How about;
| Code: |
100 => int max;
int i;
while(true)
{
<<< max - Std.abs(i++ % (2 * max) - max) >>>;
40::ms => now;
} |
Yet smaller! Yet more perverse! ;¬)
I think it's harder on the cpu though and I think it will run into issues once "i" gets larger then the largest integer we can use (that's very large but that number does exist).
I think defining a minimal value is useful too but with your version you could simply define that as a offset.
Not a very friendly expression but lots of fun, cool! _________________ Kassen |
|
|
Back to top
|
|
 |
Kassen
Janitor


Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Mon Dec 31, 2007 11:16 am Post subject:
Re: great! |
 |
|
| conexionismo wrote: |
Chuck is powerful and a new and exciting world!! |
Yes, it is! Isn't it fun? _________________ Kassen |
|
|
Back to top
|
|
 |
kijjaz

Joined: Sep 20, 2004 Posts: 765 Location: bangkok, thailand
Audio files: 4
|
Posted: Mon Dec 31, 2007 12:10 pm Post subject:
|
 |
|
hmm yes.. i can grow into a very very large int
to stop that, i might use (i + 1) % (2 * max) => i; instead of i++;
hmm -_- this is kinda fun after all those somewhat chaotic sound synthesis i've been doing. -_-" |
|
|
Back to top
|
|
 |
Kassen
Janitor


Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Mon Dec 31, 2007 1:16 pm Post subject:
|
 |
|
Yeah for sequencing I use this all the time;
++i%16 => i;
it's compact, clear and doesn't overflow. I like it.
You can set stuff like this loose on the STK pianos (using Std.mtof) as well, I bet you could do great arpeggios in very little code. There's at least as much space for math in music as there is in sound. _________________ Kassen |
|
|
Back to top
|
|
 |
conexionismo

Joined: Dec 29, 2007 Posts: 11 Location: spain
|
Posted: Mon Dec 31, 2007 5:16 pm Post subject:
Chuck it's fun! |
 |
|
Yes, Chuck it's fun.
The computer is a good musical instrument, but until now it was only to make sounds, helped by midi controllers who used to be copies of the traditional instruments.
The livecode is a good tool to ensure that the computer is, moreover, a good interface for controlling musical events. Instant is lost, but wins a immersive experience where they control many parameters at once.
Yes, Chuck it's fun. |
|
|
Back to top
|
|
 |
kijjaz

Joined: Sep 20, 2004 Posts: 765 Location: bangkok, thailand
Audio files: 4
|
Posted: Thu Jan 03, 2008 8:20 am Post subject:
|
 |
|
While playing around with more simple algorithm, i've found this:
| Code: | chuck %> { int a, b;
a++ => b++;
} |
chuck %> [chuck](VM): sporking incoming shred: 1 (chuck_file.JHxIXL)...
Segmentation fault (core dumped)
hahhaha -_-
maybe that expression's too out for ChucK. -_-" |
|
|
Back to top
|
|
 |
Frostburn

Joined: Dec 12, 2007 Posts: 255 Location: Finland
Audio files: 9
|
Posted: Thu Jan 03, 2008 1:35 pm Post subject:
|
 |
|
| Kassen wrote: |
How about;
| Code: |
100 => int max;
int i;
while(true)
{
<<< max - Std.abs(i++ % (2 * max) - max) >>>;
40::ms => now;
} |
Yet smaller! Yet more perverse! ;¬)
|
Nice. :)
What about just;
| Code: |
100 => int max;
-max => int i;
while(i <= max)
{
<<< max - Std.abs(i++) >>>;
40::ms => now;
}
|
_________________ To boldly go where no man has bothered to go before. |
|
|
Back to top
|
|
 |
kijjaz

Joined: Sep 20, 2004 Posts: 765 Location: bangkok, thailand
Audio files: 4
|
Posted: Thu Jan 03, 2008 8:53 pm Post subject:
|
 |
|
| Frostburn: wow, that's neat for up and down. ^_^ |
|
|
Back to top
|
|
 |
|