1 // wav.cpp : Defines the entry point for the console application.
9 typedef unsigned char uint8_t;
12 typedef unsigned short uint16_t;
15 typedef unsigned int uint32_t;
19 typedef signed char int8_t;
22 typedef signed short int16_t;
25 typedef signed int int32_t;
51 int _tmain(int argc, _TCHAR* argv[])
54 printf("wav_apss (wav file) [threshold]\n\n");
55 printf("Cleanup MZ-2500 tape file for APSS.\n");
56 printf("The wave file should be pcm format, 16bit and 2ch.\n");
61 threshold = _ttoi(argv[2]);
64 FILE *fp = _tfopen(argv[1],_T("rb"));
70 fread(&header, sizeof(header), 1, fp);
71 fseek(fp, header.fmt_size - 0x10, SEEK_CUR);
72 fread(&data, sizeof(data), 1, fp);
73 long sample_top = ftell(fp);
75 if(header.format_id != 1) {
76 printf("The source wave file should be a pcm format.\n");
80 if(header.channels != 2 || header.sample_bits != 16) {
81 printf("The source wave file should be 16bit and 2ch.\n");
85 int samples = data.data_len / 4;
88 int16_t* buffer_l = (int16_t *)malloc(samples * sizeof(int16_t));
89 int16_t* buffer_r = (int16_t *)malloc(samples * sizeof(int16_t));
99 int count = 0, start, prev = 0;
101 for(int i = 0; i < samples; i++) {
102 sample.b.l = fgetc(fp);
103 sample.b.h = fgetc(fp);
104 buffer_l[i] = sample.s16;
106 sample.b.l = fgetc(fp);
107 sample.b.h = fgetc(fp);
108 buffer_r[i] = sample.s16;
110 if(abs(buffer_l[i]) > threshold) {
114 count = 32 * header.sample_rate / 44100;
116 if(count > 0 && --count == 0) {
118 unsigned int width = end - start + 1;
120 if(width > header.sample_rate * 250 / 1000) {
121 int count_p = 0, count_m = 0;
123 for(int j = start; j <= end; j++) {
124 if(buffer_l[j] > threshold) {
126 } else if(buffer_l[j] < -threshold) {
129 volume += buffer_l[j];
131 if((double)count_p / (double)width > 0.2 && (double)count_m / (double)width > 0.2) {
132 int16_t volume_ave = (int16_t)(volume / (double)width + 0.5);
133 int sec = start / header.sample_rate;
134 printf("Start = %d:%d\tWidth = %d (msec)\n", sec / 60, sec % 60, (int)(1000 * width / header.sample_rate));
135 for(int j = prev; j < start; j++) {
138 for(int j = start; j <= end; j++) {
139 buffer_l[j] = buffer_l[j] > volume_ave ? 16384 : -16384;
147 for(int j = prev; j < samples; j++) {
152 FILE *fo = _tfopen(_T("output.wav"), _T("wb"));
153 fseek(fp, 0, SEEK_SET);
154 for(long i = 0; i < sample_top; i++) {
155 fputc(fgetc(fp), fo);
157 for(int i = 0; i < samples; i++) {
158 sample.s16 = buffer_l[i];
159 fputc(sample.b.l, fo);
160 fputc(sample.b.h, fo);
162 sample.s16 = buffer_r[i];
163 fputc(sample.b.l, fo);
164 fputc(sample.b.h, fo);