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

add looping to rp2040 analogbufio.BufferedIn #9438

Merged
merged 3 commits into from
Jul 22, 2024

Conversation

timchinowsky
Copy link

@timchinowsky timchinowsky commented Jul 18, 2024

This PR contains part of the changes necessary to implement the processing of live sampled audio as described in #2676. Here analogbufio.BufferedIn is modified to add a loop keyword argument to BufferedIn.readinto(). If called without the keyword or with loop=False, behavior is identical to the current readinto(). If called with loop=True, readinto() sets up chained DMA which continuously writes ADC values into the buffer.

The contrast between the two modes can be seen by plotting the output of test code listed here:

import analogbufio
import array
import audiocore
import audiopwmio
import board

samples = 100
buffer = array.array("H", [0x0000] * samples)
adc = analogbufio.BufferedIn(board.A0, sample_rate=100000)

adc.readinto(buffer)

print("Sample,Print 1, Print 2,Print 3, Print 4")
for i in range(4):
    for j in range(samples):
        print(j, "," * (i + 1), buffer[j])

adc.readinto(buffer, loop=True)

print("Sample,Print 1, Print 2,Print 3, Print 4")
for i in range(4):
    for j in range(samples):
        print(j, "," * (i + 1), buffer[j])

Here is a plot of the data output by the first print loop when pin A0 is driven by a 1 kHz sine wave:

chart (1)

The buffer is plotted 4 times, but you can only see one (green) series, because when loop=False the buffer is static. Compare the plot generated from the second print loop:

chart (2)

The slight scatter between the series shows that the data in the buffer is different in each of the 4 print sequences. If the print loop is repeated again, the plot is similar, but has drifted in phase due to small differences between the clock rates of the ADC and the signal generator:

chart (3)

These plots are deceptively simple, because the signal frequency was chosen to put exactly one period of the signal in each buffer. If you change the frequency to 500 Hz instead the complications can be seen:

chart (4)

During the ~241 ms required to print the buffer 4 times, it has been refilled by DMA 241 times, alternating between the upward-going half of the sine wave and the downward-going half. Which half shows up at each printed sample will depend upon when exactly in the sequence that particular sample value was observed.

The other difference between loop=False and loop=True is that while readinto(buffer, loop=False) blocks until DMA is complete, then scales the ADC values to 16 bits before returning, readinto(buffer, loop=True) returns immediately after starting DMA, and the buffer values are raw unscaled ADC data.

@tannewt tannewt self-requested a review July 19, 2024 17:31
Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One super minor doc thing. Looks good otherwise. Thank you!

shared-bindings/analogbufio/BufferedIn.c Outdated Show resolved Hide resolved
Co-authored-by: Scott Shawcroft <scott@tannewt.org>
Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@tannewt tannewt merged commit f3b9b35 into adafruit:main Jul 22, 2024
308 checks passed
@jepler jepler mentioned this pull request Jul 26, 2024
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

Successfully merging this pull request may close these issues.

2 participants