| Author |
Message |
jirko

Joined: Dec 15, 2009 Posts: 59
|
Posted: Thu May 13, 2010 2:18 am Post subject:
map values |
 |
|
| I'm working on a midi class and I have done a little formula to map/scale values, but the code gets very unreadable and pretty long. Isn't there a unit conversation like "map" in Java? |
|
|
Back to top
|
|
 |
Kassen
Janitor


Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Thu May 13, 2010 12:59 pm Post subject:
|
 |
|
Wouldn't a array work? _________________ Kassen |
|
|
Back to top
|
|
 |
jirko

Joined: Dec 15, 2009 Posts: 59
|
Posted: Thu May 13, 2010 1:16 pm Post subject:
|
 |
|
Hi Kassen,
what do you mean with an array? |
|
|
Back to top
|
|
 |
Kassen
Janitor


Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Thu May 13, 2010 5:31 pm Post subject:
|
 |
|
Well, a array is a series of numbers (or other things). You can then pick one number by looking it up using a index. A array of 128 elements should be able to "map" (link) a incoming MIDi value to the number or thing you want it to represent.
I don't really know Java, but is that the kind of thing you are looking for?
There is a lot more about arrays than I have time to cover now in the manual, have a look there if that sounds like a good solution. Otherwise we could have a look at exactly how and why your code becomes a mess and see what we could do about that. _________________ Kassen |
|
|
Back to top
|
|
 |
witt0191
Joined: Feb 13, 2008 Posts: 23 Location: UK
|
Posted: Thu May 13, 2010 8:57 pm Post subject:
|
 |
|
I use this class to do midi input conversions rather than using an array based system.
| Code: | class m
{
//midi 0 - 127 number remapped between 0 and 1.0
function static float map(int a)
{
a * 0.007874 => float f;
return(f);
}
//midi 0 - 127 number remapped between arg2 and arg3
function static float map(int a, int b, int c)
{
(a * 0.007874 * (c-b) ) + b => float f;
return(f);
}
//midi 0 - 127 number remapped between float arg2 and arg3
function static float map(float a, float b, float c)
{
(a * 0.007874 * (c-b) ) + b => float f;
return(f);
}
}
<<< m.map(127) >>>;
<<< m.map(100) >>>;
<<< m.map(0) >>>;
<<< m.map(127, 100, 1000) >>>;
<<< m.map(100, 100, 1000) >>>;
<<< m.map(0, 100, 1000) >>>;
<<< m.map(127, 0.1, 5.5) >>>;
<<< m.map(100, 0.1, 5.5) >>>;
<<< m.map(0, 0.1, 5.5) >>>;
<<< "test">>>; |
|
|
|
Back to top
|
|
 |
jirko

Joined: Dec 15, 2009 Posts: 59
|
Posted: Fri May 14, 2010 1:10 am Post subject:
|
 |
|
Hi Kassen,
I know about arrays, but for this kind of job an array is to lavish, I guess.
I was looking for a Ugen like f.e:
map(value, input low value, input high value, output low value, output high value);
It seems there's nothing similar in Chuck?
Hi witt0191,
nice Class, I will take a look at this.
thanks! |
|
|
Back to top
|
|
 |
Kassen
Janitor


Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Fri May 14, 2010 5:02 am Post subject:
|
 |
|
so something like;
| Code: | fun float map(int value, float low, float high)
{
return low + (value * ( (high - low) / 127.0 ));
} |
where value would expect a number coming from a MIDI signal, low would be the desired lowest value to be returned and high would be the desired highest output.
this would of course give a linear range, which might not be desired, but this is the overall idea that could be extended. _________________ Kassen |
|
|
Back to top
|
|
 |
jirko

Joined: Dec 15, 2009 Posts: 59
|
Posted: Mon May 17, 2010 1:30 pm Post subject:
|
 |
|
thanks kassen! Sometimes I need numbers outside the midi range, this seems to work for that case:
| Code: | fun float map(float value, float low_input_value, float high_input_value, float low_output_value, float high_output_value)
{
return((value - high_input_value) / (low_output_value - high_input_value)) * (low_input_value - high_output_value) + high_output_value;
}
<<<map(64, 0, 127, 1, 10000)>>>; |
Cheers |
|
|
Back to top
|
|
 |
Kassen
Janitor


Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Tue May 18, 2010 12:53 am Post subject:
|
 |
|
Happy to have been of help  _________________ Kassen |
|
|
Back to top
|
|
 |
|