问题描述
我要显示音频波形color-由当地频率内容各部分codeD。基本上什么的Serato /拖拉机或其他任何DJ软件确实在那里你可以看一下的声音,并告诉频率那里。它看起来是这样的:
I want to display audio waveforms color-coded in each part by the local frequency content. Basically exactly what Serato/Traktor or any other DJ software does where you can look at the sound and tell what frequencies are there. It looks like this:
所以基本上,我会做一个FFT来获得在我指定的任何滨宽的频率,但任何人都可以参考我的一些code(preferably C),将在实际绘制它是有用的?
So essentially, I will do an FFT to get frequencies at whatever bin-width I specify, but can anyone refer me to some code (preferably c) that would be useful in actually DRAWING it?
推荐答案
让我们尝试一个真正的答案这个时候。 : - )
Let's try a real answer this time. :-)
这个问题太复杂,给一个完整的解决方案,在这个空间中的所有code,但我会用伪code和假设你有一些库,可以窗口样本和计算的FFT块。
The problem is too complex to give a complete solution with all the code in this space, but I will use pseudocode and assume you have some library that can window a block of samples and compute FFTs.
它类似于建立一个波形显示。当你建立你的波形显示,确定样品多少适合成一个单一的水平像素在当前缩放级别,在那里他们开始给你的X-滚动位置,计算出该段的最小和最大样本值,让您的最小/为波形像素最大Y位置。 (这实际上是一个有点简单,我写的波形呈现code早在一天,但这是一个很好的近似。)
It's similar to building a waveform display. When you build your waveform display, you determine how many samples "fit" into a single horizontal pixel at the current zoom level, where they start given your X-scroll position, calculate the minimum and maximum sample value for that segment and gives you the min/max Y position for that waveform pixel. (This is actually a bit simplified, I've written waveform rendering code back in the day but this is a good approximation.)
要与频率颜色的电波,要preproces使用具有短小箱短时间的FFT的波数据,并为每个仓确定predominant频率是什么,然后将其上映射到一个彩色从红色光谱紫色。
To color your waves with frequency, you want to preproces the wave data using a short time FFT with smallish bins, and for each bin determine what the predominant frequency is and then map it to a color on the spectrum from red to violet.
比方说,你的音频采样在名为样品阵列
,这里的伪code。
Let's say your audio samples in an array called samples
, here's the pseudo code.
// sample rate
float fS = 44100;
// size of frame for analysis, you may want to play with this
float frameMsec = 10;
// samples in a frame
int frameSamples = (int)(fS / (frameMsec * 1000));
// how much overlap each frame, you may want to play with this one too
int overlapSamples = (frameSamples / 2);
// number of samples in the sound file
int numSamples = ...;
// input array of samples
float inSamples[] = ...;
// color to use for each frame
RGB outColors[] = new float[(numSamples / frameOverlap) + 1];
// scratch buffers
float tmpWindow[frameSamples];
float tmpFFT[frameSamples];
// helper function to apply a windowing function to a frame of samples
void calcWindow(float* dst, const float* src, int size);
// helper function to compute FFT
void fft(float* dst, const float* src, int size);
// find the index of array element with the highest absolute value
// probably want to take some kind of moving average of buf[i]^2
// and return the maximum found
int maxFreqIndex(const float* buf, int size);
// map a frequency to a color, red = lower freq -> violet = high freq
RGB freqToColor(int i);
for (int i = 0, outptr = 0; i < numSamples; i += frameOverlap, outptr++)
{
// window another frame for FFT
calcWindow(tmpWindow, &inSamples[i], frameSamples);
// compute the FFT on the next frame
fft(tmpFFT, tmpWindow, frameSamples);
// which frequency is the highest?
int freqIndex = maxFreqIndex(tmpFFT, frameSamples);
// map to color
outColor[outptr] = freqToColor(freqIndex);
}
这会给你RGB数组,你可以进出放大作为你变焦的波形显示和缩小。当你放大你可能要平均adjancent帧的RGB值给你一个总体看法。
This will give you an array of RGB which you can zoom in and out of as your zoom your waveform display in and out. As you zoom you probably want to average the RGB values of adjancent frames to give you an overall view.
我希望这有助于。
这篇关于code至颜色由波形频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!