views:

81

answers:

3

Hi,

My DAC is internally clocked at 48khz, I need to feed it from a stream that is also 48khz but is clocked from a different source.

The two sources will drift over time and any buffering in between will inevitably overflow or underflow.

Unfortunately, there is no rate control mechanism available for the source.

How can this best be handled?

Would it be acceptable to duplicate or toss the occasional sample to compensate? I'm estimating 1 sample every ~70k samples for 30ppm difference.

+2  A: 

As your clocks are running at essentially two different speeds, you have no choice but to duplicate or discard samples.

Keep a count of how many samples have been read/written, and calculate how many samples you expect to have been read/written based on the current time. A discrepancies can then be adjusted for by duplicating/discarding samples.

MrZebra
A: 

This may sould like a stupid question, but hear me out: Does the output rate really matter?

Consider this scenario - you have an audio input device. You are sampling an analog waveform input at 48Khz. That waveform could be generated by anything at any rate- from the continuous pressure level measurement of a microphone to an 8Khz voice codec, or lower. The fact that you happen to know that this particular output is also at 48Khz has no bearing on what you are doing.

If there were perfect transmission between the output device and your input, when you compare the original digital waveform to what you read, you will occassionally see that you have duplicated a sample (if your clock is faster) or missed one (if you are slower). But this will happen automatically, without any compensation by you needed. This will easily be as accurate as any attempt by you to figure out when to "duplicate or toss the occasional sample".

In practice, you probably won't get an exact duplicate anyway, between filtering effects and noise in both the DAC and ADC. Unless you are talking about some encoded digital audio format - in which case, ignore everything I've said.

EDIT - Ok, so it is digital audio - S/PDIF?
The signal should carry enough information to implement a phase lock. Ideally in hardware - the rising edges of the signal define the clock rate - you may be able to use PLL hardware to lock your A/D clock to the input. Or oversample for a while and count the rising edges to determine the rate. Or at the software level, measure the rate of framing errors in the 1st four time slots, and adjust your clock to compensate.

AShelly
It is digital audio. My concern is not so much about fidelity. I am more concerned with buffer overflow / underflow and having to throw out or makeup a large block of data in the event of an xxxxflow condition.
JeffV
A: 

If you are concerned about fidelity, instead of dropping or duplicating samples, use a Sample Rate Converter. First, use timestamping to measure the relative frequency ratio between the source sample rate and your DAC's sample rate. Use this ratio to control the sample rate converter. A decent quality sample rate converter can give you 95 db S/N ratio. More computationally expensive algorithms can give you > 120 db S/N ratio.

See:

--jdkoftinoff

jdkoftinoff
@jdkoftinoff: I'm not sure if that will work with a sample rates that are very close to each other. You would have to oversample to much. ??
JeffV
No, it works just fine. The key is that with a polyphase filter you don't calculate the oversampled values. libsamplerate can do arbitary ratios nicely - even change them dynamically. Typically though you want the coefficients pre-calculated for a specific tight range.
jdkoftinoff