gpt4 book ai didi

java - 请求有关可视化器的Android Kotlin音频分析的建议

转载 作者:行者123 更新时间:2023-11-30 10:00:30 25 4
gpt4 key购买 nike

,这是我想要达到的目标:

我希望能够将电话麦克风录制的音频并以给定频率处理电平到一个数组中,以创建一种“条形图”可视化器。我还需要计算播放歌曲的BPM,以了解更新可视化程序的节奏。

我真正要寻找的只是获取频率阵列和BPM计算。如果我能弄清楚如何处理音频,则可以处理实际的可视化部分。

这就是我要制作的:

This is pretty much what I'm looking to make:

从我所做的研究来看,似乎可能的解决方案是使用FFT(快速傅立叶变换)。为此,我找到了这个:stackoverflow。但是该stackoverflow线程是用于Java的,而我的应用程序当前是用Kotlin编写的。我读过有可能将Java导入Kotlin,但是经过几次尝试,我仍然无法使其工作。因此,对此的一些建议可能会有所帮助。

无论如何,我最终还是找到了一个为Kotlin Nier Vizualizer编写的库。它确实具有类似于我想要复制的形式,但是对于我的一生,我无法弄清楚从何处提取频率阵列。我尝试在将缓冲区传递到可视化器之前先读取它们,但这只是我收到的一个疯狂的长字符串。我的意思是,我不确定我期望的是什么,但是我绝对可以使用一些帮助来理解如何将其转换为可以实际使用的数据格式。

这是我所在位置的示例:KeyFrameMaker.kt

    fun makeKeyFrame() {
val waveFrac = mWaveAnimator.computeCurrentValue()
val fftFrac = mFftAnimator.computeCurrentValue()
if (mWaveAnimator.hasValueUpdated) {
computedWaveData.originMap { idx, _ ->
(((mDestWaveData[idx].toInt() and 0xFF) - (mPrevWaveData[idx].toInt() and 0xFF)) * waveFrac + (mPrevWaveData[idx].toInt() and 0xFF)).toByte()
}
// Create String for computedWaveData ByteArray
var hexBuffer = ""
for (b in computedWaveData) {
hexBuffer += String.format("%02X", b)
}
println("WAVE COMPUTED: $hexBuffer")
}
if (mFftAnimator.hasValueUpdated) {
computedFftData.originMap { idx, _ ->
((mDestFftData[idx] - mPrevFftData[idx]) * fftFrac + mPrevFftData[idx]).toByte()
}
// Create String for computedFftData ByteArray
var hexBuffer = ""
for (b in computedFftData) {
hexBuffer += String.format("%02X", b)
}
println("FFT COMPUTED: $hexBuffer")
}
}

在这里,我正在打印ByteArray,它是假定的computeFftData,并且它打印的值只是疯狂的长字符串,例如:“ED000F01020A0D01FAFAFF0003F700FE00FFFFFFFFFE0000000000FF000000000000000000FF000000010000000001000000000000000000FF ...”。至少可以说,我不太确定如何使用它来达到我的一系列频率水平的目标。

在这一点上,我认为我将只保留它,看看是否能得到任何建议。我也做了很多其他的研究,但是我不确定添加更多的东西是否真的会有所帮助。绝对希望只与更了解此事的人交谈,以指示我正确的方向。

作为参考,我将使用此Android Visualizer类和在此处找到的RECORD_AUDIO权限: Android Visualizer reference

编辑3:终于初始化了可视化器!

因此事实证明,从@ Tenfour04的挖掘来看,他是正确的,并且某些android设备在可视化器初始化方面存在问题。此处的更多信息: Github Thread

此时,我正在从Visualizer和音频记录 session 的FftDataCapture侦听器获取数据。我可能会在此时解决问题,但是如果有人想为我和其他最终阅读本文的人提供最佳实现的建议,那将不会有伤害!我肯定会继续用我发现的内容更新此帖子,以便将来的人们至少可以从一个例子中受益。

下一步是获取由FFT函数生成的幅度和相位数组,并找出将大量值数组减小到大约12左右的最佳方法... @ Tenfour04提到只是在给定位置获取设置值,我认为这将是一个很好的解决方案。从我对频率知之甚少的 Angular 来看,整个频谱之间没有相等的分布-因此,如果我没记错的话,我们需要从频谱乞讨中获取比最终更多的值...可能是错误的那,并且将不得不更多地研究它。

我看到的另一个问题是数组的长度始终相等。我相信这意味着我需要等待下一次数据捕获并重新捕获缓冲区?但是不确定这是否正确,也许我可以从给定大小数组的某些位置提取值?看起来好像onWavFormDataCapture的默认声明有一种方法可以等待ByteArray的大小等于mWaveBuffer byteArray的大小,但是ByteArray似乎是可变大小的-因此,这似乎无效根据我的理解解决方案。

无论如何,我只是在吐口水,我会得到更好的信息。

这是应用了FFT的音频数据片段:
Magnitudes: 57.0, 32.649654, 123.967735, 31.622776, 121.49486, 48.104053, 47.0, 14.142136, 5.0, 7.2111025, 6.708204, 5.0, 5.8309517, 2.236068, 6.708204, 9.899495, 11.401754, 9.219544, 4.472136, 3.6055512, 4.472136, 1.4142135, 4.1231055, 8.944272, 8.5440035, 18.973665, 17.888544, 20.09975, 15.264338, 9.055386, 6.708204, 4.472136, 3.1622777, 3.1622777, 2.236068, 2.236068, 2.0, 2.236068, 2.0, 0.0, 2.828427, 4.1231055, 3.6055512, 3.6055512, 2.828427, 4.472136, 2.828427, 2.236068, 2.0, 2.0, 2.0, 1.4142135, 2.0, 2.236068, 2.0, 1.4142135, 1.0, 1.0, 2.0, 2.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 0.0, 1.0, 1.4142135, 1.0, 1.0, 1.0, 1.4142135, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.4142135, 1.4142135, 1.0, 1.0, 1.4142135, 1.0, 1.4142135, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.4142135, 1.0, 1.0, 1.0, 0.0, 1.0, 1.4142135, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0
Magnitudes: 25.0, 69.289246, 132.63861, 129.71121, 21.095022, 23.537205, 23.537205, 9.219544, 22.847319, 14.422205, 10.816654, 8.944272, 2.236068, 0.0, 3.1622777, 8.062258, 20.615528, 2.828427, 4.1231055, 7.81025, 4.0, 2.236068, 3.1622777, 5.656854, 13.038404, 34.713108, 53.712196, 9.848858, 21.023796, 8.062258, 5.0990195, 5.3851647, 3.6055512, 3.1622777, 3.1622777, 3.1622777, 3.1622777, 3.1622777, 2.236068, 2.236068, 2.236068, 2.828427, 2.236068, 1.0, 4.0, 4.0, 3.1622777, 2.0, 2.0, 1.0, 2.0, 1.4142135, 1.4142135, 2.0, 3.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 1.0, 2.236068, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.4142135, 1.0, 1.0, 1.0, 1.0, 1.0, 1.4142135, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0
Magnitudes: 2.0, 19.104973, 30.232433, 73.3553, 6.4031243, 27.513634, 76.537575, 43.462627, 17.262676, 12.369317, 13.601471, 13.341664, 9.486833, 11.18034, 15.811388, 13.601471, 45.01111, 37.01351, 44.64303, 21.023796, 1.4142135, 7.28011, 8.5440035, 4.2426405, 13.416408, 21.023796, 16.492422, 11.401754, 3.0, 5.0, 5.0990195, 2.0, 3.6055512, 3.0, 2.0, 2.828427, 4.472136, 2.0, 3.1622777, 3.0, 2.0, 2.828427, 3.6055512, 0.0, 3.1622777, 2.236068, 2.0, 3.0, 2.236068, 3.1622777, 2.236068, 2.0, 2.236068, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 1.0, 2.0, 2.0, 1.0, 1.0, 1.0, 1.0, 2.0, 1.0, 2.0, 2.0, 1.0, 1.0, 2.0, 1.4142135, 1.4142135, 2.0, 1.0, 1.0, 2.0, 1.4142135, 1.4142135, 1.0, 2.236068, 1.0, 1.4142135, 3.0, 2.828427, 1.4142135, 3.0, 1.4142135, 2.236068, 4.0, 1.0, 1.4142135, 2.0, 1.0, 1.4142135, 1.0, 0.0, 1.4142135, 1.0, 1.4142135, 2.0, 1.4142135, 1.0, 1.4142135, 1.4142135, 0.0, 1.4142135, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.4142135, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0

Phases: 0.0, 2.0638473, 0.52183425, 0.15991312, 1.94013, -1.1383885, -1.2847449, 0.8519663, 2.4980915, 2.3561945, 2.7367008, 2.9441972, 2.7610862, 3.1415927, 2.0899425, 2.0344439, -0.32175055, -2.2794225, 2.3561945, 1.2490457, -1.5707964, -0.98279375, 2.4668517, 1.5707964, 2.3561945, 1.5707964, 3.1415927, 3.1415927, 1.5707964, 2.3561945, 3.1415927, 1.5707964, 1.5707964, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 2.3561945, 1.5707964, -1.5707964, 3.1415927, 0.0, 3.1415927, -2.6779451, 0.0, 3.1415927, -2.3561945, 2.3561945, 0.0, -2.3561945, 3.1415927, 3.1415927, 3.1415927, 0.0, 3.1415927, 3.1415927, -2.3561945, 3.1415927, 3.1415927, 3.1415927, 0.0, 3.1415927, 3.1415927, 3.1415927, 0.0, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, -2.3561945, -2.3561945, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 0.0, 3.1415927, 1.5707964, 0.0, 0.0, 0.0, 3.1415927, 2.6779451, 3.1415927, 2.3561945, -0.7853982, 0.0, -2.819842, 0.0, -1.5707964, -2.0344439, 2.6779451, 3.1415927, 1.5707964, 2.6779451, 1.5707964, -2.3561945, 0.0, 3.1415927, 2.3561945, 2.3561945, 0.0, -2.3561945, 2.3561945, -1.5707964, 3.1415927, 0.0, -2.6779451, 1.5707964, 0.0, 0.0, 3.1415927, 0.0, 0.0, 3.1415927, 3.1415927, 3.1415927, 0.0, -1.5707964, 3.1415927, -2.3561945, -2.3561945, 3.1415927, 0.0, -2.3561945, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 2.3561945, 0.0, 3.1415927, -2.3561945, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 0.0, 3.1415927, 0.0, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 0.0, -2.3561945, 3.1415927, -2.3561945, -2.3561945, 3.1415927, 3.1415927, 3.1415927, -2.3561945, -2.3561945, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, -2.3561945, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, -2.3561945, 3.1415927, -2.3561945, -2.3561945, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, -2.3561945, 3.1415927, 3.1415927, 3.1415927, -2.3561945, 3.1415927, 3.1415927, 3.1415927, 3.1415927, -2.3561945, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, -2.3561945, 3.1415927, 3.1415927, -2.3561945, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, 3.1415927, -2.3561945, 3.1415927, 3.1415927, -2.3561945, 3.1415927, -2.3561945, -2.3561945, 3.1415927, 3.1415927, -2.3561945, -2.3561945, -2.3561945, -2.3561945, -2.3561945, 0.0
Phases: 0.0, 2.2694561, -1.7492068, -1.8925469, -0.15702978, 1.2095926, 0.0, 1.4288993, -0.9272952, -0.5880026, -1.1071488, -0.6435011, -0.5404195, 0.4636476, -2.6779451, -2.3561945, -2.2318394, -0.86217004, 1.1071488, 2.158799, -2.6779451, -0.7853982, 1.815775, 2.6779451, -1.929567, 0.32175055, 1.1071488, 1.4711276, 2.1224513, -1.6814536, -1.1071488, -1.1071488, -1.2490457, -1.2490457, -1.1071488, -1.1071488, -1.5707964, -1.1071488, 0.0, 0.0, -2.3561945, -1.3258177, 0.5880026, 2.55359, -2.3561945, -1.1071488, -0.7853982, -1.1071488, -1.5707964, -1.5707964, -1.5707964, -0.7853982, 3.1415927, -2.6779451, -1.5707964, -0.7853982, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, 0.0, 3.1415927, -2.3561945, -1.5707964, -1.5707964, -1.5707964, -2.3561945, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -2.3561945, -2.3561945, -1.5707964, -1.5707964, -2.3561945, -1.5707964, -2.3561945, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -2.3561945, -1.5707964, -1.5707964, -1.5707964, 0.0, -1.5707964, -2.3561945, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, 0.0, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, 0.0, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, 0.0, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, 0.0, -1.5707964, 0.0, -1.5707964, -1.5707964, -1.5707964, 0.0, 0.0, 0.0, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, 0.0, -1.5707964, 0.0, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, 0.0, -1.5707964, -1.5707964, 0.0, 0.0, 0.0, 0.0, -1.5707964, 0.0, -1.5707964, -1.5707964, -1.5707964, -1.5707964, 0.0, -1.5707964, 0.0, 0.0, -1.5707964, 0.0
Phases: 0.0, 1.2170932, -2.5222197, 1.1243883, 0.094951704, 0.21406068, 1.784857, -2.4329665, -0.4048918, -0.5880026, -0.98279375, -1.1071488, 1.1071488, 0.0, 1.2490457, 2.0899425, 2.7430701, 0.7853982, -1.815775, -0.87605804, 1.5707964, -1.1071488, -2.819842, -0.7853982, 1.0040671, 2.2950463, 2.6363025, 2.7233684, -1.1284221, -1.0516502, -1.3734008, -1.19029, -0.98279375, -1.2490457, -1.2490457, -1.2490457, -1.2490457, -1.2490457, -1.1071488, -1.1071488, -1.1071488, -0.7853982, 0.4636476, -1.5707964, -1.5707964, -1.5707964, -1.2490457, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -0.7853982, -0.7853982, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -2.0344439, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -2.3561945, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -2.3561945, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, 0.0, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, 0.0, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, 0.0, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, 0.0, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, 0.0, -1.5707964, 0.0, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, 0.0, -1.5707964, -1.5707964, -1.5707964, -1.5707964, 0.0, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, 0.0, -1.5707964, -1.5707964, 0.0, 0.0, -1.5707964, 0.0, -1.5707964, -1.5707964, -1.5707964, 0.0, 0.0, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, -1.5707964, 0.0, 0.0, -1.5707964, -1.5707964, 0.0, 0.0, 0.0

这是我现在正在使用的代码:
class MainActivity : AppCompatActivity() {

val SAMPLING_RATE = 44100
val REQUEST_CODE_AUDIO_PERMISSION = 1

private var visualizer: Visualizer? = null

lateinit var magnitudesArray: FloatArray

private var mWaveBuffer: ByteArray? = null
private var mFftBuffer: ByteArray? = null
private var mDataCaptureSize: Int = 0

private var mAudioBufferSize: Int = 0
private var mAudioRecord: AudioRecord? = null

private var mAudioRecordState: Boolean = false

@TargetApi(Build.VERSION_CODES.KITKAT)
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

ensurePermissionAllowed()
println("OUTSIDE PERMISSIONS CHECK")

mAudioBufferSize = AudioRecord.getMinBufferSize(SAMPLING_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_8BIT)
mAudioRecord = AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLING_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_8BIT, mAudioBufferSize)

println("mAudioBufferSize: $mAudioBufferSize")

if (mAudioRecord!!.state != AudioRecord.STATE_INITIALIZED) println("AudioRecord init failed")
else println("AudioRecord init success")

try {
println("BEGIN INITIALIZING VIZUALIZER.")
println("Audio Session ID: ${mAudioRecord!!.audioSessionId}")
//visualizer = Visualizer(mAudioRecord.audioSessionId).apply {
visualizer = Visualizer(0).apply {
enabled = false
captureSize = 512
//captureSize = captureSizeRange[1]

try {
scalingMode = Visualizer.SCALING_MODE_NORMALIZED
} catch (e: NoSuchMethodError) {
println("CANT SET SCALING MODE.")
}
measurementMode = Visualizer.MEASUREMENT_MODE_NONE

setDataCaptureListener(object : Visualizer.OnDataCaptureListener {
override fun onFftDataCapture(visualizer: Visualizer?, fft: ByteArray?, samplingRate: Int) {
val n = fft?.size
val magnitudes = FloatArray(n!! / 2 + 1)
val phases = FloatArray(n / 2 + 1)
magnitudes[0] = Math.abs(fft[0].toFloat())
magnitudes[n / 2] = Math.abs(fft[1].toFloat())
phases[n / 2] = 0f
phases[0] = phases[n / 2]
for (k in 1 until n / 2) {
val i = k * 2
magnitudes[k] = Math.hypot(fft[i].toDouble(), fft[i + 1].toDouble()).toFloat()
phases[k] = Math.atan2(fft[i + 1].toDouble(), fft[i].toDouble()).toFloat()
}
println(magnitudes.joinToString(", "))
println(phases.joinToString(", "))
}
override fun onWaveFormDataCapture(visualizer: Visualizer?, waveform: ByteArray?, samplingRate: Int) {
val waveBuffer = mWaveBuffer ?: return
if (waveform == null || waveform.size != waveBuffer.size) {
return
}
System.arraycopy(waveform, 0, waveBuffer, 0, waveform.size)
}

}, Visualizer.getMaxCaptureRate(), true, true)
}.apply {
mDataCaptureSize = captureSize.apply {
mWaveBuffer = ByteArray(this)
mFftBuffer = ByteArray(this)
}
}
} catch (e: RuntimeException) {
println("ERROR DURING VISUALIZER INITIALIZATION: $e")
}

record_button.setOnClickListener {
if (!mAudioRecordState) {
mAudioRecord!!.startRecording()
visualizer?.enabled = true

mAudioRecordState = true
}
else {
mAudioRecord!!.stop()
visualizer?.enabled = false

mAudioRecordState = false
}
}
}

private fun ensurePermissionAllowed() {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
println("PERMISSION TO RECORD AUDIO DENIED. REQUESTING.")
ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.RECORD_AUDIO), REQUEST_CODE_AUDIO_PERMISSION)
}
else {
println("PERMISSION TO RECORD AUDIO GRANTED.")
}
}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when (requestCode) {
REQUEST_CODE_AUDIO_PERMISSION -> {
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Demo need record permission, please allow it to show this visualize effect!", Toast.LENGTH_LONG).show()
finish()
}
}
}
}

最佳答案

如果您使用的是Android Visualizer类,则不需要其他FFT库,因为Visualizer类提供了getFFT()方法,该方法可向您返回已应用FFT的音频。您只需要将其转换为dB级别的幅度,即可使其在图形中看起来不错。

在尝试实例化Visualizer之前,您需要请求麦克风权限,否则它将无法初始化。 Visualizer主要用C语言实现,并且主要是JNI包装器,因此如果出现问题,则会引发大量RuntimeExceptions,因此您需要将初始化和设置调用包装在try / catch块中。

lateinit var magnitudesArray: FloatArray

//...

val captureSizeRange = Visualizer.getCaptureSizeRange()

try {
visualizer = Visualizer(0)
visualizer?.let {
scalingMode = Visualizer.SCALING_MODE_NORMALIZED
captureSize = captureSizeRange[1]
setDataCaptureListener(this, Visualizer.getMaxCaptureRate(), false, true)
enabled = true
}
} catch (e: RuntimeException) {
//..log it
}
magnitudesArray = FloatArray(captureSizeRange[1] / 2 + 1)

然后,您可以实现数据捕获侦听器的 onFftDataCapture函数,以使用 documentation中给出的公式将传入的FFT传输到 magnitudesArray。将log10函数应用于这些值以获得适合于可视化的幅度(人耳以大约log10的比例解释相对声能,这就是为什么声音通常以dB单位描述的原因)。

它们的典型捕获大小为1024或2048,因此阵列中的大小将为1024。为了获得像您的图片一样的频谱棒可视化效果,我发现,如果您只是从幅度数组中选择均匀间隔的值,而不是尝试对它们的范围求平均,则看起来似乎是最好的。

关于您对Java代码的评论,它只是在您的项目中添加Java库并从Kotlin调用它。您并不是真正地“导入”它。 Kotlin文档具有 all the details

关于java - 请求有关可视化器的Android Kotlin音频分析的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57931246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com