Skip to content

Commit

Permalink
WIP: Add playing demo at Android app
Browse files Browse the repository at this point in the history
  • Loading branch information
Danand committed Mar 10, 2024
1 parent 5d6efdb commit 6c18f1f
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.danand.juicynoise

import android.media.AudioAttributes
import android.media.AudioFormat
import android.media.AudioTrack

import kotlin.random.Random

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch

class AudioOutput {
private lateinit var scope: CoroutineScope

fun play(
samplingRate: Int,
bufferSize: Int,
) {
scope = CoroutineScope(Dispatchers.IO + SupervisorJob())

scope.launch(Dispatchers.IO) {
val audioTrack = AudioTrack.Builder()
.setAudioAttributes(
AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build()
)
.setAudioFormat(
AudioFormat.Builder()
.setEncoding(AudioFormat.ENCODING_PCM_FLOAT)
.setSampleRate(samplingRate)
.setChannelMask(AudioFormat.CHANNEL_OUT_STEREO)
.build()
)
.setTransferMode(AudioTrack.MODE_STREAM)
.setBufferSizeInBytes(bufferSize)
.build()

audioTrack.play()

while (isActive) {
val floatArray = FloatArray(bufferSize) { Random.Default.nextFloat() }
audioTrack.write(floatArray, 0, bufferSize, AudioTrack.WRITE_NON_BLOCKING)
}

audioTrack.stop()
}
}

fun stop() {
scope.cancel()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class MainActivity : ComponentActivity(), SensorEventListener {

private lateinit var sensorManager: SensorManager

private var audioOutput: AudioOutput = AudioOutput()

private var gyroscope: Sensor? = null
private var accelerometer: Sensor? = null
private var rotationVector: Sensor? = null
Expand Down Expand Up @@ -157,6 +159,7 @@ class MainActivity : ComponentActivity(), SensorEventListener {
sampleRateState,
locationClient,
connectivityManager,
audioOutput,
)
}
}
Expand Down Expand Up @@ -290,6 +293,7 @@ fun ColumnMain(
sampleRateState: MutableState<Int>,
locationClient: FusedLocationProviderClient,
connectivityManager: ConnectivityManager,
audioOutput: AudioOutput,
) {
LaunchedEffect(portState) {
val subnet = findSubnet(connectivityManager)
Expand Down Expand Up @@ -377,6 +381,11 @@ fun ColumnMain(
ButtonDisconnect(
isRunningState,
)

ButtonStopDemo(
isRunningState,
audioOutput,
)
} else {
ButtonConnect(
ipState.value,
Expand All @@ -390,6 +399,15 @@ fun ColumnMain(
) {
checkIsValidIp(ipState.value)
}

ButtonPlayDemo(
isRunningState,
sensorsState,
audioBufferSizeState,
sampleRateState,
locationClient,
audioOutput,
)
}

Spacer(modifier = Modifier.height(24.dp))
Expand Down Expand Up @@ -537,6 +555,44 @@ fun ButtonConnect(
}
}

@Composable
fun ButtonPlayDemo(
isRunningState: MutableState<Boolean>,
sensorsState: MutableState<Sensors>,
audioBufferSizeState: MutableState<AudioBufferSize>,
sampleRateState: MutableState<Int>,
locationClient: FusedLocationProviderClient,
audioOutput: AudioOutput,
) {
Button(
onClick = {
isRunningState.value = true

runReadingLocation(
locationClient,
isRunningState,
sensorsState,
)

audioOutput.play(
sampleRateState.value,
audioBufferSizeState.value.value,
)
},
colors = textButtonColors(
containerColor = MaterialTheme.colorScheme.primaryContainer
),
elevation = ButtonDefaults.buttonElevation(
defaultElevation = 0.dp,
pressedElevation = 16.dp,
disabledElevation = 0.dp,
),
enabled = true
) {
Text("Play demo")
}
}

@Composable
fun ButtonDisconnect(
isRunning: MutableState<Boolean>,
Expand All @@ -558,6 +614,29 @@ fun ButtonDisconnect(
}
}

@Composable
fun ButtonStopDemo(
isRunning: MutableState<Boolean>,
audioOutput: AudioOutput,
) {
Button(
onClick = {
isRunning.value = false
audioOutput.stop()
},
colors = textButtonColors(
containerColor = MaterialTheme.colorScheme.primaryContainer
),
elevation = ButtonDefaults.buttonElevation(
defaultElevation = 0.dp,
pressedElevation = 16.dp,
disabledElevation = 0.dp,
),
) {
Text("Stop demo")
}
}

fun createAddressState(): AddressState = AddressState(
ip = mutableStateOf("192.168.0.128"),
port = mutableStateOf(6660u),
Expand Down

0 comments on commit 6c18f1f

Please sign in to comment.