arduino-audio-tools
Loading...
Searching...
No Matches
AudioPlayerThreadSafe.h
1#pragma once
2
3#include "AudioTools/CoreAudio/AudioPlayer.h"
4
5namespace audio_tools {
7
9 Begin,
10 End,
11 Next,
12 SetIndex,
13 SetPath,
14 SetVolume,
15 SetMuted,
16 SetActive
17};
18
21 int index = 0; // begin/setIndex
22 bool isActive = true; // begin/setActive
23 int offset = 1; // next
24 float volume = 0.0f; // setVolume
25 bool muted = false; // setMuted
26};
27
77template <template <class> class QueueT>
79 public:
85 AudioPlayerThreadSafe(AudioPlayer& p, QueueT<AudioPlayerCommand>& queue)
86 : player(p), queue(queue) {}
87
88 // Control API: enqueue only; applied in copy()
89 bool begin(int index = 0, bool isActive = true) {
90 AudioPlayerCommand c{AudioPlayerCommandType::Begin};
91 c.index = index;
92 c.isActive = isActive;
93 return enqueue(c);
94 }
95
96 void end() {
97 AudioPlayerCommand c{AudioPlayerCommandType::End};
98 enqueue(c);
99 }
100
101 bool next(int offset = 1) {
102 AudioPlayerCommand c{AudioPlayerCommandType::Next};
103 c.offset = offset;
104 return enqueue(c);
105 }
106
107 bool setIndex(int idx) {
108 AudioPlayerCommand c{AudioPlayerCommandType::SetIndex};
109 c.index = idx;
110 return enqueue(c);
111 }
112
113 bool setPath(const char* path) {
114 AudioPlayerCommand c{AudioPlayerCommandType::SetPath};
115 this->path = path;
116 return enqueue(c);
117 }
118
119 size_t copy() {
120 if (queue.size() > 0) processCommands();
121 return player.copy();
122 }
123
124 size_t copy(size_t bytes) {
125 if (queue.size() > 0) processCommands();
126 return player.copy(bytes);
127 }
128
129 void setActive(bool active) {
130 AudioPlayerCommand c{AudioPlayerCommandType::SetActive};
131 c.isActive = active;
132 enqueue(c);
133 }
134
135 bool setVolume(float v) {
136 AudioPlayerCommand c{AudioPlayerCommandType::SetVolume};
137 c.volume = v;
138 return enqueue(c);
139 }
140
141 bool setMuted(bool muted) {
142 AudioPlayerCommand c{AudioPlayerCommandType::SetMuted};
143 c.muted = muted;
144 return enqueue(c);
145 }
146
147 private:
148 AudioPlayer& player;
149 // Internal command queue
150 QueueT<AudioPlayerCommand>& queue;
151 Str path;
152
153 // Drain command queue and apply to underlying player
154 void processCommands() {
155 AudioPlayerCommand cmd;
156 // Attempt non-blocking dequeue loop; requires queue configured non-blocking
157 while (dequeue(cmd)) {
158 switch (cmd.type) {
159 case AudioPlayerCommandType::Begin:
160 player.begin(cmd.index, cmd.isActive);
161 break;
162 case AudioPlayerCommandType::End:
163 player.end();
164 break;
165 case AudioPlayerCommandType::Next:
166 player.next(cmd.offset);
167 break;
168 case AudioPlayerCommandType::SetIndex:
169 player.setIndex(cmd.index);
170 break;
171 case AudioPlayerCommandType::SetPath:
172 player.setPath(path.c_str());
173 break;
174 case AudioPlayerCommandType::SetVolume:
175 player.setVolume(cmd.volume);
176 break;
177 case AudioPlayerCommandType::SetMuted:
178 player.setMuted(cmd.muted);
179 break;
180 case AudioPlayerCommandType::SetActive:
181 player.setActive(cmd.isActive);
182 break;
183 }
184 if (queue.size() == 0) break;
185 }
186 }
187
188 // Queue facade wrappers to allow both internal/external queues
189 bool enqueue(AudioPlayerCommand& c) { return queue.enqueue(c); }
190
191 bool dequeue(AudioPlayerCommand& c) { return queue.dequeue(c); }
192};
193
194} // namespace audio_tools
High-level audio playback pipeline and controller.
Definition AudioPlayer.h:51
size_t copy()
Definition AudioPlayer.h:415
Lock-free asynchronous control wrapper for AudioPlayer using a command queue.
Definition AudioPlayerThreadSafe.h:78
AudioPlayerThreadSafe(AudioPlayer &p, QueueT< AudioPlayerCommand > &queue)
Construct an async-control wrapper around an AudioPlayer.
Definition AudioPlayerThreadSafe.h:85
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
AudioPlayerCommandType
Control AudioPlayer command types processed in copy()
Definition AudioPlayerThreadSafe.h:8
Definition AudioPlayerThreadSafe.h:19