Error handling: don't continue with zero or negative ratio; don't continue if input file lacks sample rate. Both cases will be caught by subsequent calls anyway (e.g. in sf_open) but only with more confusing error reporting

This commit is contained in:
Chris Cannam
2020-02-04 13:24:37 +00:00
parent 419a9bcf70
commit 948e9a25fb

View File

@@ -267,6 +267,11 @@ int main(int argc, char **argv)
return 2;
}
if (ratio <= 0.0) {
cerr << "ERROR: Invalid time ratio " << ratio << endl;
return 1;
}
if (crispness >= 0 && crispchanged) {
cerr << "WARNING: Both crispness option and transients, lamination or window options" << endl;
cerr << " provided -- crispness will override these other options" << endl;
@@ -345,6 +350,7 @@ int main(int argc, char **argv)
SF_INFO sfinfo;
SF_INFO sfinfoOut;
memset(&sfinfo, 0, sizeof(SF_INFO));
memset(&sfinfoOut, 0, sizeof(SF_INFO));
sndfile = sf_open(fileName, SFM_READ, &sfinfo);
if (!sndfile) {
@@ -353,22 +359,27 @@ int main(int argc, char **argv)
return 1;
}
if (sfinfo.samplerate == 0) {
cerr << "ERROR: File lacks sample rate in header" << endl;
return 1;
}
if (duration != 0.0) {
if (sfinfo.frames == 0 || sfinfo.samplerate == 0) {
cerr << "ERROR: File lacks frame count or sample rate in header, cannot use --duration" << endl;
if (sfinfo.frames == 0) {
cerr << "ERROR: File lacks frame count in header, cannot use --duration" << endl;
return 1;
}
double induration = double(sfinfo.frames) / double(sfinfo.samplerate);
if (induration != 0.0) ratio = duration / induration;
}
sfinfoOut.channels = sfinfo.channels;
sfinfoOut.format = sfinfo.format;
sfinfoOut.frames = int(sfinfo.frames * ratio + 0.1);
sfinfoOut.samplerate = sfinfo.samplerate;
sfinfoOut.sections = sfinfo.sections;
sfinfoOut.seekable = sfinfo.seekable;
sndfileOut = sf_open(fileNameOut, SFM_WRITE, &sfinfoOut) ;
if (!sndfileOut) {
cerr << "ERROR: Failed to open output file \"" << fileNameOut << "\" for writing: "