diff options
| author | Andre Bezerra <amelco.herman@gmail.com> | 2026-11-17 13:09:49 -0300 |
|---|---|---|
| committer | Andre Bezerra <amelco.herman@gmail.com> | 2026-11-17 13:09:49 -0300 |
| commit | 719c62b10468985c76b39e1002dc861b1f654a53 (patch) | |
| tree | 1b68b1dec9251baa00336a155dfbbc1ab696eed2 /tests | |
| parent | 433cc1890a77e6c16583e1d65ad71645c7463826 (diff) | |
Replace the hand-written WAV reader and writer with dr_wavmaster
Vendor dr_wav 0.14.7 at ebfea72a, keep the strict RIFF/sample checks, extract wav_writer for stem and metronome output, and leave mpv and MP3 unchanged.
Diffstat (limited to 'tests')
| -rwxr-xr-x | tests/run.sh | 177 | ||||
| -rw-r--r-- | tests/wavtool.c | 680 |
2 files changed, 757 insertions, 100 deletions
diff --git a/tests/run.sh b/tests/run.sh index 483274f..a641a07 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -75,7 +75,11 @@ REAL_BIN=$BIN_DIR/stemsep-tests FAKE_BIN=$BIN_DIR/stemsep-tests-fake WAVTOOL=$BIN_DIR/wavtool mkdir -p "$BIN_DIR" "$scratch/stub-bin" "$scratch/runtime" || exit 1 -printf '#!/bin/sh\nexit 97\n' >"$scratch/stub-bin/mpv" || exit 1 +{ + printf '#!/bin/sh\n' + printf 'printf "%%s\\n" "$@" >"%s/mpv-args"\n' "$scratch" + printf 'exit 97\n' +} >"$scratch/stub-bin/mpv" || exit 1 chmod +x "$scratch/stub-bin/mpv" || exit 1 PATH="$scratch/stub-bin:$PATH" export PATH @@ -109,13 +113,13 @@ build_binaries() { compile "the fake-model test binary" \ gcc -std=c11 -O3 -DNDEBUG -Wall -Wextra -Wpedantic -Werror \ - -DSTEMSEP_TEST_FAKE_MODEL -Ithird_party/dr_mp3 \ - main.c audio.c audio_wav.c audio_mp3.c playback.c metronome.c \ + -DSTEMSEP_TEST_FAKE_MODEL -Ithird_party/dr_mp3 -Ithird_party/dr_wav \ + main.c audio.c audio_wav.c audio_mp3.c playback.c metronome.c wav_writer.c \ -lm -o "$FAKE_BIN" || return 1 compile "the real-model test binary" \ gcc -std=c11 -O3 -DNDEBUG -Wall -Wextra -Wpedantic -Werror \ - -Ithird_party/onnxruntime/include -Ithird_party/dr_mp3 \ - main.c audio.c audio_wav.c audio_mp3.c playback.c metronome.c \ + -Ithird_party/onnxruntime/include -Ithird_party/dr_mp3 -Ithird_party/dr_wav \ + main.c audio.c audio_wav.c audio_mp3.c playback.c metronome.c wav_writer.c \ -Lthird_party/onnxruntime/lib -Wl,--no-as-needed -lonnxruntime \ -lm -ldl -pthread -o "$REAL_BIN" || return 1 compile "the WAV fixture tool" \ @@ -437,6 +441,165 @@ test_fake_multi_chunk() "$RECON_TOLERANCE" >"$scratch/info.pipeline.fake.multi-chunk" } +test_fake_input_formats() +{ + for case in pcm16 pcm8-mono pcm24 pcm32 float32 ext-pcm16 ext-float32; do + fixture_case=$scratch/input-$case.wav + identifier=fmt-$case + "$WAVTOOL" gen "$fixture_case" "$FIXTURE_FRAMES" "$case" >/dev/null || + fail "cannot generate the $case input fixture" + "$FAKE_BIN" -i "$fixture_case" -m "$identifier" >stdout.log 2>stderr.log + status=$? + if [ "$status" -ne 0 ]; then + fail "$case separation failed with exit $status: $(tail -n 2 stderr.log)" + fi + [ -d "${identifier}_stems" ] || fail "$case stems are missing" + expected_line="format=3 channels=2 rate=44100 bits=32 frames=$FIXTURE_FRAMES finite=yes" + for stem in $STEMS; do + output=$("$WAVTOOL" inspect "${identifier}_stems/${identifier}_${stem}.wav" \ + "$FIXTURE_FRAMES" 2>&1) || fail "$case $stem: $output" + if [ "$output" != "$expected_line" ]; then + fail "$case $stem: $output" + fi + done + output=$("$WAVTOOL" recon "$RECON_TOLERANCE" "$fixture_case" \ + "${identifier}_stems" "$identifier" 2>&1) || + fail "$case reconstruction failed: $output" + printf '%s %s\n' "$case" "$output" \ + >>"$scratch/info.pipeline.fake.input-formats" + done +} + +test_fake_wav_errors() +{ + for case in alaw 3ch rate7999 partial-frame nan inf dup-fmt dup-data empty-data rf64 rifx ext-bad-guid ext-bad-subtype; do + bad_fixture=$scratch/bad-$case.wav + identifier=bad-$case + "$WAVTOOL" gen "$bad_fixture" 1024 "$case" >/dev/null || + fail "cannot generate the $case error fixture" + "$FAKE_BIN" -i "$bad_fixture" -m "$identifier" >stdout.log 2>stderr.log + status=$? + if [ "$status" -ne 3 ]; then + fail "$case: expected exit 3, got $status" + fi + [ -s stderr.log ] || fail "$case: diagnostic missing" + case $case in + alaw) expected_error='unsupported compressed WAV format tag' ;; + 3ch) expected_error='one or two channels' ;; + rate7999) expected_error='unsupported WAV sample rate' ;; + partial-frame) expected_error='partial audio frame' ;; + nan | inf) expected_error='NaN or infinite' ;; + dup-fmt) expected_error='multiple fmt chunks' ;; + dup-data) expected_error='multiple data chunks' ;; + empty-data) expected_error='data chunk is empty' ;; + rf64) expected_error='RF64' ;; + rifx) expected_error='RIFX' ;; + ext-bad-guid | ext-bad-subtype) + expected_error='unsupported WAVE_FORMAT_EXTENSIBLE subtype' ;; + esac + grep -q "$expected_error" stderr.log || + fail "$case: missing diagnostic '$expected_error'" + [ ! -e "${identifier}_stems" ] || + fail "$case: an output directory was created" + done + + "$WAVTOOL" gen "$scratch/truncated-source.wav" 4410 >/dev/null || + fail "cannot generate the truncated fixture" + head -c 1000 "$scratch/truncated-source.wav" >"$scratch/bad-truncated.wav" || + fail "cannot truncate the fixture" + identifier=bad-truncated + "$FAKE_BIN" -i "$scratch/bad-truncated.wav" -m "$identifier" >stdout.log 2>stderr.log + status=$? + if [ "$status" -ne 3 ]; then + fail "truncated: expected exit 3, got $status" + fi + [ -s stderr.log ] || fail "truncated: diagnostic missing" + grep -q 'RIFF size extends beyond' stderr.log || + fail "truncated: missing RIFF-size diagnostic" + [ ! -e "${identifier}_stems" ] || + fail "truncated: an output directory was created" +} + +test_fake_sample_rates() +{ + for case in rate8000 rate768000; do + case $case in + rate8000) + input_rate=8000 + expected_frames=243101 + ;; + rate768000) + input_rate=768000 + expected_frames=2532 + ;; + esac + fixture_case=$scratch/$case.wav + identifier=sr-$case + "$WAVTOOL" gen "$fixture_case" "$FIXTURE_FRAMES" "$case" >/dev/null || + fail "cannot generate the $case fixture" + "$FAKE_BIN" -i "$fixture_case" -m "$identifier" >stdout.log 2>stderr.log + status=$? + if [ "$status" -ne 0 ]; then + fail "$case separation failed with exit $status: $(tail -n 2 stderr.log)" + fi + grep -q "Input: WAV, 2 channels, $input_rate Hz, $FIXTURE_FRAMES frames" \ + stderr.log || fail "$case: the input was not read at $input_rate Hz" + expected_line="format=3 channels=2 rate=44100 bits=32 frames=$expected_frames finite=yes" + for stem in $STEMS; do + output=$("$WAVTOOL" inspect "${identifier}_stems/${identifier}_${stem}.wav" \ + "$expected_frames" 2>&1) || fail "$case $stem: $output" + if [ "$output" != "$expected_line" ]; then + fail "$case $stem: $output" + fi + done + if find "${identifier}_stems" -name '*.part' | grep -q .; then + fail "$case: leftover .part files" + fi + printf '%s input_rate=%s output_frames=%s\n' "$case" "$input_rate" \ + "$expected_frames" >>"$scratch/info.pipeline.fake.sample-rates" + done + + "$WAVTOOL" gen "$scratch/rate768001.wav" "$FIXTURE_FRAMES" rate768001 >/dev/null || + fail "cannot generate the rate768001 fixture" + identifier=sr-rate768001 + "$FAKE_BIN" -i "$scratch/rate768001.wav" -m "$identifier" >stdout.log 2>stderr.log + status=$? + if [ "$status" -ne 3 ]; then + fail "rate768001: expected exit 3, got $status" + fi + [ -s stderr.log ] || fail "rate768001: diagnostic missing" + grep -q 'unsupported WAV sample rate: 768001' stderr.log || + fail "rate768001: missing the specific sample-rate diagnostic" + [ ! -e "${identifier}_stems" ] || + fail "rate768001: an output directory was created" +} + +test_metronome_bar() +{ + bar=$XDG_RUNTIME_DIR/stemsep-metronome.wav + + "$REAL_BIN" --bpm 120 -t 4 >stdout.log 2>stderr.log + status=$? + if [ "$status" -ne 97 ]; then + fail "expected the stub mpv exit 97, got $status" + fi + [ -f "$bar" ] || fail "the metronome bar file is missing" + args=$scratch/mpv-args + [ -f "$args" ] || fail "the stub mpv did not run" + grep -q -- '--loop-file=inf' "$args" || + fail "mpv did not receive --loop-file=inf" + if [ "$(tail -n 1 "$args")" != "$bar" ]; then + fail "mpv did not receive the metronome bar path" + fi + output=$("$WAVTOOL" inspect "$bar" 88200 1 2>&1) || + fail "metronome bar: $output" + expected="format=3 channels=1 rate=44100 bits=32 frames=88200 finite=yes" + if [ "$output" != "$expected" ]; then + fail "metronome bar: $output" + fi + printf 'metronome bar: %s\n' "$output" >"$scratch/info.metronome.bar" +} + test_model_smoke() { [ -f "$MODEL" ] || fail "the ONNX model is missing" @@ -492,6 +655,10 @@ run_test pipeline.fake.existing-directory test_fake_existing_directory run_test pipeline.fake.failure-cleanup test_fake_failure_cleanup run_test pipeline.fake.option-order test_fake_option_order run_test pipeline.fake.multi-chunk test_fake_multi_chunk +run_test pipeline.fake.input-formats test_fake_input_formats +run_test pipeline.fake.wav-errors test_fake_wav_errors +run_test pipeline.fake.sample-rates test_fake_sample_rates +run_test metronome.bar test_metronome_bar run_test pipeline.model.smoke test_model_smoke yes printf '\n%d passed, %d failed, %d skipped\n' "$passed" "$failed" "$skipped" diff --git a/tests/wavtool.c b/tests/wavtool.c index 6a83cf6..b2ee25d 100644 --- a/tests/wavtool.c +++ b/tests/wavtool.c @@ -15,7 +15,8 @@ #define WAV_SAMPLE_RATE UINT32_C(44100) #define WAV_BLOCK_FRAMES 4096U #define WAV_MAX_FRAMES UINT64_C(2646000) -#define WAV_HEADER_BYTES 44U +#define WAV_MAX_GENERATED_CHANNELS 3U +#define WAV_OUTPUT_HEADER_BYTES 44U #define FAKE_STEM_SUM_SCALE 3.5 #ifndef M_PI @@ -34,6 +35,8 @@ typedef struct { uint16_t channels; uint32_t sample_rate; uint16_t bits; + uint16_t valid_bits; + uint32_t channel_mask; uint16_t block_align; uint64_t frames; uint64_t data_offset; @@ -41,6 +44,11 @@ typedef struct { uint64_t file_size; } Wav; +static const uint8_t GUID_TAIL[12] = { + 0x00U, 0x00U, 0x10U, 0x00U, 0x80U, 0x00U, + 0x00U, 0xAAU, 0x00U, 0x38U, 0x9BU, 0x71U +}; + static uint16_t read_u16_le(const uint8_t *bytes) { return (uint16_t)((uint16_t)bytes[0] | ((uint16_t)bytes[1] << 8)); @@ -105,7 +113,7 @@ static bool wav_parse(Wav *wav, const char *path, char *message, { uint8_t header[12]; uint8_t chunk_header[8]; - uint8_t format_data[40]; + uint8_t format_data[64]; uint64_t riff_end; uint64_t position = 12U; bool found_format = false; @@ -157,6 +165,8 @@ static bool wav_parse(Wav *wav, const char *path, char *message, uint32_t byte_rate; uint16_t block_align; uint16_t bits; + uint16_t valid_bits; + uint32_t channel_mask = 0U; if (found_format) { (void)snprintf(message, message_size, @@ -182,15 +192,45 @@ static bool wav_parse(Wav *wav, const char *path, char *message, byte_rate = read_u32_le(format_data + 8U); block_align = read_u16_le(format_data + 12U); bits = read_u16_le(format_data + 14U); + valid_bits = bits; + if (format_tag == UINT16_C(0xFFFE)) { + uint16_t extension_size; + uint16_t subtype; + + if (chunk_size < 40U) { + (void)snprintf(message, message_size, + "'%s': extensible fmt chunk is smaller than 40 bytes", + path); + return false; + } + extension_size = read_u16_le(format_data + 16U); + if (extension_size < 22U || + (uint32_t)extension_size + 18U > chunk_size) { + (void)snprintf(message, message_size, + "'%s': invalid extensible fmt size", path); + return false; + } + valid_bits = read_u16_le(format_data + 18U); + channel_mask = read_u32_le(format_data + 20U); + subtype = read_u16_le(format_data + 24U); + if (memcmp(format_data + 28U, GUID_TAIL, sizeof(GUID_TAIL)) != 0 || + (subtype != 1U && subtype != 3U)) { + (void)snprintf(message, message_size, + "'%s': unsupported extensible subtype", path); + return false; + } + format_tag = subtype; + } if (format_tag != 1U && format_tag != 3U) { (void)snprintf(message, message_size, "'%s': unsupported WAV format tag 0x%04x", path, (unsigned)format_tag); return false; } - if (format_tag == 1U && bits != 16U) { + if (format_tag == 1U && + bits != 8U && bits != 16U && bits != 24U && bits != 32U) { (void)snprintf(message, message_size, - "'%s': PCM samples must be 16-bit, found %u bits", path, + "'%s': unsupported PCM bit depth %u", path, (unsigned)bits); return false; } @@ -200,6 +240,17 @@ static bool wav_parse(Wav *wav, const char *path, char *message, (unsigned)bits); return false; } + if (valid_bits == 0U || valid_bits > bits) { + (void)snprintf(message, message_size, + "'%s': invalid valid-bit depth %u", path, + (unsigned)valid_bits); + return false; + } + if (format_tag == 3U && valid_bits != 32U) { + (void)snprintf(message, message_size, + "'%s': float samples must have 32 valid bits", path); + return false; + } if (channels != 1U && channels != 2U) { (void)snprintf(message, message_size, "'%s': expected one or two channels, found %u", path, @@ -220,6 +271,8 @@ static bool wav_parse(Wav *wav, const char *path, char *message, wav->channels = channels; wav->sample_rate = sample_rate; wav->bits = bits; + wav->valid_bits = valid_bits; + wav->channel_mask = channel_mask; wav->block_align = block_align; found_format = true; } else if (memcmp(chunk_header, "data", 4U) == 0) { @@ -306,6 +359,61 @@ static void wav_close(Wav *wav) } } +static bool decode_pcm_sample(const Wav *wav, const uint8_t *bytes, float *sample, + char *message, size_t message_size) +{ + switch (wav->bits) { + case 8U: + *sample = ((float)(int)bytes[0] - 128.0F) / 128.0F; + break; + case 16U: + { + uint16_t encoded = read_u16_le(bytes); + int16_t value; + + memcpy(&value, &encoded, sizeof(value)); + *sample = (float)value / 32768.0F; + } break; + case 24U: + { + uint32_t encoded = (uint32_t)bytes[0] | ((uint32_t)bytes[1] << 8) | + ((uint32_t)bytes[2] << 16); + int32_t value = (encoded & UINT32_C(0x800000)) != 0U + ? (int32_t)(encoded | UINT32_C(0xFF000000)) + : (int32_t)encoded; + + *sample = (float)((double)value / 8388608.0); + } break; + case 32U: + { + uint32_t encoded = read_u32_le(bytes); + int32_t value; + + memcpy(&value, &encoded, sizeof(value)); + *sample = (float)((double)value / 2147483648.0); + } break; + default: + (void)snprintf(message, message_size, "unsupported PCM bit depth %u", + (unsigned)wav->bits); + return false; + } + return true; +} + +static bool decode_float_sample(const uint8_t *bytes, float *sample, char *message, + size_t message_size) +{ + uint32_t encoded = read_u32_le(bytes); + + memcpy(sample, &encoded, sizeof(encoded)); + if (!isfinite(*sample)) { + (void)snprintf(message, message_size, + "WAV contains a NaN or infinite float sample"); + return false; + } + return true; +} + static bool wav_read_frames(Wav *wav, uint64_t frame_offset, size_t frame_count, float *stereo, char *message, size_t message_size) { @@ -339,123 +447,475 @@ static bool wav_read_frames(Wav *wav, uint64_t frame_offset, size_t frame_count, return false; } for (frame = 0U; frame < frame_count; ++frame) { + float samples[WAV_CHANNELS] = {0.0F, 0.0F}; + for (channel = 0U; channel < wav->channels; ++channel) { const uint8_t *source = raw + frame * frame_bytes + channel * sample_bytes; - float sample; - if (wav->format_tag == 1U) { - float value = (float)(int16_t)read_u16_le(source); - sample = value / 32768.0F; - } else { - uint32_t encoded = read_u32_le(source); - memcpy(&sample, &encoded, sizeof(sample)); - if (!isfinite(sample)) { - (void)snprintf(message, message_size, - "WAV contains a NaN or infinite float sample"); - free(raw); - return false; - } + bool ok = wav->format_tag == 1U + ? decode_pcm_sample(wav, source, &samples[channel], message, + message_size) + : decode_float_sample(source, &samples[channel], message, + message_size); + + if (!ok) { + free(raw); + return false; } - stereo[frame * WAV_CHANNELS + channel] = sample; } + if (wav->channels == 1U) { + samples[1] = samples[0]; + } + stereo[frame * WAV_CHANNELS] = samples[0]; + stereo[frame * WAV_CHANNELS + 1U] = samples[1]; } free(raw); return true; } -static bool check_stereo(const Wav *wav, uint16_t format_tag, char *message, +static bool check_format(const Wav *wav, uint16_t format_tag, uint16_t channels, + uint32_t sample_rate, uint16_t bits, char *message, size_t message_size) { - if (wav->format_tag != format_tag || wav->channels != WAV_CHANNELS || - wav->sample_rate != WAV_SAMPLE_RATE) { + if (wav->format_tag != format_tag || wav->channels != channels || + wav->sample_rate != sample_rate || wav->bits != bits) { (void)snprintf(message, message_size, - "expected %s stereo %u Hz, found tag %u, %u channel(s), %u Hz", - format_tag == 1U ? "16-bit PCM" : "32-bit float", - WAV_SAMPLE_RATE, (unsigned)wav->format_tag, - (unsigned)wav->channels, wav->sample_rate); + "expected tag %u, %u channel(s), %u Hz, %u bits; " + "found tag %u, %u channel(s), %u Hz, %u bits", + (unsigned)format_tag, (unsigned)channels, sample_rate, + (unsigned)bits, (unsigned)wav->format_tag, + (unsigned)wav->channels, wav->sample_rate, + (unsigned)wav->bits); return false; } return true; } -static bool generate(const char *path, uint64_t frames) +typedef struct { + uint16_t format_tag; + uint16_t subtype; + uint16_t subtype_high; + uint16_t bits; + uint16_t channels; + uint32_t sample_rate; + uint16_t valid_bits; + uint32_t channel_mask; + bool extensible; + bool nan_sample; + bool inf_sample; + bool duplicate_fmt; + bool duplicate_data; + bool empty_data; + bool partial_frame; + bool rf64; + bool rifx; + bool bad_guid; +} Generator; + +static bool generator_for(const char *name, Generator *generator) { - uint8_t header[WAV_HEADER_BYTES]; - int16_t block[WAV_BLOCK_FRAMES * WAV_CHANNELS]; - uint64_t frame_bytes; - uint64_t data_bytes; - FILE *file; - uint64_t offset = 0U; + memset(generator, 0, sizeof(*generator)); + generator->format_tag = 1U; + generator->bits = 16U; + generator->channels = WAV_CHANNELS; + generator->sample_rate = WAV_SAMPLE_RATE; + generator->valid_bits = 16U; - frame_bytes = (uint64_t)WAV_CHANNELS * 2U; - if (frames == 0U || frames > WAV_MAX_FRAMES || - frames > (UINT64_C(0xFFFFFFFF) - WAV_HEADER_BYTES) / frame_bytes) { - fprintf(stderr, "wavtool: invalid frame count for a generated WAV\n"); - return false; - } - data_bytes = frames * frame_bytes; - memset(header, 0, sizeof(header)); - memcpy(header, "RIFF", 4U); - store_u32_le(header + 4U, (uint32_t)(data_bytes + WAV_HEADER_BYTES - 8U)); - memcpy(header + 8U, "WAVE", 4U); - memcpy(header + 12U, "fmt ", 4U); - store_u32_le(header + 16U, 16U); - store_u16_le(header + 20U, 1U); - store_u16_le(header + 22U, WAV_CHANNELS); - store_u32_le(header + 24U, WAV_SAMPLE_RATE); - store_u32_le(header + 28U, WAV_SAMPLE_RATE * WAV_CHANNELS * 2U); - store_u16_le(header + 32U, WAV_CHANNELS * 2U); - store_u16_le(header + 34U, 16U); - memcpy(header + 36U, "data", 4U); - store_u32_le(header + 40U, (uint32_t)data_bytes); + if (name == NULL || strcmp(name, "pcm16") == 0) { + return true; + } + if (strcmp(name, "pcm8-mono") == 0) { + generator->bits = 8U; + generator->valid_bits = 8U; + generator->channels = 1U; + return true; + } + if (strcmp(name, "pcm24") == 0) { + generator->bits = 24U; + generator->valid_bits = 24U; + return true; + } + if (strcmp(name, "pcm32") == 0) { + generator->bits = 32U; + generator->valid_bits = 32U; + return true; + } + if (strcmp(name, "float32") == 0) { + generator->format_tag = 3U; + generator->bits = 32U; + generator->valid_bits = 32U; + return true; + } + if (strcmp(name, "ext-pcm16") == 0) { + generator->extensible = true; + generator->subtype = 1U; + generator->valid_bits = 16U; + generator->channel_mask = UINT32_C(0x00000003); + return true; + } + if (strcmp(name, "ext-float32") == 0) { + generator->format_tag = 3U; + generator->extensible = true; + generator->subtype = 3U; + generator->bits = 32U; + generator->valid_bits = 32U; + generator->channel_mask = UINT32_C(0x00000003); + return true; + } + if (strcmp(name, "ext-bad-guid") == 0) { + generator->extensible = true; + generator->subtype = 1U; + generator->valid_bits = 16U; + generator->channel_mask = UINT32_C(0x00000003); + generator->bad_guid = true; + return true; + } + if (strcmp(name, "ext-bad-subtype") == 0) { + generator->extensible = true; + generator->subtype = 1U; + generator->subtype_high = 1U; + generator->valid_bits = 16U; + generator->channel_mask = UINT32_C(0x00000003); + return true; + } + if (strcmp(name, "alaw") == 0) { + generator->format_tag = 6U; + generator->bits = 8U; + generator->valid_bits = 8U; + return true; + } + if (strcmp(name, "3ch") == 0) { + generator->channels = 3U; + return true; + } + if (strcmp(name, "rate7999") == 0) { + generator->sample_rate = UINT32_C(7999); + return true; + } + if (strcmp(name, "rate8000") == 0) { + generator->sample_rate = UINT32_C(8000); + return true; + } + if (strcmp(name, "rate768000") == 0) { + generator->sample_rate = UINT32_C(768000); + return true; + } + if (strcmp(name, "rate768001") == 0) { + generator->sample_rate = UINT32_C(768001); + return true; + } + if (strcmp(name, "partial-frame") == 0) { + generator->partial_frame = true; + return true; + } + if (strcmp(name, "nan") == 0) { + generator->format_tag = 3U; + generator->bits = 32U; + generator->valid_bits = 32U; + generator->nan_sample = true; + return true; + } + if (strcmp(name, "inf") == 0) { + generator->format_tag = 3U; + generator->bits = 32U; + generator->valid_bits = 32U; + generator->inf_sample = true; + return true; + } + if (strcmp(name, "dup-fmt") == 0) { + generator->duplicate_fmt = true; + return true; + } + if (strcmp(name, "dup-data") == 0) { + generator->duplicate_data = true; + return true; + } + if (strcmp(name, "empty-data") == 0) { + generator->empty_data = true; + return true; + } + if (strcmp(name, "rf64") == 0) { + generator->rf64 = true; + return true; + } + if (strcmp(name, "rifx") == 0) { + generator->rifx = true; + return true; + } + return false; +} - file = fopen(path, "wb"); - if (file == NULL) { - fprintf(stderr, "wavtool: cannot create '%s': %s\n", path, strerror(errno)); +static bool write_bytes(FILE *file, const void *data, size_t size) +{ + return fwrite(data, 1U, size, file) == size; +} + +static bool write_u16(FILE *file, uint16_t value) +{ + uint8_t bytes[2]; + + store_u16_le(bytes, value); + return write_bytes(file, bytes, sizeof(bytes)); +} + +static bool write_u32(FILE *file, uint32_t value) +{ + uint8_t bytes[4]; + + store_u32_le(bytes, value); + return write_bytes(file, bytes, sizeof(bytes)); +} + +static double signal_sample(const Generator *generator, uint64_t position, + unsigned channel) +{ + double seconds = (double)position / (double)generator->sample_rate; + double left = 0.5 * sin(2.0 * M_PI * 440.0 * seconds) + + 0.25 * sin(2.0 * M_PI * 1320.0 * seconds); + double right = 0.5 * sin(2.0 * M_PI * 523.25 * seconds) - + 0.25 * sin(2.0 * M_PI * 110.0 * seconds); + + if (channel == 0U) { + return left; + } + if (channel == 1U) { + return right; + } + return 0.2 * sin(2.0 * M_PI * (660.0 * (double)channel) * seconds); +} + +static void encode_sample(const Generator *generator, uint64_t position, + double value, uint8_t *output) +{ + if (generator->format_tag == 1U) { + switch (generator->bits) { + case 8U: + { + long quantized = lrint(value * 127.0) + 128L; + + if (quantized < 0L) { + quantized = 0L; + } + if (quantized > 255L) { + quantized = 255L; + } + output[0] = (uint8_t)quantized; + } break; + case 16U: + { + long quantized = lrint(value * 32767.0); + + store_u16_le(output, (uint16_t)((uint32_t)quantized & 0xFFFFU)); + } break; + case 24U: + { + long quantized = lrint(value * 8388607.0); + uint32_t encoded = (uint32_t)quantized; + + output[0] = (uint8_t)(encoded & 0xFFU); + output[1] = (uint8_t)((encoded >> 8) & 0xFFU); + output[2] = (uint8_t)((encoded >> 16) & 0xFFU); + } break; + case 32U: + { + long long quantized = llrint(value * 2147483647.0); + + store_u32_le(output, (uint32_t)quantized); + } break; + default: + break; + } + } else if (generator->format_tag == 3U) { + float sample = (float)value; + uint32_t encoded; + + if (generator->nan_sample && position == 0U) { + sample = NAN; + } else if (generator->inf_sample && position == 0U) { + sample = INFINITY; + } + memcpy(&encoded, &sample, sizeof(encoded)); + store_u32_le(output, encoded); + } else { + output[0] = 128U; + } +} + +static bool write_fmt_chunk(FILE *file, const Generator *generator, uint32_t fmt_size) +{ + uint16_t format_tag = generator->extensible ? UINT16_C(0xFFFE) + : generator->format_tag; + uint32_t block_align = + (uint32_t)generator->channels * ((uint32_t)generator->bits / 8U); + uint32_t byte_rate = generator->sample_rate * block_align; + uint8_t guid_tail[sizeof(GUID_TAIL)]; + + memcpy(guid_tail, GUID_TAIL, sizeof(guid_tail)); + if (generator->bad_guid) { + guid_tail[sizeof(guid_tail) - 1U] = 0x72U; + } + if (!write_bytes(file, "fmt ", 4U) || !write_u32(file, fmt_size) || + !write_u16(file, format_tag) || !write_u16(file, generator->channels) || + !write_u32(file, generator->sample_rate) || + !write_u32(file, byte_rate) || !write_u16(file, (uint16_t)block_align) || + !write_u16(file, generator->bits)) { return false; } - if (fwrite(header, 1U, sizeof(header), file) != sizeof(header)) { - fprintf(stderr, "wavtool: cannot write '%s': %s\n", path, strerror(errno)); - (void)fclose(file); + if (!generator->extensible) { + return true; + } + return write_u16(file, 22U) && write_u16(file, generator->valid_bits) && + write_u32(file, generator->channel_mask) && + write_u16(file, generator->subtype) && + write_u16(file, generator->subtype_high) && + write_bytes(file, guid_tail, sizeof(guid_tail)); +} + +static bool write_data_chunk(FILE *file, const Generator *generator, + uint64_t data_bytes, uint64_t frames) +{ + uint8_t block[WAV_BLOCK_FRAMES * 4U * WAV_MAX_GENERATED_CHANNELS]; + size_t sample_bytes = (size_t)generator->bits / 8U; + size_t block_align = (size_t)generator->channels * sample_bytes; + uint64_t written = 0U; + uint64_t frame_offset = 0U; + + if (!write_bytes(file, "data", 4U) || + !write_u32(file, (uint32_t)data_bytes)) { return false; } - while (offset < frames) { - size_t count = (size_t)(frames - offset); + while (written < data_bytes) { + size_t count = (size_t)(frames - frame_offset); + size_t block_bytes; size_t index; + unsigned channel; + if (count > WAV_BLOCK_FRAMES) { count = WAV_BLOCK_FRAMES; } + if (count == 0U) { + return false; + } for (index = 0U; index < count; ++index) { - uint64_t position = offset + index; - double seconds = (double)position / (double)WAV_SAMPLE_RATE; - double left = 0.5 * sin(2.0 * M_PI * 440.0 * seconds) + - 0.25 * sin(2.0 * M_PI * 1320.0 * seconds); - double right = 0.5 * sin(2.0 * M_PI * 523.25 * seconds) - - 0.25 * sin(2.0 * M_PI * 110.0 * seconds); - long quantized_left = lrint(left * 32767.0); - long quantized_right = lrint(right * 32767.0); - store_u16_le((uint8_t *)&block[index * WAV_CHANNELS], - (uint16_t)((uint32_t)quantized_left & 0xFFFFU)); - store_u16_le((uint8_t *)&block[index * WAV_CHANNELS + 1U], - (uint16_t)((uint32_t)quantized_right & 0xFFFFU)); + for (channel = 0U; channel < generator->channels; ++channel) { + uint64_t position = frame_offset + index; + double value = signal_sample(generator, position, channel); + + encode_sample(generator, position, value, + block + index * block_align + channel * sample_bytes); + } + } + block_bytes = count * block_align; + if (block_bytes > data_bytes - written) { + block_bytes = (size_t)(data_bytes - written); + } + if (!write_bytes(file, block, block_bytes)) { + return false; + } + written += block_bytes; + frame_offset += count; + } + if ((data_bytes & 1U) != 0U) { + uint8_t zero = 0U; + + return write_bytes(file, &zero, 1U); + } + return true; +} + +static bool generate(const char *path, uint64_t frames, const char *case_name) +{ + Generator generator; + uint64_t block_align; + uint64_t data_bytes; + uint64_t fmt_bytes; + uint64_t data_chunk_bytes; + uint64_t total; + FILE *file; + bool ok; + + if (!generator_for(case_name, &generator)) { + fprintf(stderr, "wavtool: unknown WAV case '%s'\n", case_name); + return false; + } + if (frames == 0U || frames > WAV_MAX_FRAMES || + generator.channels == 0U || + generator.channels > WAV_MAX_GENERATED_CHANNELS || + generator.bits == 0U || (generator.bits % 8U) != 0U) { + fprintf(stderr, "wavtool: invalid parameters for a generated WAV\n"); + return false; + } + block_align = (uint64_t)generator.channels * (generator.bits / 8U); + if (frames > UINT64_MAX / block_align) { + fprintf(stderr, "wavtool: generated WAV is too long\n"); + return false; + } + data_bytes = frames * block_align; + if (generator.empty_data) { + data_bytes = 0U; + } else if (generator.partial_frame) { + if (data_bytes < 2U) { + fprintf(stderr, "wavtool: generated WAV is too short\n"); + return false; + } + data_bytes -= 2U; + } + fmt_bytes = 8U + (generator.extensible ? 40U : 16U); + data_chunk_bytes = 8U + data_bytes + (data_bytes & 1U); + total = 12U + (generator.duplicate_fmt ? 2U * fmt_bytes : fmt_bytes) + + (generator.duplicate_data ? 2U * data_chunk_bytes : data_chunk_bytes); + if (total > (uint64_t)UINT32_MAX + 8U) { + fprintf(stderr, "wavtool: generated WAV exceeds the RIFF limit\n"); + return false; + } + + file = fopen(path, "wb"); + if (file == NULL) { + fprintf(stderr, "wavtool: cannot create '%s': %s\n", path, strerror(errno)); + return false; + } + if (generator.rf64 || generator.rifx) { + const char *magic = generator.rf64 ? "RF64" : "RIFX"; + bool ok = write_bytes(file, magic, 4U) && write_u32(file, 0U) && + write_bytes(file, "WAVE", 4U); + + if (fclose(file) != 0) { + ok = false; } - if (fwrite(block, sizeof(int16_t), count * WAV_CHANNELS, file) != - count * WAV_CHANNELS) { + if (!ok) { fprintf(stderr, "wavtool: cannot write '%s': %s\n", path, strerror(errno)); - (void)fclose(file); return false; } - offset += count; + printf("frames=%" PRIu64 " rate=%u channels=%u format=%s bits=%u\n", + frames, (unsigned)generator.sample_rate, + (unsigned)generator.channels, magic, (unsigned)generator.bits); + return true; } + + ok = write_bytes(file, "RIFF", 4U) && + write_u32(file, (uint32_t)(total - 8U)) && + write_bytes(file, "WAVE", 4U) && + write_fmt_chunk(file, &generator, generator.extensible ? 40U : 16U) && + (!generator.duplicate_fmt || + write_fmt_chunk(file, &generator, generator.extensible ? 40U : 16U)) && + write_data_chunk(file, &generator, data_bytes, frames) && + (!generator.duplicate_data || + write_data_chunk(file, &generator, data_bytes, frames)); if (fclose(file) != 0) { - fprintf(stderr, "wavtool: cannot close '%s': %s\n", path, strerror(errno)); + ok = false; + } + if (!ok) { + fprintf(stderr, "wavtool: cannot write '%s': %s\n", path, + errno != 0 ? strerror(errno) : "I/O error"); return false; } - printf("frames=%" PRIu64 " rate=%u channels=%u format=1 bits=16\n", frames, - (unsigned)WAV_SAMPLE_RATE, (unsigned)WAV_CHANNELS); + printf("frames=%" PRIu64 " rate=%u channels=%u format=%u bits=%u\n", frames, + (unsigned)generator.sample_rate, (unsigned)generator.channels, + (unsigned)generator.format_tag, (unsigned)generator.bits); return true; } -static bool inspect(const char *path, uint64_t expected_frames) +static bool inspect(const char *path, uint64_t expected_frames, + uint16_t expected_channels) { Wav wav; char message[256] = ""; @@ -466,8 +926,17 @@ static bool inspect(const char *path, uint64_t expected_frames) fprintf(stderr, "wavtool: %s\n", message); return false; } - if (!check_stereo(&wav, 3U, message, sizeof(message)) || - wav.bits != 32U || wav.frames != expected_frames) { + if (wav.data_offset != WAV_OUTPUT_HEADER_BYTES) { + fprintf(stderr, + "wavtool: '%s': expected a %u-byte WAV header, found data offset %" + PRIu64 "\n", + path, (unsigned)WAV_OUTPUT_HEADER_BYTES, wav.data_offset); + wav_close(&wav); + return false; + } + if (!check_format(&wav, 3U, expected_channels, WAV_SAMPLE_RATE, 32U, message, + sizeof(message)) || + wav.frames != expected_frames) { if (message[0] == '\0') { (void)snprintf(message, sizeof(message), "expected %" PRIu64 " frames, found %" PRIu64, @@ -495,8 +964,9 @@ static bool inspect(const char *path, uint64_t expected_frames) offset += count; } wav_close(&wav); - printf("format=3 channels=2 rate=44100 bits=32 frames=%" PRIu64 " finite=yes\n", - wav.frames); + printf("format=3 channels=%u rate=44100 bits=32 frames=%" PRIu64 + " finite=yes\n", + (unsigned)expected_channels, wav.frames); return true; } @@ -540,8 +1010,14 @@ static bool reconstruction(const char *tolerance_text, const char *input_path, fprintf(stderr, "wavtool: %s\n", message); return false; } - if (!check_stereo(&input, 1U, message, sizeof(message)) || - input.bits != 16U) { + if ((input.format_tag != 1U && input.format_tag != 3U) || + (input.channels != 1U && input.channels != 2U) || + input.sample_rate != WAV_SAMPLE_RATE) { + (void)snprintf(message, sizeof(message), + "expected a supported 44100 Hz WAV input; found tag %u, " + "%u channel(s), %u Hz", + (unsigned)input.format_tag, (unsigned)input.channels, + input.sample_rate); fprintf(stderr, "wavtool: '%s': %s\n", input_path, message); wav_close(&input); return false; @@ -554,8 +1030,9 @@ static bool reconstruction(const char *tolerance_text, const char *input_path, fprintf(stderr, "wavtool: %s\n", message); goto cleanup; } - if (!check_stereo(&stems[stem], 3U, message, sizeof(message)) || - stems[stem].bits != 32U || stems[stem].frames != input.frames) { + if (!check_format(&stems[stem], 3U, WAV_CHANNELS, WAV_SAMPLE_RATE, 32U, + message, sizeof(message)) || + stems[stem].frames != input.frames) { if (message[0] == '\0') { (void)snprintf(message, sizeof(message), "expected %" PRIu64 " frames, found %" PRIu64, @@ -633,28 +1110,41 @@ static void print_usage(void) { fprintf(stderr, "Usage:\n" - " wavtool gen <path> <frames>\n" - " wavtool inspect <path> <expected_frames>\n" + " wavtool gen <path> <frames> [case]\n" + " wavtool inspect <path> <expected_frames> [channels]\n" " wavtool recon <tolerance> <input.wav> <stem_dir> <identifier>\n"); } int main(int argc, char **argv) { - if (argc == 4 && strcmp(argv[1], "gen") == 0) { + if ((argc == 4 || argc == 5) && strcmp(argv[1], "gen") == 0) { uint64_t frames; + const char *case_name = argc == 5 ? argv[4] : NULL; + if (!parse_u64(argv[3], &frames)) { fprintf(stderr, "wavtool: invalid frame count '%s'\n", argv[3]); return 2; } - return generate(argv[2], frames) ? 0 : 1; + return generate(argv[2], frames, case_name) ? 0 : 1; } - if (argc == 4 && strcmp(argv[1], "inspect") == 0) { + if ((argc == 4 || argc == 5) && strcmp(argv[1], "inspect") == 0) { uint64_t frames; + uint64_t channels = WAV_CHANNELS; + if (!parse_u64(argv[3], &frames)) { fprintf(stderr, "wavtool: invalid expected frame count '%s'\n", argv[3]); return 2; } - return inspect(argv[2], frames) ? 0 : 1; + if (argc == 5 && !parse_u64(argv[4], &channels)) { + fprintf(stderr, "wavtool: invalid channel count '%s'\n", argv[4]); + return 2; + } + if (channels != 1U && channels != 2U) { + fprintf(stderr, "wavtool: expected one or two channels, found %" PRIu64 + "\n", channels); + return 2; + } + return inspect(argv[2], frames, (uint16_t)channels) ? 0 : 1; } if (argc == 6 && strcmp(argv[1], "recon") == 0) { return reconstruction(argv[2], argv[3], argv[4], argv[5]) ? 0 : 1; |
