Author |
Message |
wppk
Joined: Jan 25, 2008 Posts: 31 Location: Los Angeles
|
Posted: Fri Feb 15, 2008 1:34 pm Post subject:
Anyone help me figure out why this isn't working? Subject description: Midi CC sweeper not working. |
|
|
Can anyone take a look at this and shed some light as to why it isn't working correctly?
it's a function to sweep a midi cc.
Basically, i want the function to return the incremented/decremented value each time it goes through the loop.
It only returns the value of the first argument right now.
fun int sweep( int s, int e) {
s => int start;
e => int end;
for(0 => int i;;i++) {
while(true) {
e => end;
100::ms => now;
if(start == end ) break;
return start++; }
while(true) {
s => start;
100::ms => now;
if(end == start) break;
return end++; }}
}
MidiOut mout;
MidiMsg output;
mout.open(0);
while(true){
sweep(1, 10) => output.data3;
<<<output.data3>>>;
} |
|
Back to top
|
|
|
Kassen
Janitor
Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Fri Feb 15, 2008 2:37 pm Post subject:
Re: Anyone help me figure out why this isn't working? Subject description: Midi CC sweeper not working. |
|
|
wppk wrote: |
for(0 => int i;;i++) {
|
are you sure about that bit? _________________ Kassen |
|
Back to top
|
|
|
wppk
Joined: Jan 25, 2008 Posts: 31 Location: Los Angeles
|
Posted: Fri Feb 15, 2008 3:30 pm Post subject:
|
|
|
No. But it works if the function doesn't return a value. If I just had
<<>> or <<<e>>> in each while loop it would print the number sweep.
Would another control structure be more appropiate? another while loop maybe? |
|
Back to top
|
|
|
wppk
Joined: Jan 25, 2008 Posts: 31 Location: Los Angeles
|
Posted: Fri Feb 15, 2008 3:31 pm Post subject:
|
|
|
sorry about the lines. wasn't intended. |
|
Back to top
|
|
|
Kassen
Janitor
Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Fri Feb 15, 2008 6:16 pm Post subject:
|
|
|
yeah, I'd go with a "while" loop in the interest of clarity, using a explicit break if/when needed, once it works we can work on compactness but since you have a bug in your code it would really help if the code was as clear as possible to others who may not yet grasp your reasoning. The "code" tag also helps with this in preserving indentation. If you start a line with tabs or spaces the board will otherwise remove those. Non-working code we have to parse in our head, you see.... :¬)
We'll get to the bottom of this. _________________ Kassen |
|
Back to top
|
|
|
wppk
Joined: Jan 25, 2008 Posts: 31 Location: Los Angeles
|
Posted: Fri Feb 15, 2008 9:05 pm Post subject:
|
|
|
O.k this should be a bit more clear. Sorry, I should get used to putting explanations within the code. I kind of hastily wrote it out at work today.
The first code is and example of what the function is supposed to do. It is set to print the number sweep in the console monitor (mini audicle).
what I want to be able to do is return the inc/dec value each time the code loops and then assign that value to another variable. A midi out msg to be specific.
this is what the second code is attempting to do.
I'm not sure if I am advancing time is the correct places. maybe it shouldn't be done within the inc/dec while loops, but if I don't advance some time within them I get stack overflow. Maybe there's a better solution.
EXAMPLE CODE:
fun void sweep(int s, int e) {
int start;
int end;
while(true) {
// ramps up from start integer
while (true) {
e => end;
s => start;
// prints the current number and increments it by one
<<< start++ >>>;
// when the start int equals the end int the loop breaks
// and moves to the next loop
if( end == start ) break;
100::ms => now; }
//this loop ramps downward from the end int to the start
while(true) {
s => start;
e => end;
// prints current number and decreases its value by one
<<< end-- >>>;
//when end equals start loop breaks and returns
//to the previous loop
if( start == end ) break;
100::ms => now; }
}
}
//execute the sweep function see it printed in console monitor
sweep( 0, 10 );
EXAMPLE 2: (NOT WORKING SO WELL)
this only returns the value of the first argument.
fun int sweep(int s, int e) {
int start;
int end;
while(true){
// numbers ramp up to end
while(true) {
//assign e and s
e => end;
s => start;
//increment value by one
start++;
// break if arguments are equal
if( end == start ) break;
//advance time
100::ms => now;
//should return the incremented value of start
return start; }
while(true) {
// assign e and s
e => end;
s => start;
//decrease value of end by one
end--;
//break loop if arguments are equal
if( start == end ) break;
//advance time
100::ms => now;
//should return the decreased value of end
return end; } }
}
//execute the function and assign the returned value to new variable
while(true) {
sweep( 0, 10 ) => int cc;
<<< cc >>>;
100::ms => now; }
|
|
Back to top
|
|
|
wppk
Joined: Jan 25, 2008 Posts: 31 Location: Los Angeles
|
Posted: Fri Feb 15, 2008 9:14 pm Post subject:
|
|
|
I think the problem might be in the actual execution of the sweep() function within the final while loop. Maybe every time the code loops around it resets to the value of the first argument. therefore it's never actually able to increment the value and always ends up returning that one value. Hmm.. |
|
Back to top
|
|
|
Kassen
Janitor
Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Sat Feb 16, 2008 9:49 am Post subject:
|
|
|
Ok.
I did some editing on your first version and this is a working version;
Code: | fun void sweep(int s, int e)
{
int start;
int end;
while(true)
{
e => end;
s => start;
// ramps up from start integer
while (true)
{
// prints the current number and increments it by one
<<< start++ >>>;
// when the start int equals the end int the loop breaks
// and moves to the next loop
if( end == start ) break;
100::ms => now;
}
//this loop ramps downward from the end int to the start
s => start;
while(true)
{
// prints current number and decreases its value by one
<<< end-- >>>;
//when end equals start loop breaks and returns
//to the previous loop
if( start == end ) break;
100::ms => now;
}
}
}
//execute the sweep function see it printed in console monitor
sweep( 0, 10 ); |
and this is a example of how I'd personally write the same thing.
Code: | fun void sweep(int s, int e)
{
//safety first!
//this would get stuck so instead we abort the function
if (s == e) return;
//swap arguments if needed
else if ( s > e)
{
s => int temp;
e => s;
temp => e;
}
//now we start properly
s => int value;
while (true)
{
until(value >= e)
{
value++;
<<< value >>>;
100::ms => now;
}
until(value <= s)
{
value--;
<<< value >>>;
100::ms => now;
}
}
}
//execute the sweep function see it printed in console monitor
sweep( 0, 10 ); |
I'm using "until" because I feel it's more expressive here. I'm also using "greater then or equal to" and the inverse as a safety measure. The way indentation is used is the same as the one in the official examples and the style I tend to use, this is not meant to imply it's "better" or whatever.
Also; if you'd like to use the "code" tag the easiest way is to highlight all of your code, then click on the "code" button above the text field. We can also go over your second example if you'd like?
Hope that helps. _________________ Kassen |
|
Back to top
|
|
|
wppk
Joined: Jan 25, 2008 Posts: 31 Location: Los Angeles
|
Posted: Sat Feb 16, 2008 2:42 pm Post subject:
|
|
|
Absolutely thanks so much. I really appreciate seeing your take on it. It definitely helps me.
The first example was really just to illustrate what I was trying to do. What I would like to be able to do is return every inc/dec value as the function loops through it's code. Is this possible? Then i can assign it to a new variable which would give me more flexibility with this particular function. this is what I am trying to accomplish with the second example, but it only returns the value of the first argument.
I tried to use the code tag last time but it didn't seem to work. I will definitely try to figure it out before posting again. And thanks again for your time in checking out the code. It is really appreciated. |
|
Back to top
|
|
|
Kassen
Janitor
Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Sat Feb 16, 2008 2:56 pm Post subject:
|
|
|
I think using "return" will break off execution of the function in any case. Perhaps the best option here if you really want the MIDI stuff to be outside of this function would be a event? Another option might be a class that would keep track of numbers like the top value, the bottom value and the direction we are going in and that gives you the next number in the sequence every time you call a certain function? Those questions really depend on the larger structure of your program, your style and so on.
I put this (above) usage of "return" in the WiKi page about missing bit in the manual, I just learned about it today by trying this out and finding "break" doesn't allow us to break out of functions (just loops) so I learned something too :¬), it was a fun puzzle over a cup of coffee so thank you as well!
BTW, if you find the board tags don't work as you wanted to; everybody can edit his or her own posts so you can re-try if you need to or like to. I can edit your posts here as well, BTW, because I'm the local moderator but I don't really like to use that power because it's so easy to misunderstand what was meant. _________________ Kassen |
|
Back to top
|
|
|
wppk
Joined: Jan 25, 2008 Posts: 31 Location: Los Angeles
|
Posted: Sat Feb 16, 2008 3:45 pm Post subject:
|
|
|
I will look into using an event or the new class option. I was planning on adding this to a MIDI class from which I could call this function and not be tied to any specific cc types like breath control or pitch wheel etc. Basically, this way I thought it could be applied to any outgoing midi message. That way it would be more flexible when adding it to new code. I thought about just adding in arguments for those variables but it could turn into quite a few arguments. I'm using all this to really get my head around how to write and apply functions the best way. |
|
Back to top
|
|
|
|