shithub: mp3dec

Download patch

ref: be96391e230b8379ead22742b96fdb6c39f738a8
parent: 9fa044bfbbf0f5689e93368d19b75f2497847765
parent: a1b518e8dc10df442ca810d4fb96757042ce2301
author: Lion <lieff@users.noreply.github.com>
date: Thu Jan 18 16:36:25 EST 2018

Merge pull request #8 from bonki/bonki-fix-wave-output
 minimp3_test: Create WAVE based on output file extension

--- a/minimp3_test.c
+++ b/minimp3_test.c
@@ -3,8 +3,9 @@
 #include "minimp3.h"
 #include <stdio.h>
 #include <math.h>
+#include <string.h>
 
-/*static char *wav_header(int hz, int ch, int bips, int data_bytes)
+static char *wav_header(int hz, int ch, int bips, int data_bytes)
 {
     static char hdr[44] = "RIFFsizeWAVEfmt \x10\0\0\0\1\0ch_hz_abpsbabsdatasize";
     unsigned long nAvgBytesPerSec = bips*ch*hz >> 3;
@@ -19,19 +20,20 @@
     *(short *)(hdr + 0x22) = bips;
     *(int *  )(hdr + 0x28) = data_bytes;
     return hdr;
-}*/
+}
 
-static void decode_file(FILE *file_mp3, FILE *file_ref, FILE *file_out)
+static void decode_file(FILE *file_mp3, FILE *file_ref, FILE *file_out, const int wave_out)
 {
     static mp3dec_t mp3d;
     mp3dec_frame_info_t info;
-    int i, /*data_bytes, */samples, total_samples = 0, nbuf = 0, maxdiff = 0;
+    int i, data_bytes, samples, total_samples = 0, nbuf = 0, maxdiff = 0;
     double MSE = 0.0, psnr;
     unsigned char buf[4096];
 
     mp3dec_init(&mp3d);
 
-    //fwrite(wav_header(0, 0, 0, 0), 1, 44, file_wav);
+    if (wave_out)
+        fwrite(wav_header(0, 0, 0, 0), 1, 44, file_out);
 
     do
     {
@@ -72,9 +74,12 @@
         exit(1);
     }
 
-    //data_bytes = ftell(file_wav) - 44;
-    //rewind(file_wav);
-    //fwrite(wav_header(info.hz, info.channels, 16, data_bytes), 1, 44, file_wav);
+    if (wave_out)
+    {
+        data_bytes = ftell(file_out) - 44;
+        rewind(file_out);
+        fwrite(wav_header(info.hz, info.channels, 16, data_bytes), 1, 44, file_out);
+    }
     fclose(file_mp3);
     if (file_ref)
         fclose(file_ref);
@@ -87,11 +92,20 @@
     char *input_file_name  = (argc > 1) ? argv[1] : NULL;
     char *ref_file_name    = (argc > 2) ? argv[2] : NULL;
     char *output_file_name = (argc > 3) ? argv[3] : NULL;
+    int wave_out = 0;
+
+    if (output_file_name)
+    {
+        char *ext = strrchr(output_file_name, '.');
+        if (ext && !strcasecmp(ext+1, "wav"))
+            wave_out = 1;
+    }
+
     if (!input_file_name)
     {
         printf("error: no file names given\n");
         return 1;
     }
-    decode_file(fopen(input_file_name, "rb"), fopen(ref_file_name, "rb"), output_file_name ? fopen(output_file_name, "wb") : NULL);
+    decode_file(fopen(input_file_name, "rb"), fopen(ref_file_name, "rb"), output_file_name ? fopen(output_file_name, "wb") : NULL, wave_out);
     return 0;
 }