Record Audio along with sound classification in android using tensorflow

Viewed 53

I am using AudioClassifier to classify the sound from mic of the phone.

 'val classifier = AudioClassifier.createFromFile(this, MODEL_FILE)'
   'val tensor = classifier.createInputTensorAudio()'
    val record = classifier!!.createAudioRecord()
       record?.startRecording()

My record object is the type of AudioRecord class, classification is being performed in the following code snippet

val filePath:String = externalCacheDir?.absolutePath + "/audioFile.wav"

val waveRecorder = WaveRecorder(filePath)
waveRecorder.startRecording()

val run = object : Runnable {
    override fun run() {
        if (isNotificationGenerated){

        }
        else{
            val startTime = System.currentTimeMillis()
            if (record != null) {                       
                    try {
                        tensor?.load(record)
                        val output = classifier!!.classify(tensor)

                        // Filter out results above a certain threshold, and sort them descendingly
                        val filteredModelOutput = output[0].categories.filter {
                            it.score > DashboardActivity.MINIMUM_DISPLAY_THRESHOLD
                        }.sortedBy {
                            -it.score
                        }
                        val finishTime = System.currentTimeMillis()
                        Log.d(DashboardActivity.TAG, "Latency = ${finishTime - startTime}ms")

                        // Updating the UI
                        runOnUiThread {
                            categoryList = filteredModelOutput
                            openNotification(categoryList)
                        }
                    } catch (e: Exception) {
                        e.message
                    }


            }
        }


        // Load the latest audio sample

        // Rerun the classification after a certain interval
        handler.postDelayed(this, dataManager.classificationInterval)
    }
}

// Start the classification process

handler.post(run!!)

// Save the instances we just created for use later
audioClassifier = classifier
audioRecord = record

I am using Android Wave Recorder https://github.com/squti/Android-Wave-Recorder to record the sound and save it as wav file.

val filePath:String = externalCacheDir?.absolutePath + "/audioFile.wav"

val waveRecorder = WaveRecorder(filePath)
waveRecorder.startRecording()

whenever i try to record the sound using this recorder my audiorecord object record throws an error in my catch block which is INVALID_OPERATION. My task is to record the wav file 2 sec before and 2 sec after the recognition. can anyone suggest me what to do??

0 Answers
Related