@john33
The current version has still some subtle issues. Here is a patch which fixes these and some portability issues. (You really should copnsider developing in Linux using valgrind or at least use a similar tool in Windows - but I don't know of any free one...)
The patch
diff -urd wv126o/audio.c wavegain-1.2.6/audio.c
--- wv126o/audio.c 2005-11-05 15:25:26.000000000 +0100
+++ wavegain-1.2.6/audio.c 2005-11-17 10:38:47.965415424 +0100
@@ -30,7 +30,11 @@
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
+#ifdef _WIN32
#include <io.h>
+#else
+#include <sys/io.h>
+#endif
#include <fcntl.h>
#include <malloc.h>
#include <math.h>
@@ -416,6 +420,8 @@
if (format.format == 1) {
samplesize = format.samplesize/8;
opt->read_samples = wav_read;
+ /* works with current enum */
+ opt->format = samplesize;
}
else if (format.format == 3) {
samplesize = 4;
Nur in wavegain-1.2.6: CMakeLists.txt.
diff -urd wv126o/main.c wavegain-1.2.6/main.c
--- wv126o/main.c 2005-11-15 18:08:24.000000000 +0100
+++ wavegain-1.2.6/main.c 2005-11-17 10:39:36.812989464 +0100
@@ -66,8 +66,7 @@
node = calloc(1, sizeof(*node));
if (node != NULL) {
-// node->filename = strdup(file);
- node->filename = _strdup(file);
+ node->filename = strdup(file);
if (node->filename != NULL) {
node->track_peak = NO_PEAK;
Nur in wavegain-1.2.6: Makefile.
diff -urd wv126o/misc.h wavegain-1.2.6/misc.h
--- wv126o/misc.h 2004-03-05 13:02:24.000000000 +0100
+++ wavegain-1.2.6/misc.h 2005-11-17 10:35:13.575007720 +0100
@@ -4,6 +4,9 @@
#if defined _WIN32 && !defined __MINGW32__ // Microsoft and Intel call it __int64
typedef signed __int64 Int64_t;
typedef unsigned __int64 Uint64_t;
+#define chdir _chdir
+#define strdup _strdup
+#define getcwd _getcwd
#else
typedef signed long long Int64_t;
typedef unsigned long long Uint64_t;
diff -urd wv126o/recurse.c wavegain-1.2.6/recurse.c
--- wv126o/recurse.c 2005-11-12 12:29:32.000000000 +0100
+++ wavegain-1.2.6/recurse.c 2005-11-17 10:40:18.380670216 +0100
@@ -430,8 +430,7 @@
char* old_path;
int result = -1;
-// old_path = getcwd(NULL, 1024);
- old_path = _getcwd(NULL, 1024);
+ old_path = getcwd(NULL, 1024);
if (old_path == NULL) {
file_error(_("Couldn't get name of current directory: "));
@@ -453,8 +452,7 @@
strcat(full_path, path);
-// if (chdir(path) == 0) {
- if (_chdir(path) == 0) {
+ if (chdir(path) == 0) {
DIRECTORY* directory;
FILE_LIST* file_list = NULL;
@@ -500,8 +498,7 @@
free_list(file_list);
-// if ((result == 0) && (chdir(old_path) != 0)) {
- if ((result == 0) && (_chdir(old_path) != 0)) {
+ if ((result == 0) && (chdir(old_path) != 0)) {
file_error(_("Couldn't go back to folder '%s': "), old_path);
result = 0;
}
@@ -528,8 +525,7 @@
*/
int process_argument(const char* path, SETTINGS* settings)
{
-// char* buffer = strdup(path);
- char* buffer = _strdup(path);
+ char* buffer = strdup(path);
char* my_path;
int my_path_len;
int dir;
diff -urd wv126o/wavegain.c wavegain-1.2.6/wavegain.c
--- wv126o/wavegain.c 2005-11-15 22:03:58.000000000 +0100
+++ wavegain-1.2.6/wavegain.c 2005-11-17 10:47:53.851428136 +0100
@@ -21,12 +21,15 @@
#include <math.h>
#include <string.h>
#include <ctype.h>
+#ifdef _WIN32
#include <io.h>
+#else
+#include <sys/io.h>
+#endif
#include <fcntl.h>
#include <malloc.h>
#include "gain_analysis.h"
#include "i18n.h"
-#include "config.h"
#include "getopt.h"
#include "misc.h"
#include "audio.h"
@@ -559,8 +562,8 @@
for (i = 0; i < wg_opts->channels; i++)
if (pcm[i]) free(pcm[i]);
if (pcm) free(pcm);
- close_audio_file(infile, aufile, wg_opts);
format->close_func(wg_opts->readdata);
+ close_audio_file(infile, aufile, wg_opts);
fclose(infile);
if (!settings->std_out) {
What it fixes:
diff -urd wv126o/audio.c wavegain-1.2.6/audio.c
--- wv126o/audio.c 2005-11-05 15:25:26.000000000 +0100
+++ wavegain-1.2.6/audio.c 2005-11-17 10:38:47.965415424 +0100
@@ -416,6 +420,8 @@
if (format.format == 1) {
samplesize = format.samplesize/8;
opt->read_samples = wav_read;
+ /* works with current enum */
+ opt->format = samplesize;
}
else if (format.format == 3) {
samplesize = 4;
You should really check the wav_open and aiff_open functon whether they set ALL values of opt. Currently it is not the case and above was the one I tracked. Otherwise you read uninitialised values leading to non-deterministic (and crashing-prone) behaviour. I think above is what was intended.
--- wv126o/wavegain.c 2005-11-15 22:03:58.000000000 +0100
+++ wavegain-1.2.6/wavegain.c 2005-11-17 10:47:53.851428136 +0100
@@ -21,12 +21,15 @@
#include "gain_analysis.h"
#include "i18n.h"
-#include "config.h"
#include "getopt.h"
#include "misc.h"
#include "audio.h"
It was really a bad idea to include config.h w/o the HAVE_CONFIG_H define check only here. This breaks in a subtle way if you look at the settings struct... (In one object ENABLE_RECURSION is defined, in the other not -> havoc.)
So either put config.h in *every* file or leave it like this. Just pass HAVE_CONFIG_H to your compiler, which would be the best thing and everything should run nicely.
@@ -559,8 +562,8 @@
for (i = 0; i < wg_opts->channels; i++)
if (pcm[i]) free(pcm[i]);
if (pcm) free(pcm);
- close_audio_file(infile, aufile, wg_opts);
format->close_func(wg_opts->readdata);
+ close_audio_file(infile, aufile, wg_opts);
fclose(infile);
if (!settings->std_out) {
Here the order is important otherwise wg_opts will be freed and you later try to access an invalid pointer.
The rest are portability patches, which should make wavegain compilable on unix and windows w/o further defines, please check if this is the case.
This is the updated CMakeLists.tst file:
# cmake project file by Prakash Punnoor
PROJECT(Wavegain)
ADD_DEFINITIONS(-DHAVE_CONFIG_H)
ADD_EXECUTABLE(wavegain
audio.c
dither.c
gain_analysis.c
getopt1.c
getopt.c
main.c
misc.c
recurse.c
wavegain.c
)
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-lm ")
INSTALL_TARGETS(/bin wavegain)
Again I have not checked every possible input and path in wavegain, just analysed and applied it to a pcm wav.
BTW, the issues are not linux only. Most of the time you just won't see the problems in Windows and linux as the program seems to behave normaly (but not always) - so these bugs are hard to track w/o a help of a tool like valgrind...