Skip to content

Commit c6c7a65

Browse files
committed
RCAudioPlayerOSC: setActive()
1 parent 8ddd1a9 commit c6c7a65

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/AudioTools/Communication/RCAudioPlayerOSC.h

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,41 @@ namespace audio_tools {
1212
* @author Phil Schatzmann
1313
*/
1414
class RCAudioPlayerOSCSender {
15-
RCAudioPlayerOSCSender(Print &out) { p_out = &out; }
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;
1624

17-
void play() {
1825
uint8_t data[20];
1926
OSCData msg{data, sizeof(data)};
2027

2128
msg.setAddress("/play");
2229
msg.setFormat("");
2330
p_out->write(msg.data(), msg.size());
31+
return true;
2432
}
2533

2634
/// halts the playing: same as setActive(false)
27-
void stop() {
35+
bool stop() {
36+
if (p_out == nullptr) return false;
2837
uint8_t data[20];
2938
OSCData msg{data, sizeof(data)};
3039

3140
msg.setAddress("/stop");
3241
msg.setFormat("");
3342
p_out->write(msg.data(), msg.size());
43+
return true;
3444
}
3545

3646
/// moves to next file or nth next file when indicating an offset. Negative
3747
/// values are supported to move back.
3848
bool next(int offset = 1) {
49+
if (p_out == nullptr) return false;
3950
uint8_t data[80];
4051
OSCData msg{data, sizeof(data)};
4152

@@ -47,6 +58,7 @@ class RCAudioPlayerOSCSender {
4758
}
4859

4960
bool previous(int offset = 1) {
61+
if (p_out == nullptr) return false;
5062
uint8_t data[80];
5163
OSCData msg{data, sizeof(data)};
5264

@@ -59,6 +71,7 @@ class RCAudioPlayerOSCSender {
5971

6072
/// moves to the selected file position
6173
bool setIndex(int idx) {
74+
if (p_out == nullptr) return false;
6275
uint8_t data[80];
6376
OSCData msg{data, sizeof(data)};
6477

@@ -71,6 +84,7 @@ class RCAudioPlayerOSCSender {
7184

7285
/// Moves to the selected file w/o updating the actual file position
7386
bool setPath(const char *path) {
87+
if (p_out == nullptr) return false;
7488
uint8_t data[strlen(path) + 20];
7589
OSCData msg{data, sizeof(data)};
7690

@@ -82,6 +96,7 @@ class RCAudioPlayerOSCSender {
8296
}
8397

8498
bool setVolume(float volume) {
99+
if (p_out == nullptr) return false;
85100
uint8_t data[80];
86101
OSCData msg{data, sizeof(data)};
87102

@@ -104,11 +119,14 @@ class RCAudioPlayerOSCSender {
104119
* @author Phil Schatzmann
105120
*/
106121
class RCAudioPlayerOSCReceiver {
107-
RCAudioPlayerOSCReceiver(AudioPlayer &player) {
108-
p_player = &player;
109-
registerCallbacks();
122+
RCAudioPlayerOSCReceiver() { registerCallbacks(); }
123+
124+
RCAudioPlayerOSCReceiver(AudioPlayer &player) : RCAudioPlayerOSCReceiver() {
125+
setAudioPlayer(player);
110126
}
111127

128+
void setAudioPlayer(AudioPlayer &player) { p_player = &player; }
129+
112130
/// Process the incoming OSC messages
113131
bool processInputMessage(Stream &in) {
114132
if (!is_active) return false;

0 commit comments

Comments
 (0)