From c29eca959bb20186ad9ec106cfd290f8cb57d5fc Mon Sep 17 00:00:00 2001 From: loliball <26589867+loliball@users.noreply.github.com> Date: Sat, 31 Aug 2024 03:38:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Client.kt | 118 ++++++++++++++++++++++------------------------ src/Server.kt | 128 +++++++++++++++++++++++++------------------------- 2 files changed, 118 insertions(+), 128 deletions(-) diff --git a/src/Client.kt b/src/Client.kt index a409d46..4f00e78 100644 --- a/src/Client.kt +++ b/src/Client.kt @@ -1,75 +1,67 @@ +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking import java.io.IOException import java.io.InputStream import java.io.OutputStream import java.net.Socket import javax.sound.sampled.AudioFormat import javax.sound.sampled.AudioSystem -import javax.sound.sampled.SourceDataLine -import javax.sound.sampled.TargetDataLine + +var `in`: InputStream? = null +var out: OutputStream? = null +var sample = FloatArray(BUFFER_SIZE) +var bufferOut = ByteArray(sample.size * 2) +var bufferIn = ByteArray(sample.size * 2) +var SAMPLE_RATE: Int = 48000 +var CHANNELS: Int = 1 +var format: AudioFormat = AudioConvert.getAudioFormat(SAMPLE_RATE, CHANNELS) +var socket: Socket? = null +var targetDataLine = AudioSystem.getTargetDataLine(format) +var sourceDataLine = AudioSystem.getSourceDataLine(format) object Client { - var `in`: InputStream? = null - var out: OutputStream? = null - var sourceDataLine: SourceDataLine? = null - var targetDataLine: TargetDataLine? = null - var bufferOut: ByteArray - var bufferIn: ByteArray - var sample: FloatArray - var SAMPLE_RATE: Int = 48000 - var CHANNELS: Int = 1 - var format: AudioFormat = AudioConvert.getAudioFormat(SAMPLE_RATE, CHANNELS) - var socket: Socket? = null - - @Throws(Exception::class) @JvmStatic fun main(args: Array) { - socket = Socket("127.0.0.1", 7860) - `in` = socket!!.getInputStream() - out = socket!!.getOutputStream() - sample = FloatArray(Server.BUFFER_SIZE) - bufferIn = ByteArray(sample.size * 2) - bufferOut = ByteArray(sample.size * 2) - - targetDataLine = AudioSystem.getTargetDataLine(format) - targetDataLine.open(format, bufferIn.size) - targetDataLine.start() - - - sourceDataLine = AudioSystem.getSourceDataLine(format) - sourceDataLine.open(format, bufferIn.size) - sourceDataLine.start() - - - Thread { - while (true) { - //Arrays.fill(bufferOut, (byte) 0); - targetDataLine.read(bufferOut, 0, bufferOut.size) - try { - out.write(bufferOut, 0, bufferOut.size) - out.flush() - } catch (e: Exception) { - e.printStackTrace() - System.exit(0) - } - } - }.start() - - - Thread { - while (true) { - try { - `in`.read(bufferIn, 0, bufferIn.size) - sourceDataLine.write(bufferIn, 0, bufferIn.size) - } catch (e: IOException) { - e.printStackTrace() - System.exit(0) - } - } - }.start() - - - /* - audioOutputLine.setAudioProcessor(); - audioOutputLine.open(48000, 1, 20480);*/ + mainClient() } } + +fun mainClient() = runBlocking { + socket = Socket("127.0.0.1", 7860) + `in` = socket!!.getInputStream() + out = socket!!.getOutputStream() + + targetDataLine.open(format, bufferIn.size) + targetDataLine.start() + + sourceDataLine.open(format, bufferIn.size) + sourceDataLine.start() + + launch(Dispatchers.Default) { + while (true) { + //Arrays.fill(bufferOut, (byte) 0); + targetDataLine.read(bufferOut, 0, bufferOut.size) + try { + out!!.write(bufferOut, 0, bufferOut.size) + out!!.flush() + } catch (e: Exception) { + e.printStackTrace() + System.exit(0) + } + } + } + + launch { + while (true) { + try { + `in`!!.read(bufferIn, 0, bufferIn.size) + sourceDataLine.write(bufferIn, 0, bufferIn.size) + } catch (e: IOException) { + e.printStackTrace() + System.exit(0) + } + } + } + +} \ No newline at end of file diff --git a/src/Server.kt b/src/Server.kt index 4319099..aa1d5db 100644 --- a/src/Server.kt +++ b/src/Server.kt @@ -1,3 +1,7 @@ +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking import java.io.IOException import java.io.InputStream import java.io.OutputStream @@ -6,91 +10,85 @@ import java.net.Socket import java.util.* import java.util.concurrent.CopyOnWriteArrayList -object Server { - var clientAudios: MutableList = CopyOnWriteArrayList() - @kotlin.jvm.JvmField - var BUFFER_SIZE: Int = 2048 * Client.CHANNELS - var mixSample: FloatArray = FloatArray(BUFFER_SIZE) - var buffer: ByteArray = ByteArray(BUFFER_SIZE * 2) +var clientAudios: MutableList = CopyOnWriteArrayList() +var BUFFER_SIZE: Int = 2048 * CHANNELS - @kotlin.Throws(Exception::class) - @kotlin.jvm.JvmStatic - fun main(args: Array) { - //混音 - Thread { - while (true) { - for (client in clientAudios) { - if (!client.isConnected) { - clientAudios.remove(client) - println("现在是: " + clientAudios) - continue - } +var buffer: ByteArray = ByteArray(BUFFER_SIZE * 2) - client.read() +fun main() = runBlocking { + //混音 + launch(Dispatchers.Default) { + while (true) { + for (client in clientAudios) { + if (!client.isConnected) { + clientAudios.remove(client) + println("现在是: " + clientAudios) + continue } + client.read() + } - - for (client in clientAudios) { - Arrays.fill(mixSample, 0f) + for (client in clientAudios) { + launch(Dispatchers.Default) { for (audio in clientAudios) { - //if (audio != client) - audio.mix(mixSample) + if (audio != client) + audio.mix(client.mixSample) } - AudioConvert.convertFloatToShortByte(mixSample, buffer) + AudioConvert.convertFloatToShortByte(client.mixSample, buffer) client.send(buffer) } - - //System.out.println("混音"); } - }.start() - - val sever = ServerSocket(7860) - while (true) { - val socket = sever.accept() - println("Accepted connection from " + socket.remoteSocketAddress) - clientAudios.add(ClientAudio(socket)) } } - class ClientAudio(var socket: Socket) { - var `in`: InputStream? = null - var out: OutputStream? = null - var buffer: ByteArray = ByteArray(BUFFER_SIZE * 2) - var sample: FloatArray = FloatArray(BUFFER_SIZE) + val sever = ServerSocket(7860) + while (true) { + val socket = sever.accept() + println("Accepted connection from " + socket.remoteSocketAddress) + clientAudios.add(ClientAudio(socket)) + } +} - init { - try { - `in` = socket.getInputStream() - out = socket.getOutputStream() - } catch (e: IOException) { - throw RuntimeException(e) - } +class ClientAudio(var socket: Socket) { + var `in`: InputStream? = null + var out: OutputStream? = null + var buffer: ByteArray = ByteArray(BUFFER_SIZE * 2) + var sample: FloatArray = FloatArray(BUFFER_SIZE) + var mixSample: FloatArray = FloatArray(BUFFER_SIZE) + + init { + try { + `in` = socket.getInputStream() + out = socket.getOutputStream() + } catch (e: IOException) { + throw RuntimeException(e) } + } - val isConnected: Boolean - get() = socket.isConnected + val isConnected: Boolean + get() = socket.isConnected - fun send(buffer: ByteArray) { - try { - out!!.write(buffer) - out!!.flush() - } catch (e: IOException) { - } + fun send(buffer: ByteArray) { + try { + out!!.write(buffer) + out!!.flush() + } catch (e: IOException) { } + } - fun read() { - try { - `in`!!.read(buffer) - AudioConvert.convertShortByteToFloat(buffer, sample) - } catch (e: IOException) { - //throw new RuntimeException(e); - } + fun read() { + try { + `in`!!.read(buffer) + AudioConvert.convertShortByteToFloat(buffer, sample) + Arrays.fill(mixSample, 0f) + } catch (e: IOException) { + //throw new RuntimeException(e); } + } - fun mix(sample: FloatArray) { - for (i in sample.indices) { - sample[i] += this.sample[i] - } + fun mix(sample: FloatArray) { + for (i in sample.indices) { + sample[i] += this.sample[i] } } }