/** * Loads the requested file into a MultiChannelBuffer. The buffer's channel count and buffer size * will be adjusted to match the file. * * @shortdesc Loads the requested file into a MultiChannelBuffer. * @example Advanced/loadFileIntoBuffer * @param filename the file to load * @param outBuffer the MultiChannelBuffer to fill with the file's audio samples * @return the sample rate of audio samples in outBuffer, or 0 if the load failed. * @related MultiChannelBuffer */ public float loadFileIntoBuffer(String filename, MultiChannelBuffer outBuffer) { final int readBufferSize = 4096; float sampleRate = 0; AudioRecordingStream stream = mimp.getAudioRecordingStream(filename, readBufferSize, false); if (stream != null) { stream.open(); stream.play(); sampleRate = stream.getFormat().getSampleRate(); final int channelCount = stream.getFormat().getChannels(); // for reading the file in, in chunks. MultiChannelBuffer readBuffer = new MultiChannelBuffer(channelCount, readBufferSize); // make sure the out buffer is the correct size and type. outBuffer.setChannelCount(channelCount); // how many samples to read total final long totalSampleCount = stream.getSampleFrameLength(); outBuffer.setBufferSize((int) totalSampleCount); // now read in chunks. long totalSamplesRead = 0; while (totalSamplesRead < totalSampleCount) { // is the remainder smaller than our buffer? if (totalSampleCount - totalSamplesRead < readBufferSize) { readBuffer.setBufferSize((int) (totalSampleCount - totalSamplesRead)); } stream.read(readBuffer); // copy data from one buffer to the other. for (int i = 0; i < channelCount; ++i) { // a faster way to do this would be nice. for (int s = 0; s < readBuffer.getBufferSize(); ++s) { outBuffer.setSample(i, (int) totalSamplesRead + s, readBuffer.getSample(i, s)); } } totalSamplesRead += readBuffer.getBufferSize(); } stream.close(); } else { debug("Unable to load an AudioRecordingStream for " + filename); } return sampleRate; }