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
map values
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [9 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Author Message
jirko



Joined: Dec 15, 2009
Posts: 59

PostPosted: Thu May 13, 2010 2:18 am    Post subject: map values Reply with quote  Mark this post and the followings unread

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
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: Thu May 13, 2010 12:59 pm    Post subject: Reply with quote  Mark this post and the followings unread

Wouldn't a array work?
_________________
Kassen
Back to top
View user's profile Send private message Send e-mail Visit poster's website
jirko



Joined: Dec 15, 2009
Posts: 59

PostPosted: Thu May 13, 2010 1:16 pm    Post subject: Reply with quote  Mark this post and the followings unread

Hi Kassen,
what do you mean with an array?
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: Thu May 13, 2010 5:31 pm    Post subject: Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message Send e-mail Visit poster's website
witt0191



Joined: Feb 13, 2008
Posts: 23
Location: UK

PostPosted: Thu May 13, 2010 8:57 pm    Post subject: Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message
jirko



Joined: Dec 15, 2009
Posts: 59

PostPosted: Fri May 14, 2010 1:10 am    Post subject: Reply with quote  Mark this post and the followings unread

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
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: Fri May 14, 2010 5:02 am    Post subject: Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message Send e-mail Visit poster's website
jirko



Joined: Dec 15, 2009
Posts: 59

PostPosted: Mon May 17, 2010 1:30 pm    Post subject: Reply with quote  Mark this post and the followings unread

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
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: Tue May 18, 2010 12:53 am    Post subject: Reply with quote  Mark this post and the followings unread

Happy to have been of help Smile
_________________
Kassen
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [9 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