arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
RCAudioPlayerOSC.h
1
2#include "AudioTools/CoreAudio/AudioPlayer.h"
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
122 RCAudioPlayerOSCReceiver() { registerCallbacks(); }
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 }
150 osc.setReference(p_player);
151 is_active = true;
152 return true;
153 }
154
156 void end() {
157 is_active = false;
158 osc.clear();
159 }
160
161 protected:
162 AudioPlayer *p_player = nullptr;
163 bool is_active = false;
164 OSCData osc;
165
166 static bool play(OSCData &data, void *ref) {
167 AudioPlayer *p_player = (AudioPlayer *)ref;
168 p_player->play();
169 return true;
170 }
171
172 static bool stop(OSCData &data, void *ref) {
173 AudioPlayer *p_player = (AudioPlayer *)ref;
174 p_player->stop();
175 return true;
176 }
177
178 static bool next(OSCData &data, void *ref) {
179 AudioPlayer *p_player = (AudioPlayer *)ref;
180 int offset = data.readInt32();
181 return p_player->next(offset);
182 }
183
184 static bool previous(OSCData &data, void *ref) {
185 AudioPlayer *p_player = (AudioPlayer *)ref;
186 int offset = data.readInt32();
187 return p_player->previous(offset);
188 }
189 static bool setIndex(OSCData &data, void *ref) {
190 AudioPlayer *p_player = (AudioPlayer *)ref;
191 int idx = data.readInt32();
192 return p_player->setIndex(idx);
193 }
194 static bool setPath(OSCData &data, void *ref) {
195 AudioPlayer *p_player = (AudioPlayer *)ref;
196 const char *path = data.readString();
197 return p_player->setPath(path);
198 }
199 static bool setVolume(OSCData &data, void *ref) {
200 AudioPlayer *p_player = (AudioPlayer *)ref;
201 float volume = data.readFloat();
202 return p_player->setVolume(volume);
203 }
204
205 void registerCallbacks() {
206 osc.addCallback("/play", play, OSCCompare::StartsWith);
207 osc.addCallback("/stop", stop, OSCCompare::StartsWith);
208 osc.addCallback("/next", next, OSCCompare::StartsWith);
209 osc.addCallback("/previous", previous, OSCCompare::StartsWith);
210 osc.addCallback("/index", setIndex, OSCCompare::StartsWith);
211 osc.addCallback("/path", setPath, OSCCompare::StartsWith);
212 osc.addCallback("/volume", setVolume, OSCCompare::StartsWith);
213 }
214};
215
216} // namespace audio_tools
Implements a simple audio player which supports the following commands:
Definition AudioPlayer.h:38
bool setVolume(float volume) override
sets the volume - values need to be between 0.0 and 1.0
Definition AudioPlayer.h:359
void play()
starts / resumes the playing after calling stop(): same as setActive(true)
Definition AudioPlayer.h:247
bool next(int offset=1)
Definition AudioPlayer.h:284
void stop()
halts the playing: same as setActive(false)
Definition AudioPlayer.h:277
bool previous(int offset=1)
moves to previous file
Definition AudioPlayer.h:311
bool setPath(const char *path)
Moves to the selected file w/o updating the actual file position.
Definition AudioPlayer.h:302
bool setIndex(int idx)
moves to the selected file position
Definition AudioPlayer.h:293
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
Audio Player OSC Receiver: Receives OSC messages from the AudioPlayerOSCSender and applies it to the ...
Definition RCAudioPlayerOSC.h:121
Audio Player OSC Sender: Remote control for AudioPlayer class. Sends OSC messages to the RCAudioPlaye...
Definition RCAudioPlayerOSC.h:14
Definition NoArduino.h:142
void stop()
Public generic methods.
Definition AudioRuntime.h:14
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10