|
| 1 | +/* |
| 2 | + * Copyright 2020 The TensorFlow Authors. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.tensorflow.lite.examples.soundclassifier |
| 18 | + |
| 19 | +import android.Manifest |
| 20 | +import android.content.pm.PackageManager |
| 21 | +import android.os.Build |
| 22 | +import android.os.Bundle |
| 23 | +import android.util.Log |
| 24 | +import android.view.WindowManager |
| 25 | +import androidx.annotation.RequiresApi |
| 26 | +import androidx.appcompat.app.AppCompatActivity |
| 27 | +import androidx.core.content.ContextCompat |
| 28 | +import org.tensorflow.lite.examples.soundclassifier.databinding.ActivityMainBinding |
| 29 | + |
| 30 | +class MainActivity : AppCompatActivity() { |
| 31 | + private val probabilitiesAdapter by lazy { ProbabilitiesAdapter() } |
| 32 | + |
| 33 | + private lateinit var soundClassifier: SoundClassifier |
| 34 | + |
| 35 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 36 | + super.onCreate(savedInstanceState) |
| 37 | + |
| 38 | + val binding = ActivityMainBinding.inflate(layoutInflater) |
| 39 | + setContentView(binding.root) |
| 40 | + |
| 41 | + soundClassifier = SoundClassifier(this, SoundClassifier.Options()).also { |
| 42 | + it.lifecycleOwner = this |
| 43 | + } |
| 44 | + |
| 45 | + with(binding) { |
| 46 | + recyclerView.apply { |
| 47 | + setHasFixedSize(true) |
| 48 | + adapter = probabilitiesAdapter.apply { |
| 49 | + labelList = soundClassifier.labelList |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + keepScreenOn(inputSwitch.isChecked) |
| 54 | + inputSwitch.setOnCheckedChangeListener { _, isChecked -> |
| 55 | + soundClassifier.isPaused = !isChecked |
| 56 | + keepScreenOn(isChecked) |
| 57 | + } |
| 58 | + |
| 59 | + overlapFactorSlider.value = soundClassifier.overlapFactor |
| 60 | + overlapFactorSlider.addOnChangeListener { _, value, _ -> |
| 61 | + soundClassifier.overlapFactor = value |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + soundClassifier.probabilities.observe(this) { resultMap -> |
| 66 | + if (resultMap.isEmpty() || resultMap.size > soundClassifier.labelList.size) { |
| 67 | + Log.w(TAG, "Invalid size of probability output! (size: ${resultMap.size})") |
| 68 | + return@observe |
| 69 | + } |
| 70 | + probabilitiesAdapter.probabilityMap = resultMap |
| 71 | + probabilitiesAdapter.notifyDataSetChanged() |
| 72 | + } |
| 73 | + |
| 74 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 75 | + requestMicrophonePermission() |
| 76 | + } else { |
| 77 | + soundClassifier.start() |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + override fun onTopResumedActivityChanged(isTopResumedActivity: Boolean) { |
| 82 | + // Handles "top" resumed event on multi-window environment |
| 83 | + if (isTopResumedActivity) { |
| 84 | + soundClassifier.start() |
| 85 | + } else { |
| 86 | + soundClassifier.stop() |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + override fun onRequestPermissionsResult( |
| 91 | + requestCode: Int, |
| 92 | + permissions: Array<out String>, |
| 93 | + grantResults: IntArray |
| 94 | + ) { |
| 95 | + if (requestCode == REQUEST_RECORD_AUDIO) { |
| 96 | + if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { |
| 97 | + Log.i(TAG, "Audio permission granted :)") |
| 98 | + soundClassifier.start() |
| 99 | + } else { |
| 100 | + Log.e(TAG, "Audio permission not granted :(") |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @RequiresApi(Build.VERSION_CODES.M) |
| 106 | + private fun requestMicrophonePermission() { |
| 107 | + if (ContextCompat.checkSelfPermission( |
| 108 | + this, |
| 109 | + Manifest.permission.RECORD_AUDIO |
| 110 | + ) == PackageManager.PERMISSION_GRANTED |
| 111 | + ) { |
| 112 | + soundClassifier.start() |
| 113 | + } else { |
| 114 | + requestPermissions(arrayOf(Manifest.permission.RECORD_AUDIO), REQUEST_RECORD_AUDIO) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private fun keepScreenOn(enable: Boolean) = |
| 119 | + if (enable) { |
| 120 | + window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) |
| 121 | + } else { |
| 122 | + window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) |
| 123 | + } |
| 124 | + |
| 125 | + companion object { |
| 126 | + const val REQUEST_RECORD_AUDIO = 1337 |
| 127 | + private const val TAG = "AudioDemo" |
| 128 | + } |
| 129 | +} |
0 commit comments