src/AudioPlayer.js
function splitAudioPacket(data) {

        var minValue = Number.MAX_VALUE;
        var optimalSplitLength = data.length;

        // Calculate number of whole samples in the provided audio packet AND
        // in the minimum possible split packet
        var samples = Math.floor(data.length / format.channels);
        var minSplitSamples = Math.floor(format.rate * MIN_SPLIT_SIZE);

        // Calculate the beginning of the "end" of the audio packet
        var start = Math.max(
            format.channels * minSplitSamples,
            format.channels * (samples - minSplitSamples)
        );

        // For all samples at the end of the given packet, find a point where
        // the perceptible volume across all channels is lowest (and thus is
        // the optimal point to split)
        for (var offset = start; offset < data.length; offset += format.channels) {

            // Calculate the sum of all values across all channels (the result
            // will be proportional to the average volume of a sample)
            var totalValue = 0;
            for (var channel = 0; channel < format.channels; channel++) {
                totalValue += Math.abs(data[offset + channel]);
            }

            // If this is the smallest average value thus far, set the split
            // length such that the first packet ends with the current sample
            if (totalValue <= minValue) {
                optimalSplitLength = offset + format.channels;
                minValue = totalValue;
            }

        }

        // If packet is not split, return the supplied packet untouched
        if (optimalSplitLength === data.length)
            return [data];

        // Otherwise, split the packet into two new packets according to the
        // calculated optimal split length
        return [
            new SampleArray(data.buffer.slice(0, optimalSplitLength * format.bytesPerSample)),
            new SampleArray(data.buffer.slice(optimalSplitLength * format.bytesPerSample))
        ];

    }
function splitAudioPacket(data) {

        var minValue = Number.MAX_VALUE;
        var optimalSplitLength = data.length;

        // Calculate number of whole samples in the provided audio packet AND
        // in the minimum possible split packet
        var samples = Math.floor(data.length / format.channels);
        var minSplitSamples = Math.floor(format.rate * MIN_SPLIT_SIZE);

        // Calculate the beginning of the "end" of the audio packet
        var start = Math.max(
            format.channels * minSplitSamples,
            format.channels * (samples - minSplitSamples)
        );

        // For all samples at the end of the given packet, find a point where
        // the perceptible volume across all channels is lowest (and thus is
        // the optimal point to split)
        for (var offset = start; offset < data.length; offset += format.channels) {

            // Calculate the sum of all values across all channels (the result
            // will be proportional to the average volume of a sample)
            var totalValue = 0;
            for (var channel = 0; channel < format.channels; channel++) {
                totalValue += Math.abs(data[offset + channel]);
            }

            // If this is the smallest average value thus far, set the split
            // length such that the first packet ends with the current sample
            if (totalValue <= minValue) {
                optimalSplitLength = offset + format.channels;
                minValue = totalValue;
            }

        }

        // If packet is not split, return the supplied packet untouched
        if (optimalSplitLength === data.length)
            return [data];

        // Otherwise, split the packet into two new packets according to the
        // calculated optimal split length
        return [
            new SampleArray(data.buffer.slice(0, optimalSplitLength * format.bytesPerSample)),
            new SampleArray(data.buffer.slice(optimalSplitLength * format.bytesPerSample))
        ];

    }
function splitAudioPacket ( data ) { var minValue = Number . MAX_VALUE ; var optimalSplitLength = data . length ; // Calculate number of whole samples in the provided audio packet AND // in the minimum possible split packet var samples = Math . floor ( data . length / format . channels ) ; var minSplitSamples = Math . floor ( format . rate * MIN_SPLIT_SIZE ) ; // Calculate the beginning of the "end" of the audio packet var start = Math . max ( format . channels * minSplitSamples , format . channels * ( samples - minSplitSamples ) ) ; // For all samples at the end of the given packet, find a point where // the perceptible volume across all channels is lowest (and thus is // the optimal point to split) for ( var offset = start ; offset < data . length ; offset += format . channels ) { // Calculate the sum of all values across all channels (the result // will be proportional to the average volume of a sample) var totalValue = 0 ; for ( var channel = 0 ; channel < format . channels ; channel ++ ) { totalValue += Math . abs ( data [ offset + channel ] ) ; } // If this is the smallest average value thus far, set the split // length such that the first packet ends with the current sample if ( totalValue <= minValue ) { optimalSplitLength = offset + format . channels ; minValue = totalValue ; } } // If packet is not split, return the supplied packet untouched if ( optimalSplitLength === data . length ) return [ data ] ; // Otherwise, split the packet into two new packets according to the // calculated optimal split length return [ new SampleArray ( data . buffer . slice ( 0 , optimalSplitLength * format . bytesPerSample ) ) , new SampleArray ( data . buffer . slice ( optimalSplitLength * format . bytesPerSample ) ) ] ; }
Given a single packet of audio data, splits off an arbitrary length of
audio data from the beginning of that packet, returning the split result
as an array of two packets. The split location is determined through an
algorithm intended to minimize the liklihood of audible clicking between
packets. If no such split location is possible, an array containing only
the originally-provided audio packet is returned.

@private
@param {SampleArray} data
The audio packet to split.

@returns {SampleArray[]}
An array of audio packets containing the result of splitting the
provided audio packet. If splitting is possible, this array will
contain two packets. If splitting is not possible, this array will
contain only the originally-provided packet.
Given a single packet of audio data splits off an arbitrary length of audio data from the beginning of that packet returning the split result as an array of two packets . The split location is determined through an algorithm intended to minimize the liklihood of audible clicking between packets . If no such split location is possible an array containing only the originally - provided audio packet is returned .