Frequency Detection Libraries with Android Jetpack Compose

67 Views Asked by At

I have a problem in Frequency Detection. I was using TarsosDSP for Android, but when I updated to use Android 14, the Jetpack Compose libraries, and the ViewModel, it isn't running on physical devices. I have not been able to see the log.

View Model

class PitchDetectionViewModel: ViewModel() {
    private var _currentFrequency = MutableStateFlow(262.5f)

    val currentFrequency: StateFlow<Float>
        get() = _currentFrequency

    private var _currentNote = MutableStateFlow(Frequency convertFrequencyToNote currentFrequency.value)

    val currentNote: StateFlow<String>
        get() = _currentNote

    private var _scale = MutableStateFlow(if(ShrutiBox.currentNote == "") "G" else ShrutiBox.currentNote)

    val scale: StateFlow<String>
        get() = _scale

    private var _format = MutableStateFlow("World")

    val format: StateFlow<String>
        get() = _format

    private var _userFrequency = MutableStateFlow(0f)

    val userFrequency: StateFlow<Float>
        get() = _userFrequency

    private var _userNote = MutableStateFlow(PitchDetectionRunner.prompt)

    val userNote: StateFlow<String>
        get() = _userNote

    private var _statusMessage = MutableStateFlow("")

    val statusMessage: StateFlow<String>
        get() = _statusMessage

    private var elapsedTimeSinceUpdate = MutableStateFlow(0)

    private var freqList = MutableStateFlow(mutableListOf<Float>())

    val pitchDetectionHandler = PitchDetectionHandler{ result, _ ->
        val pitch = result.pitch
        elapsedTimeSinceUpdate.value++
        val mList = freqList.value
        mList.add(pitch)
        freqList.value = mList
        if(elapsedTimeSinceUpdate.value == 7) {
            val freq: Float? = try{
                freqList.value.mostCommon()
            } catch (e: ConcurrentModificationException){
                pitch
            } catch (e: NullPointerException){
                pitch
            }

            freq?.let {
                _userFrequency.value = it
                _userNote.value = Converter(
                    "World",
                    format.value,
                    scale.value,
                    userFrequency.value.freq.toNote()
                ).convert()
                Log.v("SF-ML", "$userFrequency $userNote")
                if(!currentFrequency.value.isNaN() && !userFrequency.value.isNaN()) {
                    if(currentFrequency != userFrequency) {
                        val diff = PitchDetectionRunner.findDifference(
                            currentFrequency.value.toDouble(),
                            userFrequency.value.toDouble()
                        )
                        _statusMessage.value = "${diff.first} notes ${diff.second}"
                    }
                }
                if(_userNote.value == PitchDetectionRunner.prompt) {
                    _userFrequency.value = 0f
                }
            }

            val nList = freqList.value
            nList.clear()
            freqList.value = nList
            elapsedTimeSinceUpdate.value = 0
        }
    }


    fun createNewNote() {
        viewModelScope.launch {
            val curNote = currentNote.value
            _currentFrequency.value = GenerateRandom(scale.value).value.toFloat()
            _currentNote.value = Converter("World", format.value, scale.value, Frequency convertFrequencyToNote _currentFrequency.value).convert()
            while(currentNote.value == curNote) {
                _currentFrequency.value = GenerateRandom(scale.value).value.toFloat()
                _currentNote.value = Converter("World", format.value, scale.value, Frequency convertFrequencyToNote _currentFrequency.value).convert()
            }
            Log.i("Current Frequency", "$currentFrequency")
            val diff = PitchDetectionRunner.findDifference(
                _currentFrequency.value.toDouble(),
                _userFrequency.value.toDouble()
            )
            _statusMessage.value = "${diff.first} notes ${diff.second}"
        }
    }

    fun changeFormat(f: String) {
        val isPleaseSing = _userNote.value == PitchDetectionRunner.prompt
        _format.value = f
        _currentNote.value = Converter("World", f, scale.value, Frequency convertFrequencyToNote currentFrequency.value).convert()
        _userNote.value = Converter("World", f, scale.value, Frequency convertFrequencyToNote _userFrequency.value).convert()
        if(isPleaseSing) {
            _userNote.value = PitchDetectionRunner.prompt
        }
    }

    var player: MediaPlayer? = null

    fun playSound(context: Context) {
        if(player == null) {
            player = MediaPlayer.create(context, R.raw.correct)
        }
        try {
            player?.start()
        } catch (_: java.lang.IllegalStateException) {}

        player?.setOnCompletionListener {
            player?.release()
        }
    }
}

UI Screen

if(recordAudioState.isPermissionAccepted()) {
        DisposableEffect(true){

            val runner = PitchDetectionRunner(pitchDetectionHandler = model.pitchDetectionHandler)

            runner.start {
                Log.e("SF-ML", it.toString(), it)
            }

            onDispose {
                runner.dispatcher.stop()
                runner.thread.interrupt()
            }
        }
    }

Only these blocks of code can have a problem. runner: PitchDetectionRunner

0

There are 0 best solutions below