diff --git a/src/main.cpp b/src/main.cpp
index d99fb45c08772a7bc9b33be160ebe2b2789f4d29..0eb4009721e0af7320721daccd52c8f424a2d9b3 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);