From f3c15c402f137eff233096ff731c27bc24fcb779 Mon Sep 17 00:00:00 2001
From: Paul Kaplan <pkaplan@media.mit.edu>
Date: Mon, 14 Jan 2019 12:17:52 -0500
Subject: [PATCH] Prevent auto-trim from producing invalid start and stop
 points

---
 src/lib/audio/audio-recorder.js | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/lib/audio/audio-recorder.js b/src/lib/audio/audio-recorder.js
index 2d1bcb91d..b452b1f26 100644
--- a/src/lib/audio/audio-recorder.js
+++ b/src/lib/audio/audio-recorder.js
@@ -96,8 +96,14 @@ class AudioRecorder {
             }
         }
 
-        const trimStart = Math.max(2, firstChunkAboveThreshold - 2) / this.buffers.length;
-        const trimEnd = Math.min(this.buffers.length - 2, lastChunkAboveThreshold + 2) / this.buffers.length;
+        let trimStart = Math.max(2, firstChunkAboveThreshold - 2) / this.buffers.length;
+        let trimEnd = Math.min(this.buffers.length - 2, lastChunkAboveThreshold + 2) / this.buffers.length;
+
+        // With very few samples, the automatic trimming can produce invalid values
+        if (trimStart >= trimEnd) {
+            trimStart = 0;
+            trimEnd = 1;
+        }
 
         const buffer = new Float32Array(this.buffers.length * this.bufferLength);
 
-- 
GitLab