39 for (power_of_two = 1; 1U << power_of_two < sz; ++power_of_two);
40 return 1U << power_of_two;
139 size_t read (T *dest,
size_t cnt,
bool commit =
true,
size_t offset = 0);
142 size_t write (T
const * src,
size_t cnt);
160 cnt = std::min (cnt, res);
187 }
else if (cnt < 0) {
188 return reserved.load () >= (size_t) -cnt;
215 template<
class T>
size_t
218 size_t w = write_idx.load ();
219 const size_t free_cnt = write_space ();
225 const size_t to_write = cnt > free_cnt ? free_cnt : cnt;
226 const size_t cnt2 = w + to_write;
231 n2 = cnt2 & size_mask;
237 memcpy (&buf[w], src, n1 *
sizeof (T));
238 w = (w + n1) & size_mask;
241 memcpy (buf, src+n1, n2 *
sizeof (T));
249 template<
class T>
size_t
252 size_t w = write_idx.load ();
253 const size_t free_cnt = write_space ();
259 const size_t to_write = cnt > free_cnt ? free_cnt : cnt;
260 const size_t cnt2 = w + to_write;
265 n2 = cnt2 & size_mask;
271 memset (&buf[w], 0, n1 *
sizeof (T));
272 w = (w + n1) & size_mask;
275 memset (buf, 0, n2 *
sizeof (T));
283 template<
class T>
size_t
292 size_t r = read_idx.load ();
293 const size_t w = write_idx.load ();
295 size_t free_cnt = (w > r) ? (w - r) : ((w - r + size) & size_mask);
297 if (!commit && offset > 0) {
298 if (offset > free_cnt) {
302 r = (r + offset) & size_mask;
305 const size_t to_read = cnt > free_cnt ? free_cnt : cnt;
307 const size_t cnt2 = r + to_read;
312 n2 = cnt2 & size_mask;
318 memcpy (dest, &buf[r], n1 *
sizeof (T));
319 r = (r + n1) & size_mask;
322 memcpy (dest + n1, buf, n2 *
sizeof (T));
329 reserved.store (std::min (reservation, reserved.load () + to_read));
spinlock_t _reservation_lock
bool can_seek(int64_t cnt)
size_t reservation_size() const
virtual ~PlaybackBuffer()
static size_t power_of_two_size(size_t sz)
Glib::Threads::Mutex _reset_lock
size_t decrement_read_ptr(size_t cnt)
std::atomic< size_t > read_idx
std::atomic< size_t > write_idx
size_t read(T *dest, size_t cnt, bool commit=true, size_t offset=0)
size_t read_space() const
size_t increment_read_ptr(size_t cnt)
std::atomic< size_t > reserved
PlaybackBuffer(size_t sz, size_t res=8191)
size_t write(T const *src, size_t cnt)
size_t write_zero(size_t cnt)
size_t reserved_size() const
size_t write_space() const
void align_to(PlaybackBuffer const &other)
size_t overwritable_at(size_t r) const
size_t increment_write_ptr(size_t cnt)
void memset(float *data, const float val, const uint32_t n_samples)