|
With square brackets, you can get the values inside some objects.
The outcomes of the following examples can be checked with the calculator.
object ["Matrix hello", 10, 3]
object [5, 10, 3]
object ["Sound hello", 0, 10000]
object [23, 1, 10000]
object [23, 2, 10000]
object ["TableOfReal tokens", 5, 12]
object ["TableOfReal tokens", 5, "F1"]
object ["TableOfReal tokens", "\ct", "F1"]
object ["Table listeners", 3, "m3ae"]
object ["Table listeners", 3, 12]
object$ ["Table results", 3, "response"]
object$ ["Table results", 3, 12]
object ["PitchTier hello", 8]
Cells (or samples, or points) outside the objects are considered to contain zeroes.
The values inside some objects can be interpolated.
object ("Sound hello", 0.7, 0)
object ("Sound hello", 0.7, 1)
object ("Sound hello", 0.7, 2)
object ("Spectrogram hallo", 0.7, 2500)
object ("PitchTier hullo", 0.7)
In the interpolation, times outside the time domain of the objects are considered to contain zeroes (this does not apply to PitchTiers and the like, which undergo constant extrapolation).
Suppose you want to do the difficult way of reversing the contents of a Sound called hello
(the easy way is to choose Reverse from the Modify menu). You select this sound, then choose Copy... to duplicate it to a new Sound, which you name hello_reverse
. You select this new Sound and choose Formula... from the Modify menu. The formula will be
object ["Sound hello", row, ncol + 1 - col]
From this example, you see that the indices between [ ] may be formulas themselves, and that you can use implicit attributes like ncol and position references like col (also row, which here means that the reversal is performed for each channel). An alternative formula is
object ("Sound hello", xmax - x, y)
at least if xmin is zero. The advantage of the second method is that it also works correctly if the two sounds have different sampling frequencies; the disadvantage is that it may do some interpolation between the samples, which deteriorates the sound quality (the use of y here means that the reversal is done for all y values, i.e. all channels).
In scripts, the indices between [ ] and the values between ( ) may be formulas themselves and contain variables. The following script computes the sum of all the cells along the diagonal of a Matrix.
matrix = Create simple matrix: 10, 10, "x*y"
sumDiagonal = 0
for i to object[matrix].ncol
sumDiagonal += object [matrix, i, i]
endfor
writeInfoLine: "The sum of the cells along the diagonal is ", sumDiagonal, "."
This example could have been written completely with commands from the dynamic menu:
matrix = Create simple matrix: 10, 10, "x*y"
sumDiagonal = 0
ncol = Get number of columns
for i to ncol
value = Get value in cell: i, i
sumDiagonal += value
endfor
writeInfoLine: "The sum of the cells along the diagonal is ", sumDiagonal, "."
The first version, which accesses the contents directly, is not only two lines shorter, but also three times faster.
© ppgb 20170614