Search in sources :

Example 11 with SourceDataLine

use of javax.sound.sampled.SourceDataLine in project jdk8u_jdk by JetBrains.

the class JavaSoundAudioClip method createSourceDataLine.

private boolean createSourceDataLine() {
    if (DEBUG || Printer.debug)
        Printer.debug("JavaSoundAudioClip.createSourceDataLine()");
    try {
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, loadedAudioFormat);
        if (!(AudioSystem.isLineSupported(info))) {
            if (DEBUG || Printer.err)
                Printer.err("Line not supported: " + loadedAudioFormat);
            // fail silently
            return false;
        }
        SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info);
        datapusher = new DataPusher(source, loadedAudioFormat, loadedAudio, loadedAudioByteLength);
    } catch (Exception e) {
        if (DEBUG || Printer.err)
            e.printStackTrace();
        // fail silently
        return false;
    }
    if (datapusher == null) {
        // fail silently
        return false;
    }
    if (DEBUG || Printer.debug)
        Printer.debug("Created SourceDataLine.");
    return true;
}
Also used : DataLine(javax.sound.sampled.DataLine) SourceDataLine(javax.sound.sampled.SourceDataLine) SourceDataLine(javax.sound.sampled.SourceDataLine) InvalidMidiDataException(javax.sound.midi.InvalidMidiDataException) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) IOException(java.io.IOException) MidiUnavailableException(javax.sound.midi.MidiUnavailableException)

Example 12 with SourceDataLine

use of javax.sound.sampled.SourceDataLine in project Minim by ddf.

the class JSStreamingSampleRecorder method save.

/**
   * Finishes the recording process by closing the file.
   */
public AudioRecordingStream save() {
    try {
        aos.close();
    } catch (IOException e) {
        Minim.error("AudioRecorder.save: An error occurred when trying to save the file:\n" + e.getMessage());
    }
    String filePath = filePath();
    AudioInputStream ais = system.getAudioInputStream(filePath);
    SourceDataLine sdl = system.getSourceDataLine(ais.getFormat(), 1024);
    // this is fine because the recording will always be 
    // in a raw format (WAV, AU, etc).
    long length = AudioUtils.frames2Millis(ais.getFrameLength(), format);
    BasicMetaData meta = new BasicMetaData(filePath, length, ais.getFrameLength());
    JSPCMAudioRecordingStream recording = new JSPCMAudioRecordingStream(system, meta, ais, sdl, 1024);
    return recording;
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) SourceDataLine(javax.sound.sampled.SourceDataLine) IOException(java.io.IOException)

Example 13 with SourceDataLine

use of javax.sound.sampled.SourceDataLine 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)

Example 14 with SourceDataLine

use of javax.sound.sampled.SourceDataLine in project Minim by ddf.

the class JSMinim method getAudioRecording.

/** @deprecated */
public AudioRecording getAudioRecording(String filename) {
    AudioMetaData meta = null;
    AudioInputStream ais = getAudioInputStream(filename);
    byte[] samples;
    if (ais != null) {
        AudioFormat format = ais.getFormat();
        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
            ais = getAudioInputStream(format, ais);
            //	 get a map of properties so we can find out how long it is
            Map<String, Object> props = getID3Tags(filename);
            // there is a property called mp3.length.bytes, but that is
            // the length in bytes of the mp3 file, which will of course
            // be much shorter than the decoded version. so we use the
            // duration of the file to figure out how many bytes the
            // decoded file will be.
            long dur = ((Long) props.get("duration")).longValue();
            int toRead = (int) AudioUtils.millis2Bytes(dur / 1000, format);
            samples = loadByteAudio(ais, toRead);
            meta = new MP3MetaData(filename, dur / 1000, props);
        } else {
            samples = loadByteAudio(ais, (int) ais.getFrameLength() * format.getFrameSize());
            long length = AudioUtils.bytes2Millis(samples.length, format);
            meta = new BasicMetaData(filename, length, samples.length);
        }
        SourceDataLine line = getSourceDataLine(format, 2048);
        if (line != null) {
            return new JSAudioRecording(this, samples, line, meta);
        }
    }
    return null;
}
Also used : AudioMetaData(ddf.minim.AudioMetaData) MpegAudioFormat(javazoom.spi.mpeg.sampled.file.MpegAudioFormat) AudioInputStream(javax.sound.sampled.AudioInputStream) SourceDataLine(javax.sound.sampled.SourceDataLine) AudioFormat(javax.sound.sampled.AudioFormat) MpegAudioFormat(javazoom.spi.mpeg.sampled.file.MpegAudioFormat)

Aggregations

SourceDataLine (javax.sound.sampled.SourceDataLine)14 AudioInputStream (javax.sound.sampled.AudioInputStream)8 DataLine (javax.sound.sampled.DataLine)8 IOException (java.io.IOException)7 AudioFormat (javax.sound.sampled.AudioFormat)7 LineUnavailableException (javax.sound.sampled.LineUnavailableException)7 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)3 MpegAudioFormat (javazoom.spi.mpeg.sampled.file.MpegAudioFormat)3 FileNotFoundException (java.io.FileNotFoundException)2 Mixer (javax.sound.sampled.Mixer)2 TargetDataLine (javax.sound.sampled.TargetDataLine)2 AudioMetaData (ddf.minim.AudioMetaData)1 AudioRecordingStream (ddf.minim.spi.AudioRecordingStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 FloatBuffer (java.nio.FloatBuffer)1 InvalidMidiDataException (javax.sound.midi.InvalidMidiDataException)1