From 2ac967fff1b227259b1898b5c412bf0f1b672cd3 Mon Sep 17 00:00:00 2001
From: ferrari <maxence.ferrari@gmail.com>
Date: Wed, 4 May 2022 16:31:39 +0200
Subject: [PATCH] Fix total length option

---
 src/main.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/main.cpp b/src/main.cpp
index d99fb45..0eb4009 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -66,6 +66,7 @@ int record(size_t channels, size_t rate, size_t depth, size_t filter,  std::stri
         size_t total_samples_wanted = totallen * rate;
         size_t total_samples_read = 0;
         size_t failed_attempts = 0;
+        size_t sample_size = channels * depth;
         std::vector<std::uint8_t> samples;
         try {
             std::cout << "Setting recording format to " << channels << " channels at " << rate << " Hz " << (1<<(2+depth)) << " bits" << std::endl;
@@ -78,10 +79,10 @@ int record(size_t channels, size_t rate, size_t depth, size_t filter,  std::stri
                 }
                 recorder.get_samples(samples, false, 500);
                 if (samples.size() > 0) {
-                    total_samples_read += samples.size() / channels;
+                    total_samples_read += samples.size() / sample_size;
                     // if we have too much now, crop the last packet
                     if ((total_samples_wanted > 0) && (total_samples_read > total_samples_wanted)) {
-                        samples.resize(samples.size() - (total_samples_read - total_samples_wanted) * channels);
+                        samples.resize(samples.size() - (total_samples_read - total_samples_wanted) * sample_size);
                     }
                     // pass it on to the file writer
                     filewriter->write(samples);
-- 
GitLab