├── app
├── .gitignore
├── src
│ └── main
│ │ ├── ic_launcher-web.png
│ │ ├── res
│ │ ├── values
│ │ │ ├── strings.xml
│ │ │ └── styles.xml
│ │ ├── font
│ │ │ └── product_sans_bold.ttf
│ │ ├── mipmap-hdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── drawable
│ │ │ ├── background_main.xml
│ │ │ ├── ic_launcher_background.xml
│ │ │ └── ic_launcher_foreground.xml
│ │ ├── mipmap-anydpi-v26
│ │ │ ├── ic_launcher.xml
│ │ │ └── ic_launcher_round.xml
│ │ └── layout
│ │ │ └── activity_main.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── com
│ │ └── egeniq
│ │ └── exovisualizer
│ │ ├── ExoVisualizer.kt
│ │ ├── MainActivity.kt
│ │ ├── FFTBandView.kt
│ │ └── FFTAudioProcessor.kt
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── .gitattributes
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── README.md
├── .idea
├── codeStyles
│ ├── codeStyleConfig.xml
│ └── Project.xml
├── misc.xml
└── runConfigurations.xml
├── LICENSE
├── gradle.properties
├── .gitignore
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 | rootProject.name='ExoVisualizer'
3 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
--------------------------------------------------------------------------------
/app/src/main/ic_launcher-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dzolnai/ExoVisualizer/HEAD/app/src/main/ic_launcher-web.png
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | ExoVisualizer
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dzolnai/ExoVisualizer/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/font/product_sans_bold.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dzolnai/ExoVisualizer/HEAD/app/src/main/res/font/product_sans_bold.ttf
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dzolnai/ExoVisualizer/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dzolnai/ExoVisualizer/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dzolnai/ExoVisualizer/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dzolnai/ExoVisualizer/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dzolnai/ExoVisualizer/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dzolnai/ExoVisualizer/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dzolnai/ExoVisualizer/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dzolnai/ExoVisualizer/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dzolnai/ExoVisualizer/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dzolnai/ExoVisualizer/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ExoVisualizer
2 |
3 | Technology demo of a Visualizer based on an ExoPlayer Audioprocessor
4 |
5 | For more information, see [my blog post about an alternative Android visualizer](https://www.egeniq.com/blog/alternative-android-visualizer).
6 |
--------------------------------------------------------------------------------
/.idea/codeStyles/codeStyleConfig.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/background_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Thu Sep 05 08:09:38 CEST 2019
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
9 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 Dániel Zolnai
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 | apply plugin: 'kotlin-android'
3 | apply plugin: 'kotlin-android-extensions'
4 |
5 | android {
6 | compileSdkVersion 30
7 | defaultConfig {
8 | applicationId "com.egeniq.exovisualizer"
9 | minSdkVersion 21
10 | targetSdkVersion 30
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | compileOptions {
21 | targetCompatibility JavaVersion.VERSION_1_8
22 | }
23 | }
24 |
25 | dependencies {
26 | implementation fileTree(dir: 'libs', include: ['*.jar'])
27 | implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
28 | implementation 'androidx.appcompat:appcompat:1.3.0'
29 | implementation 'androidx.core:core-ktx:1.6.0'
30 | implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
31 |
32 | implementation 'com.google.android.exoplayer:exoplayer:2.14.1'
33 | // Library for FFT processing
34 | implementation 'com.github.paramsen:noise:2.0.0'
35 | }
36 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 | # Kotlin code style for this project: "official" or "obsolete":
21 | kotlin.code.style=official
22 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
21 |
22 |
29 |
30 |
--------------------------------------------------------------------------------
/app/src/main/java/com/egeniq/exovisualizer/ExoVisualizer.kt:
--------------------------------------------------------------------------------
1 | package com.egeniq.exovisualizer
2 |
3 | import android.content.Context
4 | import android.util.AttributeSet
5 | import android.widget.FrameLayout
6 | import com.google.android.exoplayer2.Player
7 |
8 | /**
9 | * The visualizer is a view which listens to the FFT changes and forwards it to the band view.
10 | */
11 | class ExoVisualizer @JvmOverloads constructor(
12 | context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
13 | ) : FrameLayout(context, attrs, defStyleAttr), Player.EventListener, FFTAudioProcessor.FFTListener {
14 |
15 | var processor: FFTAudioProcessor? = null
16 |
17 | private var currentWaveform: FloatArray? = null
18 |
19 | private val bandView = FFTBandView(context, attrs)
20 |
21 | init {
22 | addView(bandView, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT))
23 | }
24 |
25 | private fun updateProcessorListenerState(enable: Boolean) {
26 | if (enable) {
27 | processor?.listener = this
28 | } else {
29 | processor?.listener = null
30 | currentWaveform = null
31 | }
32 | }
33 |
34 | override fun onAttachedToWindow() {
35 | super.onAttachedToWindow()
36 | updateProcessorListenerState(true)
37 | }
38 |
39 | override fun onDetachedFromWindow() {
40 | super.onDetachedFromWindow()
41 | updateProcessorListenerState(false)
42 | }
43 |
44 | override fun onFFTReady(sampleRateHz: Int, channelCount: Int, fft: FloatArray) {
45 | currentWaveform = fft
46 | bandView.onFFT(fft)
47 | }
48 |
49 | }
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 | *.aab
5 |
6 | # Files for the ART/Dalvik VM
7 | *.dex
8 |
9 | # Java class files
10 | *.class
11 |
12 | # Generated files
13 | bin/
14 | gen/
15 | out/
16 | # Uncomment the following line in case you need and you don't have the release build type files in your app
17 | # release/
18 |
19 | # Gradle files
20 | .gradle/
21 | build/
22 |
23 | # Local configuration file (sdk path, etc)
24 | local.properties
25 |
26 | # Proguard folder generated by Eclipse
27 | proguard/
28 |
29 | # Log Files
30 | *.log
31 |
32 | # Android Studio Navigation editor temp files
33 | .navigation/
34 |
35 | # Android Studio captures folder
36 | captures/
37 |
38 | # IntelliJ
39 | *.iml
40 | .idea/workspace.xml
41 | .idea/tasks.xml
42 | .idea/gradle.xml
43 | .idea/assetWizardSettings.xml
44 | .idea/dictionaries
45 | .idea/libraries
46 | # Android Studio 3 in .gitignore file.
47 | .idea/caches
48 | .idea/modules.xml
49 | # Comment next line if keeping position of elements in Navigation Editor is relevant for you
50 | .idea/navEditor.xml
51 |
52 | # Keystore files
53 | # Uncomment the following lines if you do not want to check your keystore files in.
54 | #*.jks
55 | #*.keystore
56 |
57 | # External native build folder generated in Android Studio 2.2 and later
58 | .externalNativeBuild
59 |
60 | # Google Services (e.g. APIs or Firebase)
61 | # google-services.json
62 |
63 | # Freeline
64 | freeline.py
65 | freeline/
66 | freeline_project_description.json
67 |
68 | # fastlane
69 | fastlane/report.xml
70 | fastlane/Preview.html
71 | fastlane/screenshots
72 | fastlane/test_output
73 | fastlane/readme.md
74 |
75 | # Version control
76 | vcs.xml
77 |
78 | # lint
79 | lint/intermediates/
80 | lint/generated/
81 | lint/outputs/
82 | lint/tmp/
83 | # lint/reports/
84 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/app/src/main/java/com/egeniq/exovisualizer/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.egeniq.exovisualizer
2 |
3 | import android.content.Context
4 | import android.net.Uri
5 | import android.os.Bundle
6 | import android.os.Handler
7 | import androidx.appcompat.app.AppCompatActivity
8 | import com.google.android.exoplayer2.*
9 | import com.google.android.exoplayer2.audio.*
10 | import com.google.android.exoplayer2.mediacodec.MediaCodecSelector
11 | import com.google.android.exoplayer2.source.ProgressiveMediaSource
12 | import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
13 |
14 | class MainActivity : AppCompatActivity() {
15 |
16 | private var player: ExoPlayer? = null
17 |
18 | override fun onCreate(savedInstanceState: Bundle?) {
19 | super.onCreate(savedInstanceState)
20 | setContentView(R.layout.activity_main)
21 |
22 | initPlayer()
23 | }
24 |
25 | private val fftAudioProcessor = FFTAudioProcessor()
26 |
27 | private fun initPlayer() {
28 | // We need to create a renderers factory to inject our own audio processor at the end of the list
29 | val context = this
30 | val renderersFactory = object : DefaultRenderersFactory(context) {
31 |
32 | override fun buildAudioRenderers(
33 | context: Context,
34 | extensionRendererMode: Int,
35 | mediaCodecSelector: MediaCodecSelector,
36 | enableDecoderFallback: Boolean,
37 | audioSink: AudioSink,
38 | eventHandler: Handler,
39 | eventListener: AudioRendererEventListener,
40 | out: ArrayList
41 | ) {
42 | out.add(
43 | MediaCodecAudioRenderer(
44 | context,
45 | mediaCodecSelector,
46 | enableDecoderFallback,
47 | eventHandler,
48 | eventListener,
49 | DefaultAudioSink(
50 | AudioCapabilities.getCapabilities(context),
51 | arrayOf(fftAudioProcessor)
52 | )
53 | )
54 | )
55 |
56 | super.buildAudioRenderers(
57 | context,
58 | extensionRendererMode,
59 | mediaCodecSelector,
60 | enableDecoderFallback,
61 | audioSink,
62 | eventHandler,
63 | eventListener,
64 | out
65 | )
66 | }
67 | }
68 | player = SimpleExoPlayer.Builder(context, renderersFactory)
69 | .build()
70 |
71 | val visualizer = findViewById(R.id.visualizer)
72 | visualizer.processor = fftAudioProcessor
73 |
74 | // Online radio:
75 | val uri = Uri.parse("http://listen.livestreamingservice.com/181-xsoundtrax_128k.mp3")
76 | // 1 kHz test sound:
77 | // val uri = Uri.parse("https://www.mediacollege.com/audio/tone/files/1kHz_44100Hz_16bit_05sec.mp3")
78 | // 10 kHz test sound:
79 | // val uri = Uri.parse("https://www.mediacollege.com/audio/tone/files/10kHz_44100Hz_16bit_05sec.mp3")
80 | // Sweep from 20 to 20 kHz
81 | // val uri = Uri.parse("https://www.churchsoundcheck.com/CSC_sweep_20-20k.wav")
82 | val mediaSource = ProgressiveMediaSource.Factory(
83 | DefaultDataSourceFactory(context, "ExoVisualizer")
84 | ).createMediaSource(MediaItem.Builder().setUri(uri).build())
85 | player?.playWhenReady = true
86 | player?.setMediaSource(mediaSource)
87 | player?.prepare()
88 | }
89 |
90 | override fun onResume() {
91 | super.onResume()
92 | player?.playWhenReady = true
93 | }
94 |
95 | override fun onPause() {
96 | super.onPause()
97 | player?.playWhenReady = false
98 | }
99 |
100 | override fun onDestroy() {
101 | super.onDestroy()
102 | player?.stop()
103 | player?.release()
104 | }
105 | }
106 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 | xmlns:android
20 |
21 | ^$
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 | xmlns:.*
31 |
32 | ^$
33 |
34 |
35 | BY_NAME
36 |
37 |
38 |
39 |
40 |
41 |
42 | .*:id
43 |
44 | http://schemas.android.com/apk/res/android
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 | .*:name
54 |
55 | http://schemas.android.com/apk/res/android
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 | name
65 |
66 | ^$
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 | style
76 |
77 | ^$
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 | .*
87 |
88 | ^$
89 |
90 |
91 | BY_NAME
92 |
93 |
94 |
95 |
96 |
97 |
98 | .*
99 |
100 | http://schemas.android.com/apk/res/android
101 |
102 |
103 | ANDROID_ATTRIBUTE_ORDER
104 |
105 |
106 |
107 |
108 |
109 |
110 | .*
111 |
112 | .*
113 |
114 |
115 | BY_NAME
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/app/src/main/java/com/egeniq/exovisualizer/FFTBandView.kt:
--------------------------------------------------------------------------------
1 | package com.egeniq.exovisualizer
2 |
3 | import android.content.Context
4 | import android.graphics.Canvas
5 | import android.graphics.Color
6 | import android.graphics.Paint
7 | import android.graphics.Path
8 | import android.util.AttributeSet
9 | import android.view.View
10 | import java.lang.System.arraycopy
11 | import kotlin.math.cos
12 | import kotlin.math.floor
13 | import kotlin.math.pow
14 |
15 |
16 | /**
17 | * Based on FFTBandView by Pär Amsen:
18 | * https://github.com/paramsen/noise/blob/master/sample/src/main/java/com/paramsen/noise/sample/view/FFTBandView.kt
19 | *
20 | */
21 | class FFTBandView @JvmOverloads constructor(
22 | context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
23 | ) : View(context, attrs, defStyleAttr) {
24 |
25 | companion object {
26 | // Taken from: https://en.wikipedia.org/wiki/Preferred_number#Audio_frequencies
27 | private val FREQUENCY_BAND_LIMITS = arrayOf(
28 | 20, 25, 32, 40, 50, 63, 80, 100, 125, 160, 200, 250, 315, 400, 500, 630,
29 | 800, 1000, 1250, 1600, 2000, 2500, 3150, 4000, 5000, 6300, 8000, 10000,
30 | 12500, 16000, 20000
31 | )
32 | }
33 |
34 | private val bands = FREQUENCY_BAND_LIMITS.size
35 | private val size = FFTAudioProcessor.SAMPLE_SIZE / 2
36 | private val maxConst = 25_000 // Reference max value for accum magnitude
37 |
38 | private val fft: FloatArray = FloatArray(size)
39 | private val paintBandsFill = Paint()
40 | private val paintBands = Paint()
41 | private val paintAvg = Paint()
42 | private val paintPath = Paint()
43 |
44 | // We average out the values over 3 occurences (plus the current one), so big jumps are smoothed out
45 | private val smoothingFactor = 3
46 | private val previousValues = FloatArray(bands * smoothingFactor)
47 |
48 | private val fftPath = Path()
49 |
50 | private var startedAt: Long = 0
51 |
52 | init {
53 | keepScreenOn = true
54 | paintBandsFill.color = Color.parseColor("#20FFFFFF")
55 | paintBandsFill.style = Paint.Style.FILL
56 |
57 | paintBands.color = Color.parseColor("#60FFFFFF")
58 | paintBands.strokeWidth = 1f
59 | paintBands.style = Paint.Style.STROKE
60 |
61 | paintAvg.color = Color.parseColor("#1976d2")
62 | paintAvg.strokeWidth = 2f
63 | paintAvg.style = Paint.Style.STROKE
64 |
65 | paintPath.color = Color.WHITE
66 | paintPath.strokeWidth = 8f
67 | paintPath.isAntiAlias = true
68 | paintPath.style = Paint.Style.STROKE
69 | }
70 |
71 | private fun drawAudio(canvas: Canvas) {
72 | // Clear the previous drawing on the screen
73 | canvas.drawColor(Color.TRANSPARENT)
74 |
75 | // Set up counters and widgets
76 | var currentFftPosition = 0
77 | var currentFrequencyBandLimitIndex = 0
78 | fftPath.reset()
79 | fftPath.moveTo(0f, height.toFloat())
80 | var currentAverage = 0f
81 |
82 | // Iterate over the entire FFT result array
83 | while (currentFftPosition < size) {
84 | var accum = 0f
85 |
86 | // We divide the bands by frequency.
87 | // Check until which index we need to stop for the current band
88 | val nextLimitAtPosition =
89 | floor(FREQUENCY_BAND_LIMITS[currentFrequencyBandLimitIndex] / 20_000.toFloat() * size).toInt()
90 |
91 | synchronized(fft) {
92 | // Here we iterate within this single band
93 | for (j in 0 until (nextLimitAtPosition - currentFftPosition) step 2) {
94 | // Convert real and imaginary part to get energy
95 | val raw = (fft[currentFftPosition + j].toDouble().pow(2.0) +
96 | fft[currentFftPosition + j + 1].toDouble().pow(2.0)).toFloat()
97 |
98 | // Hamming window (by frequency band instead of frequency, otherwise it would prefer 10kHz, which is too high)
99 | // The window mutes down the very high and the very low frequencies, usually not hearable by the human ear
100 | val m = bands / 2
101 | val windowed = raw * (0.54f - 0.46f * cos(2 * Math.PI * currentFrequencyBandLimitIndex / (m + 1))).toFloat()
102 | accum += windowed
103 | }
104 | }
105 | // A window might be empty which would result in a 0 division
106 | if (nextLimitAtPosition - currentFftPosition != 0) {
107 | accum /= (nextLimitAtPosition - currentFftPosition)
108 | } else {
109 | accum = 0.0f
110 | }
111 | currentFftPosition = nextLimitAtPosition
112 |
113 | // Here we do the smoothing
114 | // If you increase the smoothing factor, the high shoots will be toned down, but the
115 | // 'movement' in general will decrease too
116 | var smoothedAccum = accum
117 | for (i in 0 until smoothingFactor) {
118 | smoothedAccum += previousValues[i * bands + currentFrequencyBandLimitIndex]
119 | if (i != smoothingFactor - 1) {
120 | previousValues[i * bands + currentFrequencyBandLimitIndex] =
121 | previousValues[(i + 1) * bands + currentFrequencyBandLimitIndex]
122 | } else {
123 | previousValues[i * bands + currentFrequencyBandLimitIndex] = accum
124 | }
125 | }
126 | smoothedAccum /= (smoothingFactor + 1) // +1 because it also includes the current value
127 |
128 | // We display the average amplitude with a vertical line
129 | currentAverage += smoothedAccum / bands
130 |
131 |
132 | val leftX = width * (currentFrequencyBandLimitIndex / bands.toFloat())
133 | val rightX = leftX + width / bands.toFloat()
134 |
135 | val barHeight =
136 | (height * (smoothedAccum / maxConst.toDouble()).coerceAtMost(1.0).toFloat())
137 | val top = height - barHeight
138 |
139 | canvas.drawRect(
140 | leftX,
141 | top,
142 | rightX,
143 | height.toFloat(),
144 | paintBandsFill
145 | )
146 | canvas.drawRect(
147 | leftX,
148 | top,
149 | rightX,
150 | height.toFloat(),
151 | paintBands
152 | )
153 |
154 | fftPath.lineTo(
155 | (leftX + rightX) / 2,
156 | top
157 | )
158 |
159 | currentFrequencyBandLimitIndex++
160 | }
161 |
162 | canvas.drawPath(fftPath, paintPath)
163 |
164 | canvas.drawLine(
165 | 0f,
166 | height * (1 - (currentAverage / maxConst)),
167 | width.toFloat(),
168 | height * (1 - (currentAverage / maxConst)),
169 | paintAvg
170 | )
171 | }
172 |
173 | fun onFFT(fft: FloatArray) {
174 | synchronized(this.fft) {
175 | if (startedAt == 0L) {
176 | startedAt = System.currentTimeMillis()
177 | }
178 | // The resulting graph is mirrored, because we are using real numbers instead of imaginary
179 | // Explanations: https://www.mathworks.com/matlabcentral/answers/338408-why-are-fft-diagrams-mirrored
180 | // https://dsp.stackexchange.com/questions/4825/why-is-the-fft-mirrored/4827#4827
181 | // So what we do here, we only check the left part of the graph.
182 | arraycopy(fft, 2, this.fft, 0, size)
183 | // By calling invalidate, we request a redraw.
184 | invalidate()
185 | }
186 | }
187 |
188 | override fun onDraw(canvas: Canvas) {
189 | super.onDraw(canvas)
190 | drawAudio(canvas)
191 | // By calling invalidate, we request a redraw. See https://github.com/dzolnai/ExoVisualizer/issues/2
192 | invalidate()
193 | }
194 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/egeniq/exovisualizer/FFTAudioProcessor.kt:
--------------------------------------------------------------------------------
1 | package com.egeniq.exovisualizer
2 |
3 | import android.media.AudioTrack
4 | import android.media.AudioTrack.ERROR_BAD_VALUE
5 | import com.google.android.exoplayer2.C
6 | import com.google.android.exoplayer2.Format
7 | import com.google.android.exoplayer2.audio.AudioProcessor
8 | import com.google.android.exoplayer2.util.Assertions
9 | import com.google.android.exoplayer2.util.Util
10 | import com.paramsen.noise.Noise
11 | import java.nio.ByteBuffer
12 | import java.nio.ByteOrder
13 | import kotlin.math.max
14 |
15 | /**
16 | * An audio processor which forwards the input to the output,
17 | * but also takes the input and executes a Fast-Fourier Transformation (FFT) on it.
18 | * The results of this transformation is a 'list' of frequencies with their amplitudes,
19 | * which will be forwarded to the listener
20 | */
21 | class FFTAudioProcessor : AudioProcessor {
22 |
23 | companion object {
24 | const val SAMPLE_SIZE = 4096
25 |
26 | // From DefaultAudioSink.java:160 'MIN_BUFFER_DURATION_US'
27 | private const val EXO_MIN_BUFFER_DURATION_US: Long = 250000
28 |
29 | // From DefaultAudioSink.java:164 'MAX_BUFFER_DURATION_US'
30 | private const val EXO_MAX_BUFFER_DURATION_US: Long = 750000
31 |
32 | // From DefaultAudioSink.java:173 'BUFFER_MULTIPLICATION_FACTOR'
33 | private const val EXO_BUFFER_MULTIPLICATION_FACTOR = 4
34 |
35 | // Extra size next in addition to the AudioTrack buffer size
36 | private const val BUFFER_EXTRA_SIZE = SAMPLE_SIZE * 8
37 | }
38 |
39 | private var noise: Noise? = null
40 |
41 | private var isActive: Boolean = false
42 |
43 | private var processBuffer: ByteBuffer
44 | private var fftBuffer: ByteBuffer
45 | private var outputBuffer: ByteBuffer
46 |
47 | var listener: FFTListener? = null
48 | private var inputEnded: Boolean = false
49 |
50 | private lateinit var srcBuffer: ByteBuffer
51 | private var srcBufferPosition = 0
52 | private val tempByteArray = ByteArray(SAMPLE_SIZE * 2)
53 |
54 | private var audioTrackBufferSize = 0
55 |
56 | private val src = FloatArray(SAMPLE_SIZE)
57 | private val dst = FloatArray(SAMPLE_SIZE + 2)
58 |
59 |
60 | interface FFTListener {
61 | fun onFFTReady(sampleRateHz: Int, channelCount: Int, fft: FloatArray)
62 | }
63 |
64 | init {
65 | processBuffer = AudioProcessor.EMPTY_BUFFER
66 | fftBuffer = AudioProcessor.EMPTY_BUFFER
67 | outputBuffer = AudioProcessor.EMPTY_BUFFER
68 | }
69 |
70 | /**
71 | * The following method matches the implementation of getDefaultBufferSize in DefaultAudioSink
72 | * of ExoPlayer.
73 | * Because there is an AudioTrack buffer between the processor and the sound output, the processor receives everything early.
74 | * By putting the audio data to process in a buffer which has the same size as the audiotrack buffer,
75 | * we will delay ourselves to match the audio output.
76 | */
77 | private fun getDefaultBufferSizeInBytes(audioFormat: AudioProcessor.AudioFormat): Int {
78 | val outputPcmFrameSize = Util.getPcmFrameSize(audioFormat.encoding, audioFormat.channelCount)
79 | val minBufferSize =
80 | AudioTrack.getMinBufferSize(
81 | audioFormat.sampleRate,
82 | Util.getAudioTrackChannelConfig(audioFormat.channelCount),
83 | audioFormat.encoding
84 | )
85 | Assertions.checkState(minBufferSize != ERROR_BAD_VALUE)
86 | val multipliedBufferSize = minBufferSize * EXO_BUFFER_MULTIPLICATION_FACTOR
87 | val minAppBufferSize =
88 | durationUsToFrames(EXO_MIN_BUFFER_DURATION_US).toInt() * outputPcmFrameSize
89 | val maxAppBufferSize = max(
90 | minBufferSize.toLong(),
91 | durationUsToFrames(EXO_MAX_BUFFER_DURATION_US) * outputPcmFrameSize
92 | ).toInt()
93 | val bufferSizeInFrames = Util.constrainValue(
94 | multipliedBufferSize,
95 | minAppBufferSize,
96 | maxAppBufferSize
97 | ) / outputPcmFrameSize
98 | return bufferSizeInFrames * outputPcmFrameSize
99 | }
100 |
101 | private fun durationUsToFrames(durationUs: Long): Long {
102 | return durationUs * inputAudioFormat.sampleRate / C.MICROS_PER_SECOND
103 | }
104 |
105 | override fun isActive(): Boolean {
106 | return isActive
107 | }
108 |
109 | private lateinit var inputAudioFormat :AudioProcessor.AudioFormat
110 |
111 | override fun configure(inputAudioFormat: AudioProcessor.AudioFormat): AudioProcessor.AudioFormat {
112 | if (inputAudioFormat.encoding != C.ENCODING_PCM_16BIT) {
113 | throw AudioProcessor.UnhandledAudioFormatException(
114 | inputAudioFormat
115 | )
116 | }
117 | this.inputAudioFormat = inputAudioFormat
118 | isActive = true
119 |
120 | noise = Noise.real(SAMPLE_SIZE)
121 |
122 | audioTrackBufferSize = getDefaultBufferSizeInBytes(inputAudioFormat)
123 |
124 | srcBuffer = ByteBuffer.allocate(audioTrackBufferSize + BUFFER_EXTRA_SIZE)
125 | srcBufferPosition = 0
126 | return inputAudioFormat
127 | }
128 |
129 | override fun queueInput(inputBuffer: ByteBuffer) {
130 | var position = inputBuffer.position()
131 | val limit = inputBuffer.limit()
132 | val frameCount = (limit - position) / (2 * inputAudioFormat.channelCount)
133 | val singleChannelOutputSize = frameCount * 2
134 | val outputSize = frameCount * inputAudioFormat.channelCount * 2
135 |
136 |
137 | if (processBuffer.capacity() < outputSize) {
138 | processBuffer = ByteBuffer.allocateDirect(outputSize).order(ByteOrder.nativeOrder())
139 | } else {
140 | processBuffer.clear()
141 | }
142 |
143 | if (fftBuffer.capacity() < singleChannelOutputSize) {
144 | fftBuffer =
145 | ByteBuffer.allocateDirect(singleChannelOutputSize).order(ByteOrder.nativeOrder())
146 | } else {
147 | fftBuffer.clear()
148 | }
149 |
150 | while (position < limit) {
151 | var summedUp = 0
152 | for (channelIndex in 0 until inputAudioFormat.channelCount) {
153 | val current = inputBuffer.getShort(position + 2 * channelIndex)
154 | processBuffer.putShort(current)
155 | summedUp += current
156 | }
157 | // For the FFT, we use an currentAverage of all the channels
158 | fftBuffer.putShort((summedUp / inputAudioFormat.channelCount).toShort())
159 | position += inputAudioFormat.channelCount * 2
160 | }
161 |
162 | inputBuffer.position(limit)
163 |
164 | processFFT(this.fftBuffer)
165 |
166 | processBuffer.flip()
167 | outputBuffer = this.processBuffer
168 | }
169 |
170 | private fun processFFT(buffer: ByteBuffer) {
171 | if (listener == null) {
172 | return
173 | }
174 | if(srcBuffer.remaining() < buffer.array().size){
175 | // Expand the srcBuffer when the capacity is insufficient
176 | val newBuffer = ByteBuffer.allocate(srcBuffer.capacity() + buffer.array().size)
177 | srcBuffer.flip()
178 | newBuffer.put(srcBuffer)
179 | srcBuffer = newBuffer
180 | }
181 | srcBuffer.put(buffer.array())
182 | srcBufferPosition += buffer.array().size
183 | // Since this is PCM 16 bit, each sample will be 2 bytes.
184 | // So to get the sample size in the end, we need to take twice as many bytes off the buffer
185 | val bytesToProcess = SAMPLE_SIZE * 2
186 | var currentByte: Byte? = null
187 | while (srcBufferPosition > audioTrackBufferSize) {
188 | srcBuffer.position(0)
189 | srcBuffer.get(tempByteArray, 0, bytesToProcess)
190 |
191 | tempByteArray.forEachIndexed { index, byte ->
192 | if (currentByte == null) {
193 | currentByte = byte
194 | } else {
195 | src[index / 2] =
196 | (currentByte!!.toFloat() * Byte.MAX_VALUE + byte) / (Byte.MAX_VALUE * Byte.MAX_VALUE)
197 | dst[index / 2] = 0f
198 | currentByte = null
199 | }
200 |
201 | }
202 | srcBuffer.position(bytesToProcess)
203 | srcBuffer.compact()
204 | srcBufferPosition -= bytesToProcess
205 | srcBuffer.position(srcBufferPosition)
206 | val fft = noise?.fft(src, dst)!!
207 | listener?.onFFTReady(inputAudioFormat.sampleRate, inputAudioFormat.channelCount, fft)
208 | }
209 | }
210 |
211 | override fun queueEndOfStream() {
212 | inputEnded = true
213 | processBuffer = AudioProcessor.EMPTY_BUFFER
214 | }
215 |
216 | override fun getOutput(): ByteBuffer {
217 | val outputBuffer = this.outputBuffer
218 | this.outputBuffer = AudioProcessor.EMPTY_BUFFER
219 | return outputBuffer
220 | }
221 |
222 | override fun isEnded(): Boolean {
223 | return inputEnded && processBuffer === AudioProcessor.EMPTY_BUFFER
224 | }
225 |
226 | override fun flush() {
227 | outputBuffer = AudioProcessor.EMPTY_BUFFER
228 | inputEnded = false
229 | // A new stream is incoming.
230 | }
231 |
232 | override fun reset() {
233 | flush()
234 | processBuffer = AudioProcessor.EMPTY_BUFFER
235 | inputAudioFormat = AudioProcessor.AudioFormat(Format.NO_VALUE,Format.NO_VALUE,Format.NO_VALUE)
236 | }
237 | }
--------------------------------------------------------------------------------