Search in sources :

Example 6 with AudioRecordingStream

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

the class JSMinim method getAudioRecordingStream.

public AudioRecordingStream getAudioRecordingStream(String filename, int bufferSize, boolean inMemory) {
    // TODO: deal with the case of wanting to have the file fully in memory
    AudioRecordingStream mstream = null;
    AudioInputStream ais = getAudioInputStream(filename);
    if (ais != null) {
        if (inMemory && ais.markSupported()) {
            ais.mark((int) ais.getFrameLength() * ais.getFormat().getFrameSize());
        }
        debug("Reading from " + ais.getClass().toString());
        debug("File format is: " + ais.getFormat().toString());
        AudioFormat format = ais.getFormat();
        // they need to be converted to PCM
        if (format instanceof MpegAudioFormat) {
            AudioFormat baseFormat = format;
            format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
            // converts the stream to PCM audio from mp3 audio
            AudioInputStream decAis = getAudioInputStream(format, ais);
            // source data line is for sending the file audio out to the
            // speakers
            SourceDataLine line = getSourceDataLine(format, bufferSize);
            if (decAis != null && line != null) {
                Map<String, Object> props = getID3Tags(filename);
                long lengthInMillis = -1;
                if (props.containsKey("duration")) {
                    Long dur = (Long) props.get("duration");
                    if (dur.longValue() > 0) {
                        lengthInMillis = dur.longValue() / 1000;
                    }
                }
                MP3MetaData meta = new MP3MetaData(filename, lengthInMillis, props);
                mstream = new JSMPEGAudioRecordingStream(this, meta, ais, decAis, line, bufferSize);
            }
        } else // format instanceof MpegAudioFormat
        {
            // source data line is for sending the file audio out to the
            // speakers
            SourceDataLine line = getSourceDataLine(format, bufferSize);
            if (line != null) {
                long length = AudioUtils.frames2Millis(ais.getFrameLength(), format);
                BasicMetaData meta = new BasicMetaData(filename, length, ais.getFrameLength());
                mstream = new JSPCMAudioRecordingStream(this, meta, ais, line, bufferSize);
            }
        }
    // else
    }
    // ais != null
    return mstream;
}
Also used : MpegAudioFormat(javazoom.spi.mpeg.sampled.file.MpegAudioFormat) AudioInputStream(javax.sound.sampled.AudioInputStream) AudioRecordingStream(ddf.minim.spi.AudioRecordingStream) SourceDataLine(javax.sound.sampled.SourceDataLine) AudioFormat(javax.sound.sampled.AudioFormat) MpegAudioFormat(javazoom.spi.mpeg.sampled.file.MpegAudioFormat)

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