Skip to content
This repository has been archived by the owner on Jul 8, 2022. It is now read-only.

Commit

Permalink
Sync from next
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed Jun 26, 2021
1 parent 4bdc4ae commit f2425a0
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 36 deletions.
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# sytleguide
kotlin.code.style=official

# Kotlin 1.5.0: https://github.com/korlibs/easy-kotlin-mpp-gradle-plugin
easyPluginVersion=0.15.2
# Kotlin 1.5.20: https://github.com/korlibs/easy-kotlin-mpp-gradle-plugin
easyPluginVersion=0.16.0

# version
group=com.soywiz.korlibs.korau
version=2.0.0-SNAPSHOT

korioVersion=2.1.1
korioVersion=2.2.0

# bintray location
project.bintray.org=korlibs
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class AndroidNativeSoundProvider : NativeSoundProvider() {
@Volatile
var running = true

init {
this.isDaemon = true
}

override fun run() {
val bufferSamples = 4096

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1306,7 +1306,7 @@ object JavaMp3Decoder {
var bits: Int = bits
var number: Int = 0
while (bits > 0) {
val advance: Int = min2(bits, 8 - reader.current)
val advance: Int = min(bits, 8 - reader.current)
bits -= advance
reader.current += advance
number = number or ((((reader.array[reader.index].toInt() and 0xFF) ushr (8 - reader.current)) and (0xFF ushr (8 - advance))) shl bits)
Expand All @@ -1322,7 +1322,7 @@ object JavaMp3Decoder {
var bits: Int = bits
var number: Int = 0
while (bits > 0) {
val advance: Int = min2(bits, 8 - buffer.current)
val advance: Int = min(bits, 8 - buffer.current)
bits -= advance
buffer.current += advance
if (bits != 0 && buffer.lastByte == -1) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.soywiz.korau.internal

@PublishedApi internal fun min2(a: Int, b: Int) = if (a < b) a else b
@PublishedApi internal fun max2(a: Int, b: Int) = if (a > b) a else b

@PublishedApi internal fun min2(a: Float, b: Float) = if (a < b) a else b
@PublishedApi internal fun max2(a: Float, b: Float) = if (a > b) a else b

@PublishedApi internal fun min2(a: Double, b: Double) = if (a < b) a else b
@PublishedApi internal fun max2(a: Double, b: Double) = if (a > b) a else b






Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package com.soywiz.korau.sound
import com.soywiz.klock.*
import com.soywiz.kmem.*
import com.soywiz.korau.format.*
import com.soywiz.korau.internal.*
import com.soywiz.korau.internal.min2
import com.soywiz.korio.file.*
import com.soywiz.korio.lang.*
import kotlin.math.*
Expand Down Expand Up @@ -46,7 +44,7 @@ fun AudioData.withRate(rate: Int) = AudioData(rate, samples)
// for (chunk in 0 until ceil(out.totalSamples.toDouble() / MAX_CHUNK_SIZE).toInt()) {
// val offset = chunk * MAX_CHUNK_SIZE
// val available = out.totalSamples - offset
// val chunkSize = min2(MAX_CHUNK_SIZE, available)
// val chunkSize = min(MAX_CHUNK_SIZE, available)
// //println("chunk=$chunk, offset=$offset, available=$available, chunkSize=$chunkSize")
// for (channel in 0 until channels) {
// for (n in 0 until chunkSize) {
Expand All @@ -72,7 +70,7 @@ class AudioDataStream(val data: AudioData) : AudioStream(data.rate, data.channel

override suspend fun read(out: AudioSamples, offset: Int, length: Int): Int {
val available = data.samples.totalSamples - cursor
val toread = min2(available, length)
val toread = min(available, length)
if (toread > 0) {
for (n in 0 until channels) {
arraycopy(data.samples[n], cursor, out[n], offset, toread)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class AudioSamplesProcessor(val channels: Int, val totalSamples: Int, val data:
return this
}
fun add(samples: AudioSamples, scale: Float = 1f): AudioSamplesProcessor {
for (ch in 0 until min2(channels, samples.channels)) {
for (ch in 0 until min(channels, samples.channels)) {
val odata = this.data[ch]
val idata = samples.data[ch]
for (n in 0 until samples.totalSamples) {
Expand All @@ -57,7 +57,7 @@ class AudioSamplesProcessor(val channels: Int, val totalSamples: Int, val data:
val odata = this.data[ch]
var maxAbs = 0f
for (n in 0 until totalSamples) {
maxAbs = max2(maxAbs, odata[n].absoluteValue)
maxAbs = max(maxAbs, odata[n].absoluteValue)
}
if (maxAbs > 1f) {
val invMaxAbs = 1f / maxAbs
Expand All @@ -69,7 +69,7 @@ class AudioSamplesProcessor(val channels: Int, val totalSamples: Int, val data:
return this
}
fun copyTo(samples: AudioSamples) {
for (ch in 0 until min2(channels, samples.channels)) {
for (ch in 0 until min(channels, samples.channels)) {
val idata = this.data[ch]
val odata = samples.data[ch]
for (n in 0 until samples.totalSamples) {
Expand Down Expand Up @@ -105,8 +105,8 @@ class AudioSamples(override val channels: Int, override val totalSamples: Int, v
}

fun copyTo(that: AudioSamples) {
for (ch in 0 until min2(channels, that.channels)) {
arraycopy(this.data[ch], 0, that.data[ch], 0, min2(totalSamples, that.totalSamples))
for (ch in 0 until min(channels, that.channels)) {
arraycopy(this.data[ch], 0, that.data[ch], 0, min(totalSamples, that.totalSamples))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,19 @@ class AudioSamplesDeque(val channels: Int) {
}

fun read(out: AudioSamples, offset: Int = 0, len: Int = out.totalSamples - offset): Int {
val result = min2(len, availableRead)
val result = min(len, availableRead)
for (channel in 0 until out.channels) this.buffer[channel].read(out[channel], offset, len)
return result
}

fun read(out: AudioSamplesInterleaved, offset: Int = 0, len: Int = out.totalSamples - offset): Int {
val result = min2(len, availableRead)
val result = min(len, availableRead)
for (channel in 0 until out.channels) for (n in 0 until len) out[channel, offset + n] = this.read(channel)
return result
}

fun read(out: IAudioSamples, offset: Int = 0, len: Int = out.totalSamples - offset): Int {
val result = min2(len, availableRead)
val result = min(len, availableRead)
when (out) {
is AudioSamples -> read(out, offset, len)
is AudioSamplesInterleaved -> read(out, offset, len)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ abstract class AudioStream(
break
}
}
val read = min2(length, availableRead)
val read = min(length, availableRead)
deque.read(out, offset, read)
return read
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ object AL {
// ALC

@JvmStatic external fun alcCreateContext(device: Pointer, attrlist: IntArray?): Pointer?
@JvmStatic external fun alcMakeContextCurrent(context: Pointer): Boolean
@JvmStatic external fun alcMakeContextCurrent(context: Pointer?): Boolean
@JvmStatic external fun alcProcessContext(context: Pointer)
@JvmStatic external fun alcSuspendContext(context: Pointer)
@JvmStatic external fun alcDestroyContext(context: Pointer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.soywiz.korau.sound.*
import com.soywiz.korio.async.*
import com.soywiz.korio.util.*
import com.soywiz.krypto.encoding.*
import com.sun.jna.*
import kotlinx.coroutines.*
import java.lang.RuntimeException
import java.nio.*
Expand All @@ -23,16 +24,8 @@ class JnaOpenALNativeSoundProvider : NativeSoundProvider() {
val MAX_AVAILABLE_SOURCES = 100
}

val device = (AL.alcOpenDevice(null) ?: throw OpenALException("Can't open OpenAL device")).also { device ->
Runtime.getRuntime().addShutdownHook(Thread {
AL.alcCloseDevice(device)
})
}
val context = (AL.alcCreateContext(device, null) ?: throw OpenALException("Can't get OpenAL context")).also { context ->
Runtime.getRuntime().addShutdownHook(Thread {
//alc?.alcDestroyContext(context) // Crashes on mac!
})
}
val device = (AL.alcOpenDevice(null) ?: throw OpenALException("Can't open OpenAL device"))
val context = (AL.alcCreateContext(device, null) ?: throw OpenALException("Can't get OpenAL context"))

val sourcePool = Pool {
alGenSourceAndInitialize()
Expand All @@ -47,6 +40,10 @@ class JnaOpenALNativeSoundProvider : NativeSoundProvider() {
AL.alcMakeContextCurrent(context)
}

fun unmakeCurrent() {
AL.alcMakeContextCurrent(Pointer.NULL)
}

init {
makeCurrent()

Expand All @@ -56,6 +53,12 @@ class JnaOpenALNativeSoundProvider : NativeSoundProvider() {
checkAlErrors("alListener3f", 0)
AL.alListenerfv(AL.AL_ORIENTATION, floatArrayOf(0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f))
checkAlErrors("alListenerfv", 0)

Runtime.getRuntime().addShutdownHook(Thread {
unmakeCurrent()
AL.alcDestroyContext(context)
AL.alcCloseDevice(device)
})
}

override val audioFormats = nativeAudioFormats
Expand Down

0 comments on commit f2425a0

Please sign in to comment.