8000 play_tone() not working · Issue #40 · adafruit/Adafruit_CircuitPython_CircuitPlayground · GitHub
[go: up one dir, main page]

Skip to content

play_tone() not working #40

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

Closed
caternuson opened this issue Sep 18, 2018 · 12 comments
Closed

play_tone() not working #40

caternuson opened this issue Sep 18, 2018 · 12 comments
Assignees

Comments

@caternuson
Copy link
Contributor

Confirmed. See here for more info and sample code:
https://forums.adafruit.com/viewtopic.php?f=58&t=140985

@caternuson
Copy link
Contributor Author

For completeness, here's firmware I tested with:

Adafruit CircuitPython 3.0.1 on 2018-08-21; Adafruit CircuitPlayground Express with samd21g18 
>>> from adafruit_circuitplayground.express import cpx
>>> cpx.play_tone(330, 1)
>>> cpx.play_tone(660, 1)
>>>

@ladyada
Copy link
Member
ladyada commented Sep 18, 2018

huh! ok odd, @kattni wanna take a look in the next day or so?

@sommersoft
Copy link
Collaborator

I can third this with 3.0.2. Used...

>>> for i in range(50):
...     tone = 300 + (i * 10)
...     print("Tone:", tone)
...     cpx.play_tone(tone, 0.5)
...

… which has a noticeable droop in the ~540Hz to ~690Hz range.

Additionally, I ran this with a factory fresh CPX with 2.2.0. The tones increase accordingly with above code. May need to debug audioio.AudioOut and/or audioio.RawSample.

@caternuson
Copy link
Contributor Author

Agree, I think it's going to be something lower level than CPX. I can recreate with this:

import array
import math
import time

import board
import digitalio
import audioio

speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)

# Generator thing
def sine_sample(length):
    tone_volume = (2 ** 15) - 1
    shift = 2 ** 15
    for i in range(length):
        yield int(tone_volume * math.sin(2*math.pi*(i / length)) + shift)

# Generate the sample
length = 100
sine_wave = array.array("H", sine_sample(length))
sample = audioio.AudioOut(board.SPEAKER)
sine_wave_sample = audioio.RawSample(sine_wave)

# Play tone
frequency = 330
sample_rate = int(len(sine_wave) * frequency)
speaker_enable = True
sine_wave_sample.sample_rate = sample_rate
sample.play(sine_wave_sample, loop=True)
time.sleep(1)
sample.stop()
sample.deinit()
speaker_enable = False

@ladyada
Copy link
Member
ladyada commented Sep 18, 2018

ooooh ok maybe this is a @tannewt @dhalbert level issue. (i thought it was just a bug in the cpx library)
will re-assign!

@tannewt
Copy link
Member
tannewt commented Sep 18, 2018

I've opened adafruit/circuitpython#1196 to continue this investigation deeper. I'll also take a quick look now. Thanks!

@tannewt tannewt closed this as completed Sep 18, 2018
@tannewt
Copy link
Member
tannewt commented Sep 18, 2018

This is actually an issue with both.

The limit of the DAC is 350ksps which we weren't doing because the input clock was too slow. With the fix for adafruit/circuitpython#1196 660hz will work.

However, this library will start to crash when play_tones frequency is over 3500 ( * 100 is 350ksps) because it is now checked correctly.

So, I'm reopening this as well. The sample length should be reduced when the frequency is more than 3500 in order to keep the sample rate below the limit.

@tannewt tannewt reopened this Sep 18, 2018
@caternuson
Copy link
Contributor Author
caternuson commented Oct 1, 2018

The sample length should be reduced when the frequency is more than 3500 in order to keep the sample rate below the limit.

Take it you mean this needs to be done here in this lib? Instead of hardwired length of 100?

@ladyada should this be re-re-assigned?

@ladyada
Copy link
Member
ladyada commented Oct 2, 2018

i dont know 😂
scott, do ya want carter to limit this here?

@tannewt
Copy link
Member
tannewt commented Oct 2, 2018

Yes please @caternuson ! Thank you!

@caternuson caternuson assigned caternuson and unassigned tannewt and dhalbert Oct 2, 2018
@caternuson
Copy link
Contributor Author

OK. I'll give it a go. Removing you and Dan and re-re-assigning to me.

@caternuson
Copy link
Contributor Author

Closing. Fixed with #41.

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

6 participants
0