Old 07-22-2017, 01:52 AM   #1
bezusheist
Human being with feelings
 
bezusheist's Avatar
 
Join Date: Nov 2010
Location: Mullet
Posts: 829
Default "counting" in JSFX

i need help to understand how "counting" works for JS script.

for example...i want to make a pulse every second, on the second.
i thought it was pretty straight forward, but i run into a problem when i try and do this sort of thing...

Code:
@sample

t += 1;
t == srate ?
(
spl0 = 1;
t = 0;
);
that "pulse" seems to be 1 sample early (i assume it should land on sample #44,100 but it lands on #44,099...(44.1kHz sr)
but if i do the following...

Code:
@sample

t += 1;
t == 1 ?
(
spl0 = 1;
);
t == srate ?
(
t = 0;
);
that "pulse" is right on time. (every second on the second)

what is wrong with my "logic" in the first code ?
(i guess i don't understand how things are supposed to work for this...)

thanks
__________________
I like turtles
bezusheist is offline   Reply With Quote
Old 07-22-2017, 11:48 AM   #2
jcjr
Human being with feelings
 
Join Date: Dec 2015
Location: SE TN USA
Posts: 77
Default

Hi, I think it is similar to the issues using zero-based arrays vs one-based arrays.

To process all elements of an array which begins at ary[1] -- for i = 1 to sizeof(ary)

To process all elements of an array which begins at ary[0] -- for i = 0 to (sizeof(ary) - 1)

In your first example you increment the counter t before comparing against samplerate. So every time thru the loop, when the code gets to the compare line-- At sample 0, t = 1. At sample = 44099, t = 44100 and the compare sends the 1 second pulse signal and resets the counter. All the pulse signals would be at 1 seond intervals but all the pulses are offset 1 sample early.

One fix is to increment counter t AFTER you do the comparison.

Code:
@sample
  //use >= to make sure some error can't accidentally miss 
  //  the threshold and quit sending pulses
  t >= srate ? 
  (
    spl0 = 1;
    t = 0;
  );
  t += 1;

Last edited by jcjr; 07-22-2017 at 11:56 AM.
jcjr is offline   Reply With Quote
Old 07-22-2017, 03:33 PM   #3
bezusheist
Human being with feelings
 
bezusheist's Avatar
 
Join Date: Nov 2010
Location: Mullet
Posts: 829
Default

thanks !
i was not aware that sample "0" was actually a sample, i assumed it started at 1.
__________________
I like turtles
bezusheist is offline   Reply With Quote
Old 07-22-2017, 07:36 PM   #4
jcjr
Human being with feelings
 
Join Date: Dec 2015
Location: SE TN USA
Posts: 77
Default

Quote:
Originally Posted by bezusheist View Post
thanks !
i was not aware that sample "0" was actually a sample, i assumed it started at 1.
It can be numbered however you like so long as it is consistent.

Some languages seem "most naturally" zero-based, and other languages seemed to attract more programmers who like to start numbering array elements at 1. Some languages you can define arrays starting and ending at any number you like. I'm not a scholar on computer languages, especially newer ones. I usually tried to always use zero-based indexing just to help avoid errors. Trying to avoid accidentally handling a 1-based array as an 0-based array or vice versa.

At 44100 samplerate-- With 0-based indexing the first trigger is at sample 0 and the second trigger is at sample 44100. With 1-based indexing the first trigger is at sample 1 and the second trigger is at sample 44101.

Such issues ought to be very simple but it is easy to get confused and make mistakes.

One argument I favor for starting to number samples at sample 0-- I think it most logical to number seconds or milli, micro, or nanoseconds starting at 0. The song starts at time = 0 and then 1 nanosecond later the song is at 1 nanosecond, etc. Song starts at 0 and then 1 second later it is at 1 second. It would seem weird to me for song start to be at 1 second, or 1 millisecond, or 1 nanosecond.

But someone else might prefer to number the song start at 1 nanosecond or 1 second or whatever.

If going for 0-based time then it would be confusing to me to start counting samples from 1. Conversions between 0-based time and 1-based samples would make me confused and encourage me to make even more dumb mistakes than usual!
jcjr is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 10:40 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.