arduino-audio-tools
Loading...
Searching...
No Matches
AudioPlayerThreadSafe.h
Go to the documentation of this file.
1#pragma once
2
4
5namespace audio_tools {
7
9 Begin,
10 End,
11 Next,
13 SetPath,
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:
86 : player(p), queue(queue) {}
87
88 // Control API: enqueue only; applied in copy()
89 bool begin(int index = 0, bool isActive = true) {
91 c.index = index;
92 c.isActive = isActive;
93 return enqueue(c);
94 }
95
96 void end() {
98 enqueue(c);
99 }
100
101 bool next(int offset = 1) {
103 c.offset = offset;
104 return enqueue(c);
105 }
106
107 bool setIndex(int idx) {
109 c.index = idx;
110 return enqueue(c);
111 }
112
113 bool setPath(const char* path) {
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) {
131 c.isActive = active;
132 enqueue(c);
133 }
134
135 bool setVolume(float v) {
137 c.volume = v;
138 return enqueue(c);
139 }
140
141 bool setMuted(bool muted) {
143 c.muted = muted;
144 return enqueue(c);
145 }
146
147 private:
148 AudioPlayer& player;
149 // Internal command queue
151 Str path;
152
153 // Drain command queue and apply to underlying player
154 void processCommands() {
156 // Attempt non-blocking dequeue loop; requires queue configured non-blocking
157 while (dequeue(cmd)) {
158 switch (cmd.type) {
160 player.begin(cmd.index, cmd.isActive);
161 break;
163 player.end();
164 break;
166 player.next(cmd.offset);
167 break;
169 player.setIndex(cmd.index);
170 break;
172 player.setPath(path.c_str());
173 break;
175 player.setVolume(cmd.volume);
176 break;
178 player.setMuted(cmd.muted);
179 break;
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
bool setVolume(float volume) override
Sets volume in range [0.0, 1.0]; updates VolumeStream.
Definition AudioPlayer.h:389
bool next(int offset=1)
Moves to the next/previous stream by offset (negative supported)
Definition AudioPlayer.h:313
void end()
Ends playback and resets decoder/intermediate stages.
Definition AudioPlayer.h:222
bool setPath(const char *path)
Selects stream by path without changing the source iterator.
Definition AudioPlayer.h:331
bool begin(int index=0, bool isActive=true)
Starts or restarts playback from the first or given stream index.
Definition AudioPlayer.h:166
void setActive(bool isActive)
Toggles playback activity; triggers fade and optional silence.
Definition AudioPlayer.h:375
size_t copy()
Definition AudioPlayer.h:423
bool setIndex(int idx)
Selects stream by absolute index in the source.
Definition AudioPlayer.h:322
bool setMuted(bool muted)
Mutes or unmutes the audio player.
Definition AudioPlayer.h:546
Lock-free asynchronous control wrapper for AudioPlayer using a command queue.
Definition AudioPlayerThreadSafe.h:78
size_t copy(size_t bytes)
Definition AudioPlayerThreadSafe.h:124
AudioPlayerThreadSafe(AudioPlayer &p, QueueT< AudioPlayerCommand > &queue)
Construct an async-control wrapper around an AudioPlayer.
Definition AudioPlayerThreadSafe.h:85
bool setVolume(float v)
Definition AudioPlayerThreadSafe.h:135
bool next(int offset=1)
Definition AudioPlayerThreadSafe.h:101
void setActive(bool active)
Definition AudioPlayerThreadSafe.h:129
void end()
Definition AudioPlayerThreadSafe.h:96
bool setPath(const char *path)
Definition AudioPlayerThreadSafe.h:113
bool begin(int index=0, bool isActive=true)
Definition AudioPlayerThreadSafe.h:89
size_t copy()
Definition AudioPlayerThreadSafe.h:119
bool setIndex(int idx)
Definition AudioPlayerThreadSafe.h:107
bool setMuted(bool muted)
Definition AudioPlayerThreadSafe.h:141
Str which keeps the data on the heap. We grow the allocated memory only if the copy source is not fit...
Definition Str.h:24
virtual const char * c_str()
provides the string value as const char*
Definition StrView.h:379
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
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:512
Definition AudioPlayerThreadSafe.h:19
float volume
Definition AudioPlayerThreadSafe.h:24
bool muted
Definition AudioPlayerThreadSafe.h:25
int index
Definition AudioPlayerThreadSafe.h:21
bool isActive
Definition AudioPlayerThreadSafe.h:22
int offset
Definition AudioPlayerThreadSafe.h:23
AudioPlayerCommandType type
Definition AudioPlayerThreadSafe.h:20