4

My app uses an external USB microphone with a very accurate thermo compensated quartz oscillator (TCXO). The sample rate is 48KHz. I plug it in iOS through the camera kit connector. I'm using EZAudio library and everything works fine except that iOS seems to keep its own internal clock source for audio sampling instead of my accurate 48KHz.

I read all the documentation about CoreAudio but I didn't find anything related to clock source when using lightning audio.

Is there any way to choose between internal or external clock source ?

Thanks !

var audioFormatIn = AudioStreamBasicDescription(mSampleRate: Float64(48000),
                                                mFormatID: AudioFormatID(kAudioFormatLinearPCM),
                                                mFormatFlags: AudioFormatFlags(kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked),
                                                mBytesPerPacket: 2,
                                                mFramesPerPacket: 1,
                                                mBytesPerFrame: 2,
                                                mChannelsPerFrame: 1,
                                                mBitsPerChannel: 16,
                                                mReserved: 0)

func initAudio()
{       
    let session : AVAudioSession = AVAudioSession.sharedInstance()
    do {
        try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
        try session.setMode(AVAudioSessionModeMeasurement)
        try session.setActive(true)
    }
    catch {
        print("Error Audio")
    }
    self.microphone = EZMicrophone(microphoneDelegate: self, withAudioStreamBasicDescription: audioFormatIn)
}

UPDATE : Thanks to @Rhythmic Fistman, setting the preferred sample rate partly solved the problem. No more resampling from iOS and the TCXO remains the clock master source. But the signal is now quickly corrupted with something that seems to be empty samples in the buffers. This problem is getting worse and worse with the recording length. Of course, as I need the lightning port to plug the hardware, it's really difficult for me to debug easily!

Screenshot of the waveform after 7 minutes :

enter image description here

Screenshot of the waveform after 15 minutes :

enter image description here

jcr
  • 303
  • 1
  • 6
  • 16

1 Answers1

1

Does setting the audio session's preferred sample rate to 48kHz before activating it help?

session.setPreferredSampleRate(48000)

I don't know if it will do anything to clock sources, or if it will even affect the input (or output) sampling rate, but it's worth a shot.

Rhythmic Fistman
  • 34,352
  • 5
  • 87
  • 159
  • I've just tried (as it's not implemented in EZAudio) but it doesn't work unfortunately. – jcr Jun 19 '16 at 11:05
  • As I'm apparently able to set a sample rate lower than my 48KHz, it really means that iOS resample the incoming audio data which is definitely what I want to avoid. – jcr Jun 19 '16 at 11:36
  • What does `session.sampleRate` report before you setup the microphone? Can you step through `EZMicrophone` to see what happens to `inputFormat` here https://github.com/syedhali/EZAudio/blob/master/EZAudio/EZMicrophone.m#L246 ? – Rhythmic Fistman Jun 19 '16 at 12:02
  • I did deeper tests with a GPS timestamp signal. Indeed, 'setPreferredSampleRate' solved my problem. My first comment was wrong. But now I have another problem : the signal is quickly corrupted with kind of empty samples in the buffer and it's getting worse and worse. This is really strange. – jcr Jun 19 '16 at 20:32
  • Are you recording to file? Can you show a screenshot of the corrupted waveform? Do you mean GPS in the satellite sense? – Rhythmic Fistman Jun 19 '16 at 21:51
  • No, I'm not recording to file. It's only audio processing from the input. Yes, a GPS in the satellite sense. I use it to produce a perfectly accurate "bip" sound to check the timebase. I updated my question with pictures of the waveform. Thanks ! – jcr Jun 20 '16 at 10:41
  • I think you should move this new problem to a new question and show the code that deals with the audio input, along with the screen shots. Also some labels on the time axis would help too. – Rhythmic Fistman Jun 20 '16 at 11:56
  • You're probably right. I just discovered that there's the same problem on Mac (using audacity) when choosing 48kHz. This problem doesn't exist on Windows with the same audacity parameters. There's something wrong with Core Audio I guess. Thanks @Rhytmic Fistman. – jcr Jun 20 '16 at 14:00