Skip to content

Commit

Permalink
Imported Upstream version 1.2.8
Browse files Browse the repository at this point in the history
  • Loading branch information
MestreLion committed Oct 5, 2012
1 parent e6eda17 commit 274a3ab
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 47 deletions.
12 changes: 8 additions & 4 deletions WaveGain/WaveGain.sln
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WaveGain", "WaveGain.vcproj", "{519BA429-0A0D-44F3-98E0-0C47D9A45F70}"
Project("{EAF909A5-FA59-4C3D-9431-0FCC20D5BCF9}") = "WaveGain", "WaveGain.icproj", "{0CD1B744-6DF0-4891-AC3D-130D38F56D3D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Release|Win32 = Release|Win32
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{519BA429-0A0D-44F3-98E0-0C47D9A45F70}.Debug|Win32.ActiveCfg = Debug|Win32
{519BA429-0A0D-44F3-98E0-0C47D9A45F70}.Debug|Win32.Build.0 = Debug|Win32
{519BA429-0A0D-44F3-98E0-0C47D9A45F70}.Release|Win32.ActiveCfg = Release|Win32
{0CD1B744-6DF0-4891-AC3D-130D38F56D3D}.Debug|Win32.ActiveCfg = Debug|Win32
{0CD1B744-6DF0-4891-AC3D-130D38F56D3D}.Debug|Win32.Build.0 = Debug|Win32
{0CD1B744-6DF0-4891-AC3D-130D38F56D3D}.Release|Win32.ActiveCfg = Release|Win32
{0CD1B744-6DF0-4891-AC3D-130D38F56D3D}.Release|Win32.Build.0 = Release|Win32
{519BA429-0A0D-44F3-98E0-0C47D9A45F70}.Release|Win32.Build.0 = Release|Win32
{519BA429-0A0D-44F3-98E0-0C47D9A45F70}.Release|Win32.ActiveCfg = Release|Win32
{519BA429-0A0D-44F3-98E0-0C47D9A45F70}.Debug|Win32.Build.0 = Debug|Win32
{519BA429-0A0D-44F3-98E0-0C47D9A45F70}.Debug|Win32.ActiveCfg = Debug|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
119 changes: 79 additions & 40 deletions audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,21 @@ input_format formats[] = {
{NULL, 0, NULL, NULL, NULL, NULL}
};

extern int __cdecl _fseeki64(FILE *, __int64, int);
extern __int64 __cdecl _ftelli64(FILE *);
#if (defined (WIN32) || defined (_WIN32))
extern int __cdecl _fseeki64(FILE *, Int64_t, int);
extern Int64_t __cdecl _ftelli64(FILE *);

#define FSEEK64 _fseeki64
#define FTELL64 _ftelli64
#else
#define FSEEK64 fseeko
#define FTELL64 ftello
#endif

/* Global */
__int64 current_pos_t;
Int64_t current_pos_t;

#if (defined (WIN32) || defined (_WIN32))
__inline long int lrint(double flt)
{
int intgr;
Expand All @@ -96,6 +105,37 @@ __inline long int lrint(double flt)

return intgr;
}
#elif (defined (__MACOSX__))
#define lrint double2int
inline static long
double2int (register double in)
{ int res [2] ;

__asm__ __volatile__
( "fctiw %1, %1\n\t"
"stfd %1, %0"
: "=m" (res) /* Output */
: "f" (in) /* Input */
: "memory"
) ;

return res [1] ;
}
#else
#define lrint double2int
static inline long double2int (double in)
{ long retval ;

__asm__ __volatile__
( "fistpl %0"
: "=m" (retval)
: "t" (in)
: "st"
) ;

return retval ;
}
#endif

double read_d64_be(unsigned char *cptr)
{
Expand Down Expand Up @@ -281,14 +321,14 @@ input_format *open_audio_file(FILE *in, wavegain_opt *opt)
return NULL;
}

static int seek_forward(FILE *in, __int64 length)
static int seek_forward(FILE *in, Int64_t length)
{
if (_fseeki64(in, length, SEEK_CUR)) {
if (FSEEK64(in, length, SEEK_CUR)) {
/* Failed. Do it the hard way. */
unsigned char buf[1024];
int seek_needed = length, seeked;
while (seek_needed > 0) {
seeked = fread(buf, 1, seek_needed>1024?1024:seek_needed, in);
seeked = fread(buf, 1, seek_needed > 1024 ? 1024:seek_needed, in);
if (!seeked)
return 0; /* Couldn't read more, can't read file */
else
Expand All @@ -299,7 +339,7 @@ static int seek_forward(FILE *in, __int64 length)
}


static int find_wav_chunk(FILE *in, char *type, __int64 *len)
static int find_wav_chunk(FILE *in, char *type, Int64_t *len)
{
unsigned char buf[8];

Expand All @@ -325,7 +365,7 @@ static int find_wav_chunk(FILE *in, char *type, __int64 *len)
}
}

static int find_gain_chunk(FILE *in, __int64 *len)
static int find_gain_chunk(FILE *in, Int64_t *len)
{
unsigned char buf[8];

Expand Down Expand Up @@ -548,8 +588,8 @@ int wav_id(unsigned char *buf, int len)
int wav_open(FILE *in, wavegain_opt *opt, unsigned char *oldbuf, int buflen)
{
unsigned char buf[16];
__int64 len;
__int64 current_pos;
Int64_t len;
Int64_t current_pos;
int samplesize;
wav_fmt format;
wavfile *wav = malloc(sizeof(wavfile));
Expand Down Expand Up @@ -597,17 +637,16 @@ int wav_open(FILE *in, wavegain_opt *opt, unsigned char *oldbuf, int buflen)
format.align = READ_U16_LE(buf+12);
format.samplesize = READ_U16_LE(buf+14);

current_pos = _ftelli64(in);
if (!find_gain_chunk(in, &len))
_fseeki64(in, current_pos, SEEK_SET);
else {
char buf_double[8];
opt->gain_chunk = 1;
fread(buf_double, 1, 8, in);
opt->gain_scale = READ_D64(buf_double);
// opt->gain_scale = *(double*)buf_double;
//fprintf(stderr, "opt->gain_scale = %lf\n", opt->gain_scale);
// return 0;
if (!opt->std_in) {
current_pos = FTELL64(in);
if (!find_gain_chunk(in, &len))
FSEEK64(in, current_pos, SEEK_SET);
else {
char buf_double[8];
opt->gain_chunk = 1;
fread(buf_double, 1, 8, in);
opt->gain_scale = READ_D64(buf_double);
}
}

if (!find_wav_chunk(in, "data", &len)) {
Expand All @@ -616,16 +655,16 @@ int wav_open(FILE *in, wavegain_opt *opt, unsigned char *oldbuf, int buflen)
}

if (opt->apply_gain) {
current_pos = _ftelli64(in);
current_pos = FTELL64(in);
current_pos_t = current_pos + len;
_fseeki64(in, 0, SEEK_SET);
FSEEK64(in, 0, SEEK_SET);
if ((opt->header = malloc(sizeof(char) * current_pos)) == NULL)
fprintf(stderr, "Error: unable to allocate memory for header\n");
else {
opt->header_size = current_pos;
fread(opt->header, 1, opt->header_size, in);
}
_fseeki64(in, current_pos, SEEK_SET);
FSEEK64(in, current_pos, SEEK_SET);
}

if (format.format == 1) {
Expand Down Expand Up @@ -668,13 +707,13 @@ int wav_open(FILE *in, wavegain_opt *opt, unsigned char *oldbuf, int buflen)
if (len)
opt->total_samples_per_channel = len/(format.channels*samplesize);
else {
__int64 pos;
pos = _ftelli64(in);
if (_fseeki64(in, 0, SEEK_END) == -1)
Int64_t pos;
pos = FTELL64(in);
if (FSEEK64(in, 0, SEEK_END) == -1)
opt->total_samples_per_channel = 0; /* Give up */
else {
opt->total_samples_per_channel = (_ftelli64(in) - pos)/(format.channels * samplesize);
_fseeki64(in, pos, SEEK_SET);
opt->total_samples_per_channel = (FTELL64(in) - pos)/(format.channels * samplesize);
FSEEK64(in, pos, SEEK_SET);
}
}
wav->totalsamples = opt->total_samples_per_channel;
Expand All @@ -701,7 +740,7 @@ long wav_read(void *in, double **buffer, int samples, int fast, int chunk)
if (fast) {
chunk /= (sampbyte * f->channels);
chunk *= (sampbyte * f->channels);
_fseeki64(f->f, chunk, SEEK_SET);
FSEEK64(f->f, chunk, SEEK_SET);
}

bytes_read = fread(buf, 1, samples * sampbyte * f->channels, f->f);
Expand Down Expand Up @@ -801,7 +840,7 @@ long wav_ieee_read(void *in, double **buffer, int samples, int fast, int chunk)
if (fast) {
chunk /= (sizeof(float) * f->channels);
chunk *= (sizeof(float) * f->channels);
_fseeki64(f->f, chunk, SEEK_SET);
FSEEK64(f->f, chunk, SEEK_SET);
}

bytes_read = fread(buf, 1, samples * 4 * f->channels, f->f);
Expand Down Expand Up @@ -924,24 +963,24 @@ int write_audio_file(audio_file *aufile, void *sample_buffer, int samples)
void close_audio_file( FILE *in, audio_file *aufile, wavegain_opt *opt)
{
unsigned char *ch;
__int64 pos;
Int64_t pos;

if (!opt->std_out) {

switch (aufile->outputFormat) {
case WAV_FMT_AIFF:
_fseeki64(aufile->sndfile, 0, SEEK_SET);
FSEEK64(aufile->sndfile, 0, SEEK_SET);
write_aiff_header(aufile);
break;
case WAV_FMT_8BIT:
case WAV_FMT_16BIT:
case WAV_FMT_24BIT:
case WAV_FMT_32BIT:
case WAV_FMT_FLOAT: {
_fseeki64(in, 0, SEEK_END);
pos = _ftelli64 (in);
FSEEK64(in, 0, SEEK_END);
pos = FTELL64 (in);
if ((pos - current_pos_t) > 0) {
_fseeki64 (in, current_pos_t, SEEK_SET);
FSEEK64 (in, current_pos_t, SEEK_SET);
ch = malloc (sizeof(char) * (pos - current_pos_t));

fread (ch, 1, pos - current_pos_t, in);
Expand All @@ -950,9 +989,9 @@ void close_audio_file( FILE *in, audio_file *aufile, wavegain_opt *opt)
if (ch)
free (ch);
}
_fseeki64(aufile->sndfile, 0, SEEK_END);
pos = _ftelli64 (aufile->sndfile);
_fseeki64(aufile->sndfile, 0, SEEK_SET);
FSEEK64(aufile->sndfile, 0, SEEK_END);
pos = FTELL64 (aufile->sndfile);
FSEEK64(aufile->sndfile, 0, SEEK_SET);
write_wav_header(aufile, opt, pos - 8);
break;
}
Expand All @@ -978,7 +1017,7 @@ void close_audio_file( FILE *in, audio_file *aufile, wavegain_opt *opt)
#define WRITE_U16(buf, x) *(buf) = (unsigned char)((x)&0xff);\
*((buf)+1) = (unsigned char)(((x)>>8)&0xff);

static int write_wav_header(audio_file *aufile, wavegain_opt *opt, __int64 file_size)
static int write_wav_header(audio_file *aufile, wavegain_opt *opt, Int64_t file_size)
{
unsigned short channels = opt->channels;
unsigned long samplerate = opt->rate;
Expand Down
4 changes: 3 additions & 1 deletion audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ extern "C" {
#endif

#include <stdio.h>
#include "misc.h"

typedef long (*audio_read_func)(void *src,
double **buffer,
Expand All @@ -51,6 +52,7 @@ typedef struct
int format;
int gain_chunk;
double gain_scale;
int std_in;
int std_out;
int apply_gain;
int write_chunk;
Expand Down Expand Up @@ -143,7 +145,7 @@ typedef struct
audio_file *open_output_audio_file(char *infile, wavegain_opt *opt);
int write_audio_file(audio_file *aufile, void *sample_buffer, int samples);
void close_audio_file(FILE *in, audio_file *aufile, wavegain_opt *opt);
static int write_wav_header(audio_file *aufile, wavegain_opt *opt, __int64 file_size);
static int write_wav_header(audio_file *aufile, wavegain_opt *opt, Int64_t file_size);
static int write_aiff_header(audio_file *aufile);
static int write_audio_8bit(audio_file *aufile, void *sample_buffer, unsigned int samples);
static int write_audio_16bit(audio_file *aufile, void *sample_buffer, unsigned int samples);
Expand Down
2 changes: 1 addition & 1 deletion main.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define MAIN_H

/* Some version numbers */
#define WAVEGAIN_VERSION "1.2.7"
#define WAVEGAIN_VERSION "1.2.8"

#define BUFFER_LEN 16384
#define TEMP_NAME "wavegain.tmp"
Expand Down
2 changes: 1 addition & 1 deletion misc.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef MISC_H
#define MISC_H

#ifdef _MSC_VER
#ifdef _WIN32
typedef signed __int64 Int64_t;
typedef unsigned __int64 Uint64_t;
#define chdir _chdir
Expand Down
1 change: 1 addition & 0 deletions wavegain.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ int get_gain(const char *filename, double *track_peak, double *track_gain,
if(!strcmp(filename, "-")) {
infile = stdin;
settings->apply_gain = 0;
wg_opts->std_in = 1;
#ifdef _WIN32
_setmode( _fileno(stdin), _O_BINARY );
#endif
Expand Down

0 comments on commit 274a3ab

Please sign in to comment.