Search in sources :

Example 1 with AudioRecordingStream

use of ddf.minim.spi.AudioRecordingStream in project Minim by ddf.

the class Minim method loadMetaData.

/**
	 * Load the metadata for the file without keeping a stream open. 
	 * Use this to get access to ID3 tags or similar.
	 * 
	 * @example Basics/GetMetaData
	 * 
	 * @param filename
	 * 			String: the name of the file to load
	 * @return
	 * 			AudioMetaData: the metadata for the file
	 */
public AudioMetaData loadMetaData(String filename) {
    AudioRecordingStream stream = mimp.getAudioRecordingStream(filename, 0, false);
    AudioMetaData data = stream.getMetaData();
    stream.close();
    return data;
}
Also used : AudioRecordingStream(ddf.minim.spi.AudioRecordingStream)

Example 2 with AudioRecordingStream

use of ddf.minim.spi.AudioRecordingStream in project Minim by ddf.

the class Minim method loadFileStream.

/**
	 * Loads the file into an AudioRecordingStream, which allows you to stream 
	 * audio data from the file yourself. Note that doing this will not 
	 * result in any sound coming out of your speakers, unless of course you
	 * send it there. You would primarily use this to perform offline-analysis
	 * of a file or for very custom sound streaming schemes.
	 * 
	 * @shortdesc Loads the file into an AudioRecordingStream.
	 * 
	 * @example Analysis/offlineAnalysis
	 * 
	 * @param filename
	 *            the file to load
	 * @param bufferSize
	 *            int: the bufferSize to use, which controls how much 
	 *            of the streamed file is stored in memory at a time.
	 * @param inMemory
	 *            boolean: whether or not the file should be cached in memory as it is read
	 *            
	 * @return an AudioRecordingStream that you can use to read from the file.
	 * 
	 * 
	 */
public AudioRecordingStream loadFileStream(String filename, int bufferSize, boolean inMemory) {
    AudioRecordingStream stream = mimp.getAudioRecordingStream(filename, bufferSize, inMemory);
    streams.add(stream);
    return stream;
}
Also used : AudioRecordingStream(ddf.minim.spi.AudioRecordingStream)

Example 3 with AudioRecordingStream

use of ddf.minim.spi.AudioRecordingStream in project Minim by ddf.

the class Minim method loadFileIntoBuffer.

/**
	 * 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
        long totalSampleCount = stream.getSampleFrameLength();
        if (totalSampleCount == -1) {
            totalSampleCount = AudioUtils.millis2Frames(stream.getMillisecondLength(), stream.getFormat());
        }
        debug("Total sample count for " + filename + " is " + totalSampleCount);
        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));
            }
            int samplesRead = stream.read(readBuffer);
            if (samplesRead == 0) {
                debug("loadSampleIntoBuffer: got 0 samples read");
                break;
            }
            // 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 < samplesRead; ++s) {
                    outBuffer.setSample(i, (int) totalSamplesRead + s, readBuffer.getSample(i, s));
                }
            }
            totalSamplesRead += samplesRead;
        }
        if (totalSamplesRead != totalSampleCount) {
            outBuffer.setBufferSize((int) totalSamplesRead);
        }
        debug("loadSampleIntoBuffer: final output buffer size is " + outBuffer.getBufferSize());
        stream.close();
    } else {
        debug("Unable to load an AudioRecordingStream for " + filename);
    }
    return sampleRate;
}
Also used : AudioRecordingStream(ddf.minim.spi.AudioRecordingStream)

Example 4 with AudioRecordingStream

use of ddf.minim.spi.AudioRecordingStream in project Minim by ddf.

the class Minim method loadFile.

/**
	 * Loads the requested file into an {@link AudioPlayer} with the request
	 * buffer size.
	 * 
	 * @param filename
	 *            the file or URL you want to load
	 * @param bufferSize
	 *            int: the sample buffer size you want, which determines the 
	 *            size of the left, right, and mix AudioBuffer fields of the 
	 *            returned AudioPlayer.
	 * 
	 * @return an <code>AudioPlayer</code> with a sample buffer of the requested
	 *         size, or null if we were unable to load the file
	 */
public AudioPlayer loadFile(String filename, int bufferSize) {
    AudioPlayer player = null;
    AudioRecordingStream rec = mimp.getAudioRecordingStream(filename, bufferSize, false);
    if (rec != null) {
        AudioFormat format = rec.getFormat();
        AudioOut out = mimp.getAudioOutput(format.getChannels(), bufferSize, format.getSampleRate(), format.getSampleSizeInBits());
        if (out != null) {
            player = new AudioPlayer(rec, out);
        } else {
            rec.close();
        }
    }
    if (player != null) {
        addSource(player);
    } else {
        error("Couldn't load the file " + filename);
    }
    return player;
}
Also used : AudioRecordingStream(ddf.minim.spi.AudioRecordingStream) AudioFormat(javax.sound.sampled.AudioFormat) AudioOut(ddf.minim.spi.AudioOut)

Example 5 with AudioRecordingStream

use of ddf.minim.spi.AudioRecordingStream in project Minim by ddf.

the class AudioRecordingStreamLoop method Start.

void Start(String[] args) {
    fileFolder = args[0];
    minim = new Minim(this);
    AudioRecordingStream recording = minim.loadFileStream(args[1]);
    int loopCount = 1;
    // return -1 for the current test mp3, which means i probably need to test with a wav
    long expectedReads = recording.getSampleFrameLength() * (loopCount + 1);
    long reads = 0;
    recording.loop(loopCount);
    while (recording.isPlaying() && reads < expectedReads) {
        if (reads == 743041) {
            System.out.println("..");
        }
        recording.read();
        ++reads;
        if (recording.getLoopCount() == -1) {
            System.err.println("Loop count became -1 after " + reads + " reads!");
            break;
        }
    }
    if (expectedReads != reads) {
        System.err.println("Expected " + expectedReads + " reads, and made " + reads);
    } else if (recording.isPlaying()) {
        System.err.println("Recording is still playing after expected number of read.");
    }
}
Also used : AudioRecordingStream(ddf.minim.spi.AudioRecordingStream) Minim(ddf.minim.Minim)

Aggregations

AudioRecordingStream (ddf.minim.spi.AudioRecordingStream)6 AudioFormat (javax.sound.sampled.AudioFormat)2 Minim (ddf.minim.Minim)1 AudioOut (ddf.minim.spi.AudioOut)1 AudioInputStream (javax.sound.sampled.AudioInputStream)1 SourceDataLine (javax.sound.sampled.SourceDataLine)1 MpegAudioFormat (javazoom.spi.mpeg.sampled.file.MpegAudioFormat)1