Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redirect to I2S for an external DAC+Amp and volume control #1

Open
knopserl opened this issue Feb 28, 2020 · 8 comments
Open

redirect to I2S for an external DAC+Amp and volume control #1

knopserl opened this issue Feb 28, 2020 · 8 comments

Comments

@knopserl
Copy link

knopserl commented Feb 28, 2020

@krzychb great solution, that saved me! I need to create a sine sweep generator from 100Hz to 10kHz. This is done in HW and generates a quite clean sine wave. So I changed the clock diverder to 64 and created a loop which allows 2Hz steps.
Now I need a hint how to redirect the internal DAC signal to the I2S for an external DAC + amplifier as I need 3V output and I need volume control for the DAC/I2S.
Any hints which brings me further?

@krzychb
Copy link
Owner

krzychb commented Feb 29, 2020

@knopserl, I am happy it works and that you are able to further tweak it!

Now I need a hint how to redirect the internal DAC signal to the I2S for an external DAC + amplifier as I need 3V output and I need volume control for the DAC/I2S.

I do not see a way to route internal DAC signal (or CW generator itself) back to the I2S.

Any hints which brings me further?

I would follow ESP_Sprite's advice in https://esp32.com/viewtopic.php?f=13&t=9342#p39062. Try code provided in https://github.com/espressif/esp-idf/tree/master/examples/peripherals/i2s.

@knopserl
Copy link
Author

knopserl commented Feb 29, 2020

@krzychb Thanks for your replay/hints. The reason why I was asking was because I saw the thread in the ESP32 forum which advises to not directly to the CW to DAC connection but go thru the I2S to the internal DAC. So if the CW can route the signal thru I2S to internal DAC, then I2S can also connect an external DAC (with an amplifierer), because I2S allows the selection of internal or external DAC.

Here is the advise from ESP_Sprite, but he does not explain HOW to implement his sdvise. I have also asked in this thread how to follow his advise with a small code snippet or example.
I also found his i2s example before, but it does not use the CW generator (which is a HW generator) but does rather a sine calculation by math calc. I did such a i2s already but was not very sucessful with it.

ESP_Sprite advice:
Don't try to write to the dac directly, you'll get jitter and the CPU use will be high. Instead, configure I2S to output to the internal DACs and feed it entire chunks of memory. (You can use the ESP-IDF I2S driver to do this.) The hardware will take care of writing to the DACs. You should be able to get up to some MHz that way, if memory serves.

@krzychb
Copy link
Owner

krzychb commented Feb 29, 2020

I saw the thread in the ESP32 forum which advises to not directly to the CW to DAC connection but go thru the I2S to the internal DAC. So if the CW can route the signal thru I2S to internal DAC...

What thread was it (suggesting CW can route the signal thru I2S)?

I am afraid ESP_Sprite meant using sine calculation by math calc and saving it to memory (to output "entire chunks of memory" using I2S to internal or external DAC).

@knopserl
Copy link
Author

knopserl commented Mar 1, 2020

@krzychb Thanks. That is where I was comming from. I used a precalculated sine sample value array (at compile time) or a calculation during runtime before feeding the I2S output (for an external DAC/Amp) and I was not really successful with the result (probably my fault). So I liked this one as it does it with HW and without CPU load, but I see now, that it only works with internal DAC and it has also only 8bit resolution.
Do you have a good working example/link for a high quality sine gen? 16 Bit and 1Hz resolution is required and I need a sweeper, so it support fast frequency changes (e.g. 100 to 10kHz in a second)

@krzychb
Copy link
Owner

krzychb commented Mar 2, 2020

I used a precalculated sine sample value array (at compile time) or a calculation during runtime before feeding the I2S output (for an external DAC/Amp) and I was not really successful with the result (probably my fault).

@knopserl what particular problem did you have? To make it work or to get desired resolution, range, and fast frequency changes?

I would like to do some testing but I see that support for I2S and Internal DAC is broken in master and I do not have external IS2 DAC to check it for external higher resolution output.

@knopserl
Copy link
Author

knopserl commented Mar 2, 2020

@krzychb thanks a lot for your willingness to spend time for my problem.
Here is the description:
I need a sine sweeper which has a lower and upper frequency input parameter. Lets say 200Hz to 8000Hz und I have additionally a amplitude and a sweep duration parameter, in which the sine should smooth scale up the frequency from lower limit to upper limit.
Now the challenges are: The sweep has to be smooth, best is to have only 1Hz steps and it has to be fast in changing the frequency from low to high, which means fast and smooth changes.
E.g. 200Hz to 8000Hz in max 1 sec but in 1Hz steps, which means 7800 Hz frequency changes in 1 sec.

I thought I can set the prescale to a value, that it allows one 1 or 2 Hz steps but it seem I misunderstood that. I probably can only set the prescaler from 1 to 15 right? And thus the smallest resolution I get is 15.61Hz.

So do you see any chance to get a 1 or 2 Hz step delta and also fulfill the other requiremenst like amplitude control and a sweep within a second with that 1-2Hz steps?

In addition I was wondering if the CW output can be redirected to the I2S and then to internal or external DAC but I guess that is not possible.
What hearts me more are the large steps of 15.61Hz and that I can't get it down to 1-2Hz steps.

very strange demand right?

with such a small module it could be done in HW (frequency and amplitude), but that would mean I give up to do it within the ESP32.
https://www.aliexpress.com/item/32462306275.html?spm=a2g0s.9042311.0.0.15a24c4dohuGPk

@github-ec
Copy link

github-ec commented Mar 4, 2020

@knopserl: If I understood you correctly, your problem should be solveable by streaming a prepared signal through the DAC? If yes, you might have a look at this
https://www.hackster.io/julianfschroeter/stream-your-audio-on-the-esp32-2e4661
titled "Stream Your Audio on the ESP32 Use the ESP32 to play audio data from a custom stream."

(The program is using ISR function to write to the DAC)
… Just checked it myself; unfortunately the cpu "panics" and reboots when the ISR routine is called more than 80000 times/s. With 256 samples this leads to max. 312,5 Hz … by reducing to 64 samples one may get up to 1.25 kHz. Not enough, sorry ...

@knopserl
Copy link
Author

knopserl commented Mar 5, 2020

@github-ec Thans for testing/spendig your time. I was also trying it with a sine wave generation in RAM and play it via internal/external DAC (using I2S). But the ESP32 seem to have a problem with sine waver and frequencies up to 10kHz. I also need to change the frequency every few micro-seconds.
If the internal cosing would support more fine granular steps, that would be the perfect solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants