adafruit / Adafruit_CircuitPython_CircuitPlayground

CircuitPython library for Circuit Playground Express
MIT License
91 stars 71 forks source link

Sine wave may be rather quiet - add option to change to square wave #49

Closed kevinjwalters closed 2 years ago

kevinjwalters commented 5 years ago

This forum post has made me wonder if MakeCode uses a square wave to drive the CPX speaker. I think these are considerably louder. Might be worth adding that as an option? Or replacement?

The new return to mid point approach for audio means that [0, 65535] isn't great as a minimalist square wave sample. Perhaps something along the lines of [32768, 65535, 65535, 65535, 65535, 65535, 0, 0, 0, 0, 0, 32768] might do the trick?

SEE DISCUSSION in comments about power and heat before selecting square wave as better option!

kevinjwalters commented 5 years ago

Worth understanding this discussion too: https://forums.adafruit.com/viewtopic.php?f=58&t=150363

I'll add some more to that forum post when I get time to test different waveforms around the problematic 2300 Hz frequency.

deshipu commented 5 years ago

By the way, I noticed that the sine wave has wildly varying loudness depending on the frequency — you can tell by playing the RTTTL library examples, some of the notes are much louder than others.

ladyada commented 5 years ago

it'll be loudest around 2-4KHz which is where its expecting to be driven, did i mention its a really simple buzzer? :)

deshipu commented 5 years ago

Oh, sorry, I tested that on the Hallowing, with a "real" speaker connected.

I guess I will report a separate issue.

ladyada commented 5 years ago

speaker will also have some resonance - adding a normalization filter would be possible for tones, but not for anything more complex

kevinjwalters commented 5 years ago

@deshipu How are you driving the "real" speaker? It's worth checking pk-pk levels and ensuring the waveform is what you expect.

For example, the full voltage output range from the CPX A0 converted to AC is beyond the expected range for some equipment, discussed on https://forums.adafruit.com/viewtopic.php?f=58&t=148520. I have a small amplifying speaker which shuts down after a few seconds when faced with full peak to peak up its 3.5mm input. For other equipment it's within spec.

deshipu commented 5 years ago

@kevinjwalters the Hallowing has a volume control, and I set it so that the speaker is far from its maximum.

dhalbert commented 5 years ago

Has anyone looked at the waveforms with a scope or Saleae analog to see the levels?

deshipu commented 5 years ago

I reported it at https://github.com/adafruit/Adafruit_CircuitPython_RTTTL/issues/12 and I will check the waveforms now and post there.

kevinjwalters commented 5 years ago

On theme of speaker peculiarities there's mention of the speaker getting "very hot" with length playing of notes. It happens to be MakeCode. Started a discussion on: https://forums.adafruit.com/viewtopic.php?f=58&t=150682

Some measurements, for just under 9 minutes of music played with samples, a square wave with less amplitude is using more power (volume sounded roughly the same to me) and perhaps does get hotter to the touch:

58mWh sawtooth [-2731, -8192, -13653, -19114, -24575, -30036, 30036, 24575, 19114, 13653, 8192, 2731]

77mWh square [-10000, -10000, -10000, -10000, -10000, -10000, 10000, 10000, 10000, 10000, 10000, 10000]

hexthat commented 5 years ago

PR #57 has volume control use like

from adafruit_circuitplayground.express import cpx

loud = (2**4)
quite = -(2**4)

cpx.play_tone(220, 1, loud)
kevinjwalters commented 5 years ago

Just tried MakeCode (15-May-2019) and output as viewed on A0 is a triangle wave with a tiny touch of jaggedness.

It looks (sounds) like MakeCode has changed since I first put that ticket it, see the whole set of posts on: https://forums.adafruit.com/viewtopic.php?f=64&t=151431

kevinjwalters commented 5 years ago

More MakeCode shenanigans:

kevinjwalters commented 5 years ago

Not relevant for CPX specific code but there's an interesting practical limit of around 53000 on PyPortal DACs: https://forums.adafruit.com/viewtopic.php?f=24&t=153703

ryanskeith commented 2 years ago

The square ton is definitely louder. We could implement volume control as well. But perhaps this is another issue.