整改
This commit is contained in:
parent
c29eca959b
commit
6d388e4db4
@ -1,10 +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
|
||||
import java.net.ServerSocket
|
||||
import java.net.Socket
|
||||
import java.util.*
|
||||
@ -13,8 +10,6 @@ import java.util.concurrent.CopyOnWriteArrayList
|
||||
var clientAudios: MutableList<ClientAudio> = CopyOnWriteArrayList()
|
||||
var BUFFER_SIZE: Int = 2048 * CHANNELS
|
||||
|
||||
var buffer: ByteArray = ByteArray(BUFFER_SIZE * 2)
|
||||
|
||||
fun main() = runBlocking {
|
||||
//混音
|
||||
launch(Dispatchers.Default) {
|
||||
@ -31,11 +26,11 @@ fun main() = runBlocking {
|
||||
for (client in clientAudios) {
|
||||
launch(Dispatchers.Default) {
|
||||
for (audio in clientAudios) {
|
||||
if (audio != client)
|
||||
audio.mix(client.mixSample)
|
||||
if (audio != client) {
|
||||
audio.mix(client.sample)
|
||||
}
|
||||
}
|
||||
AudioConvert.convertFloatToShortByte(client.mixSample, buffer)
|
||||
client.send(buffer)
|
||||
client.send()
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -50,35 +45,28 @@ fun main() = runBlocking {
|
||||
}
|
||||
|
||||
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 `in` = socket.getInputStream()
|
||||
val out = socket.getOutputStream()
|
||||
val buffer: ByteArray = ByteArray(BUFFER_SIZE * 2)
|
||||
val sample: FloatArray = FloatArray(BUFFER_SIZE)
|
||||
val mixSample: FloatArray = FloatArray(BUFFER_SIZE)
|
||||
var mixBuffer: ByteArray = ByteArray(BUFFER_SIZE * 2)
|
||||
|
||||
val isConnected: Boolean
|
||||
get() = socket.isConnected
|
||||
|
||||
fun send(buffer: ByteArray) {
|
||||
fun send() {
|
||||
try {
|
||||
out!!.write(buffer)
|
||||
out!!.flush()
|
||||
AudioConvert.convertFloatToShortByte(mixSample, mixBuffer)
|
||||
out.write(mixBuffer)
|
||||
out.flush()
|
||||
} catch (e: IOException) {
|
||||
}
|
||||
}
|
||||
|
||||
fun read() {
|
||||
try {
|
||||
`in`!!.read(buffer)
|
||||
`in`.read(buffer)
|
||||
AudioConvert.convertShortByteToFloat(buffer, sample)
|
||||
Arrays.fill(mixSample, 0f)
|
||||
} catch (e: IOException) {
|
||||
@ -88,7 +76,7 @@ class ClientAudio(var socket: Socket) {
|
||||
|
||||
fun mix(sample: FloatArray) {
|
||||
for (i in sample.indices) {
|
||||
sample[i] += this.sample[i]
|
||||
mixSample[i] += sample[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user