shithub: opus

Download patch

ref: 14fb264a0fce2cea7af26d43ecb0454536d049a0
parent: 8f887b62f0dfd216cc2423f27c9eb7c6e3c7e363
author: Jean-Marc Valin <jmvalin@jmvalin.ca>
date: Sat Jun 20 19:47:04 EDT 2020

Fix sampling bug for 16-bit rand()

According to David Rowe, when rand() returns RAND_MAX (which is likely
for 16-bit output), we end up producing a click.

--- a/dnn/nnet.c
+++ b/dnn/nnet.c
@@ -392,10 +392,10 @@
         tmp[i] = tmp[i-1] + MAX16(0, norm*tmp[i] - pdf_floor);
     }
     /* Do the sampling (from the cdf). */
-    r = tmp[N-1] * ((float)rand()/RAND_MAX);
+    r = tmp[N-1] * ((rand()+.5f)/(RAND_MAX+1.f));
     for (i=0;i<N-1;i++)
     {
-        if (r < tmp[i]) return i;
+        if (r <= tmp[i]) return i;
     }
     return N-1;
 }
--