11#pragma once
22
3- #include < stddef.h> // for size_t
4- #include < stdint.h> // for uint8_t
5- #include < atomic> // for atomic
6- #include < memory > // for shared_ptr, unique_ptr
7- #include < mutex > // for mutex
8- #include < string > // for string
9- #include < functional > // for function
3+ #include < stddef.h> // for size_t
4+ #include < stdint.h> // for uint8_t
5+ #include < atomic> // for atomic
6+ #include < functional > // for function
7+ #include < memory > // for shared_ptr, unique_ptr
8+ #include < mutex > // for mutex
9+ #include < string > // for string
1010#include " freertos/FreeRTOS.h"
1111#include " freertos/semphr.h"
1212#ifdef CONFIG_AUDIO_SINK_VS1053
1313#include " VS1053.h" // for VS1053
1414#define _AudioSink VS1053
15- #else
15+ #else
1616#include " StreamOrientedAudioSink.h"
1717#define _AudioSink StreamOrientedAudioSink
1818#endif
1919
2020class AudioControl {
21- public:
21+ public:
2222 enum CommandType {
2323 PLAY ,
2424 PAUSE ,
@@ -30,92 +30,96 @@ class AudioControl {
3030 VOLUME_LOGARITHMIC
3131 };
3232 class FeedControl {
33- public:
33+ public:
3434 FeedControl (std::shared_ptr<AudioControl> audioController) {
3535 this ->audioController = audioController;
3636 this ->audioSink = audioController->audioSink ;
3737 };
3838 std::shared_ptr<_AudioSink> audioSink = NULL ;
3939 std::shared_ptr<AudioControl> audioController;
4040 size_t feedData (uint8_t * data, size_t bytes, size_t trackId,
41- bool STORAGE_VOLATILE = 0 );
41+ bool STORAGE_VOLATILE = 0 );
4242 template <class T >
43- void feedCommand (CommandType command, T value, std::optional<T> limit = std::nullopt ) {
44- if (command != CommandType::VOLUME_LINEAR && command != CommandType::VOLUME_LOGARITHMIC ){
45- if (!this ->audioSink ->streams .size ()) return ;
43+ void feedCommand (CommandType command, T value,
44+ std::optional<T> limit = std::nullopt ) {
45+ if (command != CommandType::VOLUME_LINEAR &&
46+ command != CommandType::VOLUME_LOGARITHMIC ) {
47+ if (!this ->audioSink ->streams .size ())
48+ return ;
4649 }
47- switch (command)
48- {
49- case CommandType::PLAY :
50- if (this ->audioSink ->streams [0 ]->state != _AudioSink::Stream::State::Stopped)
51- this ->audioSink ->new_state (this ->audioSink ->streams [0 ], _AudioSink::Stream::State::Playback);
52- break ;
50+ switch (command) {
51+ case CommandType::PLAY :
52+ if (this ->audioSink ->streams [0 ]->state !=
53+ _AudioSink::Stream::State::Stopped)
54+ this ->audioSink ->new_state (this ->audioSink ->streams [0 ],
55+ _AudioSink::Stream::State::Playback);
56+ break ;
5357
54- case CommandType::PAUSE :
55- if (this ->audioSink ->streams [0 ]->state != _AudioSink::Stream::State::Stopped)
56- this ->audioSink ->new_state (this ->audioSink ->streams [0 ], _AudioSink::Stream::State::PlaybackPaused);
57- break ;
58+ case CommandType::PAUSE :
59+ if (this ->audioSink ->streams [0 ]->state !=
60+ _AudioSink::Stream::State::Stopped)
61+ this ->audioSink ->new_state (
62+ this ->audioSink ->streams [0 ],
63+ _AudioSink::Stream::State::PlaybackPaused);
64+ break ;
5865
59- case CommandType::DISC :
60- {
61- printf ( " DISC \n " ) ;
62- auto & v = this -> audioSink -> streams ;
63- if (!v. size ()) break ;
64- if (v.size () > 1 ) {
65- auto it = v.begin () + 1 ; // start after index 0
66- while (it != v.end ()) {
67- // adapt *it access to your actual element type (ptr/unique_ptr/object)
68- auto * src = static_cast <FeedControl*>((*it)->source );
69- if (src == this ) {
70- it = v.erase (it); // erase returns the next iterator
71- }
72- else {
73- ++it;
66+ case CommandType::DISC : {
67+ printf ( " DISC \n " );
68+ auto & v = this -> audioSink -> streams ;
69+ if (!v. size ())
70+ break ;
71+ if (v.size () > 1 ) {
72+ auto it = v.begin () + 1 ; // start after index 0
73+ while (it != v.end ()) {
74+ // adapt *it access to your actual element type (ptr/unique_ptr/object)
75+ auto * src = static_cast <FeedControl*>((*it)->source );
76+ if (src == this ) {
77+ it = v.erase (it); // erase returns the next iterator
78+ } else {
79+ ++it;
80+ }
7481 }
7582 }
76- }
77- if (!v.empty () && v[0 ]->source == this ) { // guard against empty
78- printf (" DISC stop\n " );
79- this ->audioSink ->stop_feed ();
80- }
81- }
82- break ;
83+ if (!v.empty () && v[0 ]->source == this ) { // guard against empty
84+ printf (" DISC stop\n " );
85+ this ->audioSink ->stop_feed ();
86+ }
87+ } break ;
8388
84- case CommandType::FLUSH :
85- if (this ->audioSink ->streams .size () > 0 )
86- this ->audioSink ->streams [0 ]->empty_feed ();
87- break ;
89+ case CommandType::FLUSH :
90+ if (this ->audioSink ->streams .size () > 0 )
91+ this ->audioSink ->streams [0 ]->empty_feed ();
92+ break ;
8893
89- case CommandType::SKIP :
90- this ->audioSink ->stop_feed ();
91- break ;
94+ case CommandType::SKIP :
95+ this ->audioSink ->stop_feed ();
96+ break ;
9297
93- case CommandType::VOLUME_LINEAR :{
94- uint8_t lin = limit.has_value ()
95- ? this ->audioSink ->to_linear_volume (value, limit)
96- : this ->audioSink ->to_linear_volume (value);
97- this ->audioController ->volume .store (lin);
98- printf (" VOLUME_LINEAR %d\n " , this ->audioController ->volume .load ());
99- this ->audioSink ->feed_command ([this ](uint8_t ) {
100- this ->audioSink ->set_volume (this ->audioController ->volume .load ());
98+ case CommandType::VOLUME_LINEAR : {
99+ uint8_t lin = limit.has_value ()
100+ ? this ->audioSink ->to_linear_volume (value, limit)
101+ : this ->audioSink ->to_linear_volume (value);
102+ this ->audioController ->volume .store (lin);
101103 printf (" VOLUME_LINEAR %d\n " , this ->audioController ->volume .load ());
104+ this ->audioSink ->feed_command ([this ](uint8_t ) {
105+ this ->audioSink ->set_volume (this ->audioController ->volume .load ());
106+ printf (" VOLUME_LINEAR %d\n " , this ->audioController ->volume .load ());
102107 });
103- }
104- break ;
105- case CommandType:: VOLUME_LOGARITHMIC :{
106- this -> audioController -> volume . store ( this ->audioSink ->to_logarithmic_volume (value));
107- this ->audioSink ->feed_command ([this , value](uint8_t ) {
108- this ->audioSink ->set_volume (this ->audioController ->volume .load ());
108+ } break ;
109+ case CommandType:: VOLUME_LOGARITHMIC : {
110+ this -> audioController -> volume . store (
111+ this ->audioSink ->to_logarithmic_volume (value));
112+ this ->audioSink ->feed_command ([this , value](uint8_t ) {
113+ this ->audioSink ->set_volume (this ->audioController ->volume .load ());
109114 });
110- }
111- break ;
115+ } break ;
112116
113- default :
114- break ;
117+ default :
118+ break ;
115119 }
116120 }
117- std::function<void (uint8_t )> state_callback = [](uint8_t state) {};
118-
121+ std::function<void (uint8_t )> state_callback = [](uint8_t state) {
122+ };
119123 };
120124 AudioControl ();
121125 size_t getHeaderOffset (size_t trackId);
@@ -131,7 +135,7 @@ class AudioControl {
131135 std::atomic<size_t > volume = 90 ;
132136 SemaphoreHandle_t SPI_semaphore;
133137
134- private:
138+ private:
135139 size_t trackId = 0 ;
136140 std::shared_ptr<_AudioSink> audioSink;
137141
0 commit comments