Search in sources :

Example 1 with AudioMetaData

use of ddf.minim.AudioMetaData in project Minim by ddf.

the class JSMinim method getAudioSample.

public AudioSample getAudioSample(String filename, int bufferSize) {
    AudioInputStream ais = getAudioInputStream(filename);
    if (ais != null) {
        AudioMetaData meta = null;
        AudioFormat format = ais.getFormat();
        FloatSampleBuffer samples = null;
        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 = loadFloatAudio(ais, toRead);
            meta = new MP3MetaData(filename, dur / 1000, props);
        } else {
            samples = loadFloatAudio(ais, (int) ais.getFrameLength() * format.getFrameSize());
            long length = AudioUtils.frames2Millis(samples.getSampleCount(), format);
            meta = new BasicMetaData(filename, length, samples.getSampleCount());
        }
        AudioOut out = getAudioOutput(format.getChannels(), bufferSize, format.getSampleRate(), format.getSampleSizeInBits());
        if (out != null) {
            SampleSignal ssig = new SampleSignal(samples);
            out.setAudioSignal(ssig);
            return new JSAudioSample(meta, ssig, out);
        } else {
            error("Couldn't acquire an output.");
        }
    }
    return null;
}
Also used : AudioMetaData(ddf.minim.AudioMetaData) MpegAudioFormat(javazoom.spi.mpeg.sampled.file.MpegAudioFormat) AudioInputStream(javax.sound.sampled.AudioInputStream) AudioFormat(javax.sound.sampled.AudioFormat) MpegAudioFormat(javazoom.spi.mpeg.sampled.file.MpegAudioFormat) AudioOut(ddf.minim.spi.AudioOut)

Example 2 with AudioMetaData

use of ddf.minim.AudioMetaData 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)

Example 3 with AudioMetaData

use of ddf.minim.AudioMetaData in project Minim by ddf.

the class JSMinim method getAudioRecordingClip.

/** @deprecated */
public AudioRecording getAudioRecordingClip(String filename) {
    Clip clip = null;
    AudioMetaData meta = null;
    AudioInputStream ais = getAudioInputStream(filename);
    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);
        }
        DataLine.Info info = new DataLine.Info(Clip.class, ais.getFormat());
        if (AudioSystem.isLineSupported(info)) {
            // Obtain and open the line.
            try {
                clip = (Clip) AudioSystem.getLine(info);
                clip.open(ais);
            } catch (Exception e) {
                error("Error obtaining Javasound Clip: " + e.getMessage());
                return null;
            }
            Map<String, Object> props = getID3Tags(filename);
            long lengthInMillis = -1;
            if (props.containsKey("duration")) {
                Long dur = (Long) props.get("duration");
                lengthInMillis = dur.longValue() / 1000;
            }
            meta = new MP3MetaData(filename, lengthInMillis, props);
        } else {
            error("File format not supported.");
            return null;
        }
    }
    if (meta == null) {
        // this means we're dealing with not-an-mp3
        meta = new BasicMetaData(filename, clip.getMicrosecondLength() / 1000, -1);
    }
    return new JSAudioRecordingClip(clip, meta);
}
Also used : TargetDataLine(javax.sound.sampled.TargetDataLine) DataLine(javax.sound.sampled.DataLine) SourceDataLine(javax.sound.sampled.SourceDataLine) AudioMetaData(ddf.minim.AudioMetaData) MpegAudioFormat(javazoom.spi.mpeg.sampled.file.MpegAudioFormat) LineUnavailableException(javax.sound.sampled.LineUnavailableException) MalformedURLException(java.net.MalformedURLException) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Clip(javax.sound.sampled.Clip) AudioInputStream(javax.sound.sampled.AudioInputStream) AudioFormat(javax.sound.sampled.AudioFormat) MpegAudioFormat(javazoom.spi.mpeg.sampled.file.MpegAudioFormat)

Aggregations

AudioMetaData (ddf.minim.AudioMetaData)3 AudioFormat (javax.sound.sampled.AudioFormat)3 AudioInputStream (javax.sound.sampled.AudioInputStream)3 MpegAudioFormat (javazoom.spi.mpeg.sampled.file.MpegAudioFormat)3 SourceDataLine (javax.sound.sampled.SourceDataLine)2 AudioOut (ddf.minim.spi.AudioOut)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 Clip (javax.sound.sampled.Clip)1 DataLine (javax.sound.sampled.DataLine)1 LineUnavailableException (javax.sound.sampled.LineUnavailableException)1 TargetDataLine (javax.sound.sampled.TargetDataLine)1 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)1