breakfastquay / rubberband

Official mirror of Rubber Band Library, an audio time-stretching and pitch-shifting library.
http://breakfastquay.com/rubberband/
GNU General Public License v2.0
561 stars 89 forks source link

TestFFT build failure in Windows #65

Closed Biswa96 closed 1 year ago

Biswa96 commented 2 years ago
meson \
--wrap-mode=nodownload \
--default-library=both \
--auto-features=enabled \
-Dfft=fftw \
-Dresampler=libsamplerate \
build
ninja -C build/
../src/test/TestFFT.cpp: In function 'void TestFFT::performTest_random()':
../src/test/TestFFT.cpp:702:5: error: 'srand48' was not declared in this scope; did you mean 'srand'?
  702 |     srand48(0);
      |     ^~~~~~~
      |     srand
../src/test/TestFFT.cpp:704:17: error: 'drand48' was not declared in this scope; did you mean 'srand'?
  704 |         in[i] = drand48() * 4.0 - 2.0;
      |                 ^~~~~~~
      |                 srand
../src/finer/../common/sysutils.h:114:22: warning: statement has no effect [-Wunused-value]
  114 | #define MUNLOCK(a,b) 1
      |                      ^
../src/finer/../common/RingBuffer.h:230:9: note: in expansion of macro 'MUNLOCK'
  230 |         MUNLOCK((void *)m_buffer, m_size * sizeof(T));
      |         ^~~~~~~
cannam commented 2 years ago

Without having seen this myself, can I ask - does adding

#include <cstdlib>

near the top of TestFFT.cpp - next to the existing cstdio and cmath includes - fix it?

Biswa96 commented 2 years ago

Tried it but same error happens. The issue is srand48 and drand48 functions are not available in Windows platform. I have tried to replace those with srand and rand respectively. Here is the diff

--- a/src/test/TestFFT.cpp
+++ b/src/test/TestFFT.cpp
@@ -699,9 +699,9 @@ ALL_IMPL_AUTO_TEST_CASE(random)
     double *re_compare = new double[n/2 + 1];
     double *im_compare = new double[n/2 + 1];
     double *back = new double[n];
-    srand48(0);
+    srand(0);
     for (int i = 0; i < n; ++i) {
-        in[i] = drand48() * 4.0 - 2.0;
+        in[i] = rand() * 4.0 - 2.0;
     }
     USING_FFT(n);
     if (eps < 1e-11) {

But the test failed after that modification. The log contains something like this:

../src/test/TestFFT.cpp(716): error: in "TestFFT/random_fftw": absolute value of back[cmp_i]/n - in[cmp_i]{-2.1827872842550278e-11} exceeds 9.9999999999999994e-12

drand48 and rand are not exactly equivalent. Those return double and int respectively.

cannam commented 2 years ago

OK, thanks. I'll take a look.

Biswa96 commented 2 years ago

Found this workaround. Build and test both succeed.

--- a/src/test/TestFFT.cpp
+++ b/src/test/TestFFT.cpp
@@ -33,6 +33,11 @@
 #include <cstdio>
 #include <cmath>

+#ifdef _WIN32
+#define srand48(x) srand(x)
+#define drand48() ((double)(rand() / (RAND_MAX + 1.0)))
+#endif
+
 using namespace RubberBand;

 BOOST_AUTO_TEST_SUITE(TestFFT)
cannam commented 1 year ago

Should be fixed in the repo now - I did something similar except it's not conditional for Windows, since we don't really care enough to use drand48 expressly (as opposed to rand) on any platform.