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;
}
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;
}
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);
}
Aggregations