Debian Codegraph @

wavcmp/wavcmp.c

raw
1 /*
2 * C Implementation: wavcmp
3 *
4 * Description:
5 *
6 *
7 * Author: Nicolas Botti <[email protected]>, (C) 2007
8 *
9 * Copyright: See COPYING file that comes with this distribution
10 *
11 */
12
13 #include <stdio.h>
14 #include <string.h>
15
16 #include <libwaveformat.h>
17
18 t_wav_uint32 wav_write(void* p_user_data, void const* p_buffer, t_wav_uint32 p_bytes)
19 {
20 return (t_wav_uint32) fwrite(p_buffer, 1, p_bytes, (FILE*) p_user_data);
21 }
22
23 t_wav_uint32 wav_seek(void* p_user_data, t_wav_uint32 p_position)
24 {
25 return (t_wav_uint32) !fseek((FILE*) p_user_data, p_position, SEEK_SET);
26 }
27
28 t_wav_uint32 wav_read(void * p_user_data,void * p_buffer,t_wav_uint32 p_bytes)
29 {
30 return (t_wav_uint32) fread(p_buffer, 1, p_bytes, (FILE*) p_user_data);
31 }
32
33 static void
34 usage(const char *exename)
35 {
36 printf("Usage: %s <file1.wav> <file2.wav> [<diff.wav>]\n", exename);
37 }
38
39 int
40 main(int argc, char **argv)
41 {
42 t_wav_output_file wav_output;
43 t_wav_input_file wav_in_1;
44 t_wav_input_file wav_in_2;
45 int is_wav_output;
46 int err;
47 int total_samples, total_diff;
48
49 if(4 < argc && argc < 3)
50 {
51 usage(argv[0]);
52 return 0;
53 }
54
55 t_wav_input_file_callback wavi_fc;
56 memset(&wav_in_1, 0, sizeof wav_in_1);
57 wavi_fc.m_read = wav_read;
58 wavi_fc.m_user_data = fopen(argv[1], "rb");
59 if(!wavi_fc.m_user_data) return 1;
60 if (! waveformat_input_open(&wav_in_1, wavi_fc)) return 1;
61
62 memset(&wav_in_2, 0, sizeof wav_in_2);
63 wavi_fc.m_read = wav_read;
64 wavi_fc.m_user_data = fopen(argv[2], "rb");
65 if(!wavi_fc.m_user_data) return 1;
66 if (! waveformat_input_open(&wav_in_2, wavi_fc)) return 1;
67
68 if (wav_in_1.m_channels != wav_in_2.m_channels) {
69 printf("Channel number doesn't match\n");
70 return 1;
71 }
72 if (wav_in_1.m_samples_per_sec != wav_in_2.m_samples_per_sec) {
73 printf("Sample rate doesn't match\n");
74 return 1;
75 }
76 if (wav_in_1.m_bits_per_sample != wav_in_2.m_bits_per_sample) {
77 printf("Sample size doesn't match\n");
78 return 1;
79 }
80 if (wav_in_1.m_data_size != wav_in_2.m_data_size) {
81 printf("File length doesn't match\n");
82 return 1;
83 }
84
85 is_wav_output = argc > 3;
86 if(is_wav_output)
87 {
88 t_wav_output_file_callback wavo_fc;
89 memset(&wav_output, 0, sizeof wav_output);
90 wavo_fc.m_seek = wav_seek;
91 wavo_fc.m_write = wav_write;
92 wavo_fc.m_user_data = fopen(argv[3], "wb");
93 if(!wavo_fc.m_user_data) return 1;
94 err = waveformat_output_open(&wav_output, wavo_fc, wav_in_1.m_channels, wav_in_1.m_bits_per_sample, 0, wav_in_1.m_samples_per_sec, wav_in_2.m_data_size);
95 if(!err) return 1;
96 }
97
98 total_samples = 0;
99 total_diff = 0;
100
101 while(1)
102 {
103 short sample_buff[2][512];
104 unsigned int samples[2];
105
106 samples[0] = waveformat_input_process_int16(& wav_in_1, sample_buff[0], 512);
107 samples[1] = waveformat_input_process_int16(& wav_in_2, sample_buff[1], 512);
108
109 if (samples[0] != samples[1] || samples[0] == 0)
110 break;
111
112 int i = 0;
113 for( ; i < samples[0]; i++) {
114 sample_buff[0][i] -= sample_buff[1][i];
115 if (sample_buff[0][i]) {
116 printf("diff @ sample %i channel %i : %i\n", (total_samples + i) / wav_in_1.m_channels, (total_samples + i) % wav_in_1.m_channels, sample_buff[0][i]);
117 total_diff++;
118 }
119 }
120 total_samples += samples[0];
121
122 if(is_wav_output)
123 if(waveformat_output_process_int16(&wav_output, sample_buff[0], samples[0]) < 0)
124 break;
125 }
126
127 if (total_diff != 0)
128 printf("%i diff found\n", total_diff);
129 else
130 printf("no diff found\n");
131
132 waveformat_input_close(&wav_in_1);
133 waveformat_input_close(&wav_in_2);
134 fclose(wav_in_1.m_callback.m_user_data);
135 fclose(wav_in_2.m_callback.m_user_data);
136
137 if(is_wav_output)
138 {
139 waveformat_output_close(&wav_output);
140 fclose(wav_output.m_callback.m_user_data);
141 }
142
143 return 0;
144 }