java - Play raw PCM audio received in UDP packets -
the remote device sending live raw pcm audio(no header included) in udp packets , need implement program in java receive these packets , play them on pc live. know raw pcm's attributes 16bit, mono, sampling rate 24khz, tried add wav header raw pcm audio , play problem don't have file size of audio.
i implemented program based on this link gives noise in output.
i bound use udp , can raw pcm remote device, library or api can play raw audio on pc?
here simple example obtaining output line , playing pcm on it. when run plays second long annoying beep.
import javax.sound.sampled.audioformat; import javax.sound.sampled.audiosystem; import javax.sound.sampled.dataline; import javax.sound.sampled.sourcedataline; public class rawaudioplay { public static void main(string[] args) { try { // select audio format parameters audioformat af = new audioformat(24000, 16, 1, true, false); dataline.info info = new dataline.info(sourcedataline.class, af); sourcedataline line = (sourcedataline) audiosystem.getline(info); // generate pcm data (a sine wave simplicity) byte[] buffer = new byte[64]; double step = math.pi / buffer.length; double angle = math.pi * 2; int = buffer.length; while (i > 0) { double sine = math.sin(angle); int sample = (int) math.round(sine * 32767); buffer[--i] = (byte) (sample >> 8); buffer[--i] = (byte) sample; angle -= step; } // prepare audio output line.open(af, 4096); line.start(); // output wave form repeatedly (int n=0; n<500; ++n) { line.write(buffer, 0, buffer.length); } // shut down audio line.drain(); line.stop(); line.close(); } catch (exception e) { throw new runtimeexception(e.getmessage(), e); } } } you see, ten lines handling line, half of code section "generate pcm" can ignore if pcm somehwere else. need pay attention creation of correct audioformat, screw booleans signed and/or endian , pcm sound garbled, possibly not recognizable.
Comments
Post a Comment