Skip to content

Commit 2d1b564

Browse files
committed
namespace
1 parent bea274d commit 2d1b564

9 files changed

+74
-42
lines changed

src/SnapClient.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "api/SnapOutput.h"
2323
#include "api/SnapProcessor.h"
2424

25+
namespace snap_arduino {
26+
2527
/**
2628
* @brief Snap Client for ESP32 Arduino
2729
* @author Phil Schatzmann
@@ -229,3 +231,5 @@ class SnapClient {
229231
#endif
230232
}
231233
};
234+
235+
}

src/SnapConfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,9 @@
6666

6767
#ifndef RTOS_TASK_PRIORITY
6868
# define RTOS_TASK_PRIORITY 2
69+
#endif
70+
71+
#ifndef SNAP_NO_NAMESPACE
72+
namespace snap_arduino {}
73+
using namespace snap_arduino;
6974
#endif

src/api/SnapCommon.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <iomanip>
88
#include <ctime>
99

10+
namespace snap_arduino {
11+
1012
enum codec_type { NO_CODEC, PCM, FLAC, OGG, OPUS };
1113
static const char *TAG="COMMON";
1214
/**
@@ -51,3 +53,4 @@ inline void logHeap() {
5153
#endif
5254
}
5355

56+
}

src/api/SnapOutput.h

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
#include "SnapTime.h"
1212
#include "SnapTimeSync.h"
1313

14+
namespace snap_arduino {
15+
1416
class SnapProcessor;
15-
class SnapProcessorRTOS;
1617

1718
/**
1819
* @brief Simple Output Class which uses the AudioTools to build an output chain
@@ -24,8 +25,6 @@ class SnapProcessorRTOS;
2425
**/
2526

2627
class SnapOutput : public AudioInfoSupport {
27-
friend SnapProcessor;
28-
friend SnapProcessorRTOS;
2928

3029
public:
3130
SnapOutput() = default;
@@ -121,6 +120,43 @@ class SnapOutput : public AudioInfoSupport {
121120

122121
bool isStarted() { return is_audio_begin_called; }
123122

123+
// writes the audio data to the decoder
124+
size_t audioWrite(const void *src, size_t size) {
125+
ESP_LOGD(TAG, "%zu", size);
126+
size_t result = decoder_stream.write((const uint8_t *)src, size);
127+
128+
return result;
129+
}
130+
131+
/// start to play audio only in valid server time: return false if to be
132+
/// ignored - update playback speed
133+
bool synchronizePlayback() {
134+
bool result = true;
135+
SnapTimeSync &ts = *p_snap_time_sync;
136+
137+
// calculate how long we need to wait to playback the audio
138+
auto delay_ms = getDelayMs();
139+
140+
if (!is_sync_started) {
141+
ts.begin(audio_info.sample_rate);
142+
143+
// start audio when first package in the future becomes valid
144+
result = synchronizeOnStart(delay_ms);
145+
} else {
146+
// provide the actual delay to the synch
147+
ts.updateActualDelay(delay_ms);
148+
149+
if (ts.isSync()) {
150+
// update speed
151+
float current_factor = playbackFactor();
152+
float new_factor = p_snap_time_sync->getFactor();
153+
if (new_factor != current_factor) {
154+
setPlaybackFactor(new_factor);
155+
}
156+
}
157+
}
158+
return result;
159+
}
124160

125161
protected:
126162
const char *TAG = "SnapOutput";
@@ -192,43 +228,6 @@ class SnapOutput : public AudioInfoSupport {
192228
out->end();
193229
}
194230

195-
// writes the audio data to the decoder
196-
size_t audioWrite(const void *src, size_t size) {
197-
ESP_LOGD(TAG, "%zu", size);
198-
size_t result = decoder_stream.write((const uint8_t *)src, size);
199-
200-
return result;
201-
}
202-
203-
/// start to play audio only in valid server time: return false if to be
204-
/// ignored - update playback speed
205-
bool synchronizePlayback() {
206-
bool result = true;
207-
SnapTimeSync &ts = *p_snap_time_sync;
208-
209-
// calculate how long we need to wait to playback the audio
210-
auto delay_ms = getDelayMs();
211-
212-
if (!is_sync_started) {
213-
ts.begin(audio_info.sample_rate);
214-
215-
// start audio when first package in the future becomes valid
216-
result = synchronizeOnStart(delay_ms);
217-
} else {
218-
// provide the actual delay to the synch
219-
ts.updateActualDelay(delay_ms);
220-
221-
if (ts.isSync()) {
222-
// update speed
223-
float current_factor = playbackFactor();
224-
float new_factor = p_snap_time_sync->getFactor();
225-
if (new_factor != current_factor) {
226-
setPlaybackFactor(new_factor);
227-
}
228-
}
229-
}
230-
return result;
231-
}
232231

233232
bool synchronizeOnStart(int delay_ms) {
234233
bool result = true;
@@ -258,4 +257,6 @@ class SnapOutput : public AudioInfoSupport {
258257
int delay_ms = diff_ms + p_snap_time_sync->getStartDelay();
259258
return delay_ms;
260259
}
261-
};
260+
};
261+
262+
}

src/api/SnapProcessor.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "SnapTime.h"
1010
#include "vector"
1111

12+
namespace snap_arduino {
13+
1214
/**
1315
* @brief Snap Processor implementation which does not rely on FreeRTOS
1416
* @author Phil Schatzmann
@@ -547,3 +549,5 @@ class SnapProcessor {
547549
return p_snap_output->writeHeader(header);
548550
}
549551
};
552+
553+
}

src/api/SnapProcessorRTOS.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include "SnapOutput.h"
33
#include "freertos-all.h" // https://github.com/pschatzmann/arduino-freertos-addons
44

5+
namespace snap_arduino {
6+
57
/**
68
* @brief Processor for which the encoded output is buffered in a queue in order to
79
* prevent any buffer underruns. A RTOS task feeds the output from the queue.
@@ -123,4 +125,6 @@ class SnapProcessorRTOS : public SnapProcessor {
123125
}
124126
};
125127

126-
SnapProcessorRTOS *SnapProcessorRTOS::self = nullptr;
128+
SnapProcessorRTOS *SnapProcessorRTOS::self = nullptr;
129+
130+
}

src/api/SnapProtocol.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#define TIME_MESSAGE_SIZE 8
1919
#define MAX_JSON_LEN 256
2020

21+
namespace snap_arduino {
22+
2123
/// @brief Buffer to read different data types
2224
struct SnapReadBuffer {
2325
const char *buffer;
@@ -457,3 +459,5 @@ struct SnapMessageTime {
457459
return result;
458460
}
459461
};
462+
463+
}

src/api/SnapTime.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <stdint.h>
55
#include <sys/time.h>
66

7+
namespace snap_arduino {
8+
79
/**
810
* @brief The the sys/time functions are used to represent the server time.
911
* The local time will be measured with the help of the Arduino millis() method.
@@ -118,3 +120,5 @@ class SnapTime {
118120
bool has_sntp_time = false;
119121

120122
};
123+
124+
}

src/api/SnapTimeSync.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "SnapLogger.h"
44
#include "SnapTime.h"
55

6+
namespace snap_arduino {
7+
68
/**
79
* @brief Abstract (Common) Time Synchronization Logic which consists of the
810
* startup synchronization and the local to server clock synchronization which
@@ -184,3 +186,4 @@ class SnapTimeSyncFixed : public SnapTimeSync {
184186
float resample_factor;
185187
};
186188

189+
}

0 commit comments

Comments
 (0)