Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to play pcm raw data in java [closed]

Tags:

java

audio

pcm

I have PCM samples in a short array. What is the best way to play this out? The format is 8000Hz, Mono, 16 bit, big endian. (The PCM samples are generated in the code and not read through some file)

Thanks

like image 546
Geos Avatar asked Jul 21 '09 04:07

Geos


2 Answers

With the javax.sound.sampled package it's pretty much straightforward, but you have to use some boilerplate.

Here's a good tutorial on that: www.wikijava.org/wiki/Play_a_wave_sound_in_Java

Basically you have to create an InputStream from your array and use that to create an AudioInputStream. There you have to specify the format of your audio data.

Then you open an output stream (SourceDataLine) and copy the bytes from the audio stream into that stream.

like image 51
Daniel Rikowski Avatar answered Sep 22 '22 13:09

Daniel Rikowski


Check out this article - http://download.oracle.com/javase/tutorial/sound/playing.html.

More specifically, read about SourceDataLine and how to set up AudioFormat.

like image 44
Zorayr Avatar answered Sep 23 '22 13:09

Zorayr