You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OpenAudio_ArduinoLibrary/AudioAlignLR_F32.h

162 lines
5.9 KiB

/* ------------------------------------------------------------------------------------
AudioAlignLR_F32.h
Function: Waits for CODEC startup and measures L-R time alignment errors.
Automatically corrects delay errors. Requires extra control to disable
analog audio path from DAC Q to both ADC I and ADC Q.
See the following for more background on the problem being solved:
https://forum.pjrc.com/threads/42336-Reset-audio-board-codec-SGTL5000-in-realtime-processing/page2
https://forum.pjrc.com/threads/57362-AudioSDR-A-single-Audio-block-SDR-(software-defined-radio)-processor-demodulator/page3
"Twin Peaks", or TP, comes from Frank, DD4WH who found, in hsi Convolution SDR, double spectral responses when
the L and R channels were out of time alignment.
Author: Bob Larkin W7PUA
Date: 28 Feb 2022
Input: data_I, data_Q
Outputs: dataOut_I, dataOut_Q, dataTransmit_Q
Update() time: About 12 microseconds for a T4.x.
Copyright (c) 2022 Robert Larkin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------- */
//REF:
// Manual I2S codec Phase Correction Utility by Ron Carr, Updated for F32, K7MDL
// Reference: [URL]https://forum.pjrc.com/threads/57362-AudioSDR-A-single-Audio-block-SDR-(software-defined-radio)-processor-demodulator?p=263048&viewfull=1#post263048[/URL]
//
// The variable, currentTPinfo.TPsignalHardware, changes source of
// the signal for going to L&R for correlation.
// If TP_SIGNAL_CODEC there needs to be a switchable hardware link between
// the DAC right channel and both of ADC right and ADC left.
// If TP_SIGNAL_IO_PIN the INO must update a roughly fs/4
// square wave using an i/o pin. That signal is generated by
// the .INO as it is not possible in an audio object.
// Default is TP_SIGNAL_CODEC.
//
// BETA NOTE That calls and operations may change. BETA
#ifndef audio_align_lr_f32_h_
#define audio_align_lr_f32_h_
#include "AudioStream_F32.h"
#include "Arduino.h"
#define TP_IDLE 0
#define TP_MEASURE 1
#define TP_RUN 2
#define TP_SIGNAL_CODEC 0
#define TP_SIGNAL_IO_PIN 1
#define ERROR_TP_EARLY -2
#define ERROR_TP_BAD_DATA -1
#define ERROR_TP_NONE 0
// Needs to be available to INO and AudioAlignLR_F32 also.
struct TPinfo{
uint16_t TPstate;
uint32_t nMeas;
float32_t xcVal[4]; // I-Q cross-correlation sums
int16_t neededShift;
int16_t TPerror;
uint16_t TPsignalHardware;
};
class AudioAlignLR_F32 : public AudioStream_F32
{
//GUI: inputs:2, outputs:3 //this line used for automatic generation of GUI node
//GUI: shortName: AlignLR
public:
AudioAlignLR_F32(uint16_t _hardware, uint16_t _controlPinNumber, bool _controlPinInvert):
AudioStream_F32(2,inputQueueArray)
{
currentTPinfo.TPsignalHardware = _hardware;
controlPinNumber = _controlPinNumber;
controlPinInvert = _controlPinInvert;
sample_rate_Hz = AUDIO_SAMPLE_RATE;
// Changes would be needed for block data sizes other than 128
block_size = AUDIO_BLOCK_SAMPLES;
initTP();
}
AudioAlignLR_F32
(uint16_t _hardware, uint16_t _controlPinNumber, bool _controlPinInvert, const AudioSettings_F32 &settings):
AudioStream_F32(2,inputQueueArray)
{
currentTPinfo.TPsignalHardware = _hardware;
controlPinNumber = _controlPinNumber;
controlPinInvert = _controlPinInvert;
sample_rate_Hz = settings.sample_rate_Hz;
block_size = settings.audio_block_samples;
initTP();
}
void initTP(void) { // Common calls for constructors, not INO's
currentTPinfo.TPstate = TP_IDLE;
currentTPinfo.neededShift = 0;
currentTPinfo.TPerror = ERROR_TP_EARLY;
needOneMore = false;
}
// Returns all the status info, available anytime
TPinfo *read(void) {
return &currentTPinfo;
}
void stateAlignLR(int _TPstate) {
currentTPinfo.TPstate = _TPstate;
if(currentTPinfo.TPstate==TP_MEASURE)
{
currentTPinfo.nMeas = 0; // Timing for the DAC & ADC delays
currentTPinfo.neededShift = 0;
currentTPinfo.TPerror = ERROR_TP_EARLY;
needOneMore = true;
if(currentTPinfo.TPsignalHardware==TP_SIGNAL_CODEC)
{
pinMode(controlPinNumber, OUTPUT); // Turn on special audio path
digitalWrite(controlPinNumber, 1 & (uint16_t)controlPinInvert);
}
}
}
void setThreshold(float32_t _TPthreshold) {
TPthreshold = _TPthreshold;
}
virtual void update(void);
private:
audio_block_f32_t *inputQueueArray[2];
float32_t sample_rate_Hz = AUDIO_SAMPLE_RATE;
uint16_t controlPinNumber = 0;
bool controlPinInvert = true;
uint16_t block_size = 128;
bool needOneMore = false;
float32_t TPthreshold = 1.0f;
float32_t TPextraI = 0.0f;
float32_t TPextraQ = 0.0f;
TPinfo currentTPinfo;
bool outputFlag = false;
uint16_t count = 0;
};
#endif