/* CD click detect, august 14, 2009. Latest version available at http://kmt.hku.nl/~pieter/EDU/c/cd_click_detect/ Find ticks and glitches in ripped audio CDs. Expects single AIFF file. Compilation: gcc -O3 -Wall click_detect.c rdaiff.c -o click_detect Copyright (c) 2009 - Pieter Suurmond Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. Any person wishing to distribute modifications to the Software is requested to send the modifications to the original developer so that they can be incorporated into the canonical version. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include "rdaiff.h" /* This program only reads a single AIFF. (In bash script 'click_detect_wav' we use sox to convert WAV files.) */ #define kVERSION "0.14" /* Files with headers that differ from this will be rejected: */ #define kBITS (16) #define kCHANNELS (2) #define kSAMPLERATE (44100) /* Extreme difference in subsequent samples: */ #define kDELTA_HIGH (64000) #define kDELTA_EXTREMELY_HIGH (65000) /* No difference between subsequent samples for longer than [samples]: 882 samples at 44100 Hz correspond with 20 milliseconds. This 20 millisecond treshold accounts for almost-silence (<8); Perfect silence (<4) is allowed for 40 ms; Near -0 dB we are 32 times as strict: only 27 samples are allowed. */ #define kDC_SAMPLES (882) #define kMAX_ACCU_ERRORS (20) static void print_time(long frame) { double s; long ss, sss; int hh = 0; int mm = 0; s = (double)frame / (double)kSAMPLERATE; ss = (long)s; /* Seconds. */ sss = (long)(1000.0 * (s - (double)ss)); /* Milliseconds. */ while (ss > 3600) /* Hours. */ { hh++; ss -= 3600; } while (ss > 60) /* Minutes. */ { mm++; ss -= 60; } printf("frame %ld (%d:%02d:%02ld.%03ld)", frame, hh, mm, ss, sss); } /* Large differences in subsequent samples (of the same channel) are reported immediately. Dependant on the extremity, 1 or 2 is returned. */ static int report_delta(long d, short channel, long frame) { int e = 1; printf("EXTREME DELTA (%ld) in channel %d at ", d, channel); print_time(frame); printf("!\n"); if (labs(d) > kDELTA_EXTREMELY_HIGH) e++; return e; } /* DC constants are reported afterwards. Dependant on the DC-length and the absolute sample-value, 0, 1, 2 or higher is returned. */ static int report_dc(long count, short sample, short channel, long frame, long total_frames) { int s, e = 0; long long count_treshold = kDC_SAMPLES; long margin = kSAMPLERATE * 10; /* 10 seconds silence-margin. */ if (total_frames < kSAMPLERATE * 120) /* Or 5 seconds for files */ margin >>= 1; /* shorter than 2 minutes. */ s = abs((int)sample); /* 32-bit, no overflow with 16-bit arg. */ if (s > 7) { count_treshold >>= 1; /* 10 ms. (5,6,etc..) */ if (s > 63) { count_treshold >>= 1; /* 5 ms. */ if (s >= 511) { count_treshold >>= 1; /* 2.5 ms. */ if (s >= 4095) { count_treshold >>= 1; /* 1.25 ms. */ if (s >= 32767) count_treshold >>= 1; /* 0.63 ms. */ } } } } /* Allow silence (<8) near the beginning and the end. */ else if ((frame - count < margin) || (frame > total_frames - margin)) count_treshold = margin; /* 5000 or 10000 ms. (7,6,5,4) */ /* Else 20 ms (7,6,5,4) or 40 ms (3,2,1,0). */ if (s < 4) count_treshold <<= 1; /* Up to 20000 ms (3,2,1,0). */ if (count > count_treshold) { printf("CONSTANT DC (%d for %ld samples) in channel %d at ", sample, count, channel); print_time(frame - count); /* Report start of the DC, not the end. */ printf("!\n"); e = count / count_treshold; /* At least 1. */ } return e; } /* Inspect a single AIFF file. */ int main(int argc, /* Number of arguments (first is name of this program). */ char** argv) /* Array of C-strings. */ { const char* info_str = "CD click_detect version "kVERSION" by Pieter Suurmond."; RDAIFFp a; int e; if (argc != 2) /* 1 argument must be there. */ { puts(info_str); puts("Supply the pathname of an existing AIFF file (or '-v').\n"); return 1; } if (!strcasecmp(argv[1], "-v")) { puts(info_str); return 0; } e = RDAIFF_open(&a, argv[1]); if (e) { printf("ERROR: RDAIFF_open(%s)=%d.\n", argv[1], e); return 1; } if (RDAIFF_channels(a) != kCHANNELS) { puts("Not a stereo file!"); e = 1; } else if (RDAIFF_bits(a) != kBITS) { printf("Not a %d-bit file!", kBITS); e = 1; } else if (RDAIFF_samplerate(a) != kSAMPLERATE) { puts("Not a 44.1 kHz file!"); e = 1; } else { short c; short prev_sample[kCHANNELS] = { 0, 0 }; long dc[kCHANNELS] = { 0, 0 }; long frame, frames; long accu_errors = 0; frames = RDAIFF_frames(a); for (frame = 0; (frame < frames) && (accu_errors < kMAX_ACCU_ERRORS); frame++) { short sample[kCHANNELS]; e = RDAIFF_short(a, sample, 1); if (e) { printf("RDAIFF_short()=%d!\n", e); e = 1; break; } for (c = 0; c < kCHANNELS; c++) { long d = (long)sample[c] - (long)prev_sample[c]; if (!d) /* Exactly the same value for */ dc[c]++; /* more than 220 samples?! */ else { if (labs(d) > kDELTA_HIGH) /* Extreme difference. */ accu_errors += report_delta(d, c, frame); accu_errors += report_dc(dc[c], prev_sample[c], c, frame, frames); dc[c] = 0; } prev_sample[c] = sample[c]; } } /* There might be a DC at the end: */ for (c = 0; c < kCHANNELS; c++) accu_errors += report_dc(dc[c], prev_sample[c], c, frame, frames); /* Indicate seriousness. */ if (accu_errors) { if (accu_errors > kMAX_ACCU_ERRORS) /* Clip. */ accu_errors = kMAX_ACCU_ERRORS; while (accu_errors--) putchar('*'); printf(" %s (%ld frames)\n", argv[1], frames); e = 1; /* Return non-zero. */ } } RDAIFF_close(&a); return e; }