我正在使用Visualizer来绘制我正在播放的声音文件的可视化。声波正在显示,但是我想让它不那么详细,因为它对我的帧速率有影响。这方面的文件非常有限。所以我试图做到以下几点:
mVisualizer.setCaptureSize(2);
将捕获率设置为非常低的值。但是,似乎正在绘制具有相同数量细节的线条。我在文档中读到:
Sets the capture size, i.e. the number of bytes returned by getWaveForm(byte[]) and getFft(byte[]) methods.
我的另一个问题是,我想在我正在播放的声音文件中检测到具有高能量级别的声音,因此我可以在屏幕上直观地表示它们。例如:屏幕随基线闪烁。以下是我到目前为止的情况:
public static void setupVisualizer() {
mVisualizer = new Visualizer(mpSong.getAudioSessionId());
mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
mVisualizer.setDataCaptureListener(
new Visualizer.OnDataCaptureListener() {
public void onWaveFormDataCapture(Visualizer visualizer,
byte[] bytes, int samplingRate) {
Game.updateVisualizer(bytes);
}
public void onFftDataCapture(Visualizer visualizer,
byte[] bytes, int samplingRate) {
}
}, Visualizer.getMaxCaptureRate() / 2, true, false);
}
是否有可能在侦听器中检测到某些声音?或者有哪些替代方案? 对不起,我的英语不好。非常感谢你的朋友们。
答案 0 :(得分:5)
好几个小时的测试和研究后我找到了解决方案。它可能不是很准确,但它是我能想到的唯一选择。我创建了一个名为BeatDetector的类:
public class BeatDetectorByFrequency {
private static final String TAG = "TEST";
private Visualizer mVisualizer = null;
private double mRunningSoundAvg[];
private double mCurrentAvgEnergyOneSec[];
private int mNumberOfSamplesInOneSec;
private long mSystemTimeStartSec;
// FREQS
private static final int LOW_FREQUENCY = 300;
private static final int MID_FREQUENCY = 2500;
private static final int HIGH_FREQUENCY = 10000;
private OnBeatDetectedListener onBeatDetectedListener = null;
public BeatDetectorByFrequency() {
init();
}
private void init() {
mRunningSoundAvg = new double[3];
mCurrentAvgEnergyOneSec = new double[3];
mCurrentAvgEnergyOneSec[0] = -1;
mCurrentAvgEnergyOneSec[1] = -1;
mCurrentAvgEnergyOneSec[2] = -1;
}
public void link(MediaPlayer player) {
if (player == null) {
throw new NullPointerException("Cannot link to null MediaPlayer");
}
mVisualizer = new Visualizer(player.getAudioSessionId());
mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
Visualizer.OnDataCaptureListener captureListener = new Visualizer.OnDataCaptureListener() {
@Override
public void onWaveFormDataCapture(Visualizer visualizer,
byte[] bytes, int samplingRate) {
// DO NOTHING
}
@Override
public void onFftDataCapture(Visualizer visualizer, byte[] bytes,
int samplingRate) {
updateVisualizerFFT(bytes);
}
};
mVisualizer.setDataCaptureListener(captureListener,
Visualizer.getMaxCaptureRate() / 2, false, true);
mVisualizer.setEnabled(true);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mVisualizer.setEnabled(false);
}
});
mSystemTimeStartSec = System.currentTimeMillis();
}
public void release() {
if (mVisualizer != null) {
mVisualizer.setEnabled(false);
mVisualizer.release();
}
}
public void pause() {
if (mVisualizer != null) {
mVisualizer.setEnabled(false);
}
}
public void resume() {
if (mVisualizer != null) {
mVisualizer.setEnabled(true);
}
}
public void updateVisualizerFFT(byte[] audioBytes) {
int energySum = 0;
energySum += Math.abs(audioBytes[0]);
int k = 2;
double captureSize = mVisualizer.getCaptureSize() / 2;
int sampleRate = mVisualizer.getSamplingRate() / 2000;
double nextFrequency = ((k / 2) * sampleRate) / (captureSize);
while (nextFrequency < LOW_FREQUENCY) {
energySum += Math.sqrt((audioBytes[k] * audioBytes[k])
* (audioBytes[k + 1] * audioBytes[k + 1]));
k += 2;
nextFrequency = ((k / 2) * sampleRate) / (captureSize);
}
double sampleAvgAudioEnergy = (double) energySum
/ (double) ((k * 1.0) / 2.0);
mRunningSoundAvg[0] += sampleAvgAudioEnergy;
if ((sampleAvgAudioEnergy > mCurrentAvgEnergyOneSec[0])
&& (mCurrentAvgEnergyOneSec[0] > 0)) {
fireBeatDetectedLowEvent(sampleAvgAudioEnergy);
}
energySum = 0;
while (nextFrequency < MID_FREQUENCY) {
energySum += Math.sqrt((audioBytes[k] * audioBytes[k])
* (audioBytes[k + 1] * audioBytes[k + 1]));
k += 2;
nextFrequency = ((k / 2) * sampleRate) / (captureSize);
}
sampleAvgAudioEnergy = (double) energySum / (double) ((k * 1.0) / 2.0);
mRunningSoundAvg[1] += sampleAvgAudioEnergy;
if ((sampleAvgAudioEnergy > mCurrentAvgEnergyOneSec[1])
&& (mCurrentAvgEnergyOneSec[1] > 0)) {
fireBeatDetectedMidEvent(sampleAvgAudioEnergy);
}
energySum = Math.abs(audioBytes[1]);
while ((nextFrequency < HIGH_FREQUENCY) && (k < audioBytes.length)) {
energySum += Math.sqrt((audioBytes[k] * audioBytes[k])
* (audioBytes[k + 1] * audioBytes[k + 1]));
k += 2;
nextFrequency = ((k / 2) * sampleRate) / (captureSize);
}
sampleAvgAudioEnergy = (double) energySum / (double) ((k * 1.0) / 2.0);
mRunningSoundAvg[2] += sampleAvgAudioEnergy;
if ((sampleAvgAudioEnergy > mCurrentAvgEnergyOneSec[2])
&& (mCurrentAvgEnergyOneSec[2] > 0)) {
fireBeatDetectedHighEvent(sampleAvgAudioEnergy);
}
mNumberOfSamplesInOneSec++;
if ((System.currentTimeMillis() - mSystemTimeStartSec) > 1000) {
mCurrentAvgEnergyOneSec[0] = mRunningSoundAvg[0]
/ mNumberOfSamplesInOneSec;
mCurrentAvgEnergyOneSec[1] = mRunningSoundAvg[1]
/ mNumberOfSamplesInOneSec;
mCurrentAvgEnergyOneSec[2] = mRunningSoundAvg[2]
/ mNumberOfSamplesInOneSec;
mNumberOfSamplesInOneSec = 0;
mRunningSoundAvg[0] = 0.0;
mRunningSoundAvg[1] = 0.0;
mRunningSoundAvg[2] = 0.0;
mSystemTimeStartSec = System.currentTimeMillis();
}
}
// USE INTERFACES IN NEXT UPDATE:
private void fireBeatDetectedLowEvent(double power) {
// Utility.log("LOW BEAT DETECTED!");
Game.lowBeat(power);
if (onBeatDetectedListener != null) {
onBeatDetectedListener.onBeatDetectedLow();
}
}
private void fireBeatDetectedMidEvent(double power) {
// Utility.log("MEDIUM BEAT DETECTED!");
Game.mediumBeat(power);
if (onBeatDetectedListener != null) {
onBeatDetectedListener.onBeatDetectedMid();
}
}
private void fireBeatDetectedHighEvent(double power) {
// Utility.log("HIGH BEAT DETECTED!");
Game.highBeat(power);
if (onBeatDetectedListener != null) {
onBeatDetectedListener.onBeatDetectedHigh();
}
}
public void setOnBeatDetectedListener(OnBeatDetectedListener listener) {
onBeatDetectedListener = listener;
}
public interface OnBeatDetectedListener {
public abstract void onBeatDetectedLow();
public abstract void onBeatDetectedMid();
public abstract void onBeatDetectedHigh();
}
}
它将MediaPlayer对象作为参数,然后根据字节数据的EnergySum计算三个不同的频率。可以根据需要多次分割频率。我正在考虑创建一个频率数组,每个人都有一个监听器。然后我使用以下绘制矩形:
public static void highBeat(double power) {
HIGH_FREQUENCY += (power * 1000); // ORIGINAL: * 1000
if (HIGH_FREQUENCY > GameValues.FREQ_MAX) {
HIGH_FREQUENCY = GameValues.FREQ_MAX;
}
updateHighFreq();
}
public static void updateHighFreq() {
prcnt = HIGH_FREQUENCY * 100 / GameValues.FREQ_MAX;
if (prcnt < 0)
prcnt = 0;
HIGH_F_HEIGHT = (int) (GameValues.FREQ_MAX_HEIGHT * (prcnt / 100));
}
通过计算基于条的最大功率和最大高度的百分比来计算矩形的高度。它不是很准确,但这是我能想到的最好的东西。同样,这可以根据您的需要为多个频率完成。以下是一些帮助我的链接:
希望我能帮助其他人解决这些问题。