Skip to content

Commit

Permalink
move package, interface audio
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-f committed Nov 13, 2023
1 parent f5152f7 commit e7f29cd
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 4 deletions.
1 change: 1 addition & 0 deletions NoiseCapture/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.noise_planet.noisecapture

import android.annotation.SuppressLint
import android.media.AudioFormat
import android.media.AudioRecord
import android.media.MediaRecorder

class AndroidAudioSource : AudioSource {
private lateinit var audioRecord: AudioRecord

@SuppressLint("MissingPermission")
override fun setup(
sampleRate: Int,
bufferSize: Int,
callback: AudioCallback
): AudioSource.InitializeErrorCode {
val channel = AudioFormat.CHANNEL_IN_MONO
val encoding = AudioFormat.ENCODING_PCM_FLOAT
val minimalBufferSize = AudioRecord.getMinBufferSize(sampleRate, channel, encoding)
when {
minimalBufferSize == AudioRecord.ERROR_BAD_VALUE || minimalBufferSize == AudioRecord.ERROR ->
return AudioSource.InitializeErrorCode.INITIALIZE_SAMPLE_RATE_NOT_SUPPORTED
minimalBufferSize > bufferSize ->
return AudioSource.InitializeErrorCode.INITIALIZE_WRONG_BUFFER_SIZE
}
audioRecord = AudioRecord(MediaRecorder.AudioSource.VOICE_RECOGNITION, sampleRate, channel, encoding, bufferSize)
return AudioSource.InitializeErrorCode.INITIALIZE_OK
}

override fun release() {
TODO("Not yet implemented")
}

override fun getMicrophoneLocation(): AudioSource.MicrophoneLocation {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.noise_planet.noisecapture

import org.noise_planet.noisecapture.AndroidAudioSource
import org.noise_planet.noisecapture.AudioSource

actual fun CreateAudioSource(): AudioSource {
return AndroidAudioSource()
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.noise_planet.noisecapture

import App
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package org.noise_planet.noisecapture

import android.os.Build

class AndroidPlatform : Platform {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.noise_planet.noisecapture

import Greeting
import org.junit.Assert.assertTrue
import org.junit.Test

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

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.noise_planet.noisecapture

interface AudioCallback {
fun onAudio(samples: FloatArray)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.noise_planet.noisecapture

/**
* Common interface to access Audio samples from device microphone
* As each device
*/
interface AudioSource {
enum class MicrophoneLocation { LOCATION_UNKNOWN, LOCATION_MAIN_BODY,
LOCATION_MAIN_BODY_MOVABLE, LOCATION_PERIPHERAL }

enum class InitializeErrorCode { INITIALIZE_OK, INITIALIZE_WRONG_BUFFER_SIZE,
INITIALIZE_SAMPLE_RATE_NOT_SUPPORTED}
/**
* @param sampleRate Sample rate in Hz
* @param bufferSize Buffer size in bytes
* @return InitializeErrorCode instance
*/
fun setup(sampleRate: Int, bufferSize: Int, callback: AudioCallback) : InitializeErrorCode

fun release()

fun getMicrophoneLocation() : MicrophoneLocation

}

expect fun CreateAudioSource(): AudioSource
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package org.noise_planet.noisecapture

class Greeting {
private val platform = getPlatform()

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

interface Platform {
val name: String
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.noise_planet.noisecapture

import Greeting
import kotlin.test.Test
import kotlin.test.assertTrue

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

import androidx.compose.ui.window.ComposeUIViewController

fun MainViewController() = ComposeUIViewController { App() }
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package org.noise_planet.noisecapture

import platform.UIKit.UIDevice

class IOSPlatform: Platform {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.noise_planet.noisecapture

import Greeting
import kotlin.test.Test
import kotlin.test.assertTrue

Expand Down

0 comments on commit e7f29cd

Please sign in to comment.