[chuck-users] Float to Int

Perry Cook prc at cs.princeton.edu
Tue Jan 21 12:24:19 EST 2020


We also added Std.ftoi(x) to the language a while back (at 1.3.2.0).  It’s much clearer to read, and more obvious as to what’s going on and why, than the casting notation.  To round, just add 0.5 to x in the function arg.

so:

> Std.ftoi(0.4*myBuf.samples()+0.5) => myBuf.pos;

would set your play pointer to the 40% point in the sound buffer.

Prc

Sent from my iPhone

>   1. Conversion float to int using SndBuf (M?che?l ? Cath?in)
>  
> 40.0 => float pctPosInit;
> pctPosInit/100*myBuf.samples() => myBuf.pos;
> 
> 
> This throws the error argument types don't match.
> I'm not seeing how to convert the float to an int here.
> Thanks for your help!
> 
> Micheal
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://lists.cs.princeton.edu/pipermail/chuck-users/attachments/20200121/6b83ba90/attachment-0001.html>
> 
> ------------------------------
> 
> Message: 2
> Date: Tue, 21 Jan 2020 09:35:12 -0600
> From: Michael Heuer <heuermh at gmail.com>
> To: ChucK Users Mailing List <chuck-users at lists.cs.princeton.edu>
> Subject: Re: [chuck-users] Conversion float to int using SndBuf
> Message-ID: <20797EB6-C4BC-4D00-91B9-B9D4E65687C0 at gmail.com>
> Content-Type: text/plain;    charset=utf-8
> 
> Hello M?che?l,
> 
> The ChucK language has a cast operator '$' which might come in handy in this situation
> 
> (pctPosInit/100*myBuf.samples()) $ int => myBuf.pos;
> 
> Cheers,
> 
>   michael
> 
> 
>> On Jan 21, 2020, at 7:08 AM, M?che?l ? Cath?in <micheal.ocathain at gmail.com> wrote:
>> 
>> Hi
>> What is the best way to convert a float to int in the following please?
>> 
>> //I want to set myBuf.pos to a percentage of myBuf.samples()...
>> //Start playing at say 40% through the total number of samples.
>> 
>> 40.0 => float pctPosInit;
>> pctPosInit/100*myBuf.samples() => myBuf.pos;
>> 
>> 
>> This throws the error argument types don't match. 
>> I'm not seeing how to convert the float to an int here.
>> Thanks for your help!
>> 
>> Micheal
>> _______________________________________________
>> chuck-users mailing list
>> chuck-users at lists.cs.princeton.edu
>> https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
> 
> 
> 
> ------------------------------
> 
> Message: 3
> Date: Tue, 21 Jan 2020 16:47:40 +0100
> From: ermina <ermina at studioplume.com>
> To: chuck-users at lists.cs.princeton.edu
> Subject: Re: [chuck-users] Conversion float to int using SndBuf
> Message-ID: <bc93ce0d-c4bd-0616-2f3c-15562f9ff600 at studioplume.com>
> Content-Type: text/plain; charset=utf-8; format=flowed
> 
> Hi,
> 
> to convert a float to an int,
> you need to explicitly cast the value with
> 4.8 $ int => int foo;  // foo == 4
> (as written here: https://chuck.cs.princeton.edu/doc/language/oper.html)
> 
> So you would do something like
> pctPosInit $ int => int pctPosInitInt
> pctPosInitInt/100*myBuf.samples() => myBuf.pos;
> 
> . e
>> On 01/21/2020 02:08 PM, M?che?l ? Cath?in wrote:
>> Hi
>> What is the best way to convert a float to int in the following please?
>> 
>> //I want to set myBuf.pos to a percentage of myBuf.samples()...
>> //Start playing at say 40% through the total number of samples.
>> 
>> 40.0 => float pctPosInit;
>> pctPosInit/100*myBuf.samples() => myBuf.pos;
>> 
>> 
>> This throws the error argument types don't match.
>> I'm not seeing how to convert the float to an int here.
>> Thanks for your help!
>> 
>> Micheal
>> 
>> 
>> _______________________________________________
>> chuck-users mailing list
>> chuck-users at lists.cs.princeton.edu
>> https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
>> 
> 
> 
> ------------------------------
> 
> Message: 4
> Date: Tue, 21 Jan 2020 12:58:36 -0300
> From: Jean Menezes da Rocha <jean at menezesdarocha.info>
> To: ChucK Users Mailing List <chuck-users at lists.cs.princeton.edu>
> Subject: Re: [chuck-users] Conversion float to int using SndBuf
> Message-ID:
>    <CALRb6hJimYGzoV7tChQ=UGG5UVfTkQNRrD3UMn8nSeLpN0Jj1g at mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
> 
> I would suggest that you call Math.round() before casting, as to prevent
> undesirable effects: notice that in the example sent by ermina, 4.8 rounds
> down to 4 when cast to int, which could cause miscalculations *depending on
> your use case*.
> The previous example would look something like this if we used Math.round():
> 
> Math.round(pctPosInit) $ int => int pctPosInitInt;
> pctPosInitInt/100*myBuf.samples() => myBuf.pos;
> 
> Best regards!
> 
>> On Tue, Jan 21, 2020 at 12:49 PM ermina <ermina at studioplume.com> wrote:
>> 
>> Hi,
>> 
>> to convert a float to an int,
>> you need to explicitly cast the value with
>> 4.8 $ int => int foo;  // foo == 4
>> (as written here: https://chuck.cs.princeton.edu/doc/language/oper.html)
>> 
>> So you would do something like
>> pctPosInit $ int => int pctPosInitInt
>> pctPosInitInt/100*myBuf.samples() => myBuf.pos;
>> 
>> . e
>>> On 01/21/2020 02:08 PM, M?che?l ? Cath?in wrote:
>>> Hi
>>> What is the best way to convert a float to int in the following please?
>>> 
>>> //I want to set myBuf.pos to a percentage of myBuf.samples()...
>>> //Start playing at say 40% through the total number of samples.
>>> 
>>> 40.0 => float pctPosInit;
>>> pctPosInit/100*myBuf.samples() => myBuf.pos;
>>> 
>>> 
>>> This throws the error argument types don't match.
>>> I'm not seeing how to convert the float to an int here.
>>> Thanks for your help!
>>> 
>>> Micheal
>>> 
>>> 
>>> _______________________________________________
>>> chuck-users mailing list
>>> chuck-users at lists.cs.princeton.edu
>>> https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
>>> 
>> _______________________________________________
>> chuck-users mailing list
>> chuck-users at lists.cs.princeton.edu
>> https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
>> 
> 
> 
> -- 
> Jean Menezes da Rocha
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://lists.cs.princeton.edu/pipermail/chuck-users/attachments/20200121/b08c368e/attachment-0001.html>
> 
> ------------------------------
> 
> _______________________________________________
> chuck-users mailing list
> chuck-users at lists.cs.princeton.edu
> https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
> 
> 
> End of chuck-users Digest, Vol 173, Issue 10
> ********************************************



More information about the chuck-users mailing list