-
Notifications
You must be signed in to change notification settings - Fork 7
/
log.c
302 lines (258 loc) · 7 KB
/
log.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* MDFourier
* A Fourier Transform analysis tool to compare game console audio
* http://junkerhq.net/MDFourier/
*
* Copyright (C)2019-2020 Artemio Urbina
*
* This file is part of the 240p Test Suite
*
* You can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Requires the FFTW library:
* http://www.fftw.org/
*
*/
#include "log.h"
#include "mdfourier.h"
#include "freq.h"
#include "cline.h"
#define CONSOLE_ENABLED 1
int do_log = 0;
char log_file[T_BUFFER_SIZE];
FILE *logfile = NULL;
void EnableLog(void) { do_log = CONSOLE_ENABLED; }
void DisableLog(void) { do_log = 0; }
int IsLogEnabled(void) { return do_log; }
void initLog(void)
{
do_log = 0;
logfile = NULL;
}
void logmsg(char *fmt, ... )
{
va_list arguments;
va_start(arguments, fmt);
vprintf(fmt, arguments);
fflush(stdout); // output to Front end ASAP
va_end(arguments);
if(do_log && logfile)
{
va_list arguments_f;
va_start(arguments_f, fmt);
vfprintf(logfile, fmt, arguments_f);
va_end(arguments_f);
#ifdef DEBUG
fflush(logfile);
#endif
}
}
void logmsgFileOnly(char *fmt, ... )
{
if(do_log && logfile)
{
va_list arguments;
va_start(arguments, fmt);
vfprintf(logfile, fmt, arguments);
va_end(arguments);
#ifdef DEBUG
fflush(logfile);
#endif
}
}
#if defined (WIN32)
void FixLogFileName(char *name)
{
int len;
if(!name)
return;
len = strlen(name);
if(len > MAX_PATH)
{
name[MAX_PATH - 5] = '.';
name[MAX_PATH - 4] = 't';
name[MAX_PATH - 3] = 'x';
name[MAX_PATH - 2] = 't';
name[MAX_PATH - 1] = '\0';
}
}
#endif
int setLogName(char *name)
{
sprintf(log_file, "%s", name);
if(!do_log)
return 0;
remove(log_file);
#if defined (WIN32)
FixLogFileName(log_file);
#endif
logfile = fopen(log_file, "w");
if(!logfile)
{
printf("Could not create log file %s\n", log_file);
return 0;
}
//printf("\tLog enabled to file: %s\n", log_file);
return 1;
}
void endLog(void)
{
if(logfile)
{
fclose(logfile);
logfile = NULL;
}
do_log = 0;
}
// no endianess considerations, PCM in RIFF is little endian and this code is little endian
void ConvertPCMSampleToByteArray(double sample, char *bytes, int size)
{
int32_t sampleInt = 0;
sampleInt = (int32_t)(sample + 0.5);
bytes[0] = (char)((sampleInt & 0x000000ff));
if(size >= 2)
bytes[1] = (char)((sampleInt & 0x0000ff00) >> 8);
if(size >= 3)
bytes[2] = (char)((sampleInt & 0x00ff0000) >> 16);
if(size == 4)
bytes[3] = (char)((sampleInt & 0xff000000) >> 24);
return;
}
int SaveWAVEChunk(char *filename, AudioSignal *Signal, double *buffer, long int block, long int loadedBlockSize, int diff, parameters *config)
{
FILE *chunk = NULL;
wav_hdr cheader;
char *samples = NULL, FName[4096];
long int i = 0;
int convertedSamples = 0;
samples = (char*)malloc(sizeof(char)*loadedBlockSize*Signal->bytesPerSample);
if(!samples)
{
logmsg("\tCould not allocate WAV memory\n");
return 0;
}
memset(samples, 0, sizeof(char)*loadedBlockSize*Signal->bytesPerSample);
cheader = Signal->header;
if(!filename)
{
char Name[2048];
sprintf(Name, "%03ld_SRC_%s_%03d_%s_%s",
block, GetBlockName(config, block), GetBlockSubIndex(config, block),
basename(Signal->SourceFile), diff ? "_diff_": "");
ComposeFileName(FName, Name, ".wav", config);
chunk = fopen(FName, "wb");
filename = FName;
}
else
chunk = fopen(filename, "wb");
if(!chunk)
{
logmsg("\tERROR: Could not open chunk file %s\n", filename);
free(samples);
return 0;
}
if(Signal->header.fmt.AudioFormat == WAVE_FORMAT_PCM ||
Signal->header.fmt.AudioFormat == WAVE_FORMAT_EXTENSIBLE)
{
for(i = 0; i < loadedBlockSize; i++)
ConvertPCMSampleToByteArray(buffer[i], samples+i*Signal->bytesPerSample, Signal->bytesPerSample);
convertedSamples = 1;
}
if(Signal->header.fmt.AudioFormat == WAVE_FORMAT_IEEE_FLOAT && Signal->header.fmt.bitsPerSample == 32)
{
float *samplesf = NULL;
samplesf = (float*)samples;
for(i = 0; i < loadedBlockSize; i++)
samplesf[i] = (float)buffer[i];
convertedSamples = 1;
}
if(Signal->header.fmt.AudioFormat == WAVE_FORMAT_IEEE_FLOAT && Signal->header.fmt.bitsPerSample == 64)
{
double *samplesd = NULL;
samplesd = (double*)samples;
for(i = 0; i < loadedBlockSize; i++)
samplesd[i] = (double)buffer[i];
convertedSamples = 1;
}
if(!convertedSamples)
{
logmsg("ERROR: Unsupported audio format, samples were not loaded\n");
return 0;
}
cheader.riff.ChunkSize = sizeof(riff_hdr)+sizeof(fmt_hdr)+Signal->fmtType*sizeof(int8_t)+
loadedBlockSize*Signal->bytesPerSample+Signal->factExists*sizeof(fact_ck);
if(fwrite(&cheader.riff, 1, sizeof(riff_hdr), chunk) != sizeof(riff_hdr))
{
fclose(chunk);
logmsg("\tERROR: Could not write RIFf header chunk to file %s\n", filename);
free(samples);
return(0);
}
if(fwrite(&cheader.fmt, 1, sizeof(fmt_hdr), chunk) != sizeof(fmt_hdr))
{
fclose(chunk);
logmsg("\tERROR: Could not write fmt header chunk to file %s\n", filename);
free(samples);
return(0);
}
// Check extended fmt header
if(Signal->fmtType != FMT_TYPE_1_SIZE)
{
if(fwrite(Signal->fmtExtra, 1, sizeof(int8_t)*Signal->fmtType, chunk) != sizeof(int8_t)*Signal->fmtType)
{
fclose(chunk);
logmsg("\tERROR: Could not write fmt extended header chunk to file %s\n", filename);
free(samples);
return(0);
}
}
cheader.data.DataSize = loadedBlockSize*Signal->bytesPerSample;
if(fwrite(&cheader.data, 1, sizeof(data_hdr), chunk) != sizeof(data_hdr))
{
fclose(chunk);
logmsg("\tERROR: Could not write data header chunk to file %s\n", filename);
free(samples);
return(0);
}
if(fwrite(samples, 1, sizeof(char)*loadedBlockSize*Signal->bytesPerSample, chunk) != sizeof(char)*loadedBlockSize*Signal->bytesPerSample)
{
fclose(chunk);
logmsg("\tERROR: Could not write samples to chunk file %s\n", filename);
free(samples);
return (0);
}
if(Signal->header.fmt.AudioFormat == WAVE_FORMAT_EXTENSIBLE && !Signal->factExists)
{
if(config->verbose)
logmsg("\tWARNING: Extensible wave requires a fact chunk. generating one.\n");
memcpy(Signal->fact.DataID, "fact", sizeof(char)*4);
Signal->fact.DataSize = 4;
Signal->factExists = 1;
}
if(Signal->factExists)
{
Signal->fact.dwSampleLength = loadedBlockSize/Signal->AudioChannels;
if(fwrite(&Signal->fact, 1, sizeof(fact_ck), chunk) != sizeof(fact_ck))
{
fclose(chunk);
logmsg("\tERROR: Could not write fact header chunk to file %s\n", filename);
free(samples);
return(0);
}
}
fclose(chunk);
free(samples);
return 1;
}