Skip to content

Commit d885ae6

Browse files
committed
Fix bug in SendDataInChunks
1 parent aafaf36 commit d885ae6

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

examples/SendDataInChunks.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ struct fake_device {
6969
auto elapsed_nano =
7070
std::chrono::duration_cast<std::chrono::nanoseconds>(now - last_time).count();
7171
int64_t elapsed_samples = std::size_t(elapsed_nano * srate * 1e-9); // truncate OK.
72-
elapsed_samples = std::min(elapsed_samples, (int64_t)buffer.size());
72+
elapsed_samples = std::min(elapsed_samples, (int64_t)(buffer.size() / n_channels));
7373
if (false) {
7474
// The fastest but no patterns.
7575
memset(&buffer[0], 23, buffer.size() * sizeof buffer[0]);
@@ -119,19 +119,18 @@ int main(int argc, char **argv) {
119119
fake_device my_device(n_channels, (float)samplingrate);
120120

121121
// Prepare buffer to get data from 'device'.
122-
// The buffer should be largery than you think you need. Here we make it twice as large.
123-
std::vector<int16_t> chunk_buffer(2 * chunk_samples * n_channels);
122+
// The buffer should be larger than you think you need. Here we make it 4x as large.
123+
std::vector<int16_t> chunk_buffer(4 * chunk_samples * n_channels);
124124

125125
std::cout << "Now sending data..." << std::endl;
126126

127127
// Your device might have its own timer. Or you can decide how often to poll
128128
// your device, as we do here.
129-
auto nextsample = std::chrono::high_resolution_clock::now();
130-
uint64_t sample_counter = 0;
129+
auto next_chunk_time = std::chrono::high_resolution_clock::now();
131130
for (unsigned c = 0;; c++) {
132131
// wait a bit
133-
nextsample += std::chrono::milliseconds(chunk_duration);
134-
std::this_thread::sleep_until(nextsample);
132+
next_chunk_time += std::chrono::milliseconds(chunk_duration);
133+
std::this_thread::sleep_until(next_chunk_time);
135134

136135
// Get data from device
137136
std::size_t returned_samples = my_device.get_data(chunk_buffer);

0 commit comments

Comments
 (0)