-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoundServer.java
137 lines (118 loc) · 4.35 KB
/
SoundServer.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
129
130
131
132
133
134
135
136
137
package com.company;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import javax.sound.sampled.*;
public class SoundServer {
ServerSocket MyService;
Socket clientSocket = null;
ObjectInputStream input;
AudioFormat audioFormat;
SourceDataLine sourceDataLine;
ByteArrayOutputStream out = null;
byte tempBuffer[] = new byte[10000];
SoundServer() {
try {
MyService = new ServerSocket(1234);
InetAddress ip = InetAddress.getLocalHost();
System.out.println("Host Name: " + ip.getHostName());
System.out.println("Waiting for client on port " +
MyService.getLocalPort() + "...");
Socket ser = MyService.accept();
System.out.println("Just connected to " + ser.getRemoteSocketAddress());
input = new ObjectInputStream(ser.getInputStream());
while (true){
playByteArray((byte[]) input.readObject());
}
} catch (Exception e) {
System.out.println(e);
}
}
public void playByteArray(byte[] bytes){
try {
System.out.println("Runs");
Thread a = new play();
((play) a).clip = AudioSystem.getClip(); //generates a generic audio clip check API doc for more info
((play) a).clip.open(getAudioFormat(), bytes, 0, bytes.length);
int start = Math.toIntExact((((play) a).clip.getMicrosecondLength()));
System.out.println(bytes.length);
a.start();
delay(start);
((play) a).clip.close();
}catch (LineUnavailableException E){
System.out.println(E);
}
}
public class play extends Thread {
public Clip clip;
public void run() {
clip.start();
}
}
private void playAudio() {
try {
byte audio[] = out.toByteArray();
InputStream input =
new ByteArrayInputStream(audio);
final AudioFormat format = getAudioFormat();
final AudioInputStream ais =
new AudioInputStream(input, format,
audio.length / format.getFrameSize());
DataLine.Info info = new DataLine.Info(
SourceDataLine.class, format);
final SourceDataLine line = (SourceDataLine)
AudioSystem.getLine(info);
line.open(format);
line.start();
Runnable runner = new Runnable() {
int bufferSize = (int) format.getSampleRate()
* format.getFrameSize();
byte buffer[] = new byte[bufferSize];
public void run() {
try {
int count;
while ((count = ais.read(
buffer, 0, buffer.length)) != -1) {
if (count > 0) {
line.write(buffer, 0, count);
}
}
line.drain();
line.close();
} catch (IOException e) {
System.err.println("I/O problems: " + e);
System.exit(-3);
}
}
};
Thread playThread = new Thread(runner);
playThread.start();
} catch (LineUnavailableException e) {
System.err.println("Line unavailable: " + e);
System.exit(-4);
}
}
public void delay(int time){
try{
Thread.sleep(time);
} catch (InterruptedException E){
System.out.println(E);
}
}
private AudioFormat getAudioFormat() {
float sampleRate = 44100.0F;
int sampleSizeInBits = 16;
int channels = 2;
boolean signed = true;
boolean bigEndian = true;
return new AudioFormat(
sampleRate,
sampleSizeInBits,
channels,
signed,
bigEndian);
}
public static void main(String [] args){
SoundServer s2 = new SoundServer();
}}