Skip to content

Incorrect playback sample_rate #1196

Closed
Closed
@tannewt

Description

@tannewt

From adafruit/Adafruit_CircuitPython_CircuitPlayground#40

@caternuson says:

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions