This repository has been archived by the owner on Feb 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
dev notes
Valera edited this page Jan 14, 2018
·
2 revisions
You want to understand how does this library works? Great! This article is specially for it.
After all, it should help me remember structure of my libary.
Of course, we need timer. After all, esp_timer
became best choice - it provide flexible API and low latency. Next, all system should be thread-safe, so we need semaphores and queues.
After some thinking, I found that best way is to queue events and handle it directly from timer's callback. If timer isn't started, I just start one-shot with zero delay.
How can I mix different frequecies in same mixer? (SoundMixer::setupTimer
)
- Caclulate LCM for all freqencies.
- Start timer with delay to
SOUND_FREQ_TO_DELAY(LCM)
. - Set
divisor
of every provider toLCM/frequency
. - Set
maxCount
to LCM ofdivisor
s.
On every tick of playing:
- Increment
conunter
and reset if too big (>counterMax
) - For active providers
- If
(conunter % divisor) == 0
, update element from queue, write it toactual
. - Play value from
actual
.
-
esp_timer_handle_t timer
- timer for this instance. -
SemaphoreHandle_t mutex
- main mutex for thread-unsafe resources. -
SemaphoreHandle_t timerMutex
- counting semaphore with two states. Given if timer is active and any event will be handled. -
QueueHandle_t queue
- queue for all events. -
dac_channel_t dacCh
- it is pretty clear. -
unsigned long int counter
- read above -
unsigned long int counterMax
- read above -
SoundChNum chCount
- total count of channels for this mixer. Always lower or equal thanCONFIG_SND_MAX_CHANNELS
. -
SoundChNum chFirstAuto
- number of first "auto" channel. If equal tochCount
, no auto channels at all. -
SemaphoreHandle_t chActiveCount
- count of active channels. I made it counting semaphore for thread-safe. -
SoundProvider* chSound[CONFIG_SND_MAX_CHANNELS]
- array of sound providers for channels. -
bool chActive[CONFIG_SND_MAX_CHANNELS]
- array of active channels, UNSAFE. -
bool chPaused[CONFIG_SND_MAX_CHANNELS]
- array of paused channels, UNSAFE. -
SoundVolume chVolume[CONFIG_SND_MAX_CHANNELS]
- volume map, UNSAFE All methods is well-commented, so I'm not going to describe them here.
-
QueueHandle_t queue
- queue for sound -
QueueHandle_t controlQueue
- queue for transmiting information from provider to mixer -
unsigned int divisor
- read above -
SoundData actual
- current sample, controlled by mixer