- Processing
- p5.js
- Processing.py
- Processing for Android
- Processing for Pi

- Processing Foundation

This reference is for Processing 3.0+. If you have a previous version, use the reference included with your software in the Help menu. If you see any errors or have suggestions, please let us know. If you prefer a more technical reference, visit the Processing Core Javadoc and Libraries Javadoc.

Class | AudioSample
Name | ### write()
Examples |
    import processing.sound.*;
    AudioSample sample;

    void setup() {
      size(640, 360);
      background(255);

      // Create a new audiosample
      sample = new AudioSample(this, 100000, 22050);

      // A freshly initiated audiosample contains nothing but zeros, so let's
      // write some data to it.
      for (int i = 0; i < sample.frames(); i++) {
        // Random numbers  will make it sound like white noise
        sample.write(i, random(-100, 100));
      }
      sample.play();
    }

    void draw() {
    }
Description | The underlying data of the audiosample can be read and (over)written in several different ways: the method taking a single float array `data` replaces the sample data with the content of the given array. The array has to contain as many floats as there are frames in this sample. It is also possible to only write parts of the sample data using the method with four arguments, which allows you to specify the index of the first frame to write, the position in the array to take the data from, as well as how many frames should be copied over. Finally, the method taking two arguments simply sets the value of the single audio frame specified by the first argument to the given float value.
Syntax |
    .write(data)
    .write(startFrame, data, startIndex, numFrames)
    .write(index, value)
Parameters |
data | float[]: the array from which the sample data, up to sample.frames() * sample.channels() floats, should be copied
startFrame | int: the index of the first frame of the audiosample that should be written to
startIndex | int: the position in the array that the first value to write should be taken from (typically 0)
numFrames | int: the number of frames that should be written (can't be greater than audiosample.channels() * data.length - startIndex)
index | int: the index of the single frame of the audiosample that should be set to the given value
value | float: the float value that the given audio frame should be set to
Returns | void

Updated on January 1, 2021 03:38:11am EST
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.