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

Adafruit Feather M0 degraded audio issue #75

Closed
gregsadetsky opened this issue Jul 16, 2019 · 15 comments
Closed

Adafruit Feather M0 degraded audio issue #75

gregsadetsky opened this issue Jul 16, 2019 · 15 comments

Comments

@gregsadetsky
Copy link

gregsadetsky commented Jul 16, 2019

Hello,

I've successfully ran most Mozzi examples on an Uno. I am however having a problem running the same examples on an Adafruit Feather M0 (which uses an ATSAMD21G18 ARM Cortex M0 processor).

The audio does come out but it seems distorted. It was pretty clear when running the Sinewave example, but it became really obvious when running the Samples Tables Arrays examples (I'm running all examples as is).

Please see below (all recordings done on the same computer using the same USB interface):

Is there an obvious fix, or at least a direction that I could try looking into? Is this related to the onboard DAC (one of its settings)..?

Thank you very much!

@tfry-git
Copy link
Collaborator

tfry-git commented Aug 1, 2019

Did you scale the audio to 12 bits on teensy? Otherwise it will simply come out too silent, and with a worse signal-to-noise ratio.

@tfry-git
Copy link
Collaborator

tfry-git commented Aug 1, 2019

Oh, sorry, did not read well. You are not using a teensy.

I don't think the SAMD21-support has received a lot of testing so far, and I'm not sure, what board it was originally developped, for. I still suspect an audio scaling issue, though 12 bits may not be the correct scaling to use. (The adafruit page mentions a 10bit DAC, but the code seems to assume 12 bits).

Experiment with scaling your audio output to 8, 10, and 12 bits, testing what sounds best. Also, you may have to adjust AudioConfigSAMD21.h, which assumes 12 bits (setting the center point to 2048).

@DavidHaitch
Copy link

I am experiencing a similar issue. Running the Sinewave demo on a Feather M0 produced no sound whatsoever, until I changed AUDIO_BIAS in AudioConfigSAMD21.h from 2048 to 1024. Then, I got output that sounded like @gregsadetsky 's "degraded" sinewave.

@tfry-git: I would like to try and assist in testing fixes to this, but I don't know what you mean by scaling the audio output to different bit widths. I thought Mozzi always used 16 bits?

@gregsadetsky
Copy link
Author

Wow, it works!!!! Setting

#define AUDIO_BIAS ((uint16_t) 512)

works!! :-)

Thank you so, so much!

Should there be a pre-processor directive, or additional documentation about this?

@DavidHaitch
Copy link

@gregsadetsky I am also working with a Feather M0, and I am unable to replicate your success. Even after setting AUDIO_BIAS to 512 in AudioConfigSAMD21.h, I still get a Sinewave that sounds like your degraded one.
Could you share the code that worked for you?

@gregsadetsky
Copy link
Author

I will take a look again, but I do remember the change being just what I posted above (the #define line).

Just to be sure (as this was a bit confusing to me initially), did you change the value in the AudioConfigSAMD21.h file that's within the Arduino library directory, or did you download a fresh copy of Mozzi that you included in your project directory, and made the change there?

I'm simply asking to make sure that you did the change in the code that was indeed compiled. Out of curiosity, if you delete the Mozzi directory where you did the change, does that create a compilation error?

Also, I remember trying much smaller values than 512 for fun and it did end up creating a "bit crushing" audio effect (a similar-ish distortion to what I posted). Could you try that (256, 128, etc.), just to make sure, again, that your changes in AudioConfigSAMD21.h are being taken into account? Cheers

@tfry-git
Copy link
Collaborator

tfry-git commented Aug 9, 2019

@DavidHaitch

I would like to try and assist in testing fixes to this, but I don't know what you mean by scaling the audio output to different bit widths. I thought Mozzi always used 16 bits?

Unfortunately, Mozzi does not always use 16 bits. For instance on the classic AVR architectures, it uses just over 8 bits, an STM32 it uses 10 bits, and on ESP8266 it uses 16 bits. You will always have to make sure that your updateAudio() returns the correct range of values for your configuration. This is not easy to fix, but see #43 for one idea (and more background).

This might also be the reason why your sinewave still sounds distorted. Make sure updateAudio() returns values between -511 and 512.

@gregsadetsky

Should there be a pre-processor directive, or additional documentation about this?

Definitely. Do you think there is a compiler defintion that will identify the Feather M0, realiably? Then this could be used in AudoConfigSAMD21.h, as appropriate.

@gregsadetsky
Copy link
Author

@tfry-git

Definitely. Do you think there is a compiler defintion that will identify the Feather M0, realiably? Then this could be used in AudoConfigSAMD21.h, as appropriate.

There's an ADAFRUIT_FEATHER_M0 flag/constant that - as far as I understand - comes from adafruit_samd's boards.txt: ... extra_flags=... -DADAFRUIT_FEATHER_M0.

adafruit_samd needs to be installed (see here) in order to use the Feather M0 family of boards with the Arduino IDE.

As an example, you can see this constant used here, for a Feather M0-based board with built-in radio (this one).

@DavidHaitch
Copy link

@tfry-git I think I've found another possible issue.
On line 518 of MozziGuts.cpp, the IS_SAMD21() case sets analogWriteResolution to 12 bits, even though the DAC on the SAMD21 is 10 bits.

According to the analogWriteResolution docs, setting the resolution to something higher than the hardware's actual resolution results in the remaining bits being dropped - only the first 10 are actually used. I don't have access to the hardware right now, but is that a possible problem?

@tfry-git
Copy link
Collaborator

Yes, this is a possible problem. But it's really hard to fix this kind of problem, without having the hardware to test (and I don't that that, either).

tfry-git added a commit that referenced this issue Nov 4, 2019
@tfry-git
Copy link
Collaborator

tfry-git commented Nov 4, 2019

Hi!

Sorry for dropping the ball on this one. I have committed an experimental fix for this. Could you please do

git pull
git checkout testing/SAMD21_analog_resolution

and then test with that? If it works, I can merge this into the official branch. In theory this should now work out of the box (you'll still have to make sure updateAudio() returns values in the +/-512 range).

Thanks!

@infovore
Copy link

infovore commented Jan 6, 2021

I've been working with a SAMD21 and have run into this issue too. @tfry-git's solution in the branch is correct for all SAMD21 chips.

The SAMD21 only has a single 10-bit DAC on it. (Its _ADC_s are 12-bit, however). This includes the Gemma M0 that support is listed for - that only has a 10-bit DAC output.

I'd put together my own PR but it seems a relatively straightforward fix...

@tfry-git
Copy link
Collaborator

tfry-git commented Jan 6, 2021

Thanks for the feedback. I have committed the following, then: f49e8d0

I assume that closes the issue, but please reopen, if it doesn't.

@tfry-git tfry-git closed this as completed Jan 6, 2021
@infovore
Copy link

infovore commented Jan 6, 2021

Looks good - only other thing you might want to change is AnalogWriteResolution in MozziGuts.cpp, line 542. I didn't find this made any difference, but it's one thing I could think of.

@infovore
Copy link

infovore commented Jan 7, 2021

👍

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

4 participants