Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Audio 按频率对波形进行颜色编码_Audio_Drawing - Fatal编程技术网

Audio 按频率对波形进行颜色编码

Audio 按频率对波形进行颜色编码,audio,drawing,Audio,Drawing,我想显示音频波形颜色编码的每一部分由当地的频率内容。基本上正是Serato/Traktor或任何其他DJ软件所做的,在那里你可以看到声音并知道那里有什么频率。看起来是这样的: 因此,本质上,我将进行FFT,以获得我指定的任何箱子宽度的频率,但有人能告诉我一些代码(最好是c)在实际绘制时有用吗?这次让我们尝试一个真实的答案。:-) 这个问题太复杂了,无法用这个空间中的所有代码给出完整的解决方案,但我将使用伪代码,并假设您有一些库可以打开一块样本并计算FFT 这类似于构建波形显示器。构建波形显示时

我想显示音频波形颜色编码的每一部分由当地的频率内容。基本上正是Serato/Traktor或任何其他DJ软件所做的,在那里你可以看到声音并知道那里有什么频率。看起来是这样的:


因此,本质上,我将进行FFT,以获得我指定的任何箱子宽度的频率,但有人能告诉我一些代码(最好是c)在实际绘制时有用吗?

这次让我们尝试一个真实的答案。:-)

这个问题太复杂了,无法用这个空间中的所有代码给出完整的解决方案,但我将使用伪代码,并假设您有一些库可以打开一块样本并计算FFT

这类似于构建波形显示器。构建波形显示时,确定当前缩放级别下单个水平像素中“适合”的采样数(从给定X滚动位置开始),计算该段的最小和最大采样值,并为该波形像素提供最小/最大Y位置。(这实际上有点简化,我早就编写了波形渲染代码,但这是一个很好的近似值。)

要使用频率对波进行着色,您需要使用带有小容器的短时FFT对波数据进行预处理,并为每个容器确定主要频率,然后将其映射到光谱上从红色到紫色的颜色

假设您的音频样本位于一个名为
samples
的数组中,下面是伪代码

// 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);
}
//采样率
浮点数fS=44100;
//用于分析的框架大小,您可能需要使用此
浮点帧毫秒=10;
//框架中的样本
int frameSamples=(int)(fS/(frameMsec*1000));
//每个帧重叠多少,你可能也想玩这个
int overlappSamples=(frameSamples/2);
//声音文件中的采样数
int numSamples=。。。;
//样本的输入数组
样本中的浮动[]=。。。;
//用于每个帧的颜色
RGB outColors[]=新浮点[(numSamples/frameOverlap)+1];
//划痕缓冲区
浮动tmpWindow[框架样本];
浮动tmpFFT[frameSamples];
//helper函数将窗口函数应用于样本帧
void calcWindow(float*dst,const float*src,int size);
//计算FFT的辅助函数
无效fft(浮点*dst,常量浮点*src,整数大小);
//查找绝对值最高的数组元素的索引
//可能想取buf[i]^2的某种移动平均值
//并返回找到的最大值
int maxFreqIndex(常量浮点*buf,int大小);
//将频率映射到颜色,红色=低频->紫色=高频
RGB频率颜色(int i);
对于(int i=0,outptr=0;i
这将为您提供一个RGB阵列,您可以在放大和缩小波形时放大和缩小该阵列。缩放时,您可能希望平均相邻帧的RGB值,以获得整体视图


我希望这能有所帮助。

Audacity是一款流行的免费/开源音频编辑器,适用于多种平台,包括可从下载的源代码,我相信如果您在那里四处看看,您会发现它绘制波形的代码。(如果您使用此代码,请确保根据其开源许可证包含所有开源属性。)@RonKuper 1)不,它没有。它的主题是将整个波浪描绘成某种颜色。这不是我想要的。2) 你对这个问题投了否决票,回答得如此漫不经心,真是个dbag。而且,我花了相当长的时间看了一些关于Serato/Traktor/Comparisonics的音频技术论文。我没有否决你的问题,事实上我只是把你的问题的票数提高到了0。