use of javax.sound.sampled.AudioFormat in project algs4 by kevin-wayne.
the class StdAudio method save.
/**
* Saves the double array as an audio file (using .wav or .au format).
*
* @param filename the name of the audio file
* @param samples the array of samples
* @throws IllegalArgumentException if unable to save {@code filename}
* @throws IllegalArgumentException if {@code samples} is {@code null}
*/
public static void save(String filename, double[] samples) {
if (samples == null) {
throw new IllegalArgumentException("samples[] is null");
}
// assumes 44,100 samples per second
// use 16-bit audio, mono, signed PCM, little Endian
AudioFormat format = new AudioFormat(SAMPLE_RATE, 16, 1, true, false);
byte[] data = new byte[2 * samples.length];
for (int i = 0; i < samples.length; i++) {
int temp = (short) (samples[i] * MAX_16_BIT);
data[2 * i + 0] = (byte) temp;
data[2 * i + 1] = (byte) (temp >> 8);
}
// now save the file
try {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
AudioInputStream ais = new AudioInputStream(bais, format, samples.length);
if (filename.endsWith(".wav") || filename.endsWith(".WAV")) {
AudioSystem.write(ais, AudioFileFormat.Type.WAVE, new File(filename));
} else if (filename.endsWith(".au") || filename.endsWith(".AU")) {
AudioSystem.write(ais, AudioFileFormat.Type.AU, new File(filename));
} else {
throw new IllegalArgumentException("unsupported audio format: '" + filename + "'");
}
} catch (IOException ioe) {
throw new IllegalArgumentException("unable to save file '" + filename + "'", ioe);
}
}
use of javax.sound.sampled.AudioFormat in project blue by kunstmusik.
the class SoundFileUtilities method getDurationInSeconds.
public static float getDurationInSeconds(String soundFileName) throws IOException, UnsupportedAudioFileException {
File soundFile = BlueSystem.findFile(soundFileName);
AudioFileFormat aFormat = AudioSystem.getAudioFileFormat(soundFile);
AudioFormat format = aFormat.getFormat();
float duration = aFormat.getByteLength() / (format.getSampleRate() * (format.getSampleSizeInBits() / 8) * format.getChannels());
return duration;
}
use of javax.sound.sampled.AudioFormat in project blue by kunstmusik.
the class SoundFileInformationPanel method setSoundFile.
public void setSoundFile(File soundFile) {
try {
AudioFileFormat aFormat = AudioSystem.getAudioFileFormat(soundFile);
AudioFormat format = aFormat.getFormat();
durationText.setText(getDuration(aFormat, format));
formatTypeText.setText(aFormat.getType().toString());
byteLengthText.setText(Integer.toString(aFormat.getByteLength()));
encodingTypeText.setText(format.getEncoding().toString());
sampleRateText.setText(Float.toString(format.getSampleRate()));
sampleSizeInBitsText.setText(Integer.toString(format.getSampleSizeInBits()));
channelsText.setText(Integer.toString(format.getChannels()));
isBigEndianText.setText(getBooleanString(format.isBigEndian()));
setFtableText(soundFile, aFormat.getByteLength());
fTableText.copy();
} catch (java.io.IOException ioe) {
JOptionPane.showMessageDialog(null, BlueSystem.getString("soundfile.infoPanel.error.couldNotOpenFile") + " " + soundFile.getAbsolutePath());
clearAudioInfo();
return;
} catch (javax.sound.sampled.UnsupportedAudioFileException uae) {
JOptionPane.showMessageDialog(null, BlueSystem.getString("soundfile.infoPanel.error.unsupportedAudio") + " " + uae.getLocalizedMessage());
clearAudioInfo();
return;
}
}
use of javax.sound.sampled.AudioFormat in project openmeetings by apache.
the class AudioTone method play.
public static void play() {
byte[] buf = new byte[1];
AudioFormat af = new AudioFormat(SAMPLE_RATE, 8, 1, true, false);
try (SourceDataLine sdl = AudioSystem.getSourceDataLine(af)) {
sdl.open(af);
sdl.start();
for (int i = 0; i < (int) SAMPLE_RATE; ++i) {
double angle = i / (SAMPLE_RATE / 440) * 2.0 * Math.PI;
buf[0] = (byte) (Math.sin(angle) * 128);
sdl.write(buf, 0, 1);
}
} catch (LineUnavailableException e) {
// no-op
}
}
use of javax.sound.sampled.AudioFormat in project Discord4J by Discord4J.
the class DiscordUtils method getPCMStream.
/**
* Converts an {@link AudioInputStream} to 48000Hz 16 bit stereo signed Big Endian PCM format.
*
* @param stream The original stream.
* @return The PCM encoded stream.
*/
public static AudioInputStream getPCMStream(AudioInputStream stream) {
AudioFormat baseFormat = stream.getFormat();
// Converts first to PCM data. If the data is already PCM data, this will not change anything.
AudioFormat toPCM = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), // AudioConnection.OPUS_SAMPLE_RATE,
baseFormat.getSampleSizeInBits() != -1 ? baseFormat.getSampleSizeInBits() : 16, baseFormat.getChannels(), // If we are given a frame size, use it. Otherwise, assume 16 bits (2 8bit shorts) per channel.
baseFormat.getFrameSize() != -1 ? baseFormat.getFrameSize() : 2 * baseFormat.getChannels(), baseFormat.getFrameRate() != -1 ? baseFormat.getFrameRate() : baseFormat.getSampleRate(), baseFormat.isBigEndian());
AudioInputStream pcmStream = AudioSystem.getAudioInputStream(toPCM, stream);
// Then resamples to a sample rate of 48000hz and ensures that data is Big Endian.
AudioFormat audioFormat = new AudioFormat(toPCM.getEncoding(), OpusUtil.OPUS_SAMPLE_RATE, toPCM.getSampleSizeInBits(), toPCM.getChannels(), toPCM.getFrameSize(), toPCM.getFrameRate(), true);
return AudioSystem.getAudioInputStream(audioFormat, pcmStream);
}
Aggregations