public void read (short[] samples, int offset, int numSamples) { if (buffer.length < numSamples * 2) buffer = new byte[numSamples * 2]; int toRead = numSamples * 2; int read = 0; while (read != toRead) read += line.read(buffer, read, toRead - read); for (int i = 0, j = 0; i < numSamples * 2; i += 2, j++) samples[offset + j] = (short)((buffer[i + 1] << 8) | (buffer[i] & 0xff)); }
public void dispose () { line.close(); } }
public JavaSoundAudioRecorder (int samplingRate, boolean isMono) { try { AudioFormat format = new AudioFormat(Encoding.PCM_SIGNED, samplingRate, 16, isMono ? 1 : 2, isMono ? 2 : 4, samplingRate, false); line = AudioSystem.getTargetDataLine(format); line.open(format, buffer.length); line.start(); } catch (Exception ex) { throw new GdxRuntimeException("Error creating JavaSoundAudioRecorder.", ex); } }
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(); }
public static AudioFormat getMP3AudioFormat() { return new AudioFormat(new AudioFormat.Encoding("MPEG1L3"), AudioSystem.NOT_SPECIFIED, AudioSystem.NOT_SPECIFIED, 1, AudioSystem.NOT_SPECIFIED, AudioSystem.NOT_SPECIFIED, false); // endianness doesn't matter }
/** * Writes the temporary file with the generated audio. * * @param inputStream input stream with the waveform * @param length length of the waveform * @return name of the generated temporary file * @throws IOException */ private static String writeWav(InputStream inputStream, int length) throws IOException { AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, SAMPLE_RATE, 8, 1, 1, SAMPLE_RATE, false); File file = File.createTempFile("wav", "."); AudioSystem.write(new AudioInputStream(inputStream, format, length), AudioFileFormat.Type.WAVE, file); return file.getAbsolutePath(); } }
import javax.sound.sampled.*; AudioInputStream audioInputStream = AudioSystem.getAudioInputStream( new File("some_file.wav")); Clip clip = AudioSystem.getClip(); clip.open(audioInputStream); FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); gainControl.setValue(-10.0f); // Reduce volume by 10 decibels. clip.start();
public JavaSoundAudioRecorder (int samplingRate, boolean isMono) { try { AudioFormat format = new AudioFormat(Encoding.PCM_SIGNED, samplingRate, 16, isMono ? 1 : 2, isMono ? 2 : 4, samplingRate, false); line = AudioSystem.getTargetDataLine(format); line.open(format, buffer.length); line.start(); } catch (Exception ex) { throw new GdxRuntimeException("Error creating JavaSoundAudioRecorder.", ex); } }
public static AudioFormat getMP3AudioFormat() { return new AudioFormat(new AudioFormat.Encoding("MPEG1L3"), AudioSystem.NOT_SPECIFIED, AudioSystem.NOT_SPECIFIED, 1, AudioSystem.NOT_SPECIFIED, AudioSystem.NOT_SPECIFIED, false); // endianness doesn't matter }
public void dispose () { line.close(); } }
public void read (short[] samples, int offset, int numSamples) { if (buffer.length < numSamples * 2) buffer = new byte[numSamples * 2]; int toRead = numSamples * 2; int read = 0; while (read != toRead) read += line.read(buffer, read, toRead - read); for (int i = 0, j = 0; i < numSamples * 2; i += 2, j++) samples[offset + j] = (short)((buffer[i + 1] << 8) | (buffer[i] & 0xff)); }
public JavaSoundAudioRecorder (int samplingRate, boolean isMono) { try { AudioFormat format = new AudioFormat(Encoding.PCM_SIGNED, samplingRate, 16, isMono ? 1 : 2, isMono ? 2 : 4, samplingRate, false); line = AudioSystem.getTargetDataLine(format); line.open(format, buffer.length); line.start(); } catch (Exception ex) { throw new GdxRuntimeException("Error creating JavaSoundAudioRecorder.", ex); } }
public static AudioFormat getOggAudioFormat() { return new AudioFormat(new AudioFormat.Encoding("VORBIS"), AudioSystem.NOT_SPECIFIED, AudioSystem.NOT_SPECIFIED, 1, AudioSystem.NOT_SPECIFIED, AudioSystem.NOT_SPECIFIED, false); }
public void dispose () { line.close(); } }
public void read (short[] samples, int offset, int numSamples) { if (buffer.length < numSamples * 2) buffer = new byte[numSamples * 2]; int toRead = numSamples * 2; int read = 0; while (read != toRead) read += line.read(buffer, read, toRead - read); for (int i = 0, j = 0; i < numSamples * 2; i += 2, j++) samples[offset + j] = (short)((buffer[i + 1] << 8) | (buffer[i] & 0xff)); }
public JavaSoundAudioRecorder (int samplingRate, boolean isMono) { try { AudioFormat format = new AudioFormat(Encoding.PCM_SIGNED, samplingRate, 16, isMono ? 1 : 2, isMono ? 2 : 4, samplingRate, false); line = AudioSystem.getTargetDataLine(format); line.open(format, buffer.length); line.start(); } catch (Exception ex) { throw new GdxRuntimeException("Error creating JavaSoundAudioRecorder.", ex); } }
public static AudioFormat getOggAudioFormat() { return new AudioFormat(new AudioFormat.Encoding("VORBIS"), AudioSystem.NOT_SPECIFIED, AudioSystem.NOT_SPECIFIED, 1, AudioSystem.NOT_SPECIFIED, AudioSystem.NOT_SPECIFIED, false); }
public void dispose () { line.close(); } }
public void read (short[] samples, int offset, int numSamples) { if (buffer.length < numSamples * 2) buffer = new byte[numSamples * 2]; int toRead = numSamples * 2; int read = 0; while (read != toRead) read += line.read(buffer, read, toRead - read); for (int i = 0, j = 0; i < numSamples * 2; i += 2, j++) samples[offset + j] = (short)((buffer[i + 1] << 8) | (buffer[i] & 0xff)); }