### 28.2.9 Setting samples in audio clips

The set_channel_data command sets the audio clip samples to desired values.

- set_channel_data takes two mandatory arguments and two optional arguments:
  - A, an audio clip.
  - data, a matrix or vector containing sample data.
  - Optionally, offset, a position in A at which data should be written (by default, offset=0).
  - Optionally, chn, a channel index or specification (left or right) (by default, the same data is written to all channels or data must be a matrix with one row for each channel).
- set_channel_data(A,data ⟨,offset,chn ⟩) returns a modified copy of A in which data is written to A at the desired offset and channel chn.
- If data is a matrix, then it must have n rows where n is the number of channels in A. In this case, the fourth argument should be dropped.
- If data is a vector, then it is written to the specified channel or to all channels if chn is not given.

#### Example

In the following example we create an audio clip containing the sine wave and add random noise to its portion.

snd:=createwav(sin(2*pi*440*soundsec(4)))

a sound clip with 176400 samples at 44100 Hz (16 bit, mono |  |  |  |  |  |  |  |  |  |

We extract the data after the first and before the third second of the audio.

data:=channel_data(snd,0,range=1.0..3.0):;

Now we create the noise which fades in and out.

noise:=hamming_window(ranv(length(data),normald(0,0.1))):;

Finally we replace the portion of the original clip with the mix of the sine wave and noise.

snd1:=set_channel_data(snd,noise+data,44100,0)

a sound clip with 176400 samples at 44100 Hz (16 bit, mono) |  |  |  |  |  |  |  |  |  |