Buffer Creation

A Buffer holds audio samples at discrete sample periods. The Buffer object knows nothing about time, endianness or bit precision. It is a generic container of floating point data.

There are 3 general ways to create an Nsound Buffer:

  1. Creating an empty Buffer
  2. Call ones(), rand() or zeros()
  3. Reading a wavefile from disk

Creating An Empty Buffer

Call the constructor:

from Nsound import *
b = Buffer()

The new Buffer b is empty. Calling the getLength() method will return 0.

The underlying data structure that is held by the Buffer class is a std::vector. One can preallocate memory when creating a buffer by specify the number of samples to preallocate:

b = Buffer(1024)

The new Buffer b is empty, even though memory was preallocated. Calling the getLength() method will return 0.

In general, you don’t need to worry about preallocating memory. It is meant to be useful when implementing new features in Nsound when the size of Buffers are already known.

Call Ones, Rand or Zeros

The Buffer class includes some convience functions for creating Buffers that are filled with oness, random numbers or zeros:

Buffer.ones(n_samples)
Buffer.rand(n_samples)
Buffer.zeros(n_samples)

Example usage:

from Nsound import *
b1 = Buffer.ones(10)
b2 = Buffer.rand(10)
b3 = Buffer.zeros(10)

In the example above, 10 samples were stored in the created Buffers.

Reading A Wavefile From Disk

A Buffer can be created from a wavefile:

b = Buffer("california.wav")

The new Buffer b will contain all the samples in “california.wav”. If the wavefile has more than one channel, only the first channel is read and stored in the new Buffer.

The wavefile’s data will be converted into float64 with a range of (-1.0, 1.0.).

Table Of Contents

Previous topic

Nsound Data Types

Next topic

AudioStream Creation

This Page