-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampleStream.cpp
More file actions
64 lines (52 loc) · 1.67 KB
/
Copy pathSampleStream.cpp
File metadata and controls
64 lines (52 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// This file is part of the Open Audio Live System project, a live audio environment
// Copyright (c) 2026 - Mathis DELGADO
//
// This project is distributed under the Creative Commons CC-BY-NC-SA licence. https://creativecommons.org/licenses/by-nc-sa/4.0
#include "SampleStream.h"
SampleStream::SampleStream() {
m_current_delay = 0;
m_delay_counter = 0;
}
void SampleStream::insert_packet(AudioPacket &pck) {
for (auto& e : pck.packet_data.samples) {
m_sample_buffer.enqueue(e);
}
}
float SampleStream::pull_sample() {
if ((m_sample_buffer.size_approx() == 0) || (m_delay_counter != 0)) {
// Delay management
if (m_delay_counter > 0) {
m_delay_counter--;
}
return 0.0f;
}
float oldest_sample = 0.0f;
m_sample_buffer.try_dequeue(oldest_sample);
return oldest_sample;
}
bool SampleStream::can_pull() {
return m_sample_buffer.size_approx() > 0;
}
size_t SampleStream::queue_size() {
return m_sample_buffer.size_approx();
}
void SampleStream::time_align(int nsample) {
// We use the delta between old delay and new delay to know how much
// we have to increase delay (by inserting zeros) or deleting samples in stream
int delta_delay = nsample - m_current_delay;
m_current_delay = nsample;
if (delta_delay > 0) {
// Zero padding
m_delay_counter = nsample;
} else {
// Sample removal
for (int i = 0; i < std::abs(delta_delay); i++) {
if (m_sample_buffer.size_approx() > 0) {
float dummy = 0.0f;
m_sample_buffer.try_dequeue(dummy);
} else {
break;
}
}
}
}