shithub: dumb

Download patch

ref: 373f8d780bb7bc51af402304d27110a453d5f485
parent: 4ddca9e4a811be15806a53bbfea28a80d45c4f20
author: Tuomas Virtanen <katajakasa@gmail.com>
date: Sat Dec 12 17:28:31 EST 2015

dumbout: Implement delay switch + cleanups

--- a/dumb/cmake/CMakeLists.txt
+++ b/dumb/cmake/CMakeLists.txt
@@ -119,7 +119,7 @@
 
 if(BUILD_EXAMPLES)
     add_executable(dumbout ../examples/dumbout.c)
-    target_link_libraries(dumbout ${ARGTABLE2_LIBRARY} dumb)
+    target_link_libraries(dumbout ${ARGTABLE2_LIBRARY} m dumb)
     include_directories(${ARGTABLE2_INCLUDE_DIR} "../examples/")
     set(EXAMPLE_TARGETS ${EXAMPLE_TARGETS} "dumbout")
 endif()
--- a/dumb/examples/dumbout.c
+++ b/dumb/examples/dumbout.c
@@ -3,6 +3,7 @@
 #include <stdint.h>
 #include <string.h>
 #include <stdbool.h>
+#include <math.h>
 
 static const int endian_test = 1;
 #define is_bigendian() ((*(char*)&endian_test) == 0)
@@ -177,7 +178,6 @@
         }
     }
 
-    // Loop as long as we have data to play
     bool run = true;
     char *buffer = malloc(streamer.bufsize);
     int read_samples;
@@ -185,8 +185,45 @@
 
     // If output endianness is different than machine endianness, and output is 16 bits, reorder bytes.
     int switch_endianness = ((is_bigendian() && settings.endianness == LITTLE_ENDIAN) ||
-                            (!is_bigendian() && settings.endianness == BIG_ENDIAN)) && settings.bits == 16;
+                            (!is_bigendian() && settings.endianness == BIG_ENDIAN));
 
+    // Write the initial delay to the file if one was requested.
+    long d = ((long)floor(settings.delay * settings.freq + 0.5f)) * settings.n_channels * (settings.bits / 8);
+    if(d) {
+        // Fill the buffer with silence. Remember to take into account endianness
+        if(settings.is_unsigned) {
+            if(settings.bits == 16) {
+                if(settings.endianness == BIG_ENDIAN) {
+                    // Unsigned 16bits big endian
+                    for(int i = 0; i < streamer.bufsize; i += 2) {
+                        buffer[i  ] = (char)0x80;
+                        buffer[i+1] = (char)0x00;
+                    }
+                } else {
+                    // Unsigned 16bits little endian
+                    for(int i = 0; i < streamer.bufsize; i += 2) {
+                        buffer[i  ] = (char)0x00;
+                        buffer[i+1] = (char)0x80;
+                    }
+                }
+            } else {
+                // Unsigned 8 bits
+                memset(buffer, 0x80, streamer.bufsize);
+            }
+        } else {
+            // Signed
+            memset(buffer, 0, streamer.bufsize);
+        }
+
+        while(d >= streamer.bufsize) {
+            fwrite(buffer, 1, streamer.bufsize, streamer.dst);
+            d -= streamer.bufsize;
+        }
+        if(d) {
+            fwrite(buffer, 1, d, streamer.dst);
+        }
+    }
+
     // Loop until we have nothing to loop through. Dumb will stop giving out bytes when the file is done.
     while(run) {
         read_samples = duh_render(streamer.renderer,
@@ -198,7 +235,7 @@
         read_bytes = read_samples * (settings.bits / 8) * settings.n_channels;
 
         // switch endianness if required
-        if(switch_endianness) {
+        if(switch_endianness && settings.bits == 16) {
             char tmp;
             for(int i = 0; i < read_bytes / 2; i++) {
                 tmp = buffer[i*2+0];