-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Myzel394/improve-architecture
Improve architecture
- Loading branch information
Showing
21 changed files
with
984 additions
and
458 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package app.myzel394.alibi.enums | ||
|
||
enum class RecorderState { | ||
IDLE, | ||
RECORDING, | ||
PAUSED, | ||
} |
67 changes: 67 additions & 0 deletions
67
app/src/main/java/app/myzel394/alibi/services/AudioRecorderService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package app.myzel394.alibi.services | ||
|
||
import android.media.MediaRecorder | ||
import android.os.Build | ||
|
||
class AudioRecorderService: IntervalRecorderService() { | ||
var amplitudesAmount = 1000 | ||
|
||
var recorder: MediaRecorder? = null | ||
private set | ||
|
||
val filePath: String | ||
get() = "$folder/$counter.${settings!!.fileExtension}" | ||
|
||
private fun createRecorder(): MediaRecorder { | ||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { | ||
MediaRecorder(this) | ||
} else { | ||
MediaRecorder() | ||
}.apply { | ||
setAudioSource(MediaRecorder.AudioSource.MIC) | ||
setOutputFile(filePath) | ||
setOutputFormat(settings!!.outputFormat) | ||
setAudioEncoder(settings!!.encoder) | ||
setAudioEncodingBitRate(settings!!.bitRate) | ||
setAudioSamplingRate(settings!!.samplingRate) | ||
} | ||
} | ||
|
||
private fun resetRecorder() { | ||
runCatching { | ||
recorder?.let { | ||
it.stop() | ||
it.release() | ||
} | ||
} | ||
} | ||
|
||
override fun startNewCycle() { | ||
super.startNewCycle() | ||
|
||
val newRecorder = createRecorder().also { | ||
it.prepare() | ||
} | ||
|
||
resetRecorder() | ||
|
||
newRecorder.start() | ||
recorder = newRecorder | ||
} | ||
|
||
override fun pause() { | ||
super.pause() | ||
|
||
resetRecorder() | ||
} | ||
|
||
override fun stop() { | ||
super.stop() | ||
|
||
resetRecorder() | ||
} | ||
|
||
override fun getAmplitudeAmount(): Int = amplitudesAmount | ||
|
||
override fun getAmplitude(): Int = recorder?.maxAmplitude ?: 0 | ||
} |
51 changes: 51 additions & 0 deletions
51
app/src/main/java/app/myzel394/alibi/services/ExtraRecorderInformationService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package app.myzel394.alibi.services | ||
|
||
import android.os.Handler | ||
import android.os.Looper | ||
import app.myzel394.alibi.enums.RecorderState | ||
import java.util.Timer | ||
import java.util.TimerTask | ||
import java.util.concurrent.Executors | ||
import java.util.concurrent.ScheduledExecutorService | ||
import java.util.concurrent.TimeUnit | ||
|
||
abstract class ExtraRecorderInformationService: RecorderService() { | ||
abstract fun getAmplitudeAmount(): Int | ||
abstract fun getAmplitude(): Int | ||
|
||
var amplitudes = mutableListOf<Int>() | ||
private set | ||
|
||
private val handler = Handler(Looper.getMainLooper()) | ||
|
||
var onAmplitudeChange: ((List<Int>) -> Unit)? = null | ||
|
||
private fun updateAmplitude() { | ||
if (state !== RecorderState.RECORDING) { | ||
return | ||
} | ||
|
||
amplitudes.add(getAmplitude()) | ||
onAmplitudeChange?.invoke(amplitudes) | ||
|
||
// Delete old amplitudes | ||
if (amplitudes.size > getAmplitudeAmount()) { | ||
amplitudes.drop(amplitudes.size - getAmplitudeAmount()) | ||
} | ||
|
||
handler.postDelayed(::updateAmplitude, 100) | ||
} | ||
|
||
private fun createAmplitudesTimer() { | ||
handler.postDelayed(::updateAmplitude, 100) | ||
} | ||
|
||
override fun start() { | ||
createAmplitudesTimer() | ||
} | ||
|
||
override fun resume() { | ||
createAmplitudesTimer() | ||
} | ||
|
||
} |
Oops, something went wrong.