Guest User

Untitled

a guest
Dec 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.54 KB | None | 0 0
  1. import java.io.ByteArrayOutputStream;
  2.  
  3. import javax.sound.sampled.AudioFormat;
  4. import javax.sound.sampled.AudioSystem;
  5. import javax.sound.sampled.DataLine;
  6. import javax.sound.sampled.LineUnavailableException;
  7. import javax.sound.sampled.TargetDataLine;
  8.  
  9. public class MicrophoneRecorder {
  10. static boolean stopped = false;
  11.  
  12. public static void main(String[] args) {
  13. AudioFormat format = new AudioFormat(8000.0f, 16, 1, true, true);
  14. TargetDataLine line = null;
  15. DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
  16. if (!AudioSystem.isLineSupported(info)) {
  17. System.out.println("Not supported!");
  18. }
  19. try {
  20. line = (TargetDataLine) AudioSystem.getLine(info);
  21. line.open(format);
  22. } catch (LineUnavailableException ex) {
  23. ex.printStackTrace();
  24. }
  25. ByteArrayOutputStream out = new ByteArrayOutputStream();
  26. int numBytesRead = 0;
  27. byte[] data = new byte[line.getBufferSize() / 5];
  28. line.start();
  29.  
  30. new Thread(new Runnable() {
  31.  
  32. @Override
  33. public void run() {
  34. try {
  35. Thread.sleep(3000);
  36. } catch (InterruptedException e) {
  37. e.printStackTrace();
  38. }
  39. stopped = true;
  40. }
  41. }).start();
  42. while (!stopped) {
  43. numBytesRead = line.read(data, 0, data.length);
  44. out.write(data, 0, numBytesRead);
  45. }
  46. }
  47.  
  48. }
  49.  
  50. import javax.swing.*;
  51. import java.awt.*;
  52. import java.awt.event.*;
  53. import java.io.*;
  54. import java.net.*;
  55. import javax.sound.sampled.*;
  56.  
  57. public class VUClient extends JFrame {
  58.  
  59. boolean stopaudioCapture = false;
  60. ByteArrayOutputStream byteOutputStream;
  61. AudioFormat adFormat;
  62. TargetDataLine targetDataLine;
  63. AudioInputStream InputStream;
  64. SourceDataLine sourceLine;
  65. Graphics g;
  66.  
  67. public static void main(String args[]) {
  68. new VUClient();
  69. }
  70.  
  71. public VUClient() {
  72. final JButton capture = new JButton("Capture");
  73. final JButton stop = new JButton("Stop");
  74. final JButton play = new JButton("Playback");
  75.  
  76. capture.setEnabled(true);
  77. stop.setEnabled(false);
  78. play.setEnabled(false);
  79.  
  80. capture.addActionListener(new ActionListener() {
  81. public void actionPerformed(ActionEvent e) {
  82. capture.setEnabled(false);
  83. stop.setEnabled(true);
  84. play.setEnabled(false);
  85. captureAudio();
  86. }
  87. });
  88. getContentPane().add(capture);
  89.  
  90. stop.addActionListener(new ActionListener() {
  91. public void actionPerformed(ActionEvent e) {
  92. capture.setEnabled(true);
  93. stop.setEnabled(false);
  94. play.setEnabled(true);
  95. stopaudioCapture = true;
  96. targetDataLine.close();
  97. }
  98. });
  99. getContentPane().add(stop);
  100.  
  101. play.addActionListener(new ActionListener() {
  102. @Override
  103. public void actionPerformed(ActionEvent e) {
  104. playAudio();
  105. }
  106. });
  107. getContentPane().add(play);
  108.  
  109. getContentPane().setLayout(new FlowLayout());
  110. setTitle("Capture/Playback Demo");
  111. setDefaultCloseOperation(EXIT_ON_CLOSE);
  112. setSize(400, 100);
  113. getContentPane().setBackground(Color.white);
  114. setVisible(true);
  115.  
  116. g = (Graphics) this.getGraphics();
  117. }
  118.  
  119. private void captureAudio() {
  120. try {
  121. adFormat = getAudioFormat();
  122. DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, adFormat);
  123. targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
  124. targetDataLine.open(adFormat);
  125. targetDataLine.start();
  126.  
  127. Thread captureThread = new Thread(new CaptureThread());
  128. captureThread.start();
  129. } catch (Exception e) {
  130. StackTraceElement stackEle[] = e.getStackTrace();
  131. for (StackTraceElement val : stackEle) {
  132. System.out.println(val);
  133. }
  134. System.exit(0);
  135. }
  136. }
  137.  
  138. private void playAudio() {
  139. try {
  140. byte audioData[] = byteOutputStream.toByteArray();
  141. InputStream byteInputStream = new ByteArrayInputStream(audioData);
  142. AudioFormat adFormat = getAudioFormat();
  143. InputStream = new AudioInputStream(byteInputStream, adFormat, audioData.length / adFormat.getFrameSize());
  144. DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, adFormat);
  145. sourceLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
  146. sourceLine.open(adFormat);
  147. sourceLine.start();
  148. Thread playThread = new Thread(new PlayThread());
  149. playThread.start();
  150. } catch (Exception e) {
  151. System.out.println(e);
  152. System.exit(0);
  153. }
  154. }
  155.  
  156. private AudioFormat getAudioFormat() {
  157. float sampleRate = 16000.0F;
  158. int sampleInbits = 16;
  159. int channels = 1;
  160. boolean signed = true;
  161. boolean bigEndian = false;
  162. return new AudioFormat(sampleRate, sampleInbits, channels, signed, bigEndian);
  163. }
  164.  
  165. class CaptureThread extends Thread {
  166.  
  167. byte tempBuffer[] = new byte[10000];
  168.  
  169. public void run() {
  170.  
  171. byteOutputStream = new ByteArrayOutputStream();
  172. stopaudioCapture = false;
  173. try {
  174. DatagramSocket clientSocket = new DatagramSocket(8786);
  175. InetAddress IPAddress = InetAddress.getByName("127.0.0.1");
  176. while (!stopaudioCapture) {
  177. int cnt = targetDataLine.read(tempBuffer, 0, tempBuffer.length);
  178. if (cnt > 0) {
  179. DatagramPacket sendPacket = new DatagramPacket(tempBuffer, tempBuffer.length, IPAddress, 9786);
  180. clientSocket.send(sendPacket);
  181. byteOutputStream.write(tempBuffer, 0, cnt);
  182. }
  183. }
  184. byteOutputStream.close();
  185. } catch (Exception e) {
  186. System.out.println("CaptureThread::run()" + e);
  187. System.exit(0);
  188. }
  189. }
  190. }
  191.  
  192. class PlayThread extends Thread {
  193.  
  194. byte tempBuffer[] = new byte[10000];
  195.  
  196. public void run() {
  197. try {
  198. int cnt;
  199. while ((cnt = InputStream.read(tempBuffer, 0, tempBuffer.length)) != -1) {
  200. if (cnt > 0) {
  201. sourceLine.write(tempBuffer, 0, cnt);
  202. }
  203. }
  204. // sourceLine.drain();
  205. // sourceLine.close();
  206. } catch (Exception e) {
  207. System.out.println(e);
  208. System.exit(0);
  209. }
  210. }
  211. }
  212. }
  213.  
  214. import java.io.*;
  215. import java.net.*;
  216. import javax.sound.sampled.*;
  217.  
  218. public class VUServer {
  219.  
  220. ByteArrayOutputStream byteOutputStream;
  221. AudioFormat adFormat;
  222. TargetDataLine targetDataLine;
  223. AudioInputStream InputStream;
  224. SourceDataLine sourceLine;
  225.  
  226. private AudioFormat getAudioFormat() {
  227. float sampleRate = 16000.0F;
  228. int sampleInbits = 16;
  229. int channels = 1;
  230. boolean signed = true;
  231. boolean bigEndian = false;
  232. return new AudioFormat(sampleRate, sampleInbits, channels, signed, bigEndian);
  233. }
  234.  
  235. public static void main(String args[]) {
  236. new VUServer().runVOIP();
  237. }
  238.  
  239. public void runVOIP() {
  240. try {
  241. DatagramSocket serverSocket = new DatagramSocket(9786);
  242. byte[] receiveData = new byte[10000];
  243. while (true) {
  244. DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
  245. serverSocket.receive(receivePacket);
  246. System.out.println("RECEIVED: " + receivePacket.getAddress().getHostAddress() + " " + receivePacket.getPort());
  247. try {
  248. byte audioData[] = receivePacket.getData();
  249. InputStream byteInputStream = new ByteArrayInputStream(audioData);
  250. AudioFormat adFormat = getAudioFormat();
  251. InputStream = new AudioInputStream(byteInputStream, adFormat, audioData.length / adFormat.getFrameSize());
  252. DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, adFormat);
  253. sourceLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
  254. sourceLine.open(adFormat);
  255. sourceLine.start();
  256. Thread playThread = new Thread(new PlayThread());
  257. playThread.start();
  258. } catch (Exception e) {
  259. System.out.println(e);
  260. System.exit(0);
  261. }
  262. }
  263. } catch (Exception e) {
  264. e.printStackTrace();
  265. }
  266. }
  267.  
  268. class PlayThread extends Thread {
  269.  
  270. byte tempBuffer[] = new byte[10000];
  271.  
  272. public void run() {
  273. try {
  274. int cnt;
  275. while ((cnt = InputStream.read(tempBuffer, 0, tempBuffer.length)) != -1) {
  276. if (cnt > 0) {
  277. sourceLine.write(tempBuffer, 0, cnt);
  278. }
  279. }
  280. // sourceLine.drain();
  281. // sourceLine.close();
  282. } catch (Exception e) {
  283. System.out.println(e);
  284. System.exit(0);
  285. }
  286. }
  287. }
  288. }
  289.  
  290. public class Sender {
  291.  
  292. public static void main(String[] args) throws IOException {
  293.  
  294. //AudioFormat format = new AudioFormat(8000.0f, 16, 1, true, true);
  295. AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 16, 2, 4, 44100, true);
  296. TargetDataLine microphone;
  297. SourceDataLine speakers;
  298. try {
  299. microphone = AudioSystem.getTargetDataLine(format);
  300.  
  301. DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
  302. microphone = (TargetDataLine) AudioSystem.getLine(info);
  303. microphone.open(format);
  304.  
  305. ByteArrayOutputStream out = new ByteArrayOutputStream();
  306. int numBytesRead;
  307. int CHUNK_SIZE = 1024;
  308. byte[] data = new byte[microphone.getBufferSize() / 5];
  309. microphone.start();
  310.  
  311.  
  312. DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format);
  313. speakers = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
  314. speakers.open(format);
  315. speakers.start();
  316.  
  317.  
  318. // Configure the ip and port
  319. String hostname = "localhost";
  320. int port = 5555;
  321.  
  322. InetAddress address = InetAddress.getByName(hostname);
  323. DatagramSocket socket = new DatagramSocket();
  324. byte[] buffer = new byte[1024];
  325. for(;;) {
  326. numBytesRead = microphone.read(data, 0, CHUNK_SIZE);
  327. // bytesRead += numBytesRead;
  328. // write the mic data to a stream for use later
  329. out.write(data, 0, numBytesRead);
  330. // write mic data to stream for immediate playback
  331. speakers.write(data, 0, numBytesRead);
  332. DatagramPacket request = new DatagramPacket(data,numBytesRead, address, port);
  333. socket.send(request);
  334.  
  335. }
  336.  
  337. } catch (LineUnavailableException e) {
  338. e.printStackTrace();
  339. }
  340. }
  341.  
  342. public class UdpClient {
  343.  
  344. public static void main(String[] args) throws LineUnavailableException {
  345. AudioFormat format = new AudioFormat(8000.0f, 16, 1, true, true);
  346. TargetDataLine microphone;
  347. SourceDataLine speakers;
  348. microphone = AudioSystem.getTargetDataLine(format);
  349.  
  350. DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
  351. microphone = (TargetDataLine) AudioSystem.getLine(info);
  352. microphone.open(format);
  353.  
  354. ByteArrayOutputStream out = new ByteArrayOutputStream();
  355. int numBytesRead;
  356. int CHUNK_SIZE = 1024;
  357. byte[] data = new byte[microphone.getBufferSize() / 5];
  358. microphone.start();
  359.  
  360. int bytesRead = 0;
  361. DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format);
  362. speakers = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
  363. speakers.open(format);
  364. speakers.start();
  365.  
  366. String hostname = "localhost";
  367. int port = 5555;
  368.  
  369. try {
  370. InetAddress address = InetAddress.getByName(hostname);
  371. DatagramSocket socket = new DatagramSocket();
  372.  
  373. DatagramSocket serverSocket = new DatagramSocket(17);
  374. byte[] receiveData = new byte[1024];
  375. byte[] sendData = new byte[1024];
  376.  
  377. while (true) {
  378.  
  379. byte[] buffer = new byte[1024];
  380. DatagramPacket response = new DatagramPacket(buffer, buffer.length);
  381. serverSocket.receive(response);
  382.  
  383. out.write(response.getData(), 0, response.getData().length);
  384. speakers.write(response.getData(), 0, response.getData().length);
  385. String quote = new String(buffer, 0, response.getLength());
  386.  
  387. System.out.println(quote);
  388. System.out.println();
  389.  
  390. //Thread.sleep(10000);
  391. }
  392.  
  393. } catch (SocketTimeoutException ex) {
  394. System.out.println("Timeout error: " + ex.getMessage());
  395. ex.printStackTrace();
  396. } catch (IOException ex) {
  397. System.out.println("Client error: " + ex.getMessage());
  398. ex.printStackTrace();
  399. }/* catch (InterruptedException ex) {
  400. ex.printStackTrace();
  401. }*/
  402. }
Add Comment
Please, Sign In to add comment