From 3ff6638e95788ad01f7c0280b8277550b4ed2c46 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Wed, 2 Aug 2023 17:44:56 +0200 Subject: [PATCH] feat: Add AppSettings --- .../myzel394/locationtest/db/AppSettings.kt | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 app/src/main/java/app/myzel394/locationtest/db/AppSettings.kt diff --git a/app/src/main/java/app/myzel394/locationtest/db/AppSettings.kt b/app/src/main/java/app/myzel394/locationtest/db/AppSettings.kt new file mode 100644 index 0000000..1dbbbc9 --- /dev/null +++ b/app/src/main/java/app/myzel394/locationtest/db/AppSettings.kt @@ -0,0 +1,83 @@ +package app.myzel394.locationtest.db + +import android.media.MediaRecorder +import android.os.Build + +data class AppSettings( + val audioRecorderSettings: AudioRecorderSettings = AudioRecorderSettings(), +) + +data class AudioRecorderSettings( + // 60 seconds + val intervalDuration: Long = 60 * 1000L, + // 320 Kbps + val bitRate: Int = 320000, + val samplingRate: Int? = null, + val outputFormat: Int? = null, + val encoder: Int? = null, +) { + fun getOutputFormat(): Int = outputFormat ?: + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) + MediaRecorder.OutputFormat.AAC_ADTS + else + MediaRecorder.OutputFormat.THREE_GPP + + fun getSamplingRate(): Int = samplingRate ?: when(getOutputFormat()) { + MediaRecorder.OutputFormat.AAC_ADTS -> 96000 + MediaRecorder.OutputFormat.THREE_GPP -> 44100 + else -> throw Exception("Unknown output format") + } + + fun getEncoder(): Int = encoder ?: + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) + MediaRecorder.AudioEncoder.AAC + else + MediaRecorder.AudioEncoder.AMR_NB + + fun getFileExtensions(): String = + when(getOutputFormat()) { + MediaRecorder.OutputFormat.AAC_ADTS -> "aac" + MediaRecorder.OutputFormat.THREE_GPP -> "3gp" + else -> throw Exception("Unknown output format") + } + + fun setIntervalDuration(duration: Long): AudioRecorderSettings { + if (duration < 10 * 1000L) { + throw Exception("Interval duration must be at least 10 seconds") + } + + return copy(intervalDuration = duration) + } + + fun setBitRate(bitRate: Int): AudioRecorderSettings { + if (bitRate < 1000) { + throw Exception("Bit rate must be at least 1000") + } + + return copy(bitRate = bitRate) + } + + fun setSamplingRate(samplingRate: Int): AudioRecorderSettings { + if (samplingRate < 1000) { + throw Exception("Sampling rate must be at least 1000") + } + + return copy(samplingRate = samplingRate) + } + + fun setOutputFormat(outputFormat: Int): AudioRecorderSettings { + if (outputFormat < 0 || outputFormat > 11) { + throw Exception("OutputFormat is not a MediaRecorder.OutputFormat constant") + } + + return copy(outputFormat = outputFormat) + } + + fun setEncoder(encoder: Int): AudioRecorderSettings { + if (encoder < 0 || encoder > 7) { + throw Exception("Encoder is not a MediaRecorder.AudioEncoder constant") + } + + return copy(encoder = encoder) + } +}