Регистрирам потока от плувки, идващ от микрофона (трябва да има тишина)
Настроил съм аудио опашки с размер на буфера 256 плаващи
Типичният буфер изглежда така:
ПАКЕТ 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.0 01587,-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.0197 45,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.00000 0,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.0 00000,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.0.0 00000,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.0 52032,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.0 55603,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.00 0000,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.01 4038,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 микрофон и това подобрява разделителната способност. Числата, различни от нула, сега изглеждат около марката .001. но все пак има забележимо прекъсване...
Чудя се дали се извършва някакво изчисление на чипа, който закръгля до 0. Ако случаят е такъв, може ли да се калибрира? Какво става?
Вторият наистина странен проблем, който забелязвам, са фалшивите ценности.
Ето примерен пакет, който съдържа някои от тези стойности (този път с помощта на USB микрофон; можете да видите как резолюцията е подобрена):
Това ме обърква. появата на тези проблеми е ниска; по-малко от един на 10 кадъра.
Това означава ли, че трябва да обработя предварително моя аудио поток?
Последният път, когато използвах аудио устройства, никога не съм го правил. Току-що прехвърлих звука направо в рутина за откриване на височина. И това нямаше проблеми. Така че се чудя дали и там получавах проблеми..
Получавам проблеми както с вградения микрофон на 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];
}
s от обратното извикване на най-ниско ниво. Ако те са бавни, е напълно възможно да пропускате буфер тук или там. Не знам дали това ще се покаже като блокове от нули или фалшиви проби, които наблюдавате, но може.
//
// 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];
}