diff --git a/CHANGELOG.md b/CHANGELOG.md index caa5a8e..1877ba7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## 2.0.0 (Unreleased) - Fixed [#350](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/issues/350) - Left most visible wave is clipped in half when recording. +- Fixed [#390](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/issues/390) - Setting continuousWaveform to false not working - **BREAKING:** Fixed [#412](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/issues/412) - Calling preparePlayer again does not cancel old waveform extraction. - Feature [#415](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/pull/415) - Add stop extraction method and asynchronous extraction of waveform in ios - **BREAKING:** Feature [#416](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/pull/416) - Add waveform extraction controller. @@ -11,12 +12,12 @@ ## 1.3.0 - Fixed [#386](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/pull/386) - On permission denied isRecording flag changed -- Fixed [#384](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/issues/384) - Provide a callback of drag,tap,start and end details on user gesture -- Fixed [#309](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/issues/309) - Added support for Liner PCM codec in iOS +- Feat [#384](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/issues/384) - Provide a callback of drag,tap,start and end details on user gesture +- Feat [#309](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/issues/309) - Added support for Liner PCM codec in iOS - Fixed [#291](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/issues/291) - Codec issue while recording audio on android - Fixed [#391](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/pull/391) - Wrong codec selection on the platform side on Android - Fixed [#389](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/pull/389) - Wrong codec selection on the platform side on iOS -- Fixed [#325](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/issues/325) - Added feature to pause all player controller at once. +- Feat [#325](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/issues/325) - Added feature to pause all player controller at once. - Fixed [#373](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/issues/373) - Getting error on dispose - Fixed [#395](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/issues/395) - Live wave gradient not getting applied diff --git a/android/src/main/kotlin/com/simform/audio_waveforms/Utils.kt b/android/src/main/kotlin/com/simform/audio_waveforms/Utils.kt index 3569175..b846c00 100644 --- a/android/src/main/kotlin/com/simform/audio_waveforms/Utils.kt +++ b/android/src/main/kotlin/com/simform/audio_waveforms/Utils.kt @@ -72,6 +72,15 @@ object Constants { const val resultFilePath = "resultFilePath" const val resultDuration = "resultDuration" const val pauseAllPlayers = "pauseAllPlayers" + + /// Indicates 128 bits in a single channel for 8-bit PCM + const val EIGHT_BITS = 128f + + /// Indicates 32767 bits in a single channel for 16-bit PCM + const val SIXTEEN_BITS = 32767f + + /// Indicates 2147483648f bits in a single channel for 32-bit PCM + const val THIRTY_TWO_BITS = 2.14748365E9f } enum class FinishMode(val value: Int) { diff --git a/android/src/main/kotlin/com/simform/audio_waveforms/WaveformExtractor.kt b/android/src/main/kotlin/com/simform/audio_waveforms/WaveformExtractor.kt index 30f72b3..18c64ec 100644 --- a/android/src/main/kotlin/com/simform/audio_waveforms/WaveformExtractor.kt +++ b/android/src/main/kotlin/com/simform/audio_waveforms/WaveformExtractor.kt @@ -137,6 +137,9 @@ class WaveformExtractor( } if (info.isEof()) { + updateProgress() + val rms = sqrt(sampleSum / perSamplePoints).toFloat() + sendProgress(rms) stop() } } @@ -160,31 +163,17 @@ class WaveformExtractor( private var sampleCount = 0L private var sampleSum = 0.0 - private fun rms(value: Float) { + private fun handleBufferDivision(value: Float) { if (sampleCount == perSamplePoints) { - currentProgress++ - progress = currentProgress / expectedPoints + updateProgress() // Discard redundant values and release resources if (progress > 1.0F) { stop() return } - - val rms = sqrt(sampleSum / perSamplePoints) - sampleData.add(rms.toFloat()) - extractorCallBack.onProgress(progress) - sampleCount = 0 - sampleSum = 0.0 - - val args: MutableMap = HashMap() - args[Constants.waveformData] = sampleData - args[Constants.progress] = progress - args[Constants.playerKey] = key - methodChannel.invokeMethod( - Constants.onCurrentExtractedWaveformData, - args - ) + val rms = sqrt(sampleSum / perSamplePoints).toFloat() + sendProgress(rms) } sampleCount++ @@ -193,11 +182,11 @@ class WaveformExtractor( private fun handle8bit(size: Int, buf: ByteBuffer) { repeat(size / if (channels == 2) 2 else 1) { - val result = buf.get().toInt() / 128f + val result = buf.get().toInt() / Constants.EIGHT_BITS if (channels == 2) { buf.get() } - rms(result) + handleBufferDivision(result) } } @@ -205,12 +194,12 @@ class WaveformExtractor( repeat(size / if (channels == 2) 4 else 2) { val first = buf.get().toInt() val second = buf.get().toInt() shl 8 - val value = (first or second) / 32767f + val value = (first or second) / Constants.SIXTEEN_BITS if (channels == 2) { buf.get() buf.get() } - rms(value) + handleBufferDivision(value) } } @@ -220,17 +209,38 @@ class WaveformExtractor( val second = buf.get().toLong() shl 8 val third = buf.get().toLong() shl 16 val forth = buf.get().toLong() shl 24 - val value = (first or second or third or forth) / 2147483648f + val value = (first or second or third or forth) / Constants.THIRTY_TWO_BITS if (channels == 2) { buf.get() buf.get() buf.get() buf.get() } - rms(value) + handleBufferDivision(value) } } + private fun updateProgress() { + currentProgress++ + progress = currentProgress / expectedPoints + } + + private fun sendProgress(rms: Float) { + sampleData.add(rms) + extractorCallBack.onProgress(progress) + sampleCount = 0 + sampleSum = 0.0 + + val args: MutableMap = HashMap() + args[Constants.waveformData] = sampleData + args[Constants.progress] = progress + args[Constants.playerKey] = key + methodChannel.invokeMethod( + Constants.onCurrentExtractedWaveformData, + args + ) + } + fun stop() { decoder?.stop() decoder?.release() diff --git a/pubspec.yaml b/pubspec.yaml index 6272ea4..b4de079 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: audio_waveforms description: A Flutter package that allow you to generate waveform while recording audio or from audio file. -version: 1.3.0 +version: 2.0.0 homepage: https://github.com/SimformSolutionsPvtLtd/audio_waveforms issue_tracker: https://github.com/SimformSolutionsPvtLtd/audio_waveforms/issues