Extracting samples from audio clips

### 27.2.8 Extracting samples from audio clips

The channel_data command gets samples from an audio clip.

- channel_data takes one mandatory argument and up to two optional argument:
  - A, an audio clip.
  - Optionally options, which can be the following (the order is unimportant):
    - n, a postive integer (channel number, zero-based) or matrix, the symbol.
    - range=[offset,length] for nonnegative integer offset and positive integer length, or range=a..b for floating point numbers a and b.
    Instead of channel number n, the symbol left resp. right may be used to specify the left resp. right channel.
- channel_data(A ⟨,options⟩) returns data from the channels as a sequence of lists. The returned sample values are all within the interval [−1.0,1.0], i.e. the amplitude of the returned signal is relative. The maximum possible amplitude is represented by the value 1.0.
  - With no options, data from all channels are returned as a sequence of lists.
  - With the option n (the channel number) or if there is only one channel, only data from this channel is returned in a single list.
  - With the option matrix, the lists representing the channel data are returned as the rows of a matrix.
  - With the option range=[offset,length], the block of length samples is extracted starting at offset.
  - With the option range=a..b, with floating point numbers a and b, then a and b are bounds in seconds.
- Alternatively, channel data can be extracted by using the [] operator on an audio clip A with one to three arguments:
  - n, a channel number (left and right are allowed).
  - Optionally, offset, an integer specifying the offset in samples (by default, offset=0).
  - Optionally, len, a positive integer (by default, len=L−offset, where L is the length of A in samples).
  Instead of specifying offset and len parameters, you can enter an interval t1..t2 as the second argument. In that case, the channel data between timestamps t1 and t2 (in seconds) is returned. The floor command is used for rounding the corresponding offsets during conversion to sample units.

#### Example

Assuming that the directory sounds contains example.wav, a wav file with three seconds of stereo sound, input:

snd:=readwav("/path/to/sounds/example.wav"):; L:=channel_data(snd,left,range=1.2..1.5)

Output: A list L resp. R containing data between 1.2 and 1.5 seconds in the left resp. right channel of the original file.

L:=snd[left,27400,100]

Output: A list of 100 samples from the left channel, starting from the sample with index 27400.

L:=snd[left,1.2..1.5]

Output: A list of samples from the left channel falling between 1.2 and 1.5 seconds.