Name

write()

Class

AudioSample

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.

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() {
    }
    

Syntax

  • audiosample.write(data)
  • audiosample.write(startFrame, data, startIndex, numFrames)
  • audiosample.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

Return

  • void