| Author |
Message |
jbum

Joined: Oct 24, 2006 Posts: 24 Location: Los Angeles
|
Posted: Sat Nov 04, 2006 12:04 am Post subject:
Casting time or dur to a float or int. |
 |
|
I'd like to be able to compute a parameter for Math.sin() that is based on the passage of time, but thus far, my attempts to put times or durations into floats or ints haven't worked.
Is there a way to do something like this?
| Code: |
SinOsc voc => dac;
now => time startTime;
// time elapses...
now - startTime => dur elapsed;
// THIS IS THE PROBLEM LINE...
(Math.sin(Math.pow(elapsed,2)) *1000 + 20) => voc.freq;
|
I realize I can accomplish this using a SinOsc instead of Math.sin, but nonetheless, it would be nice to be able to use time as a parameter for Math functions... _________________ <a href="http://www.krazydad.com/">krazydad.com</a>
<a href="http://www.coverpop.com/">coverpop.com</a> |
|
|
Back to top
|
|
 |
ge

Joined: Aug 13, 2006 Posts: 105 Location: Palo Alto, CA
|
Posted: Sat Nov 04, 2006 2:36 am Post subject:
Re: Casting time or dur to a float or int. |
 |
|
| jbum wrote: | I'd like to be able to compute a parameter for Math.sin() that is based on the passage of time, but thus far, my attempts to put times or durations into floats or ints haven't worked.
Is there a way to do something like this? |
Sure can, I think. It's all about making the unit come out. Just divide the duration or even time value by any duration, and get the result as float.
| Code: | | Math.sin(Math.pow(elapsed/ms, 2)) => ... |
One can also compute the sample rate this way.
| Code: | | <<< "sample rate:", second/samp >>>; |
Hope this helps.
Ge! |
|
|
Back to top
|
|
 |
cbit

Joined: Dec 01, 2005 Posts: 35
|
Posted: Fri Nov 06, 2009 3:45 am Post subject:
|
 |
|
I'm trying to convert a duration to a float representig a number of milliseconds. Here's my first confused attempt to do this based on the comments above:
| Code: | 10::ms=>dur d;
d/1.0 => float f; |
"cannot resolve operator '=>' on types 'dur' and 'float'"
Any help would be appreciated!
Edit:
Ah! now i understand the use of the built in ms type to do the conversion. The following works:
| Code: | 10::ms=>dur d;
d/ms => float f;
<<< f >>>; |
_________________ http://basementhum.blogspot.com |
|
|
Back to top
|
|
 |
Antimon

Joined: Jan 18, 2005 Posts: 3371 Location: Sweden
Audio files: 211
G2 patch files: 92
|
Posted: Fri Nov 06, 2009 4:46 am Post subject:
|
 |
|
Syntaxwise, I think you can look at a solitary <time unit> (what I'm calling time unit is what's after the double colon in expressions like 2::second or 365::day - second and day respectively) as shorthand for 1::<time unit>. I.e., this is a valid expression:
And since a duration divided by another duration always yields a float result, myTime/second will convert myTime from a ChucK duration to an amount of seconds as a float. This is also useful if you want to divide time into a count of some other arbitrary length in time, you can do that similarly:
| Code: | | myTime/250::ms => float beatCount; |
I do this a lot when coding sequencers in ChucK.
/Stefan _________________ Antimon's Window
@soundcloud @Flattr @myspace A blog home - you can't explain music |
|
|
Back to top
|
|
 |
cbit

Joined: Dec 01, 2005 Posts: 35
|
Posted: Fri Nov 06, 2009 4:49 am Post subject:
|
 |
|
Thanks for the additional information stefan, that's clearer now. _________________ http://basementhum.blogspot.com |
|
|
Back to top
|
|
 |
kijjaz

Joined: Sep 20, 2004 Posts: 765 Location: bangkok, thailand
Audio files: 4
|
Posted: Fri Nov 06, 2009 11:56 am Post subject:
|
 |
|
| Thanks everyone and thanks to Ge also. |
|
|
Back to top
|
|
 |
|