PortAudio 2.0
audio_analyzer.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include "qa_tools.h"
#include "audio_analyzer.h"
#include "write_wav.h"

Macros

#define PAQA_POP_THRESHOLD   (0.04)
 
#define NUM_SAMPLES   (200)
 
#define MAX_BUFFER_SIZE   2048
 

Functions

double PaQa_GetNthFrequency (double baseFrequency, int index)
 
void PaQa_EraseBuffer (float *buffer, int numFrames, int samplesPerFrame)
 
void PaQa_SetupSineGenerator (PaQaSineGenerator *generator, double frequency, double amplitude, double frameRate)
 
void PaQa_MixSine (PaQaSineGenerator *generator, float *buffer, int numSamples, int stride)
 
void PaQa_GenerateCrackDISABLED (float *buffer, int numSamples, int stride)
 
int PaQa_InitializeRecording (PaQaRecording *recording, int maxFrames, int frameRate)
 
void PaQa_TerminateRecording (PaQaRecording *recording)
 
int PaQa_WriteRecording (PaQaRecording *recording, float *buffer, int numFrames, int stride)
 
int PaQa_WriteSilence (PaQaRecording *recording, int numFrames)
 
int PaQa_RecordFreeze (PaQaRecording *recording, int numFrames)
 
int PaQa_SaveRecordingToWaveFile (PaQaRecording *recording, const char *filename)
 
double PaQa_MeasureCrossingSlope (float *buffer, int numFrames)
 
double PaQa_MeasureSineAmplitudeBySlope (PaQaRecording *recording, double frequency, double frameRate, int startFrame, int numFrames)
 
double PaQa_CorrelateSine (PaQaRecording *recording, double frequency, double frameRate, int startFrame, int numFrames, double *phasePtr)
 
void PaQa_FilterRecording (PaQaRecording *input, PaQaRecording *output, BiquadFilter *filter)
 
double PaQa_FindFirstMatch (PaQaRecording *recording, float *buffer, int numFrames, double threshold)
 
double PaQa_MeasureArea (float *buffer, int numFrames, int stride)
 
double PaQa_MeasureRootMeanSquare (float *buffer, int numFrames)
 
double PaQa_CompareAmplitudes (PaQaRecording *recording, int startAt, float *buffer, int numFrames)
 
double PaQa_ComputePhaseDifference (double phase1, double phase2)
 
int PaQa_MeasureLatency (PaQaRecording *recording, PaQaTestTone *testTone, PaQaAnalysisResult *analysisResult)
 
void PaQa_FadeInRecording (PaQaRecording *recording, int startFrame, int count)
 
int PaQa_DetectPop (PaQaRecording *recording, PaQaTestTone *testTone, PaQaAnalysisResult *analysisResult)
 
int PaQa_DetectPhaseError (PaQaRecording *recording, PaQaTestTone *testTone, PaQaAnalysisResult *analysisResult)
 
int PaQa_AnalyseRecording (PaQaRecording *recording, PaQaTestTone *testTone, PaQaAnalysisResult *analysisResult)
 

Macro Definition Documentation

◆ MAX_BUFFER_SIZE

#define MAX_BUFFER_SIZE   2048

Referenced by PaQa_MeasureLatency().

◆ NUM_SAMPLES

#define NUM_SAMPLES   (200)

◆ PAQA_POP_THRESHOLD

#define PAQA_POP_THRESHOLD   (0.04)

Referenced by PaQa_DetectPop().

Function Documentation

◆ PaQa_AnalyseRecording()

int PaQa_AnalyseRecording ( PaQaRecording * recording,
PaQaTestTone * testTone,
PaQaAnalysisResult * analysisResult )

Analyse a recording of a sine wave. Measure latency and look for dropped frames, etc.

References PaQaAnalysisResult_s::amplitudeRatio, PaQaAnalysisResult_s::latency, PaQa_DetectPhaseError(), PaQa_DetectPop(), PaQa_MeasureLatency(), QA_ASSERT_EQUALS, and PaQaAnalysisResult_s::valid.

◆ PaQa_CompareAmplitudes()

double PaQa_CompareAmplitudes ( PaQaRecording * recording,
int startAt,
float * buffer,
int numSamples )

Compare the amplitudes of these two signals. Return ratio of recorded signal over buffer signal.

References buffer, PaQaRecording_s::buffer, PaQaRecording_s::numFrames, PaQa_MeasureArea(), and QA_ASSERT_TRUE.

Referenced by PaQa_MeasureLatency().

◆ PaQa_ComputePhaseDifference()

double PaQa_ComputePhaseDifference ( double phase1,
double phase2 )

References MATH_PI, and MATH_TWO_PI.

Referenced by PaQa_DetectPhaseError().

◆ PaQa_CorrelateSine()

double PaQa_CorrelateSine ( PaQaRecording * recording,
double frequency,
double frameRate,
int startFrame,
int numFrames,
double * phasePtr )

◆ PaQa_DetectPhaseError()

◆ PaQa_DetectPop()

◆ PaQa_EraseBuffer()

void PaQa_EraseBuffer ( float * buffer,
int numFrames,
int samplesPerFrame )

References buffer.

Referenced by PaQa_FillWithSine(), and PaQa_MeasureLatency().

◆ PaQa_FadeInRecording()

void PaQa_FadeInRecording ( PaQaRecording * recording,
int startFrame,
int count )

References PaQaRecording_s::buffer, and MATH_PI.

Referenced by PaQa_DetectPop().

◆ PaQa_FilterRecording()

void PaQa_FilterRecording ( PaQaRecording * input,
PaQaRecording * output,
BiquadFilter * filter )

Apply a biquad filter to the audio from the input recording and write it to the output recording.

References BiquadFilter_Filter(), PaQaRecording_s::buffer, PaQaRecording_s::maxFrames, and PaQaRecording_s::numFrames.

Referenced by PaQa_DetectPop().

◆ PaQa_FindFirstMatch()

double PaQa_FindFirstMatch ( PaQaRecording * recording,
float * buffer,
int numFrames,
double threshold )

Scan until we get a correlation of a single that goes over the tolerance level, peaks then drops to half the peak. Look for inverse correlation as well.

References buffer, PaQaRecording_s::buffer, PaQaRecording_s::numFrames, and QA_ASSERT_TRUE.

Referenced by PaQa_MeasureLatency().

◆ PaQa_GenerateCrackDISABLED()

void PaQa_GenerateCrackDISABLED ( float * buffer,
int numSamples,
int stride )

References buffer, and MATH_TWO_PI.

◆ PaQa_GetNthFrequency()

double PaQa_GetNthFrequency ( double baseFrequency,
int index )

Calculate Nth frequency of a series for use in testing multiple channels. Series should avoid harmonic overlap between channels.

◆ PaQa_InitializeRecording()

int PaQa_InitializeRecording ( PaQaRecording * recording,
int maxSamples,
int sampleRate )

Allocate memory for containing a mono audio signal. Set up recording for writing.

References PaQaRecording_s::buffer, PaQaRecording_s::maxFrames, PaQaRecording_s::numFrames, QA_ASSERT_TRUE, and PaQaRecording_s::sampleRate.

Referenced by PaQa_DetectPop().

◆ PaQa_MeasureArea()

double PaQa_MeasureArea ( float * buffer,
int numFrames,
int stride )

Measure the area under the curve by summing absolute value of each value.

References buffer.

Referenced by PaQa_CompareAmplitudes().

◆ PaQa_MeasureCrossingSlope()

double PaQa_MeasureCrossingSlope ( float * buffer,
int numFrames )

Measure slope of the positive zero crossings.

References buffer.

Referenced by PaQa_MeasureSineAmplitudeBySlope().

◆ PaQa_MeasureLatency()

◆ PaQa_MeasureRootMeanSquare()

double PaQa_MeasureRootMeanSquare ( float * buffer,
int numFrames )

References buffer.

Referenced by PaQa_MeasureBackgroundNoise().

◆ PaQa_MeasureSineAmplitudeBySlope()

double PaQa_MeasureSineAmplitudeBySlope ( PaQaRecording * recording,
double frequency,
double frameRate,
int startFrame,
int numFrames )

Estimate the original amplitude of a clipped sine wave by measuring its average slope at the zero crossings.

References buffer, PaQaRecording_s::buffer, MATH_TWO_PI, and PaQa_MeasureCrossingSlope().

Referenced by PaQa_CheckForClippedLoopback().

◆ PaQa_MixSine()

void PaQa_MixSine ( PaQaSineGenerator * generator,
float * buffer,
int numSamples,
int stride )

◆ PaQa_RecordFreeze()

int PaQa_RecordFreeze ( PaQaRecording * recording,
int numFrames )

◆ PaQa_SaveRecordingToWaveFile()

int PaQa_SaveRecordingToWaveFile ( PaQaRecording * recording,
const char * filename )

◆ PaQa_SetupSineGenerator()

void PaQa_SetupSineGenerator ( PaQaSineGenerator * generator,
double frequency,
double amplitude,
double frameRate )

Prepare an oscillator that can generate a sine tone for testing.

References PaQaSineGenerator_s::amplitude, PaQaSineGenerator_s::frequency, MATH_PI, PaQaSineGenerator_s::phase, and PaQaSineGenerator_s::phaseIncrement.

Referenced by PaQa_FillWithSine(), and PaQa_MeasureLatency().

◆ PaQa_TerminateRecording()

void PaQa_TerminateRecording ( PaQaRecording * recording)

Free memory allocated by PaQa_InitializeRecording.

References PaQaRecording_s::buffer, and PaQaRecording_s::maxFrames.

Referenced by PaQa_DetectPop().

◆ PaQa_WriteRecording()

int PaQa_WriteRecording ( PaQaRecording * recording,
float * buffer,
int numSamples,
int stride )
Parameters
strideis the spacing of samples to skip in the input buffer. To use every samples pass 1. To use every other sample pass 2.

References buffer, PaQaRecording_s::buffer, PaQaRecording_s::maxFrames, and PaQaRecording_s::numFrames.

Referenced by PaQa_FillWithSine().

◆ PaQa_WriteSilence()

int PaQa_WriteSilence ( PaQaRecording * recording,
int numSamples )

Write zeros into a recording.

References PaQaRecording_s::buffer, PaQaRecording_s::maxFrames, and PaQaRecording_s::numFrames.