Author |
Message |
Dr. Spankenstein
Joined: Mar 03, 2007 Posts: 136 Location: Cambridge
Audio files: 1
|
Posted: Tue Mar 06, 2007 11:01 am Post subject:
How can I set the buf.pos as a float? - Please help |
|
|
I looking to change the buffer position as I'm playing back certain parts of a wav file. However, I cant seem to find a way to adjust the buffer's position buf.pos to represent a float.
There must be a way of doing this as it seems a little backwards otherwise?
Thanks
Rhys |
|
Back to top
|
|
|
Kassen
Janitor
Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Tue Mar 06, 2007 1:01 pm Post subject:
|
|
|
Well;
buf.pos() should return the current position in samples (as a int since we don't have partial samples; interpolation not withstanding.
buf.samples() should return the length in samples (same deal but it's more obvious that it will be a int).
Deviding the length by the current position should get you a number between 0 and 1 that will already be a float. If you are going the opposite direction you need to remember to cast to int or ChucK will complain (because there are no partial samples, again).
This actually isn't backwards at all, it's just a perspective and a very usefull one at that.
Does that help? _________________ Kassen |
|
Back to top
|
|
|
Dr. Spankenstein
Joined: Mar 03, 2007 Posts: 136 Location: Cambridge
Audio files: 1
|
Posted: Wed Mar 07, 2007 9:25 am Post subject:
|
|
|
If I'm being honest I'm still not understanding this procedure.
I can have all the accuracy of a float until I come to relate it to buf.pos as understandably Chuck doesnt interpolate between samples making .5 of a sample a non existant thing.
So how would I round up to the nearest sample when >.5 and down to the nearest sample when <.5? Therefore making it an int that buf.pos would understand?
Or is there a much simpler way to read from different parts of the buffer and I'm making this a bit convoluted?
Thanks
Rhys |
|
Back to top
|
|
|
Kassen
Janitor
Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Wed Mar 07, 2007 1:37 pm Post subject:
|
|
|
Dr. Spankenstein wrote: | If I'm being honest I'm still not understanding this procedure. |
No problem, we'll get there.
Quote: | I can have all the accuracy of a float until I come to relate it to buf.pos as understandably Chuck doesnt interpolate between samples making .5 of a sample a non existant thing.
|
Right, exactly.
Quote: | So how would I round up to the nearest sample when >.5 and down to the nearest sample when <.5? Therefore making it an int that buf.pos would understand?
|
You can simply cast to int which will round it for you as it goes;
153.421 $ int => myugen.parameter;
(here the dollar is pronounced as "cast to")
If you realy need that much controll over rounding the standard library has more speciffic ways of rounding in it's math functions. If those won't do you could even perform a modulus 1 on your float, then base conclusions on the remainder. At that point we are talking about very slight differences though; rounding up or down by half a sample refers to a very short period of time and detailed controll is only nesicary in some cases... But yeah, if you need that it's there for you.
Quote: |
Or is there a much simpler way to read from different parts of the buffer and I'm making this a bit convoluted?
|
I'm not realy sure what you are going for here. I find the buf to be very simple myself. It's dealt with as just a string of values; you specifie where you want to start and at what rate you want to play it back and everything follows from that.
If you could be more precise in what you need we can try to figure out a way to get there. _________________ Kassen |
|
Back to top
|
|
|
|