9extern int adpcm_encode(FILE* fp, FILE* sad,
int offset,
int sample_len,
int flag_loop,
int bytes_per_sample);
11enum{ monoF = (1 << 0), stereo = (1 << 1), loopF = (1 << 2) } sad_flag;
15 unsigned char version;
16 unsigned char channels;
18 unsigned char reserved;
23static int ConvertFile(
const char *InputFile,
const char *OutputFile,
int flag_loop){
25 int sample_freq, sample_len, result;
30 char bytes_per_sample;
34 if ( (fp = fopen(InputFile,
"rb" )) != NULL )
36 if (fread( s, 1, 4, fp )!=4 || strncmp( s,
"RIFF", 4 ) )
38 printf(
"Error: Not a WAVE-file (\"RIFF\" identifier not found)\n" );
43 fseek( fp, 8, SEEK_SET );
45 if (fread( s, 1, 4, fp )!=4 || strncmp( s,
"WAVE", 4 ) )
47 printf(
"Error: Not a WAVE-file (\"WAVE\" identifier not found)\n" );
52 fseek( fp, 8 + 4, SEEK_SET );
54 if (fread( s, 1, 4, fp )!=4 || strncmp( s,
"fmt", 3 ) )
56 printf(
"Error: Not a WAVE-file (\"fmt\" chunk not found)\n" );
61 if(fread( &chunk_data, 4, 1, fp )==1){
62 chunk_data += ftell( fp );
64 if (fread( &e, 2, 1, fp )!=1 || e != 1 )
66 printf(
"Error: No PCM data in WAVE-file\n" );
72 printf(
"Error: can't read CHUNK DATA in WAVE-file\n" );
77 if (fread( &e, 2, 1, fp )!=1 || ((e != 1) && (e != 2)))
79 printf(
"Error: WAVE file must be MONO or STEREO (max 2 channels)\n" );
86 if(fread( &sample_freq, 4, 1, fp )==1){
87 fseek( fp, 4 + 2, SEEK_CUR );
88 if (fread( &e, 2, 1, fp )!=1 || (e != 8 && e != 16) )
90 printf(
"Error: WAVE-file must 8 or 16 bit\n" );
94 bytes_per_sample = e / 8;
97 printf(
"Error: Can't read SAMPLE FREQUENCY in WAVE-file\n");
102 fseek( fp, chunk_data, SEEK_SET );
103 if(fread( s, 1, 4, fp )==4){
105 while(strncmp( s,
"data", 4 ))
107 if(fread( &chunk_data, 4, 1, fp )==1){
108 chunk_data += ftell( fp );
109 fseek( fp, chunk_data, SEEK_SET );
110 if(fread( s, 1, 4, fp )!=4){
111 printf(
"Error: Read error in WAVE-file\n");
117 printf(
"Error: Read error in WAVE-file\n");
124 printf(
"Error: Read error in WAVE-file\n");
129 if(fread( &sample_len, 4, 1, fp )==1){
130 sample_len /= (channels*bytes_per_sample);
133 printf(
"Error: Can't read SAMPLE LENGTH in WAVE-file\n");
138 if ( (sad = fopen(OutputFile,
"wb" )) != NULL )
147 .pitch=(sample_freq*4096)/48000,
154 result=adpcm_encode(fp, sad, 0, sample_len, flag_loop, bytes_per_sample);
158 int data_offset = ftell(fp);
161 if((result=adpcm_encode(fp, sad, bytes_per_sample, sample_len, flag_loop, bytes_per_sample))==0){
162 fseek(fp, data_offset+bytes_per_sample, SEEK_SET);
164 result=adpcm_encode(fp, sad, bytes_per_sample, sample_len, flag_loop, bytes_per_sample);
172 printf(
"Error: Can't write output file %s\n", OutputFile);
181 printf(
"Error: Can't open %s\n", InputFile);
188int main(
int argc,
char *argv[] )
194 if( strncmp( argv[1],
"-L", 2 ) )
196 printf(
"Error: Option '%s' not recognized\n", argv[1]);
201 result=ConvertFile(argv[2], argv[3], 1);
206 result=ConvertFile(argv[1], argv[2], 0);
210 printf(
"ADPCM Encoder %s\n"
211 "Usage: sadenc [-L] <input wave> <output sad>\n"
213 " -L Loop\n", VERSION);