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); line.drain(); line.close();
byte[] buf = new byte[ 1 ];; AudioFormat af = new AudioFormat( (float )44100, 8, 1, true, false ); SourceDataLine sdl = AudioSystem.getSourceDataLine( af ); sdl.open(); sdl.start(); for( int i = 0; i < 1000 * (float )44100 / 1000; i++ ) { double angle = i / ( (float )44100 / 440 ) * 2.0 * Math.PI; buf[ 0 ] = (byte )( Math.sin( angle ) * 100 ); sdl.write( buf, 0, 1 ); } sdl.drain(); sdl.stop();
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(); }
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); 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 line.write(abData, 0, nRead); line.drain(); line.close();
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();
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();
dataLine = AudioSystem.getSourceDataLine( clip.getFormat() ); return (frames/(long)dataLine.getFormat().getSampleRate())*1000; return (int)(milliseconds/dataLine.getFormat().getSampleRate()); format = stream.getFormat(); format = is1.getFormat(); InputStream is2; if (parent!=null) { dataLine = AudioSystem.getSourceDataLine(afTemp); dataLine.open(); inputStream = new ByteArrayInputStream( audioData ); return dataLine.isOpen(); dataLine.open(); dataLine.start(); newData = tempData; dataLine.write(newData, 0, newData.length); if (startOrMove) { data = new byte[bufSize/
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); } }
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) { } }
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);
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);
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); sourceDataLine.start(); sourceDataLine.open(format); sourceDataLine.start(); System.out.println("format? :" + sourceDataLine.getFormat()); sourceDataLine.write(soundbytes, 0, soundbytes.length); System.out.println(soundbytes.toString()); sourceDataLine.drain(); sourceDataLine.close(); } catch (Exception e) { System.out.println("Not working in speakers...");
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);
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);
/** * Initialize the audio * * @param audioHeader the audio header */ private void initAudio(EAAudioHeader audioHeader) { AudioFormat format = new AudioFormat(audioHeader.getSampleRate(), audioHeader.getBitsPerSample(), audioHeader.getNumberOfChannels(), true, false); DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); if (!AudioSystem.isLineSupported(info)) { throw new RuntimeException("Line matching " + info + " not supported."); } try { line = (SourceDataLine) AudioSystem.getLine(info); line.open(format, 1024 * 4 * format.getFrameSize()); line.start(); } catch (LineUnavailableException ex) { throw new RuntimeException(ex); } } }
public SourceDataLine openLine() throws LineUnavailableException { final AudioFormat af = new AudioFormat(sampleRate, 8, 1, true, true); final SourceDataLine line = AudioSystem.getSourceDataLine(af); line.open(af, sampleRate); line.start(); return line; }
synchronized (LinePool.this) { try { if (!line.isOpen()) { line.open(); line.addLineListener(event -> { log.debug("Event: {}", event); if (event.getType() != Type.STOP) { line.start(); } catch (LineUnavailableException e) { log.warn("Failed to open line", e); line.write(buffer, 0, buffer.length); line.drain(); line.stop(); log.debug("Line completed: {}", line); });
@Override public void start() { if (line != null) { LOG.debug("Sound already started"); return; } LOG.debug("Start sound"); try { line = AudioSystem.getSourceDataLine(FORMAT); line.open(FORMAT, BUFFER_SIZE); } catch (LineUnavailableException e) { throw new RuntimeException(e); } line.start(); buffer = new byte[line.getBufferSize()]; divider = (int) (Gameboy.TICKS_PER_SEC / FORMAT.getSampleRate()); }