Go Down

Topic: Realtime audio processing in Arduino due (Read 4278 times) previous topic - next topic

alanben

i tried to get audio through the analog pin3 and play it in the headphones using DAC0.. i can hear the sound but its very much distorted... i can nly hear the noise when i m playing a song.. can any please help me in doing it...?!!

Here is the simple code

#include <Audio.h>
#include <DAC.h>

int analogPin = 3;     

int val = 0;           

void setup()

{

  Serial.begin(9600);
    //  setup serial

}



void loop()

{
  analogReadResolution(24);
  analogWriteResolution(24);
  val = analogRead(analogPin);   
  Serial.println(val);
  analogWrite(DAC0,val); 
}

UKHeliBob

Take as much code as possible out of loop().
Why are you setting the read and write resolutions each time through loop() ?  Should that not be done just once in setup() ?  The Serial.print() will also take time so try removing that too.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

MarkT

If you connect DAC0 directly to headphones you have overloaded it.  All the Due outputs
are feeble and cannot drive anything but signal levels, so you need an audio amplifier,
preferably via a 10k resistor to protect the pin.
[ I DO NOT respond to personal messages, I WILL delete them unread, use the forum please ]

RayLivingston

There are soooooo many reasons that is never going to work well at all:

1) Due DAC and ADC resolution is only 12 bits, which is poor for audio.
2) For analogRead, default conversion time is about 100 uSec, which means a sample rate of only 10kHz.  Again, very poor for audio.  Perhaps on the Due it runs faster, not sure.
3) Sampling an a software loop will mean LOTS of jitter in the sample rate, which makes for lots of distortion and aliasing.

If you want the best quality you can get, which will still be poor, you need to do register-level programming, and setup a DMA controller to read and buffer the data, and another DMA controller to transfer the data from the buffer to the A/D.  You'll then need interrupt handlers to deal with the buffer management.

Personally, I think you're wasting your time, as the audio quality will be poor at best unless you add external 16-24-bit I2S ADCs and DACs.

Regards,
Ray L.

MorganS

There are soooooo many reasons that is never going to work well at all:

1) Due DAC and ADC resolution is only 12 bits, which is poor for audio.
2) For analogRead, default conversion time is about 100 uSec, which means a sample rate of only 10kHz.  Again, very poor for audio.  Perhaps on the Due it runs faster, not sure.
3) Sampling an a software loop will mean LOTS of jitter in the sample rate, which makes for lots of distortion and aliasing.
I'll attack your second point first: if you haven't tried it then please don't comment. This forum seems to be well-founded on relevant information from people who have actually done exactly the same as the OP. Even if the OP had an idea for a chicken feeder using XYZ accelerometer, there's usually someone who says "Well, I built a fish feeder but it uses that exact same accelerometer and I found ..."

1) I don't think we're talking HiFi here.
3) Yes, except it's being played back out of the same loop, so there will be no distortion of the output due to jitter.

I would like to ask the OP the following two questions:
1) What have you done to translate the alternating +/- audio signal into the 0-3.3V range which the Due ADC can measure?
2) What have you done to translate the 0.55-2.77V range of the DAC back into an audio waveform?
"The problem is in the code you didn't post."

MarkT

There are soooooo many reasons that is never going to work well at all:

1) Due DAC and ADC resolution is only 12 bits, which is poor for audio.
Theoretical resolution - in practice the analog stuff is utterly compromised by sharing
the 3.3V rail with the digital, massive noise spikes are present (20 lsbs or so).
Quote
2) For analogRead, default conversion time is about 100 uSec, which means a sample rate of only 10kHz.  Again, very poor for audio.  Perhaps on the Due it runs faster, not sure.
No, the Due is < 2us per channel I believe.   With DMA you get 1us/channel, which is
why people use these chips for low-performance pocket 'scopes.
Quote
3) Sampling an a software loop will mean LOTS of jitter in the sample rate, which makes for lots of distortion and aliasing.

If you want the best quality you can get, which will still be poor, you need to do register-level programming, and setup a DMA controller to read and buffer the data, and another DMA controller to transfer the data from the buffer to the A/D.  You'll then need interrupt handlers to deal with the buffer management.

Personally, I think you're wasting your time, as the audio quality will be poor at best unless you add external 16-24-bit I2S ADCs and DACs.

Regards,
Ray L.
Yes, that's probably the only sane approach, but you could use PWM sigma-delta
output rather than an external DAC to reduce component count - that goes directly
into a class D amp nicely.
[ I DO NOT respond to personal messages, I WILL delete them unread, use the forum please ]

Go Up