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

Adjustable sample-rate and frequency shifting on samples #151

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

stevesims
Copy link
Contributor

This builds on work done by @movievertigo in #143

With this PR it becomes possible to set a "base frequency" on a sample, and once that is set the sample can be played at different frequencies.

It also becomes possible to adjust the sample rate of the underlying audio system. This allows the audio system to run with a higher fidelity than the default 16khz - altho that remains the default.

Individual samples can also be set to differing sample rates. The audio system will automatically compensate for this, ensuring that samples play back at the correct speed irrespective of what the rate the sample has been set to or the underlying audio system.

Looping of samples can now also be controlled; it is possible to set the byte offset within a sample of where repeats start from as well as the length of the section to repeat.

Note duration can be changed after starting playback, or set to initiate playback. This allows for durations longer than 65s, which can be useful when playing back long samples.

documentation on the audio system API changes to support this enhanced functionality can be found in AgonConsole8/agon-docs#6

movievertigo and others added 13 commits December 15, 2023 11:26
use an enum for AudioState
use more consistent variable naming
fix some code formatting
calculate sample playback rate based on a base frequency value for the sample.  this defaults to 532hz, or C5 (the C above middle-C)
this allows frequency adjustment of sample playback to be compatible with existing code that could be providing any value for sample frequency to `playNote`
includes variants both for negative sample numbers and bufferId-based numbering
support added for defining looping on a sample

sample playback however needs reworking.  the current system has the AudioSample structure keeping track of where playback is inside a sample, however that wouldn’t allow the same sample to be played simultaneously on more than one channel

control of sample playback instead needs to move into the EnhancedSamplesGenerator.  much of what’s in this commit needs to be moved
logic moved out of AudioSample for keeping track of position within sample
this will allow the same sample to be played back on multiple channels simultaneously

hack of using frequency -1 also no longer needed, as channel and sample generator now support `seekTo`
allows for note duration to be set to a 24-bit value
allows for the the sample rate for the underlying system, or for a waveform on an individual channel, to be adjusted
@stevesims
Copy link
Contributor Author

FYI this functionality is all "additive" to the existing API - all existing sample using code will still work unchanged.

samples only become "tuneable" when a base frequency is set on them. samples still loop by default in the exact manner they did before, etc etc.

duration of a sample when played back calculation was buggy, resulting in looping samples when they shouldn’t

the sample generator’s `getDuration` call needs to calculate based on how long the _underlying_ audio system will take to step through samples, therefore it is the sample rate of the underlying system that needs to be used for calculations
avoids placing audio objects (waveform generators and AudioSample objects) in psram, as the ESP32 has cache bugs related to psram.  whilst there is a compiler work-around for this (which we're using) the fix doesn't seem to work in the context of our multi-threaded and interrupt-driven audio system

audio logging improved

audio channel state is an atomic value to ensure consistency across different threads
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