arduino-audio-tools
Loading...
Searching...
No Matches
RCAudioPlayerOSC.h
Go to the documentation of this file.
1
3#include "OSCData.h"
4
5namespace audio_tools {
6
15 RCAudioPlayerOSCSender() = default;
16 RCAudioPlayerOSCSender(Print &out) { setOutput(out); }
17
18 void setOutput(Print &out) { p_out = &out; }
19
20 bool setActive(bool active) { return active ? play() : stop(); }
21
22 bool play() {
23 if (p_out == nullptr) return false;
24
25 uint8_t data[20];
26 OSCData msg{data, sizeof(data)};
27
28 msg.setAddress("/play");
29 msg.setFormat("");
30 p_out->write(msg.data(), msg.size());
31 return true;
32 }
33
35 bool stop() {
36 if (p_out == nullptr) return false;
37 uint8_t data[20];
38 OSCData msg{data, sizeof(data)};
39
40 msg.setAddress("/stop");
41 msg.setFormat("");
42 p_out->write(msg.data(), msg.size());
43 return true;
44 }
45
48 bool next(int offset = 1) {
49 if (p_out == nullptr) return false;
50 uint8_t data[80];
51 OSCData msg{data, sizeof(data)};
52
53 msg.setAddress("/next");
54 msg.setFormat("i");
55 msg.write((int32_t)offset);
56 p_out->write(msg.data(), msg.size());
57 return true;
58 }
59
60 bool previous(int offset = 1) {
61 if (p_out == nullptr) return false;
62 uint8_t data[80];
63 OSCData msg{data, sizeof(data)};
64
65 msg.setAddress("/previous");
66 msg.setFormat("i");
67 msg.write((int32_t)offset);
68 p_out->write(msg.data(), msg.size());
69 return true;
70 }
71
73 bool setIndex(int idx) {
74 if (p_out == nullptr) return false;
75 uint8_t data[80];
76 OSCData msg{data, sizeof(data)};
77
78 msg.setAddress("/index");
79 msg.setFormat("i");
80 msg.write((int32_t)idx);
81 p_out->write(msg.data(), msg.size());
82 return true;
83 }
84
86 bool setPath(const char *path) {
87 if (p_out == nullptr) return false;
88 uint8_t data[strlen(path) + 20];
89 OSCData msg{data, sizeof(data)};
90
91 msg.setAddress("/path");
92 msg.setFormat("s");
93 msg.write(path);
94 p_out->write(msg.data(), msg.size());
95 return true;
96 }
97
98 bool setVolume(float volume) {
99 if (p_out == nullptr) return false;
100 uint8_t data[80];
101 OSCData msg{data, sizeof(data)};
102
103 msg.setAddress("/volume");
104 msg.setFormat("f");
105 msg.write(volume);
106 p_out->write(msg.data(), msg.size());
107 return true;
108 }
109
110 protected:
111 Print *p_out = nullptr;
112};
113
123
125 setAudioPlayer(player);
126 }
127
128 void setAudioPlayer(AudioPlayer &player) { p_player = &player; }
129
131 bool processInputMessage(Stream &in) {
132 if (!is_active) return false;
133
134 uint8_t data[80];
135 size_t len = in.readBytes(data, sizeof(data));
136 if (len > 0) {
137 if (osc.parse(data, len)) {
138 return true;
139 }
140 }
141 return false;
142 }
143
145 bool begin() {
146 if (p_player == nullptr) {
147 LOGE("RCAudioPlayerOSCReceiver: player is null");
148 return false;
149 }
151 is_active = true;
152 return true;
153 }
154
156 void end() {
157 is_active = false;
158 osc.clear();
159 }
160
161 protected:
163 bool is_active = false;
165
166 static bool play(OSCData &data, void *ref) {
168 p_player->play();
169 return true;
170 }
171
172 static bool stop(OSCData &data, void *ref) {
174 p_player->stop();
175 return true;
176 }
177
178 static bool next(OSCData &data, void *ref) {
180 int offset = data.readInt32();
181 return p_player->next(offset);
182 }
183
184 static bool previous(OSCData &data, void *ref) {
186 int offset = data.readInt32();
187 return p_player->previous(offset);
188 }
189 static bool setIndex(OSCData &data, void *ref) {
191 int idx = data.readInt32();
192 return p_player->setIndex(idx);
193 }
194 static bool setPath(OSCData &data, void *ref) {
196 const char *path = data.readString();
197 return p_player->setPath(path);
198 }
199 static bool setVolume(OSCData &data, void *ref) {
201 float volume = data.readFloat();
202 return p_player->setVolume(volume);
203 }
204
214};
215
216} // namespace audio_tools
#define LOGE(...)
Definition AudioLoggerIDF.h:30
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
void play()
Definition AudioPlayer.h:278
bool next(int offset=1)
Moves to the next/previous stream by offset (negative supported)
Definition AudioPlayer.h:313
void stop()
Halts playback; equivalent to setActive(false)
Definition AudioPlayer.h:307
bool previous(int offset=1)
Moves back by offset streams (defaults to 1)
Definition AudioPlayer.h:340
bool setPath(const char *path)
Selects stream by path without changing the source iterator.
Definition AudioPlayer.h:331
bool setIndex(int idx)
Selects stream by absolute index in the source.
Definition AudioPlayer.h:322
A simple OSC Data composer and parser. A OSC data starts with an address string followed by a format ...
Definition OSCData.h:56
bool parse(uint8_t *data, size_t len)
parse the data to start for reading
Definition OSCData.h:175
bool addCallback(const char *address, bool(*callback)(OSCData &data, void *ref), OSCCompare compare=OSCCompare::Matches)
register a parsing callback for a specific address matching string
Definition OSCData.h:337
void setReference(void *ref)
store a reference object (for callback)
Definition OSCData.h:334
const char * readString()
reads the next string
Definition OSCData.h:279
float readFloat()
reads the next attributes as float
Definition OSCData.h:248
void clear()
clears all data
Definition OSCData.h:158
void setAddress(const char *address)
Defines the address string (e.g. /test)
Definition OSCData.h:67
int32_t readInt32()
reads the next attribute as int32
Definition OSCData.h:254
Definition NoArduino.h:62
virtual size_t write(const uint8_t *data, size_t len)
Definition NoArduino.h:126
Audio Player OSC Receiver: Receives OSC messages from the AudioPlayerOSCSender and applies it to the ...
Definition RCAudioPlayerOSC.h:121
static bool setPath(OSCData &data, void *ref)
Definition RCAudioPlayerOSC.h:194
static bool previous(OSCData &data, void *ref)
Definition RCAudioPlayerOSC.h:184
static bool next(OSCData &data, void *ref)
Definition RCAudioPlayerOSC.h:178
bool is_active
Definition RCAudioPlayerOSC.h:163
static bool setVolume(OSCData &data, void *ref)
Definition RCAudioPlayerOSC.h:199
AudioPlayer * p_player
Definition RCAudioPlayerOSC.h:162
void registerCallbacks()
Definition RCAudioPlayerOSC.h:205
static bool setIndex(OSCData &data, void *ref)
Definition RCAudioPlayerOSC.h:189
static bool stop(OSCData &data, void *ref)
Definition RCAudioPlayerOSC.h:172
static bool play(OSCData &data, void *ref)
Definition RCAudioPlayerOSC.h:166
OSCData osc
Definition RCAudioPlayerOSC.h:164
Audio Player OSC Sender: Remote control for AudioPlayer class. Sends OSC messages to the RCAudioPlaye...
Definition RCAudioPlayerOSC.h:14
Print * p_out
Definition RCAudioPlayerOSC.h:111
Definition NoArduino.h:142
virtual size_t readBytes(uint8_t *data, size_t len)
Definition NoArduino.h:147
void stop()
stops any further processing by spinning in an endless loop
Definition AudioRuntime.h:12
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:512