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