Skip to content
This repository has been archived by the owner on Feb 19, 2023. It is now read-only.

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.

Common information

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)

  1. Caclulate LCM for all freqencies.
  2. Start timer with delay to SOUND_FREQ_TO_DELAY(LCM).
  3. Set divisor of every provider to LCM/frequency.
  4. Set maxCount to LCM of divisors.

On every tick of playing:

  1. Increment conunter and reset if too big (> counterMax)
  2. For active providers
  3. If (conunter % divisor) == 0, update element from queue, write it to actual.
  4. Play value from actual.

Hidden members of SoundMixer

  • 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 than CONFIG_SND_MAX_CHANNELS.
  • SoundChNum chFirstAuto - number of first "auto" channel. If equal to chCount, 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.

Hidden members of SoundProvider

  • 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

Common

End-user

Developers and contributors

Clone this wiki locally