一般的声音可视化做出频谱图就行了:
但我觉得频谱图看不出声音是什么样的,所以坚定要做出声像图,这两种图都要获取声音中频率的信息。
研究了官网上的auriotouch和pitchdetector两个例子,把auriotouch的声像显示和pitch detector主频获取的主要代码集成到了上节《 oc开发笔记2 AUGraph 完成同时录音与播放 》中,并用CALayer做了绘图。音频数据流处理函数PerformThru,上节中该函数只完成静音处理功能,本节他可以通过fft获取各个频率的分贝,以及当前主频的频率和分贝。把各频率数据缓存到了数组fArr中用来绘图,主要代码如下:
static OSStatus PerformThru(
void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData)
{
//界面的指针,用来获取静音开关按钮
CDYViewController *THIS=(__bridge CDYViewController*)inRefCon;
int bufferCapacity = THIS->bufferCapacity;
SInt16 index = THIS->index;
void *dataBuffer = THIS->dataBuffer;
float *outputBuffer = THIS->outputBuffer;
uint32_t log2n = THIS->log2n;
uint32_t n = THIS->n;
uint32_t nOver2 = THIS->nOver2;
Float32 kAdjust0DB =THIS->kAdjust0DB ;
COMPLEX_SPLIT A = THIS->A;
FFTSetup fftSetup = THIS->fftSetup;
static int numLevels = sizeof(colorLevels) / sizeof(GLfloat) / 5;
//AudioUnitRender将Remote I/O的输入端数据读进来,其中每次数据是以Frame存在的,
//每笔Frame有N笔音讯数据内容(这与类比转数位的概念有关,在此会以每笔Frame有N点),2声道就是乘上2倍的数据量,
//整个数据都存在例子中的ioData指针中
OSStatus renderErr = AudioUnitRender(THIS->remoteIOUnit, ioActionFlags,
inTimeStamp, 1, inNumberFrames, ioData);
//把数据读到dataBuffer中&#x