|
| 1 | +package com.company; |
| 2 | + |
| 3 | +import javax.imageio.ImageIO; |
| 4 | +import javax.sound.sampled.*; |
| 5 | +import javax.swing.*; |
| 6 | +import java.awt.*; |
| 7 | +import java.awt.image.BufferedImage; |
| 8 | +import java.io.*; |
| 9 | +import java.net.InetAddress; |
| 10 | +import java.net.ServerSocket; |
| 11 | +import java.net.Socket; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.Vector; |
| 14 | + |
| 15 | +public class ClientSound { |
| 16 | + boolean stopCapture = false; |
| 17 | + AudioFormat audioFormat; |
| 18 | + TargetDataLine targetDataLine; |
| 19 | + ByteArrayOutputStream out = null; |
| 20 | + ObjectOutputStream out2 = null; |
| 21 | + BufferedInputStream in = null; |
| 22 | + Socket sock = null; |
| 23 | + public static void main(String[] args) { |
| 24 | + ClientSound tx = new ClientSound(); |
| 25 | + tx.getFormats(); |
| 26 | + tx.captureAudio(); |
| 27 | + } |
| 28 | + private void captureAudio() { |
| 29 | + try { |
| 30 | + sock = new Socket("DESKTOP-SL4C3HK", 1234); |
| 31 | + out2 = new ObjectOutputStream(sock.getOutputStream()); |
| 32 | + Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo(); |
| 33 | + audioFormat = getAudioFormat(); |
| 34 | + DataLine.Info dataLineInfo = new DataLine.Info( |
| 35 | + TargetDataLine.class, audioFormat); |
| 36 | + Mixer mixer = AudioSystem.getMixer(mixerInfo[7]); |
| 37 | + mixer.open(); |
| 38 | + targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo); |
| 39 | + targetDataLine.open(audioFormat); |
| 40 | + targetDataLine.start(); |
| 41 | + |
| 42 | + Thread captureThread = new CaptureThread(); |
| 43 | + captureThread.start(); |
| 44 | + |
| 45 | + } catch (Exception e) { |
| 46 | + System.out.println(e); |
| 47 | + System.exit(0); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public void getFormats() { |
| 52 | + try { |
| 53 | + Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo(); |
| 54 | + Mixer mixer = AudioSystem.getMixer(mixerInfo[7]); // default mixer |
| 55 | + mixer.open(); |
| 56 | + |
| 57 | + System.out.printf("Supported SourceDataLines of default mixer (%s):\n\n", mixer.getMixerInfo().getName()); |
| 58 | + for (Line.Info info : mixer.getSourceLineInfo()) { |
| 59 | + if (SourceDataLine.class.isAssignableFrom(info.getLineClass())) { |
| 60 | + SourceDataLine.Info info2 = (SourceDataLine.Info) info; |
| 61 | + System.out.println(info2); |
| 62 | + System.out.printf(" max buffer size: \t%d\n", info2.getMaxBufferSize()); |
| 63 | + System.out.printf(" min buffer size: \t%d\n", info2.getMinBufferSize()); |
| 64 | + AudioFormat[] formats = info2.getFormats(); |
| 65 | + System.out.println(" Supported Audio formats: "); |
| 66 | + for (AudioFormat format : formats) { |
| 67 | + System.out.println(" " + format); |
| 68 | + System.out.printf(" encoding: %s\n", format.getEncoding()); |
| 69 | + System.out.printf(" channels: %d\n", format.getChannels()); |
| 70 | + System.out.printf(format.getFrameRate()==-1?"":" frame rate [1/s]: %s\n", format.getFrameRate()); |
| 71 | + System.out.printf(" frame size [bytes]: %d\n", format.getFrameSize()); |
| 72 | + System.out.printf(format.getSampleRate()==-1?"":" sample rate [1/s]: %s\n", format.getSampleRate()); |
| 73 | + System.out.printf(" sample size [bit]: %d\n", format.getSampleSizeInBits()); |
| 74 | + System.out.printf(" big endian: %b\n", format.isBigEndian()); |
| 75 | + |
| 76 | + Map<String,Object> prop = format.properties(); |
| 77 | + if(!prop.isEmpty()) { |
| 78 | + System.out.println(" Properties: "); |
| 79 | + for(Map.Entry<String, Object> entry : prop.entrySet()) { |
| 80 | + System.out.printf(" %s: \t%s\n", entry.getKey(), entry.getValue()); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + System.out.println(); |
| 85 | + } else { |
| 86 | + System.out.println(info.toString()); |
| 87 | + } |
| 88 | + } |
| 89 | + } catch (LineUnavailableException E){ |
| 90 | + System.out.println(E); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public AudioFormat getAudioFormat() { |
| 95 | + float sampleRate = 44100F; |
| 96 | + // You can try also 8000,11025,16000,22050,44100 |
| 97 | + int sampleSizeInBits = 16; |
| 98 | + int channels = 2; |
| 99 | + boolean signed = true; |
| 100 | + boolean bigEndian = true; |
| 101 | + return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, |
| 102 | + bigEndian); |
| 103 | + } |
| 104 | + |
| 105 | + class CaptureThread extends Thread { |
| 106 | + // temporary buffer |
| 107 | + byte tempBuffer[] = new byte[10000]; |
| 108 | + public void run() { |
| 109 | + boolean running = true; |
| 110 | + try { |
| 111 | + while (running) { |
| 112 | + out = new ByteArrayOutputStream(); |
| 113 | + int count = |
| 114 | + targetDataLine.read(tempBuffer, 0, tempBuffer.length); |
| 115 | + if (count > 0) { |
| 116 | + out.write(tempBuffer, 0, count); |
| 117 | + } |
| 118 | + out2.writeObject(out.toByteArray()); |
| 119 | + } |
| 120 | + out.close(); |
| 121 | + } catch (IOException e) { |
| 122 | + System.err.println("I/O problems: " + e); |
| 123 | + System.exit(-1); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments