Name

read()

Class

AudioSample

Description

The underlying data of the audiosample can be read and written in several different ways: the method taking a single float array `data` gets the current sample data and write it into the given array. The array has to be able to store as many floats as there are frames in this sample. It is also possible to only read parts of the sample data using the method with four arguments, which allows you to specify the index of the first frame to read, the position in the array to write it to, as well as how many frames to copy over into the array in total. Finally, the method taking a single integer argument `index` returns the value of the single audio frame of the sample at this index as a float.

Examples

  • import processing.sound.*;
    AudioSample sample;
    
    void setup() {
      size(640, 360);
      background(255);
    
      // Create a new audiosample
      sample = new AudioSample(this, 100000, 22050);
    
      // Read some data from it (the following calls are just for demonstration,
      // a freshly initiated audiosample actually contains nothing but zeros)
    
      // Read the very first frame:
      float frame = sample.read(0);
    
      // Read the entire sample
      float[] sampleContent = new float[100000];
      sample.read(sampleContent);
    
      // Read only a part of the sample data
      float[] subSample = new float[50000];
      // Read 500000 frames, starting at frame 30000
      sample.read(30000, subSample, 0, 50000);
    }      
    
    void draw() {
    }
    

Syntax

  • audiosample.read(data)
  • audiosample.read(startFrame, data, startIndex, numFrames)
  • audiosample.read(frameIndex)
  • audiosample.read(frameIndex, channelIndex)

Parameters

  • data(float[])the target array that the read data is written to
  • startFrame(int)the index of the first frame of the audiosample that should be read
  • startIndex(int)the position in the array where the first read frame should be written to (typically 0)
  • numFrames(int)the number of frames that should be read (can't be greater than audiosample.channels() * data.length - startIndex)
  • frameIndex(int)the index of the single frame of the audiosample that should be read and returned. `frameIndex` has to be between 0 and `sample.frames() * sample.channels() - 1` (inclusive)`. For mono files, `read(frameIndex)` is identical to `read(frameIndex, 0)`. For stereo files, unless you also specify a `channelIndex`, `read(frameIndex)` will return the samples from both the left and right channel in interleaved order. (See the Soundfile > StereoSample example for a demonstration.)
  • channelIndex(int)the channel from which to extract the frame value (0 for left, 1 for right). `read(frameIndex, channelIndex)` is the same as calling `read(frameIndex * this.channels() + channelIndex)`.

Return

  • void or float