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 
go to the radio page Live at electro-music.com radio 1 Please visit the chat
  host / artist show at your time
today> Modulator ESP Adventures In Sound
 Forum index » DIY Hardware and Software » ChucK programming language
UGens and parameters as arguments?
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [3 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Author Message
wppk



Joined: Jan 25, 2008
Posts: 31
Location: Los Angeles

PostPosted: Tue Mar 18, 2008 2:04 pm    Post subject: UGens and parameters as arguments? Reply with quote  Mark this post and the followings unread

I would like to use a UGen as an argument to a function.
I would like to be able to assign a different parameter of a different UGen every time I call the function.

When I try to run something like this then I get an error stating that "UGen has no member .freq".

what would be the best way to approach this?

like this, for instance:

Code:


fun void do_something (UGen u) {

          Std.rand2f( 100, 200) => u.freq;
         
}

//call function

while(true) {
 
      do_something( LPF );
100::ms => now;
}
Back to top
View user's profile Send private message
Frostburn



Joined: Dec 12, 2007
Posts: 255
Location: Finland
Audio files: 9

PostPosted: Wed Mar 19, 2008 1:40 am    Post subject: Reply with quote  Mark this post and the followings unread

I recommend creating a base class with the desired member functions and extending it. Then you can write functions that take the base class as an argument and do their thing to it.
Code:
//This is the base class
class MyOscillator{
        Gain out;
        //These are supposed to be overriden
        fun float freq(){
                return 0.0;
        }
        fun float freq(float _freq){
                return _freq;
        }
}
//Let's extend it:
class MySawtooth extends MyOscillator{
        BlitSaw blitsaw => out;
        //Override:
        fun float freq(){
                return blitsaw.freq();
        }
        fun float freq(float _freq){
                _freq => blitsaw.freq;
                0 => blitsaw.harmonics; //Calculate harmonics up to nyquist
                return blitsaw.freq();
        }
}
class MyFilteredBlit extends MyOscillator{
        Blit blit => LPF lpf => out;
        //You can also set some basic parameters in the constructor
        4.0 => lpf.Q;
        //Even call the member functions
        220.0 => freq;
       
        //Override:
        fun float freq(){
                return blit.freq();
        }
        fun float freq(float _freq){
                _freq => blit.freq;
                3.0*_freq => lpf.freq;
                10 => blit.harmonics;
                return blit.freq();
        }
}

//Here's our function using the base class
fun void do_something (MyOscillator mo) {

          Std.rand2f( 100, 200) => mo.freq;
         
}
//create an instance:
MyFilteredBlit myOsc;
//MySawtooth myOsc; //You can try this one too

myOsc.out => dac; //connect it

//call function

while(true) {
 
      do_something( myOsc );
      500::ms => now;
}


As an added bonus you'll be able to make neat packages out of your patches and you're not limited to chucks native ugens.

_________________
To boldly go where no man has bothered to go before.
Back to top
View user's profile Send private message
wppk



Joined: Jan 25, 2008
Posts: 31
Location: Los Angeles

PostPosted: Thu Mar 20, 2008 9:10 am    Post subject: Reply with quote  Mark this post and the followings unread

Hey thanks Frostburn. Now i'm cruisin' right along.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [3 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