Fast analog-digital conversion

Status
Not open for further replies.

Roel

Active member
Hey there!

I am having a little trouble with my teensy 3.1 project.
Hopefully my description is clear enough :)

the teensy is used in a synthesizer, it generates the sound.
In the loop() function, which a sound sample is computed and thensaved in a variable.
An interrupt running at 40 kHz reads this sample and outputs it to the dac.

Code:
void loop()
{ 
    analogRead(A0); //using analog reads to read potmeters through a multiplexer
    usbMIDI.read(); //reads usb Midi
    Voice1.Run(); //generates the output sample of one Voice
}

Now, during the loop(), I want it to read analog values from some potmeters.
This is where I get stuck:

without analogRead the loop takes around 7 us to complete.
with only one analogRead the loop already takes 16 us to complete.

This is too much, as I am not even halfway building, and the loop is limited at 25 us to complete (because of the 40 kHz sample rate).

So, the question is:
is it possible to speed up the analog reading?
if so, how is this done?

Thanks in advance,

Roel
 
If the pots don't change that quickly, then can you only read them every (say) 10 ms -- i.e. skip analogRead(A0) 399/400 times. In those instances (when you do read the pot), just replicate the previous Voice output sample instead of doing that USB read. The distortion will be negligible.
 
Thanks for your answer, I will try to see if it works.
In that case I will add a ringbuffer, so if the sample synthesis gets delayed, there still is a new sample to play.

In another thread I read about DMA, but I don't know how that will perform when using a multiplexed analog input.
 
First, to directly answer your question, you can grab the analogRead() code from hardware/teensy/cores/teensy3/analog.c. It starts the conversion and waits for a result. You could just copy this code and break it into 2 functions, one which starts the conversion and the other to wait and read it. Perhaps call each on every other run of your code. When you call the 2nd one 25 us later, the conversion started on the previous run should be finished and you can read it quickly.

However, that's not how I'd recommend solving this problem.

You should consider building your synthesis code as an object that integrates with the audio library.

http://www.pjrc.com/teensy/td_libs_Audio.html

This does have a slight learning curve, to structure your code as a C++ object that inherits from AudioStream, and uses the functions to allocate and transmit blocks. But it's worth the learning, because the audio library will make your task dramatically easier. Here's the page with details:

http://www.pjrc.com/teensy/td_libs_AudioNewObjects.html

The basic idea is you write an update() function that creates the next 128 audio samples. Use allocate() to get a buffer, and transmit() to send it. You update() function will run automatically from an interrupt every 128 samples, which means you can leave the main Arduino sketch free for stuff like analogRead() or even delay(). All you need to do is add function to your synth object to write the settings to volatile variables, which are then read by your update() function when it creates the next 128 samples.

The audio library already has output objects for the DAC, I2C and PWM. They all use highly efficient DMA transfers. They library supports user defined connections, so you could even send your synthesis output to the DAC and PWM simultaneously. Or you could send your audio to a mixer object and add in other sounds from other stuff from the audio library. All you have to do is use those simple functions to allocate, transmit, and release the 128 sample block, and then your synthesis object can work with anything else in the library.

The ARM Cortex-M4 chip has special instructions to accelerate DSP code. Most are useful when creating 2, 4 or 8 samples at once and packing them into an array. The audio library's 128 sample buffer and overall approach are designed to allow leveraging these special instructions. Usually it's best to write unoptimized code and get it working first. But if you're planning to optimize, using the 128 sample approach really allows for impressive optimizations to be made later.

If you're planning to publish this as open source code, perhaps it could be accepted into the library. There's a page with recommendation for naming conventions. But even if you're not planning to publish the code, the audio library can really make this sort of thing much easier.
 
Hello Paul,

thanks for your extensive explanation, the audio library seems to be really useful.
Yesterday I looked into the audio library, and found a lot of useful examples.
I had no luck yet with including the library, the compiler couldn't find all included files.
 
Status
Not open for further replies.
Back
Top