Get sample number from time...

A command that becomes available in the Query menu if you select a Sound or LongSound object.

The Info window will tell you the sample number belonging to the time that you specify. The result is presented as a real number.

Setting

Time (s)
the time (in seconds) for which you want to know the sample number.

Example

If the sound has a sampling frequency of 10 kHz, the sample number associated with a time of 0.1 seconds will usually be 1000.5.

Scripting

You can use this command to put the nearest sample number into a script variable:

selectObject: "Sound hallo"
sampleNumber = Get sample number from time... 0.1
nearestSample = round (sampleNumber)

In this case, the value will not be written into the Info window. To round down or up, use

leftSample = floor (sampleNumber)
rightSample = ceiling (sampleNumber)

Algorithm

the result is

1 + (timet1) / Δt

where t1 is the time associated with the first sample, and Δt is the sampling period.

Details for hackers

If you select a Sound or LongSound and click Inspect, you can see how the relation between sample numbers and times is stored in the object: t1 is the x1 attribute, and Δt is the dx attribute.


© ppgb, April 21, 2014