Skip to content

Commit

Permalink
audio main app gui
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-f committed Nov 15, 2023
1 parent 921bcfb commit 51df867
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import android.annotation.SuppressLint
import android.media.AudioFormat
import android.media.AudioRecord
import android.media.MediaRecorder
import android.os.Process
import java.util.Arrays
import java.util.concurrent.atomic.AtomicBoolean

class AndroidAudioSource : AudioSource {
class AndroidAudioSource : AudioSource, Runnable {
private lateinit var audioRecord: AudioRecord

private lateinit var callback: AudioCallback
private var bufferSize = -1
private val recording = AtomicBoolean(false)
@SuppressLint("MissingPermission")
override fun setup(
sampleRate: Int,
Expand All @@ -23,12 +28,39 @@ class AndroidAudioSource : AudioSource {
minimalBufferSize > bufferSize ->
return AudioSource.InitializeErrorCode.INITIALIZE_WRONG_BUFFER_SIZE
}
this.bufferSize = bufferSize
audioRecord = AudioRecord(MediaRecorder.AudioSource.VOICE_RECOGNITION, sampleRate, channel, encoding, bufferSize)
this.callback = callback
return AudioSource.InitializeErrorCode.INITIALIZE_OK
}

override fun run() {
recording.set(true)
try {
Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_AUDIO)
} catch (ex: IllegalArgumentException) {
// Ignore
} catch (ex: SecurityException) {
// Ignore
}
audioRecord.startRecording()
var buffer = FloatArray(bufferSize / 4)
while(recording.get()) {
val read: Int = audioRecord.read(
buffer, 0, buffer.size,
AudioRecord.READ_BLOCKING
)
if (read < buffer.size) {
buffer = buffer.copyOfRange(0, read)
}
callback(buffer)
}
}


override fun release() {
TODO("Not yet implemented")
audioRecord.stop()
recording.set(false)
}

override fun getMicrophoneLocation(): AudioSource.MicrophoneLocation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,16 @@ import org.jetbrains.compose.resources.painterResource
fun App() {
MaterialTheme {
var greetingText by remember { mutableStateOf("Hello World!") }
var showImage by remember { mutableStateOf(false) }
val audioSource = CreateAudioSource()
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
Button(onClick = {
greetingText = "Compose: ${Greeting().greet()}"
showImage = !showImage
println(audioSource.setup(48000, 8192, callback = { samples ->
println(samples.sum())
}))
}) {
Text(greetingText)
}
AnimatedVisibility(showImage) {
Image(
painterResource("compose-multiplatform.xml"),
null
)
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.noise_planet.noisecapture

typealias AudioCallback = (samples: FloatArray) -> Unit

/**
* Common interface to access Audio samples from device microphone
* As each device
Expand Down

0 comments on commit 51df867

Please sign in to comment.