Tonejs / Tone.js

A Web Audio framework for making interactive music in the browser.
https://tonejs.github.io
MIT License
13.37k stars 976 forks source link

What is `"s" | "n" | "t" | "m" | "i" | "hz" | "tr"`? #1135

Closed braebo closed 1 year ago

braebo commented 1 year ago

I've looked high and low, but I can't seem to locate the documentation describing what each time unit is. The most I've found is the type declaration in the source code. Sorry if I overlooked someplace obvious!

Wasn't sure where else to post this as I saw there's no Github Discussion sections, Discord channel or active Subreddit for the Tone community to ask questions.

dirkk0 commented 1 year ago

This? https://github.com/Tonejs/Tone.js/wiki/Time

braebo commented 1 year ago

That didn't seem to provide the clarity I'm after. Time() function's second argument is a TimeBaseUnit, which is defined as 9 options: "s" | "n" | "t" | "m" | "i" | "hz" | "tr" | "samples" | "number".

Your link describes 6 options: Numbers | Notation | Transport Time | Frequency | Ticks | Now-Relative

I'm looking for the documentation that describes what each letter of TimeBaseUnit is meant to represent. The documentation for TimeBaseUnit just says The units that the TimeBase can accept.

braebo commented 1 year ago

My confusion, more specifically:

I'd be happy to make a PR documenting these once I can sort out which is which.

paulmelnikow commented 1 year ago

Many of these are explained through example in that linked documentation.

braebo commented 1 year ago

Yes but neither the docs nor that link explicitly define what each of those strings represent, and I'm having a bit of trouble guessing. Some I can guess with reasonable confidence, like "s" as seconds and "n" as notation. "t" I thought was ticks, but the link suggests "i" is ticks. So perhaps "t" is transport time. But tr is odd and more likely related to transport so either "t" is just the default time which is descibed as seconds meaning my initial guess of s is wrong, or I'm completely stumped on what "t" is. "m" is used in both notation and now-relative examples, but assuming "n" is notation, "m" is probably now-relative which is weird because the the words now and relative don't contain the letter m. "hz" is clearly frequency, but I think it might be helpful to include a breif list in the tsdoc comments defining each letter to clear up any ambiguity for slower apes like myself.

bjarkihall commented 1 year ago

s: seconds (default unit for numerical values) n: note-values (8n is an eight-note) t: triplets (8t is an eight-note triplet) m: measures i: ticks (relative to the Transport's PPQ) hz: hertz/frequency tr: transport time

This was all listed in the link dirkk0 provided but you can also see how these values are converted back and forth in TimeBase.ts (see _getFromExpressions and fromType).

The TS type is just for the names of the units that can be parsed from the string format, I think the documentation site describes it more generally (and I think it's better than focusing strictly on the TS type).

Now-relative is described in the link as Prefix any of the above with "+" and it will be interpreted as "the current time plus whatever expression follows", so it's the plus-sign at the start of the string, it's not denoted with an alphabetical letter.

Notation is also just the concept of all of the notational units (in contrast to seconds/frequency/etc.), dotting a note for example is expressed with a dot symbol but it relies on a unit like n to be present before it.

braebo commented 1 year ago

Thanks @bjarkihall this is very helpful! The link does explain a lot, but it didn't help me map each one to the correct typescript type without a little guesswork which is what I was trying to avoid. TimeBase.ts is helpful as well. Thanks a lot for this!