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(); }
try { sourceLine = (SourceDataLine) AudioSystem.getLine(info); sourceLine.open(audioFormat); } catch (LineUnavailableException e) { e.printStackTrace();
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()) {
format); line = (SourceDataLine) AudioSystem.getLine(info); line.open(format); line.start();
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];
false); // bigEndian SourceDataLine sdl = AudioSystem.getSourceDataLine(af); sdl.open(af); sdl.start(); for (int i=0; i < msecs*8; i++) {
line.open(audioFormat); } catch (Exception e) { e.printStackTrace();
line.open(audioFormat); } catch (Exception e) { e.printStackTrace();
@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()); }
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();
try { if (!line.isOpen()) { line.open(); line.addLineListener(event -> { log.debug("Event: {}", event);
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) { } }
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) { } }
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); } }
try(SourceDataLine sdl = AudioSystem.getSourceDataLine(audioFormat)) { sdl.open(audioFormat, bufferSize); // work with sdl } catch(LineUnavailableException ex) { throw new RuntimeException(ex); }
// Added two lines. DataLine.Info info = new DataLine.Info( SourceDataLine.class, audioFormat ); SourceDataLine dataLine = (SourceDataLine) AudioSystem.getLine( info ); dataLine.open(); // Adjust the volume on the output line. if( dataLine.isControlSupported( FloatControl.Type.MASTER_GAIN)) { // If inside this if, the Master_Gain must be supported. Yes? FloatControl volume = (FloatControl) dataLine.getControl(FloatControl.Type.MASTER_GAIN); // This line throws an exception. "Master_Gain not supported" volume.setValue( 100.0F ); }
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; }
@Override public void initAudioLine() throws Exception{ if(myOutputLine != null){ return; } AudioFormat format = myWavBuffer.getFormat(); DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); myOutputLine = (SourceDataLine) AudioSystem.getLine(info); myOutputLine.open(format); myPlayLoop.initialize(myOutputLine); myMonitor = new WavProgressMonitor(this, 100); }