Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AIFF-C writing in command line tool #768

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ src/test_seeking/test_seeking
src/test_streams/test_streams
stamp-h1
test/*.aiff
test/*.aifc
test/*.cmp
test/*.cue
test/*.flac
Expand Down
2 changes: 1 addition & 1 deletion src/flac/decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ FLAC__bool write_iff_headers(FILE *f, DecoderSession *decoder_session, FLAC__uin
else if(format == FORMAT_AIFF)
iff_size = 46 + foreign_metadata_size + aligned_data_size;
else /* AIFF-C */
iff_size = 16 + foreign_metadata_size + aligned_data_size + (fm?fm->aifc_comm_length:0);
iff_size = 16 + foreign_metadata_size + aligned_data_size + (fm?fm->aifc_comm_length:36);

if(format != FORMAT_WAVE64 && format != FORMAT_RF64 && iff_size >= 0xFFFFFFF4) {
flac__utils_printf(stderr, 1, "%s: ERROR: stream is too big to fit in a single %s file\n", decoder_session->inbasefilename, fmt_desc);
Expand Down
47 changes: 39 additions & 8 deletions src/test_streams/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,8 @@ static FLAC__bool generate_unsigned_raw(const char *filename, unsigned channels,
return false;
}

static FLAC__bool generate_aiff(const char *filename, unsigned sample_rate, unsigned channels, unsigned bps, unsigned samples)
/* flavor is: 0:AIFF, 1:AIFF-C NONE, 2:AIFF-C sowt */
static FLAC__bool generate_aiff(const char *filename, unsigned sample_rate, unsigned channels, unsigned bps, unsigned samples, int flavor)
{
const unsigned bytes_per_sample = (bps+7)/8;
const unsigned true_size = channels * bytes_per_sample * samples;
Expand All @@ -765,10 +766,18 @@ static FLAC__bool generate_aiff(const char *filename, unsigned sample_rate, unsi
return false;
if(fwrite("FORM", 1, 4, f) < 4)
goto foo;
if(!write_big_endian_uint32(f, padded_size + 46))
goto foo;
if(fwrite("AIFFCOMM\000\000\000\022", 1, 12, f) < 12)
goto foo;
if(flavor == 0) {
if(!write_big_endian_uint32(f, padded_size + 46))
goto foo;
if(fwrite("AIFFCOMM\000\000\000\022", 1, 12, f) < 12)
goto foo;
}
else {
if(!write_big_endian_uint32(f, padded_size + 52))
goto foo;
if(fwrite("AIFCCOMM\000\000\000\030", 1, 12, f) < 12)
goto foo;
}
if(!write_big_endian_uint16(f, (FLAC__uint16)channels))
goto foo;
if(!write_big_endian_uint32(f, samples))
Expand All @@ -777,6 +786,14 @@ static FLAC__bool generate_aiff(const char *filename, unsigned sample_rate, unsi
goto foo;
if(!write_sane_extended(f, sample_rate))
goto foo;
if(flavor == 1) {
if(fwrite("NONE\000\000", 1, 6, f) < 6)
goto foo;
}
else if(flavor == 2) {
if(fwrite("sowt\000\000", 1, 6, f) < 6)
goto foo;
}
if(fwrite("SSND", 1, 4, f) < 4)
goto foo;
if(!write_big_endian_uint32(f, true_size + 8))
Expand All @@ -788,8 +805,14 @@ static FLAC__bool generate_aiff(const char *filename, unsigned sample_rate, unsi
for(j = 0; j < channels; j++) {
double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
FLAC__int32 v = ((FLAC__int32)(val + 0.5) + ((GET_RANDOM_BYTE>>4)-8)) << shift;
if(!write_big_endian(f, v, bytes_per_sample))
goto foo;
if(flavor == 0 || flavor == 1) {
if(!write_big_endian(f, v, bytes_per_sample))
goto foo;
}
else {
if(!write_little_endian_signed(f, v, bytes_per_sample))
goto foo;
}
}
}
for(i = true_size; i < padded_size; i++)
Expand Down Expand Up @@ -1367,7 +1390,15 @@ int main(int argc, char *argv[])
char fn[64];

flac_snprintf(fn, sizeof (fn), "rt-%u-%u-%u.aiff", channels, bits_per_sample, nsamples[samples]);
if(!generate_aiff(fn, 44100, channels, bits_per_sample, nsamples[samples]))
if(!generate_aiff(fn, 44100, channels, bits_per_sample, nsamples[samples], 0))
return 1;

flac_snprintf(fn, sizeof (fn), "rt-%u-%u-%u.aifc", channels, bits_per_sample, nsamples[samples]);
if(!generate_aiff(fn, 44100, channels, bits_per_sample, nsamples[samples], 1))
return 1;

flac_snprintf(fn, sizeof (fn), "rt-%u-%u-%u-le.aifc", channels, bits_per_sample, nsamples[samples]);
if(!generate_aiff(fn, 44100, channels, bits_per_sample, nsamples[samples], 2))
return 1;

flac_snprintf(fn, sizeof (fn), "rt-%u-%u-%u.wav", channels, bits_per_sample, nsamples[samples]);
Expand Down
2 changes: 1 addition & 1 deletion test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ EXTRA_DIST = \
write_iff.pl

clean-local:
-rm -f *.raw *.flac *.oga *.ogg *.cmp *.aiff *.wav *.w64 *.rf64 *.diff *.log *.cue core
-rm -f *.raw *.flac *.oga *.ogg *.cmp *.aifc *.aiff *.wav *.w64 *.rf64 *.diff *.log *.cue core
34 changes: 34 additions & 0 deletions test/test_flac.sh
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,34 @@ rt_test_aiff ()
rm -f rt.flac rt.aiff
}

rt_test_aifc ()
{
f="$1"
extra="$2"
echo $ECHO_N "round-trip test ($f) encode... " $ECHO_C
run_flac --force --verify --channel-map=none --no-padding --lax -o rt.flac $extra $f || die "ERROR"
echo $ECHO_N "decode... " $ECHO_C
run_flac --force --decode --channel-map=none -o rt.aifc --force-aiff-c-none-format $extra rt.flac || die "ERROR"
echo $ECHO_N "compare... " $ECHO_C
cmp $f rt.aifc || die "ERROR: file mismatch"
echo "OK"
rm -f rt.flac rt.aifc
}

rt_test_aifc_le ()
{
f="$1"
extra="$2"
echo $ECHO_N "round-trip test ($f) encode... " $ECHO_C
run_flac --force --verify --channel-map=none --no-padding --lax -o rt.flac $extra $f || die "ERROR"
echo $ECHO_N "decode... " $ECHO_C
run_flac --force --decode --channel-map=none -o rt.aifc --force-aiff-c-sowt-format $extra rt.flac || die "ERROR"
echo $ECHO_N "compare... " $ECHO_C
cmp $f rt.aifc || die "ERROR: file mismatch"
echo "OK"
rm -f rt.flac rt.aifc
}

rt_test_autokf ()
{
f="$1"
Expand Down Expand Up @@ -329,6 +357,12 @@ done
for f in rt-*.aiff ; do
rt_test_aiff $f
done
for f in rt-*[0-9].aifc ; do
rt_test_aifc $f
done
for f in rt-*le.aifc ; do
rt_test_aifc_le $f
done
for f in rt-*.wav ; do
rt_test_flac $f
done
Expand Down
Loading