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