annotate src/portaudio/qa/loopback/src/audio_analyzer.h @ 116:e448888319fc

PortAudio on OS/X
author Chris Cannam <cannam@all-day-breakfast.com>
date Thu, 09 Jan 2014 21:33:28 +0000
parents 8a15ff55d9af
children
rev   line source
cannam@89 1
cannam@89 2 /*
cannam@89 3 * PortAudio Portable Real-Time Audio Library
cannam@89 4 * Latest Version at: http://www.portaudio.com
cannam@89 5 *
cannam@89 6 * Copyright (c) 1999-2010 Phil Burk and Ross Bencina
cannam@89 7 *
cannam@89 8 * Permission is hereby granted, free of charge, to any person obtaining
cannam@89 9 * a copy of this software and associated documentation files
cannam@89 10 * (the "Software"), to deal in the Software without restriction,
cannam@89 11 * including without limitation the rights to use, copy, modify, merge,
cannam@89 12 * publish, distribute, sublicense, and/or sell copies of the Software,
cannam@89 13 * and to permit persons to whom the Software is furnished to do so,
cannam@89 14 * subject to the following conditions:
cannam@89 15 *
cannam@89 16 * The above copyright notice and this permission notice shall be
cannam@89 17 * included in all copies or substantial portions of the Software.
cannam@89 18 *
cannam@89 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@89 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@89 21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
cannam@89 22 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
cannam@89 23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@89 24 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@89 25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@89 26 */
cannam@89 27
cannam@89 28 /*
cannam@89 29 * The text above constitutes the entire PortAudio license; however,
cannam@89 30 * the PortAudio community also makes the following non-binding requests:
cannam@89 31 *
cannam@89 32 * Any person wishing to distribute modifications to the Software is
cannam@89 33 * requested to send the modifications to the original developer so that
cannam@89 34 * they can be incorporated into the canonical version. It is also
cannam@89 35 * requested that these non-binding requests be included along with the
cannam@89 36 * license above.
cannam@89 37 */
cannam@89 38
cannam@89 39 #ifndef _AUDIO_ANALYZER_H
cannam@89 40 #define _AUDIO_ANALYZER_H
cannam@89 41
cannam@89 42 #include "biquad_filter.h"
cannam@89 43
cannam@89 44 #define MATH_PI (3.141592653589793238462643)
cannam@89 45 #define MATH_TWO_PI (2.0 * MATH_PI)
cannam@89 46
cannam@89 47 typedef struct PaQaSineGenerator_s
cannam@89 48 {
cannam@89 49 double phase;
cannam@89 50 double phaseIncrement;
cannam@89 51 double frequency;
cannam@89 52 double amplitude;
cannam@89 53 } PaQaSineGenerator;
cannam@89 54
cannam@89 55 /** Container for a monophonic audio sample in memory. */
cannam@89 56 typedef struct PaQaRecording_s
cannam@89 57 {
cannam@89 58 /** Maximum number of frames that can fit in the allocated buffer. */
cannam@89 59 int maxFrames;
cannam@89 60 float *buffer;
cannam@89 61 /** Actual number of valid frames in the buffer. */
cannam@89 62 int numFrames;
cannam@89 63 int sampleRate;
cannam@89 64 } PaQaRecording;
cannam@89 65
cannam@89 66 typedef struct PaQaTestTone_s
cannam@89 67 {
cannam@89 68 int samplesPerFrame;
cannam@89 69 int startDelay;
cannam@89 70 double sampleRate;
cannam@89 71 double frequency;
cannam@89 72 double amplitude;
cannam@89 73 } PaQaTestTone;
cannam@89 74
cannam@89 75 typedef struct PaQaAnalysisResult_s
cannam@89 76 {
cannam@89 77 int valid;
cannam@89 78 /** Latency in samples from output to input. */
cannam@89 79 double latency;
cannam@89 80 double amplitudeRatio;
cannam@89 81 double popAmplitude;
cannam@89 82 double popPosition;
cannam@89 83 double numDroppedFrames;
cannam@89 84 double droppedFramesPosition;
cannam@89 85 double numAddedFrames;
cannam@89 86 double addedFramesPosition;
cannam@89 87 } PaQaAnalysisResult;
cannam@89 88
cannam@89 89
cannam@89 90 /*================================================================*/
cannam@89 91 /*================= General DSP Tools ============================*/
cannam@89 92 /*================================================================*/
cannam@89 93 /**
cannam@89 94 * Calculate Nth frequency of a series for use in testing multiple channels.
cannam@89 95 * Series should avoid harmonic overlap between channels.
cannam@89 96 */
cannam@89 97 double PaQa_GetNthFrequency( double baseFrequency, int index );
cannam@89 98
cannam@89 99 void PaQa_EraseBuffer( float *buffer, int numFrames, int samplesPerFrame );
cannam@89 100
cannam@89 101 void PaQa_MixSine( PaQaSineGenerator *generator, float *buffer, int numSamples, int stride );
cannam@89 102
cannam@89 103 void PaQa_WriteSine( float *buffer, int numSamples, int stride,
cannam@89 104 double frequency, double amplitude );
cannam@89 105
cannam@89 106 /**
cannam@89 107 * Generate a signal with a sharp edge in the middle that can be recognized despite some phase shift.
cannam@89 108 */
cannam@89 109 void PaQa_GenerateCrack( float *buffer, int numSamples, int stride );
cannam@89 110
cannam@89 111 double PaQa_ComputePhaseDifference( double phase1, double phase2 );
cannam@89 112
cannam@89 113 /**
cannam@89 114 * Measure the area under the curve by summing absolute value of each value.
cannam@89 115 */
cannam@89 116 double PaQa_MeasureArea( float *buffer, int numFrames, int stride );
cannam@89 117
cannam@89 118 /**
cannam@89 119 * Measure slope of the positive zero crossings.
cannam@89 120 */
cannam@89 121 double PaQa_MeasureCrossingSlope( float *buffer, int numFrames );
cannam@89 122
cannam@89 123
cannam@89 124 /**
cannam@89 125 * Prepare an oscillator that can generate a sine tone for testing.
cannam@89 126 */
cannam@89 127 void PaQa_SetupSineGenerator( PaQaSineGenerator *generator, double frequency, double amplitude, double frameRate );
cannam@89 128
cannam@89 129 /*================================================================*/
cannam@89 130 /*================= Recordings ===================================*/
cannam@89 131 /*================================================================*/
cannam@89 132 /**
cannam@89 133 * Allocate memory for containg a mono audio signal. Set up recording for writing.
cannam@89 134 */
cannam@89 135 int PaQa_InitializeRecording( PaQaRecording *recording, int maxSamples, int sampleRate );
cannam@89 136
cannam@89 137 /**
cannam@89 138 * Free memory allocated by PaQa_InitializeRecording.
cannam@89 139 */
cannam@89 140 void PaQa_TerminateRecording( PaQaRecording *recording );
cannam@89 141
cannam@89 142 /**
cannam@89 143 * Apply a biquad filter to the audio from the input recording and write it to the output recording.
cannam@89 144 */
cannam@89 145 void PaQa_FilterRecording( PaQaRecording *input, PaQaRecording *output, BiquadFilter *filter );
cannam@89 146
cannam@89 147
cannam@89 148 int PaQa_SaveRecordingToWaveFile( PaQaRecording *recording, const char *filename );
cannam@89 149
cannam@89 150 /**
cannam@89 151 * @param stride is the spacing of samples to skip in the input buffer. To use every samples pass 1. To use every other sample pass 2.
cannam@89 152 */
cannam@89 153 int PaQa_WriteRecording( PaQaRecording *recording, float *buffer, int numSamples, int stride );
cannam@89 154
cannam@89 155 /** Write zeros into a recording. */
cannam@89 156 int PaQa_WriteSilence( PaQaRecording *recording, int numSamples );
cannam@89 157
cannam@89 158 int PaQa_RecordFreeze( PaQaRecording *recording, int numSamples );
cannam@89 159
cannam@89 160 double PaQa_CorrelateSine( PaQaRecording *recording, double frequency, double frameRate,
cannam@89 161 int startFrame, int numSamples, double *phasePtr );
cannam@89 162
cannam@89 163 double PaQa_FindFirstMatch( PaQaRecording *recording, float *buffer, int numSamples, double tolerance );
cannam@89 164
cannam@89 165 /**
cannam@89 166 * Estimate the original amplitude of a clipped sine wave by measuring
cannam@89 167 * its average slope at the zero crossings.
cannam@89 168 */
cannam@89 169 double PaQa_MeasureSineAmplitudeBySlope( PaQaRecording *recording,
cannam@89 170 double frequency, double frameRate,
cannam@89 171 int startFrame, int numFrames );
cannam@89 172
cannam@89 173 double PaQa_MeasureRootMeanSquare( float *buffer, int numFrames );
cannam@89 174
cannam@89 175 /**
cannam@89 176 * Compare the amplitudes of these two signals.
cannam@89 177 * Return ratio of recorded signal over buffer signal.
cannam@89 178 */
cannam@89 179 double PaQa_CompareAmplitudes( PaQaRecording *recording, int startAt, float *buffer, int numSamples );
cannam@89 180
cannam@89 181 /**
cannam@89 182 * Analyse a recording of a sine wave.
cannam@89 183 * Measure latency and look for dropped frames, etc.
cannam@89 184 */
cannam@89 185 int PaQa_AnalyseRecording( PaQaRecording *recording, PaQaTestTone *testTone, PaQaAnalysisResult *analysisResult );
cannam@89 186
cannam@89 187 #endif /* _AUDIO_ANALYZER_H */