use of javax.sound.sampled.AudioFormat in project ant by apache.
the class AntSoundPlayer method play.
/**
* Plays the file for duration milliseconds or loops.
*/
private void play(Project project, File file, int loops, Long duration) {
Clip audioClip = null;
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem.getAudioInputStream(file);
} catch (UnsupportedAudioFileException uafe) {
project.log("Audio format is not yet supported: " + uafe.getMessage());
} catch (IOException ioe) {
// NOSONAR
ioe.printStackTrace();
}
if (audioInputStream != null) {
AudioFormat format = audioInputStream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format, AudioSystem.NOT_SPECIFIED);
try {
try {
audioClip = (Clip) AudioSystem.getLine(info);
audioClip.addLineListener(this);
audioClip.open(audioInputStream);
} catch (LineUnavailableException e) {
project.log("The sound device is currently unavailable");
return;
} catch (IOException e) {
// NOSONAR
e.printStackTrace();
}
if (duration != null) {
playClip(audioClip, duration);
} else {
playClip(audioClip, loops);
}
if (audioClip != null) {
audioClip.drain();
}
} finally {
FileUtils.close(audioClip);
}
} else {
project.log("Can't get data from file " + file.getName());
}
}
use of javax.sound.sampled.AudioFormat in project java-sdk by watson-developer-cloud.
the class MicrophoneWithWebSocketsExample method main.
/**
* The main method.
*
* @param args the arguments
* @throws Exception the exception
*/
public static void main(final String[] args) throws Exception {
SpeechToText service = new SpeechToText();
service.setUsernameAndPassword("<username>", "<password>");
// Signed PCM AudioFormat with 16kHz, 16 bit sample size, mono
int sampleRate = 16000;
AudioFormat format = new AudioFormat(sampleRate, 16, 1, true, false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
if (!AudioSystem.isLineSupported(info)) {
System.out.println("Line not supported");
System.exit(0);
}
TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format);
line.start();
AudioInputStream audio = new AudioInputStream(line);
RecognizeOptions options = new RecognizeOptions.Builder().audio(audio).interimResults(true).timestamps(true).wordConfidence(true).contentType(HttpMediaType.AUDIO_RAW + ";rate=" + sampleRate).build();
service.recognizeUsingWebSocket(options, new BaseRecognizeCallback() {
@Override
public void onTranscription(SpeechRecognitionResults speechResults) {
System.out.println(speechResults);
}
});
System.out.println("Listening to your voice for the next 30s...");
Thread.sleep(30 * 1000);
// closing the WebSockets underlying InputStream will close the WebSocket itself.
line.stop();
line.close();
System.out.println("Fin.");
}
use of javax.sound.sampled.AudioFormat in project Symphony by Vazkii.
the class JavaSoundAudioDevice method test.
/**
* Runs a short test by playing a short silent sound.
*/
public void test() throws JavaLayerException {
try {
open(new AudioFormat(22050, 16, 1, true, false));
short[] data = new short[22050 / 10];
write(data, 0, data.length);
flush();
close();
} catch (RuntimeException ex) {
throw new JavaLayerException("Device test failed: " + ex);
}
}
use of javax.sound.sampled.AudioFormat in project Symphony by Vazkii.
the class JavaSoundAudioDevice method getSourceLineInfo.
protected DataLine.Info getSourceLineInfo() {
AudioFormat fmt = getAudioFormat();
// DataLine.Info info = new DataLine.Info(SourceDataLine.class, fmt, 4000);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, fmt);
return info;
}
use of javax.sound.sampled.AudioFormat in project javacv by bytedeco.
the class JavaFxPlayVideoAndAudio method start.
@Override
public void start(Stage primaryStage) throws Exception {
StackPane root = new StackPane();
ImageView imageView = new ImageView();
root.getChildren().add(imageView);
imageView.fitWidthProperty().bind(primaryStage.widthProperty());
imageView.fitHeightProperty().bind(primaryStage.heightProperty());
Scene scene = new Scene(root, 640, 480);
primaryStage.setTitle("Video + audio");
primaryStage.setScene(scene);
primaryStage.show();
playThread = new Thread(() -> {
try {
String videoFilename = getParameters().getRaw().get(0);
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(videoFilename);
grabber.start();
primaryStage.setWidth(grabber.getImageWidth());
primaryStage.setHeight(grabber.getImageHeight());
AudioFormat audioFormat = new AudioFormat(grabber.getSampleRate(), 16, grabber.getAudioChannels(), true, true);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
SourceDataLine soundLine = (SourceDataLine) AudioSystem.getLine(info);
soundLine.open(audioFormat);
soundLine.start();
Java2DFrameConverter converter = new Java2DFrameConverter();
ExecutorService executor = Executors.newSingleThreadExecutor();
while (!Thread.interrupted()) {
Frame frame = grabber.grab();
if (frame == null) {
break;
}
if (frame.image != null) {
Image image = SwingFXUtils.toFXImage(converter.convert(frame), null);
Platform.runLater(() -> {
imageView.setImage(image);
});
} else if (frame.samples != null) {
ShortBuffer channelSamplesShortBuffer = (ShortBuffer) frame.samples[0];
channelSamplesShortBuffer.rewind();
ByteBuffer outBuffer = ByteBuffer.allocate(channelSamplesShortBuffer.capacity() * 2);
for (int i = 0; i < channelSamplesShortBuffer.capacity(); i++) {
short val = channelSamplesShortBuffer.get(i);
outBuffer.putShort(val);
}
/**
* We need this because soundLine.write ignores
* interruptions during writing.
*/
try {
executor.submit(() -> {
soundLine.write(outBuffer.array(), 0, outBuffer.capacity());
outBuffer.clear();
}).get();
} catch (InterruptedException interruptedException) {
Thread.currentThread().interrupt();
}
}
}
executor.shutdownNow();
executor.awaitTermination(10, TimeUnit.SECONDS);
soundLine.stop();
grabber.stop();
grabber.release();
Platform.exit();
} catch (Exception exception) {
LOG.log(Level.SEVERE, null, exception);
System.exit(1);
}
});
playThread.start();
}
Aggregations