toSampleArray = function toSampleArray(audioBuffer) {

        // Track overall amount of data read
        var inSamples = audioBuffer.length;
        readSamples += inSamples;

        // Calculate the total number of samples that should be written as of
        // the audio data just received and adjust the size of the output
        // packet accordingly
        var expectedWrittenSamples = Math.round(readSamples * format.rate / audioBuffer.sampleRate);
        var outSamples = expectedWrittenSamples - writtenSamples;

        // Update number of samples written
        writtenSamples += outSamples;

        // Get array for raw PCM storage
        var data = new SampleArray(outSamples * format.channels);

        // Convert each channel
        for (var channel = 0; channel < format.channels; channel++) {

            var audioData = audioBuffer.getChannelData(channel);

            // Fill array with data from audio buffer channel
            var offset = channel;
            for (var i = 0; i < outSamples; i++) {
                data[offset] = interpolateSample(audioData, i / (outSamples - 1)) * maxSampleValue;
                offset += format.channels;
            }

        }

        return data;

    }
Converts the given AudioBuffer into an audio packet, ready for streaming
along the underlying output stream. Unlike the raw audio packets used by
this audio recorder, AudioBuffers require floating point samples and are
split into isolated planes of channel-specific data.

@private
@param {AudioBuffer} audioBuffer
    The Web Audio API AudioBuffer that should be converted to a raw
    audio packet.

@returns {SampleArray}
    A new raw audio packet containing the audio data from the provided
    AudioBuffer.