Minim
core | ugens | analysis
 

MultiChannelBuffer

Methods

MultiChannelBuffer represents a chunk of multichannel (or mono) audio data. It is primarily used internally when passing buffers of audio around, but you will need to create one to use things like the loadFileIntoBuffer method of Minim and the setSample method of Sampler. When thinking about a buffer of audio we usually consider how many sample frames long that buffer is. This is not the same as the actual number of values stored in the buffer. Mono, or single channel audio, contains one sample per sample frame, but stereo is two, quadraphonic is four, and so forth. The buffer size of a MultiChannelBuffer is how many sample frames it stores, so when retrieving and setting values it is required to indicate which channel should be operated upon.

Constructors

Construct a MultiChannelBuffer, providing a size and number of channels.
MultiChannelBuffer(int bufferSize, int numChannels)

Parameters

bufferSize — int: The length of the buffer in sample frames.
numChannels — int: The number of channels the buffer should contain.

Related

Minim

Example

/**
  * This sketch demonstrates how to use the loadFileIntoBuffer method of the Minim class and is also a good 
  * reference for some of the methods of the MultiChannelBuffer class. When the sketch begins it loads 
  * a file from the data folder into a MultiChannelBuffer and then modifies that sample data before 
  * using it to create a Sampler UGen. You can hear the result of this modification by hitting 
  * the space bar.
  * <p>
  * For more information about Minim and additional features, 
  * visit http://code.compartmental.net/minim/
  */

import ddf.minim.*;
import ddf.minim.ugens.*;

Minim              minim;
MultiChannelBuffer sampleBuffer;

AudioOutput        output;
Sampler            sampler;

void setup()
{
  size(512, 200, P3D);
  
  // create Minim and an AudioOutput
  minim  = new Minim(this);
  output = minim.getLineOut();
  
  // construct a new MultiChannelBuffer with 2 channels and 1024 sample frames.
  // in our particular case, it doesn't really matter what we choose for these
  // two values because loadFileIntoBuffer will reconfigure the buffer 
  // to match the channel count and length of the file.
  sampleBuffer     = new MultiChannelBuffer( 1, 1024 );
  
  // we pass the buffer to the method and Minim will reconfigure it to match 
  // the file. if the file doesn't exist, or there is some other problen with 
  // loading it, the function will return 0 as the sample rate.
  float sampleRate = minim.loadFileIntoBuffer( "SD.wav", sampleBuffer );
  
  // make sure the file load worked
  if ( sampleRate > 0 )
  {
    // double the size of the buffer to give ourselves some silence to play with
    int originalBufferSize = sampleBuffer.getBufferSize();
    sampleBuffer.setBufferSize( originalBufferSize * 2 );
    
    // go through first half of the buffer, which contains the original sample,
    // and add a delayed version of each sample at some random position.
    // we happen to know that the source file is only one channel
    // but in general you'd want to iterate over all channels when doing something like this
    for( int s = 0; s < originalBufferSize; ++s )
    {
      int   delayIndex  = s + int( random( 0, originalBufferSize ) );
      float sampleValue = sampleBuffer.getSample( 0, s );
      float destValue   = sampleBuffer.getSample( 0, delayIndex ); 
      sampleBuffer.setSample( 0, // channel
                              delayIndex, // sample frame to set
                              sampleValue + destValue // the value to set
                            );
    }
    
    // create a sampler that will use our buffer to generate audio.
    // we must provide the sample rate of the audio and the number of voices. 
    sampler = new Sampler( sampleBuffer, sampleRate, 1 );
    
    // and finally, connect to the output so we can hear it
    sampler.patch( output );
  }
}

void draw()
{
  background(0);
  stroke(255);
  
  // use the mix buffer to draw the waveforms.
  for (int i = 0; i < output.bufferSize() - 1; i++)
  {
    float x1 = map(i, 0, output.bufferSize(), 0, width);
    float x2 = map(i+1, 0, output.bufferSize(), 0, width);
    line(x1, 50 - output.left.get(i)*50, x2, 50 - output.left.get(i+1)*50);
    line(x1, 150 - output.right.get(i)*50, x2, 150 - output.right.get(i+1)*50);
  }
}

void keyPressed() 
{
  if ( key == ' ' && sampler != null )
  {
    sampler.trigger();
  }
}

Usage

Web & Application