我正在记录来自麦克风的浮动流(应该是静音)
我设置了缓冲区大小为 256 个浮点数的音频队列
典型的缓冲区如下所示:
PACKET 0.004791, 0.012512,0.008423,0.000122,-0.000519,-0.002991,-0.000031,0.001801,-0.000641, 0.001190,-0.003143,-0.001587,0.001587,-0.015503,-0.019440,-0.015167,-0.017670, -0.018158,-0.019928 ,-0.019409,-0.024017,-0.019684,-0.024719,-0.044128,-0.043579, -0.043152,-0.046417,-0.045380,-0.050079,-0.050262,-0.049164,-0.040710,-0.036713, -0.051056,-0.045868,- 0.035034,-0.033722,-0.028534,-0.027161,-0.022186,-0.018036, -0.012207,0.004303,-0.000824,-0.000610,0.014496,0.018005,0.019745,0.019226, 0.016144,0.013184,0.009003,0.014557,0.003357,-0.011353,- 0.007751,-0.007660, -0.006409,-0.003357,-0.003510,-0.001038,-0.000092,0.007690,0.002655,0.000000, 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000,0。000000,0.0000,0000,0000,000000000000000000000000000000000000,0000,0.000000,0000,000000000000,0000,00000000,0000,0000000000,0000000000,0000,0000,000000,0000000000,000000000000000000000000来0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,-0.006897,-0.000549, 0.003174,0.003540,0.003632, 0.004578,0.005280,0.001831,0.014771,0.014954,0.001801,0.009247,0.011139, 0.005249,0.008087,0.008636,0.007385,0.007263,0.016571,0.020264,0.010590, 0.014801,0.023132,0.027039,0.031128,0.031799,0.037109, 0.038757,0.049438, 0.057098,0.042786,0.045593,0.052032,0.045380,0.045227,0.045837,0.043793, 0.041931,0.043976,0.046570,0.030182,0.024475,0.029877,0.026184,0.026001, 0.026611,0.031921,0.035736,0.040710,0.053070,0.042572,0.039917,0.051636, 0.053009,0.053528,0.053009,0.054962,0.055603,0.053833,0.060638,0.050171, 0.041779,0.049194,0.046356,0.000000,0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000, 0.000000, 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.041931, 0.038879,0.034515,0.031494,0.026337,0.034576,0.028992,0.014038,0.018127, 0.017822,0.015137,0.015778,0.013519,0.015564,0.014832,0.023285,0.022034, 0.006317,0.010254, 0.010742,0.004303,0.003784,-0.000153,-0.002502,~0.054962,0.055603,0.053833,0.060638,0.050171, 0.041779,0.049194,0.046356,0.000000,0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.041931, 0.038879,0.034515,0.031494,0.026337,0.034576, 0.028992,0.014038,0.018127, 0.017822,0.015137,0.015778,0.013519,0.015564,0.014832,0.023285,0.022034, 0.006317,0.010254,0.010742,0.004303,0.003784,-0.000153,-0.002502, ~0.054962,0.055603,0.053833,0.060638,0.050171, 0.041779,0.049194,0.046356,0.000000,0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.041931, 0.038879,0.034515,0.031494,0.026337,0.034576, 0.028992,0.014038,0.018127, 0.017822,0.015137,0.015778,0.013519,0.015564,0.014832,0.023285,0.022034, 0.006317,0.010254,0.010742,0.004303,0.003784,-0.000153,-0.002502, ~000000,0.000000,0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.041931, 0.038879,0.034515,0.031494, 0.026337,0.034576,0.028992,0.014038,0.018127, 0.017822,0.015137,0.015778,0.013519,0.015564,0.014832,0.023285,0.022034, 0.006317,0.010254,0.010742,0.004303,0.003784,-0.000153,-0.002502, ~000000,0.000000,0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.041931, 0.038879,0.034515,0.031494, 0.026337,0.034576,0.028992,0.014038,0.018127, 0.017822,0.015137,0.015778,0.013519,0.015564,0.014832,0.023285,0.022034, 0.006317,0.010254,0.010742,0.004303,0.003784,-0.000153,-0.002502, ~
我不明白为什么我在输入信号中似乎有随机的零串。似乎有一些不连续的东西。
我首先想到也许我有左声道和右声道,而右声道总是记录为零。但是查看我的代码,我已经清楚地将其设置为单个通道。
然后我想也许这些只是信号中的寂静之地。但这没有意义。如果我只有十几个零,0.000000 我肯定会期待非常小的数字,比如 .000007 .000014,但非零数字似乎在 0.01 左右。
我刚刚尝试将音频输入切换到外部 USB 麦克风,这提高了分辨率。非零数字现在似乎在 0.001 左右。但是仍然有明显的不连续性...
我想知道是否正在对四舍五入为0的芯片进行一些计算。如果是这种情况,是否可以校准?到底是怎么回事?
我注意到的第二个非常奇怪的问题是流氓值。
这是一个包含其中一些值的示例数据包(这次使用 USB 麦克风;您可以看到分辨率是如何提高的):
~ PACKET -0.001343, -0.001190,-0.001526,-0.001373,-0.000946,-0.001526,-0.001221,-0.001190,-0.001221, -0.001251,-0.001373,-0.001190,-0.001312,-0.001312,-0.001434,-0.001282, -0.001312, -0.001099,-0.001007,-0.001221,-0.001160,-0.001312,-0.001343,-0.001221,-0.001007, -0.001099,-0.001404,-0.001068,-0.001038,-0.001404,-0.001038,-0.001190,-0.001404 , -0.001099,-0.001282,-0.001221,-0.001007,-0.001007,-0.001099,-0.001221,-0.001160, -0.001038,-0.001038,-0.001007,-0.000946,-0.001129,-0.000916,-0.000946,-0.000946, - 0.000946,-0.000824,-0.000824,-0.001007,-0.000763,-0.001038,-0.000854,-0.000977, -0.000916,-0.000641,-0.000977,-0.000916,-0.000946,-0.000732,-0.000824,0.000000, 0.000000,0.000000, 0.0000,0.0000,0000,0000,0000,0000,0000,0000,0000,000000,0.000000,0000000000,0000,0.0000,0000,0000,0000,0000,000000,0000,0000,0000,0000,000000000000000000,0000,0000,0000,0.000000000000000000000000000000,0000,00,0 .0.000000,00,0 .0.000000,00,0。000000,0.0000,0000,0000,0000,0000,0000,0.00000000,000000000000000000000000,0000000000000000000000,0000,000000,0000,000000,0000000000,0000,0000,0000,0000,00000000,000000000000,00000000000000来0.000000,0.0000,0000,0000,0000,00000000,0000,000000000000000000000000,0000,0000,00000000000000,000000000000,0000,00000000,0000000000,0000,0000,0.0000,000000,00000000,0000000000,00000000000000来0.000000,0.0000,0000,0000,0000000000000000000000000000000000000000,0000,0000000000,0000,000000000000000000,0000,00000000,0000,0000,000000000000,0000000000,000000000000机构0.000000,0.0000,0.000000,0.000000,00000000,0000,000000,0000,0000,0000,0000,000000,0000,0000000000,0000,000000000000000000000000000000000000,0000,000000000000,0000,0000000000,000000,000000,0000,000000,00000000009.0000,000000,0000,0000,0000,0000,0000,0000,0000,0000,00000000号0.000000,0.000000, -0.000000,2.000000,-2.000000,0.000000,-0.000000,36893488147419103232.000000,-36893488147419103232.000000,0.000000, -0.000000,8589934592.000000,-8589934592.000000,0.000000,-0.000000,158456325028528675187087900672.000000,-158456325028528675187087900672.000000,0.000000, -0.000000,131072.000000,- 131072.000000,0.000000,-0.000000,2417851639229258349412352.000000,-2417851639229258349412352.000000,0.000000, -0.000000,562949953421312.000000,-562949953421312.000000,0.000031,-0.000031,10384593717069655257060992658440192.000000,-10384593717069655257060992658440192.000000,0.000000, 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000,0.0000,0000,0000,0000,0000,0000,00000000000000000000,0000,0000,0000000000,000000000000,0000,0000000000000000,0000,0000,000000,000000000000,0000000000,00000000000000来
这让我很困惑。这些故障的发生率很低;不到十分之一的帧。
这是否意味着我必须预处理我的音频流?
上次我使用音频单元时,我从来没有这样做过。我只是将音频直接输入到音高检测程序中。这没有问题。所以我想知道我是否也在那里遇到了故障。
内置 MacBook 麦克风和外置 USB 麦克风都出现故障
这是我的代码:
//
// MicRecord.m
// PitchDetect
//
// Created by Pi on 05/01/2011.
//
#import "MicRecord.h"
void AudioInputCallback(
void * inUserData,
AudioQueueRef inAQ,
AudioQueueBufferRef inBuffer,
const AudioTimeStamp * inStartTime,
UInt32 inNumberPacketDescriptions,
const AudioStreamPacketDescription * inPacketDescs) ;
@implementation MicRecord
@synthesize fftFrame;
/*
- (id) init
{
if (self = [super init])
{
[self setupWithSampleRate: 44100
buffers: 12
bufLen: 512 ];
}
return self;
}
*/
// - - - - - - - -
- (void) setupWithSampleRate: (int) in_sampRate
buffers: (int) in_nBuffers
step: (int) in_step
frameSize: (int) in_frameSize
target: (id) in_target
sel: (SEL) in_sel
{
sampRate = in_sampRate;
nBuffers = in_nBuffers;
bufLen = in_step;
frameSize = in_frameSize;
targ = in_target;
sel = in_sel;
audioBuffer = calloc(nBuffers, sizeof(AudioQueueBufferRef *) );
[self setupAudioFormat];
[self setupAudioQueue];
fftFrame = calloc(frameSize, sizeof(float) );
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- (void) setupAudioFormat
{
// Set the format to 32 bit, single channel, floating point, linear PCM
const int four_bytes_per_float = 4;
const int eight_bits_per_byte = 8;
memset(& dataFormat,
(int) 0x00,
sizeof(dataFormat) );
dataFormat.mSampleRate = sampRate;
dataFormat.mFormatID = kAudioFormatLinearPCM;
dataFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kAudioFormatFlagIsNonInterleaved;
dataFormat.mBytesPerPacket = four_bytes_per_float;
dataFormat.mFramesPerPacket = 1;
dataFormat.mBytesPerFrame = four_bytes_per_float;
dataFormat.mChannelsPerFrame = 1;
dataFormat.mBitsPerChannel = four_bytes_per_float * eight_bits_per_byte;
}
// - - - - - - - - - - - - - - - - -
- (void) setupAudioQueue
{
currentPacket = 0;
OSStatus status;
status = AudioQueueNewInput(& dataFormat,
AudioInputCallback,
self,
CFRunLoopGetCurrent(),
kCFRunLoopCommonModes,
0,
& queue);
for(int i = 0; i < nBuffers; i++)
{
status = AudioQueueAllocateBuffer(queue,
bufLen,
& audioBuffer[i]);
status = AudioQueueEnqueueBuffer(queue,
audioBuffer[i], 0, NULL);
}
status = AudioQueueFlush (queue);
printf("Status: %d", (int) status);
}
// - - - - - - - - - - - - - - - - -
- (void) start
{
OSStatus status = AudioQueueStart(queue, NULL);
printf("Status: %d", (int) status);
}
// - - - - - - - - - - - - - - -
- (void) stop
{
AudioQueueStop(queue, true);
for(int i = 0; i < nBuffers; i++)
AudioQueueFreeBuffer(queue, audioBuffer[i]);
AudioQueueDispose(queue, true);
}
// - - - - - - - - - -
- (void) dealloc
{
[self stop];
free (audioBuffer);
[super dealloc];
}
@end
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
void AudioInputCallback(
void * inUserData,
AudioQueueRef inAQ,
AudioQueueBufferRef inBuffer,
const AudioTimeStamp * inStartTime,
UInt32 inNumberPacketDescriptions,
const AudioStreamPacketDescription * inPacketDescs
)
{
MicRecord * x = (MicRecord *) inUserData;
//if(inNumberPacketDescriptions == 0 && recordState->dataFormat.mBytesPerPacket != 0)
//{
// inNumberPacketDescriptions = inBuffer->mAudioDataByteSize / recordState->dataFormat.mBytesPerPacket;
//}
if (0)
printf("Handling buffer %d\n", (int) x->currentPacket);
int step = x->bufLen;
if (inBuffer->mAudioDataBytesCapacity != step)
{
printf("---");
}
static int k = -1;
k++;
static float lastVal = 0;
static int count = 0;
if (k < 32) {
if (k == 0)
printf("\nfloat buf[32*%d=%d] = {", step, 32*step);
float * in_buf = (float *) inBuffer->mAudioData;
printf("\n ~\nPACKET\n");
for (int i = 0; i < step; i++)
{
/*
if (fabs(in_buf[i]) < .0001 && fabs(lastVal) > .0001)
{
printf("%d Nonzeros\n",count);
count = 0;
}
if (fabs(in_buf[i]) > .0001 && fabs(lastVal) < .0001)
{
printf("%d Zeros\n",count);
count = 0;
}
count++;
lastVal = in_buf[i];*/
printf("%f,", in_buf[i] );
if (i % 8 == 0)
printf("\n");
//if (count % (8 * 64) == 0)
// printf("\n");
count++;
}
if (k == 31)
printf("}\n");
}
// shifty frame data down by 'step' elements
// to make room for new data
// imagine cutting out elts [0] thru [step-1] (ie 'step' of them)
// first new elt at pos [0] will be [step]
memmove(& x->fftFrame[0], // dest first
& x->fftFrame[step], // src
x->frameSize - step
);
memcpy(& x->fftFrame[x->frameSize - step],
inBuffer->mAudioData,
step * sizeof(float)
);
x->currentPacket += inNumberPacketDescriptions;
// }
AudioQueueEnqueueBuffer(x->queue, inBuffer, 0, NULL);
[x->targ performSelector: x->sel];
}