public static void main(String[] args) throws Exception { double samplingRateFactor = Double.valueOf(args[0]).doubleValue(); for (int i = 1; i < args.length; i++) { AudioInputStream ais = AudioSystem.getAudioInputStream(new File(args[i])); AudioFormat af = new AudioFormat((int) (ais.getFormat().getSampleRate() * samplingRateFactor), ais.getFormat() .getSampleSizeInBits(), ais.getFormat().getChannels(), true, ais.getFormat().isBigEndian()); DDSAudioInputStream ais2 = new DDSAudioInputStream(new AudioDoubleDataSource(ais), af); String outFileName = args[i].substring(0, args[i].length() - 4) + "_child.wav"; AudioSystem.write(ais2, AudioFileFormat.Type.WAVE, new File(outFileName)); } } }
/** * * @param input * input * @param outputMode * as defined in AudioPlayer: STEREO, LEFT_ONLY or RIGHT_ONLY. */ public StereoAudioInputStream(AudioInputStream input, int outputMode) { super(input, input.getFormat(), input.getFrameLength()); this.newFormat = new AudioFormat(input.getFormat().getEncoding(), input.getFormat().getSampleRate(), input.getFormat() .getSampleSizeInBits(), 2, 2 * input.getFormat().getFrameSize() / input.getFormat().getChannels(), input .getFormat().getFrameRate(), input.getFormat().isBigEndian()); this.inputChannels = input.getFormat().getChannels(); this.outputMode = outputMode; }
/** * Construct an audio input stream from which <code>duration</code> seconds of silence can be read. * * @param duration * the desired duration of the silence, in seconds * @param format * the desired audio format of the audio input stream. getFrameSize() and getFrameRate() must return meaningful * values. */ public SilenceAudioInputStream(double duration, AudioFormat format) { super(new ByteArrayInputStream(new byte[(int) (format.getFrameSize() * format.getFrameRate() * duration)]), format, (long) (format.getFrameRate() * duration)); }
/** * Returns the amount of time (in seconds) this Utterance lasts. * * @return how long is this utterance */ public float getAudioTime() { return audioBuffer.size() / (audioFormat.getSampleRate() * audioFormat.getSampleSizeInBits() / 8); }
public SilenceMarker(AudioInputStream ais, int width, int height) { super(); if (!ais.getFormat().getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)) { ais = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED, ais); } if (ais.getFormat().getChannels() > 1) { throw new IllegalArgumentException("Can only deal with mono audio signals"); } int samplingRate = (int) ais.getFormat().getSampleRate(); DoubleDataSource signal = new AudioDoubleDataSource(ais); initialise(signal, samplingRate, width, height); }
public void run() { status = Status.PLAYING; AudioFormat audioFormat = ais.getFormat(); if (audioFormat.getChannels() == 1) { if (outputMode != MONO) { // mono -> convert to stereo ais = new StereoAudioInputStream(ais, outputMode); audioFormat = ais.getFormat(); assert audioFormat.getChannels() == 2 : "Unexpected number of channels: " + audioFormat.getChannels(); if (outputMode == MONO) { ais = new MonoAudioInputStream(ais); DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat); boolean bIsSupportedDirectly = AudioSystem.isLineSupported(info); if (!bIsSupportedDirectly) { AudioFormat sourceFormat = audioFormat; AudioFormat targetFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, sourceFormat.getSampleRate(), sourceFormat.getSampleSizeInBits(), sourceFormat.getChannels(), sourceFormat.getChannels() * (sourceFormat.getSampleSizeInBits() / 8), sourceFormat.getSampleRate(), sourceFormat.isBigEndian()); ais = AudioSystem.getAudioInputStream(targetFormat, ais); audioFormat = ais.getFormat(); info = new DataLine.Info(SourceDataLine.class, audioFormat); line = (SourceDataLine) AudioSystem.getLine(info);
AudioInputStream din = null; try { AudioInputStream in = AudioSystem.getAudioInputStream(new URL("http://www.howjsay.com/mp3/"+ args[0] +".mp3")); AudioFormat baseFormat = in.getFormat(); AudioFormat decodedFormat = new AudioFormat( AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false); din = AudioSystem.getAudioInputStream(decodedFormat, in); DataLine.Info info = new DataLine.Info(SourceDataLine.class, decodedFormat); SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info); if(line != null) { line.open(decodedFormat); byte[] data = new byte[4096]; line.start(); while ((nBytesRead = din.read(data, 0, data.length)) != -1) { line.write(data, 0, nBytesRead); line.drain(); line.stop(); line.close(); din.close();
return null; if (!ais.getFormat().matches(targetFormat)) { AudioInputStream intermedStream = AudioSystem.getAudioInputStream(targetFormat, ais); ais = intermedStream; } catch (IllegalArgumentException iae) { // conversion not supported boolean solved = false; if (!targetFormat.getEncoding().equals(ais.getFormat()) && targetFormat.getSampleRate() != ais.getFormat().getSampleRate()) { AudioFormat sampleRateConvFormat = new AudioFormat(ais.getFormat().getEncoding(), targetFormat.getSampleRate(), ais.getFormat().getSampleSizeInBits(), ais.getFormat().getChannels(), ais.getFormat().getFrameSize(), ais.getFormat().getFrameRate(), ais.getFormat().isBigEndian()); try { AudioInputStream intermedStream = AudioSystem.getAudioInputStream(sampleRateConvFormat, ais); ais = AudioSystem.getAudioInputStream(targetFormat, intermedStream);
public static void main(String[] args) throws Exception { if (args.length > 0) { for (int file = 0; file < args.length; file++) { AudioInputStream ais = AudioSystem.getAudioInputStream(new File(args[file])); if (!ais.getFormat().getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)) { ais = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED, ais); if (ais.getFormat().getChannels() > 1) { throw new IllegalArgumentException("Can only deal with mono audio signals"); int samplingRate = (int) ais.getFormat().getSampleRate(); DoubleDataSource signal = new AudioDoubleDataSource(ais); int framelength = (int) (0.01 /* seconds */* samplingRate); AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100.0F, 16, 1, 2, 44100.0F, false); DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat); AudioInputStream input = null; try { TargetDataLine mic = (TargetDataLine) AudioSystem.getLine(info); mic.open(audioFormat); mic.start(); int framelength = (int) (0.01 /* seconds */* audioFormat.getSampleRate()); EnergyAnalyser ea = new EnergyAnalyser(signal, framelength, framelength, (int) audioFormat.getSampleRate()); while (true) { try {
return (frames/(long)dataLine.getFormat().getSampleRate())*1000; return (int)(milliseconds/dataLine.getFormat().getSampleRate()); public void setFramePosition(int frames) { framePosition = frames; int offset = framePosition*format.getFrameSize(); try { inputStream.reset(); public int getFramePosition() { long timeSinceLastPositionSet = System.currentTimeMillis() - timelastPositionSet; int size = dataLine.getBufferSize()*(format.getChannels()/2)/bufferUpdateFactor; int framesSinceLast = (int)((timeSinceLastPositionSet/1000f)* dataLine.getFormat().getFrameRate()); int framesRemainingTillTime = size - framesSinceLast; return framePosition return audioData.length/format.getFrameSize(); if (format.getEncoding()!=AudioFormat.Encoding.PCM_SIGNED) { is1 = AudioSystem.getAudioInputStream( AudioFormat.Encoding.PCM_SIGNED, stream ); } else { is1 = stream; audioData = baos.toByteArray(); AudioFormat afTemp; if (format.getChannels()<2) {
public static double getLength(String path) throws Exception { AudioInputStream stream; stream = AudioSystem.getAudioInputStream(new URL(path)); AudioFormat format = stream.getFormat(); if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) { format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, format .getSampleRate(), format.getSampleSizeInBits() * 2, format .getChannels(), format.getFrameSize() * 2, format .getFrameRate(), true); // big endian stream = AudioSystem.getAudioInputStream(format, stream); DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat(), ((int) stream.getFrameLength() * format.getFrameSize())); Clip clip = (Clip) AudioSystem.getLine(info); clip.close(); return clip.getBufferSize() / (clip.getFormat().getFrameSize() * clip.getFormat() .getFrameRate());
public static void tone(int hz, int msecs, double vol) { try { byte[] buf = new byte[1]; AudioFormat af = new AudioFormat( SAMPLE_RATE, // sampleRate 8, // sampleSizeInBits 1, // channels true, // signed false); // bigEndian SourceDataLine sdl; sdl = AudioSystem.getSourceDataLine(af); sdl.open(af); sdl.start(); for (int i=0; i < msecs*8; i++) { double angle = i / (SAMPLE_RATE / hz) * 2.0 * Math.PI; buf[0] = (byte)(Math.sin(angle) * 127.0 * vol); sdl.write(buf,0,1); } sdl.drain(); sdl.stop(); sdl.close(); } catch (Exception e) { } }
private void load(File file) { try { audioInputStream = AudioSystem.getAudioInputStream(file); audioFormat = audioInputStream.getFormat(); // mp3 decode if (audioFormat.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) { audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, audioFormat.getSampleRate(), 16, audioFormat.getChannels(), audioFormat.getChannels() * 2, audioFormat.getSampleRate(), false); audioInputStream = AudioSystem.getAudioInputStream(audioFormat, audioInputStream); } //output DataLine.Info dataLineInfo = new DataLine.Info( SourceDataLine.class, audioFormat, AudioSystem.NOT_SPECIFIED); sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo); sourceDataLine.open(audioFormat); volume = (FloatControl) sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN); sourceDataLine.start(); } catch (Exception e) { log.error("Couldn't load file: " + file + ' ' + e); } }
soundNode.add(soundSampledNode); md = new MediaData("Suffixes", "Sound File Suffixes", AudioSystem.getAudioFileTypes()); soundSampledNode.add(new DefaultMutableTreeNode(md)); data[0][1] = new Integer(audioFormat.getChannels()); data[1][1] = audioFormat.getEncoding(); data[2][1] = new Float(audioFormat.getFrameRate()); data[3][1] = new Float(audioFormat.getSampleRate()); data[4][1] = new Integer(audioFormat.getSampleSizeInBits()); data[5][1] = new Boolean(audioFormat.isBigEndian()); data[6][1] = new Float(dataLine.getLevel()); Port.Info portInfo = (Port.Info)port.getLineInfo(); data = new Object[2+controls.length][2]; data[kk][1] = controls[index]; md = new MediaData(getShortLineName(line.getLineInfo().toString()), columnNames, data); soundSampledMixerNode.add(new DefaultMutableTreeNode(md));
private static boolean play(final InputStream arquivo, int count) { try (final AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(arquivo)) { // Carrega o formato do audio e cria uma linha final AudioFormat audioFormat = audioInputStream.getFormat(); final DataLine.Info dataLineInfo = new DataLine.Info(Clip.class, audioInputStream.getFormat(), (int) audioInputStream.getFrameLength() * audioFormat.getFrameSize()); // Carrega o som para o dispositivo try (final Clip clip = (Clip) AudioSystem.getLine(dataLineInfo)) { // Evento do LineListener clip.addLineListener(e -> { if (LineEvent.Type.STOP.equals(e.getType())) { e.getLine().close(); } }); clip.open(audioInputStream); clip.loop(count); } return true; } catch (LineUnavailableException | UnsupportedAudioFileException | IOException e) { LogUtils.generate(e); return false; } } }
for (int file = 1; file < args.length; file++) { System.out.println("\nProcessing file: " + args[file]); AudioInputStream ais = AudioSystem.getAudioInputStream(new File(wavDirectory + "/" + args[file])); if (!ais.getFormat().getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)) { ais = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED, ais); if (ais.getFormat().getChannels() > 1) { throw new IllegalArgumentException("Can only deal with mono audio signals"); int samplingRate = (int) ais.getFormat().getSampleRate(); DoubleDataSource signal = new AudioDoubleDataSource(ais); int framelength = (int) (0.01 /* seconds */* samplingRate); ais.getFrameLength(); duration = ais.getFrameLength() / ais.getFormat().getFrameRate();
if (ais.getFormat().getChannels() > 1) { throw new IllegalArgumentException("Can only deal with mono signals"); int bitsPerSample = ais.getFormat().getSampleSizeInBits(); if (bitsPerSample != 8 && bitsPerSample != 16 && bitsPerSample != 24) { throw new IllegalArgumentException("Can deal with sample size 8, 16 or 24, but not " + bitsPerSample); this.bigEndian = ais.getFormat().isBigEndian(); this.samplingRate = (int) ais.getFormat().getSampleRate(); this.byteBuf = new byte[BYTEBUFFER_LENGTH]; this.hasMoreData = true;
format = new AudioFormat(sampleRate, 16, 1, true, false); receivePacket.getData()); ais = new AudioInputStream(baiss, format, receivePacket.getLength()); try { DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format); SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo); sourceDataLine.open(format); FloatControl volumeControl = (FloatControl) sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN); volumeControl.setValue(100.0f); sourceDataLine.start(); sourceDataLine.open(format); sourceDataLine.start(); System.out.println("format? :" + sourceDataLine.getFormat());
new AudioFormat(Note.SAMPLE_RATE, 8, 1, true, true); SourceDataLine line = AudioSystem.getSourceDataLine(af); line.open(af, Note.SAMPLE_RATE); line.start(); for (Note n : Note.values()) { play(line, n, 500); play(line, Note.REST, 10); line.drain(); line.close(); ms = Math.min(ms, Note.SECONDS * 1000); int length = Note.SAMPLE_RATE * ms / 1000; int count = line.write(note.data(), 0, length);
/** * Sets the InputStream from which this StreamDataSource reads. * * @param inputStream the InputStream from which audio data comes * @param streamName the name of the InputStream */ public void setInputStream(AudioInputStream inputStream, String streamName) { dataStream = inputStream; streamEndReached = false; utteranceEndSent = false; utteranceStarted = false; AudioFormat format = inputStream.getFormat(); sampleRate = (int) format.getSampleRate(); bigEndian = format.isBigEndian(); String s = format.toString(); logger.finer("input format is " + s); if (format.getSampleSizeInBits() % 8 != 0) throw new Error("StreamDataSource: bits per sample must be a multiple of 8."); bytesPerValue = format.getSampleSizeInBits() / 8; // test whether all files in the stream have the same format AudioFormat.Encoding encoding = format.getEncoding(); if (encoding.equals(AudioFormat.Encoding.PCM_SIGNED)) signedData = true; else if (encoding.equals(AudioFormat.Encoding.PCM_UNSIGNED)) signedData = false; else throw new RuntimeException("used file encoding is not supported"); totalValuesRead = 0; }