use of javax.sound.sampled.AudioFormat in project competitive-programming by ttvi-cse.
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 aws-doc-sdk-examples by awsdocs.
the class Microphone method get.
public static TargetDataLine get() throws Exception {
AudioFormat format = new AudioFormat(16000, 16, 1, true, false);
DataLine.Info datalineInfo = new DataLine.Info(TargetDataLine.class, format);
TargetDataLine dataLine = (TargetDataLine) AudioSystem.getLine(datalineInfo);
dataLine.open(format);
return dataLine;
}
use of javax.sound.sampled.AudioFormat in project jersey by jersey.
the class ToneGenerator method writeWav.
/**
* 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();
}
use of javax.sound.sampled.AudioFormat in project bigbluebutton by bigbluebutton.
the class AudioReceiver method main.
// ******************************* MAIN *******************************
/** The main method. */
public static void main(String[] args) {
int port = 0;
int sample_rate = 8000;
int sample_size = 1;
boolean linear_signed = false;
boolean pcmu = false;
boolean big_endian = false;
String filename = null;
boolean sound = true;
boolean help = true;
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-h")) {
break;
}
if (i == 0) {
port = Integer.parseInt(args[i]);
help = false;
continue;
}
if (args[i].equals("-F") && args.length > (i + 1)) {
sound = false;
filename = args[++i];
continue;
}
if (args[i].equals("-S") && args.length > (i + 2)) {
sample_rate = Integer.parseInt(args[++i]);
sample_size = Integer.parseInt(args[++i]);
continue;
}
if (args[i].equals("-Z")) {
linear_signed = true;
continue;
}
if (args[i].equals("-U")) {
pcmu = true;
continue;
}
if (args[i].equals("-E")) {
big_endian = true;
continue;
}
// else, do:
System.out.println("unrecognized param '" + args[i] + "'\n");
help = true;
}
if (help) {
System.out.println("usage:\n java AudioReceiver <local_port> [options]");
System.out.println(" options:");
System.out.println(" -h this help");
System.out.println(" -F <audio_file> records to audio file");
System.out.println(" -S <rate> <size> sample rate [B/s], and size [B]");
System.out.println(" -Z uses PCM linear signed format (linear unsigned is used as default)");
System.out.println(" -U uses PCMU format");
System.out.println(" -E uses big endian format");
System.exit(0);
}
AudioFormat.Encoding codec;
if (pcmu)
codec = AudioFormat.Encoding.ULAW;
else if (linear_signed)
codec = AudioFormat.Encoding.PCM_SIGNED;
else
codec = AudioFormat.Encoding.PCM_UNSIGNED;
try {
RtpStreamReceiver receiver;
AudioOutput audio_output = null;
if (sound)
AudioOutput.initAudioLine();
if (sound) {
AudioFormat format = new AudioFormat(codec, sample_rate, 8 * sample_size, 1, sample_size, sample_rate, big_endian);
audio_output = new AudioOutput(format);
receiver = new RtpStreamReceiver(audio_output.getOuputStream(), port);
} else //if (filename!=null)
{
File file = new File(filename);
/*
AudioFileFormat format=AudioSystem.getAudioFileFormat(file);
System.out.println("File audio format: "+format);
OutputStream output_stream=new OutputStream() { public void write(int b) {} };
receiver=new RtpStreamReceiver(output_stream,port);
*/
FileOutputStream output_stream = new FileOutputStream(file);
receiver = new RtpStreamReceiver(output_stream, port);
}
receiver.start();
if (sound)
audio_output.play();
System.out.println("Press 'Return' to stop");
System.in.read();
receiver.halt();
if (sound)
audio_output.stop();
if (sound)
AudioOutput.closeAudioLine();
} catch (Exception e) {
e.printStackTrace();
}
}
use of javax.sound.sampled.AudioFormat in project playn by threerings.
the class BigClip method open.
@Override
public void open(AudioInputStream stream) throws IOException, LineUnavailableException {
AudioInputStream is1;
format = stream.getFormat();
if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
is1 = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED, stream);
} else {
is1 = stream;
}
format = is1.getFormat();
InputStream is2 = is1;
byte[] buf = new byte[1 << 16];
int numRead = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
numRead = is2.read(buf);
while (numRead > -1) {
baos.write(buf, 0, numRead);
numRead = is2.read(buf, 0, buf.length);
}
is2.close();
audioData = baos.toByteArray();
AudioFormat afTemp;
if (format.getChannels() < 2) {
int frameSize = format.getSampleSizeInBits() * 2 / 8;
afTemp = new AudioFormat(format.getEncoding(), format.getSampleRate(), format.getSampleSizeInBits(), 2, frameSize, format.getFrameRate(), format.isBigEndian());
} else {
afTemp = format;
}
setLoopPoints(0, audioData.length);
dataLine = AudioSystem.getSourceDataLine(afTemp);
dataLine.open();
inputStream = new ByteArrayInputStream(audioData);
}
Aggregations