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; * (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); line.addLineListener(lineListener); line.open(audioFormat); } catch (Exception e) { e.printStackTrace(); return; line.start(); int nRead = 0; byte[] abData = new byte[65532]; // needs to be a multiple of 4 and 6, to support both 16 and 24 bit stereo
(new Line.Info(TargetDataLine.class)); } else { lineInfos = AudioSystem.getTargetLineInfo (new Line.Info(TargetDataLine.class)); AudioFormat[] formats = ((TargetDataLine.Info)info).getFormats(); if (thisFormat.getEncoding() == format.getEncoding() && thisFormat.isBigEndian() == format.isBigEndian() && thisFormat.getSampleSizeInBits() == format.getSampleSizeInBits()
audioStream = AudioSystem.getAudioInputStream(soundFile); } catch (Exception e){ e.printStackTrace(); audioFormat = audioStream.getFormat(); DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat); try { sourceLine = (SourceDataLine) AudioSystem.getLine(info); sourceLine.open(audioFormat); } catch (LineUnavailableException e) { e.printStackTrace(); sourceLine.start(); while (nBytesRead != -1) { try { nBytesRead = audioStream.read(abData, 0, abData.length); } catch (IOException e) { e.printStackTrace(); int nBytesWritten = sourceLine.write(abData, 0, nBytesRead); sourceLine.drain(); sourceLine.close();
AudioInputStream ais = AudioSystem.getAudioInputStream(is); AudioFormat audioFormat = ais.getFormat(); DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat); line = (SourceDataLine) AudioSystem.getLine(info); line.open(audioFormat); line.start(); byte[] samples = new byte[BUFFER_SIZE]; int count = 0; while ((count = ais.read(samples, 0, BUFFER_SIZE)) != -1) { line.write(samples, 0, count);
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();
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();
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());
/** constructor */ public WaveFile(String path) { try { File file=new File(path); javax.sound.sampled.AudioFileFormat audioFileFormat=javax.sound.sampled.AudioSystem.getAudioFileFormat(file); m_stream=javax.sound.sampled.AudioSystem.getAudioInputStream(file); javax.sound.sampled.AudioFormat format=m_stream.getFormat(); javax.sound.sampled.DataLine.Info info=new javax.sound.sampled.DataLine.Info(javax.sound.sampled.Clip.class,format,((int)m_stream.getFrameLength()*format.getFrameSize())); m_clip=(javax.sound.sampled.Clip)javax.sound.sampled.AudioSystem.getLine(info); m_clip.open(m_stream); } catch(Exception e) { e.printStackTrace(); } }
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); } }
private static void init() { try { // 44,100 samples per second, 16-bit audio, mono, signed PCM, little Endian AudioFormat format = new AudioFormat((float) SAMPLE_RATE, BITS_PER_SAMPLE, 1, true, false); DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); line = (SourceDataLine) AudioSystem.getLine(info); line.open(format, SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE); // the internal buffer is a fraction of the actual buffer size, this choice is arbitrary // it gets divided because we can't expect the buffered data to line up exactly with when // the sound card decides to push out its samples. buffer = new byte[SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE/3]; } catch (LineUnavailableException e) { System.out.println(e.getMessage()); } // no sound gets made before this call line.start(); }
boolean signed = true; boolean bigEndian = false; return new AudioFormat(sampleRate, sampleInbits, channels, signed, bigEndian); InputStream byteInputStream = new ByteArrayInputStream(audioData); AudioFormat adFormat = getAudioFormat(); InputStream = new AudioInputStream(byteInputStream, adFormat, audioData.length / adFormat.getFrameSize()); DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, adFormat); sourceLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo); sourceLine.open(adFormat); sourceLine.start(); Thread playThread = new Thread(new PlayThread()); playThread.start(); try { int cnt; while ((cnt = InputStream.read(tempBuffer, 0, tempBuffer.length)) != -1) { if (cnt > 0) { sourceLine.write(tempBuffer, 0, cnt);
import javax.sound.sampled.*; import java.io.*; import javax.swing.*; AudioInputStream as1 = AudioSystem.getAudioInputStream(new java.io.FileInputStream("chickenDance.wav")); AudioFormat af = as1.getFormat(); Clip clip1 = AudioSystem.getClip(); DataLine.Info info = new DataLine.Info(Clip.class, af); Line line1 = AudioSystem.getLine(info); if ( ! line1.isOpen() ) { clip1.open(as1); clip1.loop(Clip.LOOP_CONTINUOUSLY); clip1.start(); }
protected void updateSound(double[] signal, int samplingRate) { AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, samplingRate, 16, 1, 2, samplingRate, false); DataLine.Info info = new DataLine.Info(Clip.class, audioFormat); final Clip clip; final Timer timer = new Timer(true); try { clip = (Clip) AudioSystem.getLine(info); clip.open(new DDSAudioInputStream(new BufferedDoubleDataSource(signal), audioFormat)); System.err.println("Created clip");
try { AudioFormat af = new AudioFormat(24000, 16, 1, true, false); DataLine.Info info = new DataLine.Info(SourceDataLine.class, af); SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info); line.open(af, 4096); line.start(); line.write(buffer, 0, buffer.length); line.drain(); line.stop(); line.close(); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e);
@Override public void setup() throws IOException { try { InputStream audioSource = sound.newInputStream(); BufferedInputStream bufferedAudio = new BufferedInputStream(audioSource); AudioInputStream origStream = AudioSystem.getAudioInputStream(bufferedAudio); AudioFormat origFormat = origStream.getFormat(); AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, // Encoding origFormat.getSampleRate(), // Sample Rate 16, // Sample Size origFormat.getChannels(), // Channels origFormat.getChannels() * 2, // Frame Size origFormat.getSampleRate(), // Frame Rate false // Big Endian ); AudioInputStream stream = AudioSystem.getAudioInputStream(decodedFormat, origStream); this.clip = (Clip) AudioSystem.getLine(new DataLine.Info(Clip.class, decodedFormat)); this.format = decodedFormat; this.stream = stream; } catch (IllegalArgumentException | LineUnavailableException | UnsupportedAudioFileException exception) { throw new IOException(exception); } }
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());
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); microphone = (TargetDataLine) AudioSystem.getLine(info); microphone.open(format); int numBytesRead; int CHUNK_SIZE = 1024; byte[] data = new byte[microphone.getBufferSize() / 5]; microphone.start(); audioInputStream = new AudioInputStream(byteArrayInputStream,format, audioData.length / format.getFrameSize()); DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format); sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo); sourceDataLine.open(format); sourceDataLine.start(); int cnt = 0; byte tempBuffer[] = new byte[10000]; try { while ((cnt = audioInputStream.read(tempBuffer, 0,tempBuffer.length)) != -1) { if (cnt > 0) { sourceDataLine.write(tempBuffer, 0, cnt); sourceDataLine.drain(); sourceDataLine.close(); microphone.close(); } catch (LineUnavailableException e) { e.printStackTrace();
wav = new AudioFormat(44100, 16, 2, true, false); info = new DataLine.Info(SourceDataLine.class, wav); stopped = false; if (!AudioSystem.isLineSupported(info)) { System.out.print("no support for " + wav.toString() ); lineIn = (SourceDataLine) AudioSystem.getLine(info); lineIn.open(wav); lineIn.start(); fis = new FileInputStream(file = new File(filename)); totalToRead = fis.available(); if (numBytesRead == -1) break; total += numBytesRead; lineIn.write(buffer, 0, numBytesRead);
audioInputStream = AudioSystem.getAudioInputStream(clipFile); } catch (Exception e) { e.printStackTrace(); AudioFormat format = audioInputStream.getFormat(); DataLine.Info info = new DataLine.Info(Clip.class, format); try { m_clip = (Clip) AudioSystem.getLine(info); m_clip.open(audioInputStream); } catch (LineUnavailableException e) {
this.format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,44100, 16, 2 , 4, 44100,false); this.info = new DataLine.Info(SourceDataLine.class, format); this.sourceDataLine = (SourceDataLine) AudioSystem.getLine(info); TargetDataLine tdl = AudioSystem.getTargetDataLine(format); sourceDataLine.open(); final Thread playerThread = new Thread(){ AudioInputStream auis = null; try { auis = AudioSystem.getAudioInputStream(soundUrl); } catch (UnsupportedAudioFileException e) { e.printStackTrace(); sourceDataLine.start(); try { auis.mark(auis.available()); numberOfBitesRead = auis.read(bytes); if(numberOfBitesRead == -1) { auis.reset(); sourceDataLine.write(bytes, 0, bytes.length); bytesWritten+=4; } else {