diff --git a/CHANGELOG.md b/CHANGELOG.md index 3beb9051..27c59068 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## 2.1.0 (#unreleased) - Feature [#468](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/pull/468) - Add macOS support +- Fixed [#491](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/issues/491) - Update totalSamples calculation to account for bytes per sample ## 2.0.2 diff --git a/android/src/main/kotlin/com/simform/audio_waveforms/AudioRecorder.kt b/android/src/main/kotlin/com/simform/audio_waveforms/AudioRecorder.kt index 24fd73bd..e2e00788 100644 --- a/android/src/main/kotlin/com/simform/audio_waveforms/AudioRecorder.kt +++ b/android/src/main/kotlin/com/simform/audio_waveforms/AudioRecorder.kt @@ -45,6 +45,13 @@ class AudioRecorder : PluginRegistry.RequestPermissionsResultListener { AudioFormat.CHANNEL_IN_STEREO -> 2 else -> 1 } + private val bytesPerSample: Int + get() = when (audioFormat) { + AudioFormat.ENCODING_PCM_8BIT -> 1 + AudioFormat.ENCODING_PCM_16BIT -> 2 + AudioFormat.ENCODING_PCM_FLOAT -> 4 + else -> 2 + } override fun onRequestPermissionsResult( requestCode: Int, permissions: Array, grantResults: IntArray @@ -153,7 +160,7 @@ class AudioRecorder : PluginRegistry.RequestPermissionsResultListener { commonEncoder.queueInputBuffer(audioData) } val rms = calculateRms(audioData, read) - totalSamples += read / channelCount + totalSamples += read / (channelCount * bytesPerSample) val durationSec = totalSamples.toDouble() / (recorderSettings?.sampleRate ?: Constants.DEFAULT_SAMPLE_RATE)