Newer
Older
/****************************************************************************
*
* Programs for processing sound files in raw- or WAV-format.
* -- Useful functions for parsing command line options and
* issuing errors, warnings, and chit chat.
*
* Name: frame.c
* Version: see static char *standardversion, below.
* Author: Mark Roberts <mark@manumark.de>
* Michael Labuschke <michael@labuschke.de> sys_errlist fixes
*
****************************************************************************/
/****************************************************************************
* These are useful functions that all DSP programs might find handy
****************************************************************************/
#include <stdio.h>
#include <math.h>
#include <stdlib.h> /* for exit and malloc */
#include <string.h>
#include <time.h>
#include <stdarg.h>
#include <errno.h>
#include <assert.h>
#include "frame.h"
time_t stopwatch; /* will hold time at start of calculation */
unsigned short samplewidth;
unsigned short channels;
int wavout; /* TRUE iff out file should be a .WAV file */
int iswav; /* TRUE iff in file was found to be a .WAV file */
FILE *in, *out;
char *infilename, *outfilename;
int verboselevel;
char *version = "";
char *usage = "";
static int test_usage;
static char *standardversion = "frame version 1.3, June 13th 2001";
static char *standardusage =
"\nOptions common to all mark-dsp programs:\n"
"-h \t\t create a WAV-header on output files.\n"
"-c#\t\t set number of channels to # (1 or 2). Default: like input.\n"
"-w#\t\t set number of bits per sample (width) to # (only 16)\n"
"-f#\t\t set sample frequency to #. Default: like input.\n"
"-V \t\t verbose: talk a lot.\n"
"-Q \t\t quiet: talk as little as possible.\n\n"
"In most cases, a filename of '-' means stdin or stdout.\n\n"
"Bug-reports: mark@manumark.de\n"
;
/* -----------------------------------------------------------------------
Writes the number of samples to result that are yet to be read from anyin.
Return values are TRUE on success, FALSE on failure.
-----------------------------------------------------------------------*/
int getremainingfilelength( FILE *anyin, long *result)
{
long i;
Kevin P. Fleming
committed
i = ftell(anyin);
Kevin P. Fleming
committed
if (fseek(anyin, 0, SEEK_END) == -1) return FALSE;
*result = ftell(anyin);
if (*result == -1) return FALSE;
(*result) -= i;
(*result) /= samplewidth;
Kevin P. Fleming
committed
if (fseek(anyin, i, SEEK_SET) == -1) return FALSE;
return TRUE;
}
/* -----------------------------------------------------------------------
Read a .pk-header from 'anyin'.
-----------------------------------------------------------------------*/
void readpkheader( FILE *anyin)
{
unsigned short tempushort;
int tempint, i, x;
unsigned char blood[8];
for (i = 0; i < 11; i++)
{
Kevin P. Fleming
committed
if (!fread( &tempint, 4, 1, anyin)) {
return;
}
printf( "%d: %d, ", i, tempint);
Kevin P. Fleming
committed
if (!fread( blood, 1, 8, anyin)) {
return;
}
Kevin P. Fleming
committed
printf( "%d ", blood[i]);
printf( "\n");
for (i = 0; i < 8; i++)
Kevin P. Fleming
committed
{
for (x = 128; x > 0; x /= 2)
printf((blood[i] & x) == 0? "0 ":"1 ");
printf(i%4==3? "\n":"| ");
}
printf( "\n");
for (i = 0; i < 2; i++)
{
Kevin P. Fleming
committed
if (!fread( &tempint, 4, 1, anyin)) {
return;
}
printf( "%d: %d, ", i, tempint);
}
printf( "\n");
for (i = 0; i < 2; i++)
{
Kevin P. Fleming
committed
if (!fread( &tempushort, 2, 1, anyin)) {
return;
}
printf( "%d: %d, ", i, tempushort);
}
printf( "\n");
}
/* -----------------------------------------------------------------------
Read a .WAV header from 'anyin'. See header for details.
-----------------------------------------------------------------------*/
void readwavheader( FILE *anyin)
{
unsigned int tempuint, sf;
unsigned short tempushort, cn;
char str[9];
int nowav = FALSE;
iswav = FALSE;
if (ftell(anyin) == -1) /* If we cannot seek this file */
Kevin P. Fleming
committed
{
nowav = TRUE; /* -> Pretend this is no wav-file */
chat("File not seekable: not checking for WAV-header.\n");
}
Kevin P. Fleming
committed
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
{
/* Expect four bytes "RIFF" and four bytes filelength */
if (!fread(str, 1, 8, anyin)) { /* 0 */
return;
}
str[4] = '\0';
if (strcmp(str, "RIFF") != 0) nowav = TRUE;
/* Expect eight bytes "WAVEfmt " */
if (!fread(str, 1, 8, anyin)) { /* 8 */
return;
}
str[8] = '\0';
if (strcmp(str, "WAVEfmt ") != 0) nowav = TRUE;
/* Expect length of fmt data, which should be 16 */
if (!fread(&tempuint, 4, 1, anyin)) { /* 16 */
return;
}
if (tempuint != 16) nowav = TRUE;
/* Expect format tag, which should be 1 for pcm */
if (!fread(&tempushort, 2, 1, anyin)) { /* 20 */
return;
}
if (tempushort != 1)
nowav = TRUE;
/* Expect number of channels */
if (!fread(&cn, 2, 1, anyin)) { /* 20 */
return;
}
if (cn != 1 && cn != 2) nowav = TRUE;
/* Read samplefrequency */
if (!fread(&sf, 4, 1, anyin)) { /* 24 */
return;
}
/* Read bytes per second: Should be samplefreq * channels * 2 */
if (!fread(&tempuint, 4, 1, anyin)) { /* 28 */
return;
}
if (tempuint != sf * cn * 2) nowav = TRUE;
/* read bytes per frame: Should be channels * 2 */
if (!fread(&tempushort, 2, 1, anyin)) { /* 32 */
return;
}
if (tempushort != cn * 2) nowav = TRUE;
/* Read bits per sample: Should be 16 */
if (!fread(&tempushort, 2, 1, anyin)) { /* 34 */
return;
}
if (tempushort != 16) nowav = TRUE;
if (!fread(str, 4, 1, anyin)) { /* 36 */
return;
}
str[4] = '\0';
if (strcmp(str, "data") != 0) nowav = TRUE;
if (!fread(&tempuint, 4, 1, anyin)) { /* 40 */
return;
}
if (nowav)
{
fseek(anyin, 0, SEEK_SET); /* Back to beginning of file */
chat("File has no WAV header.\n");
Loading
Loading full blame...