summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c251
1 files changed, 78 insertions, 173 deletions
diff --git a/main.c b/main.c
index 39da637..8088961 100644
--- a/main.c
+++ b/main.c
@@ -21,9 +21,9 @@
#include <unistd.h>
#include "audio.h"
-#include "bytes.h"
#include "metronome.h"
#include "playback.h"
+#include "wav_writer.h"
#ifndef STEMSEP_TEST_FAKE_MODEL
#include "onnxruntime_c_api.h"
@@ -306,17 +306,6 @@ static bool regular_readable_file(const char *path, Error *error, const char *de
return true;
}
-static bool seek_file(FILE *file, uint64_t offset, Error *error, const char *description)
-{
- if (offset > (uint64_t)INT64_MAX) {
- return set_error(error, "%s offset is too large", description);
- }
- if (fseeko(file, (off_t)offset, SEEK_SET) != 0) {
- return set_error(error, "cannot seek to %s: %s", description, strerror(errno));
- }
- return true;
-}
-
typedef struct {
AudioSource *reader;
float *frames;
@@ -637,17 +626,16 @@ static bool resampler_next(Resampler *resampler, float output[2], Error *error)
}
typedef struct {
- FILE *file;
+ WavWriter *writer;
char *partial_path;
char *final_path;
- uint64_t frames_written;
bool partial_created;
bool renamed;
-} WavWriter;
+} OutputFile;
typedef struct {
char *directory;
- WavWriter writers[MODEL_STEMS];
+ OutputFile files[MODEL_STEMS];
bool directory_created;
bool committed;
} OutputSet;
@@ -656,53 +644,19 @@ static const char *const STEM_NAMES[MODEL_STEMS] = {
"drums", "bass", "other", "vocals", "guitar", "piano"
};
-static bool write_exact(FILE *file, const void *buffer, size_t bytes,
- Error *error, const char *description)
-{
- if (bytes != 0U) {
- size_t written = fwrite(buffer, 1U, bytes, file);
- if (written != bytes) {
- if (ferror(file)) {
- return set_error(error, "cannot write %s: %s", description,
- errno != 0 ? strerror(errno) : "I/O error");
- }
- return set_error(error, "short write while writing %s", description);
- }
- }
- return true;
-}
-
-static bool wav_writer_write_placeholder(WavWriter *writer, Error *error)
-{
- uint8_t header[56];
-
- memset(header, 0, sizeof(header));
- memcpy(header, "RIFF", 4U);
- memcpy(header + 8U, "WAVE", 4U);
- memcpy(header + 12U, "fmt ", 4U);
- store_u32_le(header + 16U, 16U);
- store_u16_le(header + 20U, 3U);
- store_u16_le(header + 22U, MODEL_CHANNELS);
- store_u32_le(header + 24U, MODEL_SAMPLE_RATE);
- store_u32_le(header + 28U, MODEL_SAMPLE_RATE * MODEL_CHANNELS * 4U);
- store_u16_le(header + 32U, MODEL_CHANNELS * 4U);
- store_u16_le(header + 34U, 32U);
- memcpy(header + 36U, "fact", 4U);
- store_u32_le(header + 40U, 4U);
- memcpy(header + 48U, "data", 4U);
- return write_exact(writer->file, header, sizeof(header), error, "WAV header");
-}
-
-static bool wav_writer_open(WavWriter *writer, const char *directory,
- const char *stem_name, Error *error)
+static bool output_file_open(OutputFile *file, const char *directory,
+ const char *stem_name, uint64_t expected_frames,
+ Error *error)
{
size_t name_length = strlen(stem_name);
char *final_name = NULL;
char *partial_name = NULL;
+ char message[768];
+ FILE *stream;
int descriptor;
- int flags = O_RDWR | O_CREAT | O_EXCL;
+ int flags = O_WRONLY | O_CREAT | O_EXCL;
- memset(writer, 0, sizeof(*writer));
+ memset(file, 0, sizeof(*file));
if (name_length > SIZE_MAX - sizeof(".wav.part")) {
return set_error(error, "output filename is too long");
}
@@ -716,11 +670,11 @@ static bool wav_writer_open(WavWriter *writer, const char *directory,
(void)snprintf(final_name, name_length + sizeof(".wav"), "%s.wav", stem_name);
(void)snprintf(partial_name, name_length + sizeof(".wav.part"), "%s.wav.part",
stem_name);
- writer->final_path = join_path(directory, final_name, error);
- writer->partial_path = join_path(directory, partial_name, error);
+ file->final_path = join_path(directory, final_name, error);
+ file->partial_path = join_path(directory, partial_name, error);
free(final_name);
free(partial_name);
- if (writer->final_path == NULL || writer->partial_path == NULL) {
+ if (file->final_path == NULL || file->partial_path == NULL) {
return false;
}
@@ -730,134 +684,85 @@ static bool wav_writer_open(WavWriter *writer, const char *directory,
#ifdef O_NOFOLLOW
flags |= O_NOFOLLOW;
#endif
- descriptor = open(writer->partial_path, flags, 0644);
+ descriptor = open(file->partial_path, flags, 0644);
if (descriptor < 0) {
- return set_error(error, "cannot create output '%s': %s", writer->partial_path,
+ return set_error(error, "cannot create output '%s': %s", file->partial_path,
strerror(errno));
}
- writer->partial_created = true;
- writer->file = fdopen(descriptor, "wb+");
- if (writer->file == NULL) {
+ file->partial_created = true;
+ stream = fdopen(descriptor, "wb");
+ if (stream == NULL) {
int saved_errno = errno;
(void)close(descriptor);
- (void)unlink(writer->partial_path);
- writer->partial_created = false;
+ (void)unlink(file->partial_path);
+ file->partial_created = false;
return set_error(error, "cannot open output stream '%s': %s",
- writer->partial_path, strerror(saved_errno));
+ file->partial_path, strerror(saved_errno));
}
- if (!wav_writer_write_placeholder(writer, error)) {
- return false;
+ message[0] = '\0';
+ file->writer = wav_writer_open(stream, file->partial_path, MODEL_SAMPLE_RATE,
+ MODEL_CHANNELS, expected_frames, message,
+ sizeof(message));
+ if (file->writer == NULL) {
+ (void)unlink(file->partial_path);
+ file->partial_created = false;
+ return set_error(error, "%s",
+ message[0] != '\0' ? message : "cannot create output WAV");
}
return true;
}
-static bool wav_writer_write_frames(WavWriter *writer, const float *interleaved,
- size_t frame_count, Error *error)
+static bool output_file_write(OutputFile *file, const float *interleaved,
+ size_t frame_count, Error *error)
{
- uint8_t bytes[WAV_WRITE_BLOCK_FRAMES * MODEL_CHANNELS * sizeof(float)];
- size_t offset = 0U;
-
- while (offset < frame_count) {
- size_t block = frame_count - offset;
- size_t sample;
- if (block > WAV_WRITE_BLOCK_FRAMES) {
- block = WAV_WRITE_BLOCK_FRAMES;
- }
- for (sample = 0U; sample < block * MODEL_CHANNELS; ++sample) {
- float value = interleaved[(offset * MODEL_CHANNELS) + sample];
- uint32_t encoded;
- if (!isfinite(value)) {
- return set_error(error, "model produced a NaN or infinite sample for %s",
- writer->final_path);
- }
- memcpy(&encoded, &value, sizeof(encoded));
- store_u32_le(bytes + sample * sizeof(float), encoded);
- }
- if (!write_exact(writer->file, bytes,
- block * MODEL_CHANNELS * sizeof(float), error,
- writer->partial_path)) {
- return false;
- }
- writer->frames_written += block;
- offset += block;
+ char message[768];
+
+ message[0] = '\0';
+ if (!wav_writer_write(file->writer, interleaved, frame_count, message,
+ sizeof(message))) {
+ return set_error(error, "%s",
+ message[0] != '\0' ? message : "cannot write the output WAV");
}
return true;
}
-static bool wav_writer_finish(WavWriter *writer, uint64_t expected_frames,
- Error *error)
+static bool output_file_finish(OutputFile *file, Error *error)
{
- uint64_t data_bytes_64;
- uint32_t data_bytes;
- uint8_t value[4];
- int close_result;
-
- if (writer->frames_written != expected_frames) {
- return set_error(error, "internal output duration mismatch for '%s'",
- writer->final_path);
- }
- if (!checked_mul_u64(writer->frames_written, MODEL_CHANNELS * sizeof(float),
- &data_bytes_64) || data_bytes_64 > UINT32_MAX - 48U) {
- return set_error(error, "output '%s' exceeds the standard RIFF 4 GiB limit",
- writer->final_path);
- }
- data_bytes = (uint32_t)data_bytes_64;
+ char message[768];
+ bool ok;
- if (!seek_file(writer->file, 4U, error, "RIFF size field")) {
- return false;
- }
- store_u32_le(value, data_bytes + 48U);
- if (!write_exact(writer->file, value, sizeof(value), error, "RIFF size")) {
- return false;
- }
- if (!seek_file(writer->file, 44U, error, "WAV fact sample count")) {
- return false;
- }
- store_u32_le(value, (uint32_t)writer->frames_written);
- if (!write_exact(writer->file, value, sizeof(value), error, "WAV fact sample count")) {
- return false;
- }
- if (!seek_file(writer->file, 52U, error, "WAV data size")) {
- return false;
- }
- store_u32_le(value, data_bytes);
- if (!write_exact(writer->file, value, sizeof(value), error, "WAV data size")) {
- return false;
- }
- if (fflush(writer->file) != 0) {
- return set_error(error, "cannot flush output '%s': %s", writer->partial_path,
- strerror(errno));
- }
- close_result = fclose(writer->file);
- writer->file = NULL;
- if (close_result != 0) {
- return set_error(error, "cannot close output '%s': %s", writer->partial_path,
- strerror(errno));
+ message[0] = '\0';
+ ok = wav_writer_finish(file->writer, message, sizeof(message));
+ file->writer = NULL;
+ if (!ok) {
+ return set_error(error, "%s",
+ message[0] != '\0' ? message : "cannot finish the output WAV");
}
return true;
}
-static void wav_writer_cleanup(WavWriter *writer, bool remove_files)
+static void output_file_cleanup(OutputFile *file, bool remove_files)
{
- if (writer->file != NULL) {
- (void)fclose(writer->file);
- writer->file = NULL;
+ if (file->writer != NULL) {
+ wav_writer_abort(file->writer);
+ file->writer = NULL;
}
if (remove_files) {
- if (writer->partial_created && writer->partial_path != NULL) {
- (void)unlink(writer->partial_path);
+ if (file->partial_created && file->partial_path != NULL) {
+ (void)unlink(file->partial_path);
}
- if (writer->renamed && writer->final_path != NULL) {
- (void)unlink(writer->final_path);
+ if (file->renamed && file->final_path != NULL) {
+ (void)unlink(file->final_path);
}
}
- free(writer->partial_path);
- free(writer->final_path);
- memset(writer, 0, sizeof(*writer));
+ free(file->partial_path);
+ free(file->final_path);
+ memset(file, 0, sizeof(*file));
}
static bool output_set_create(OutputSet *outputs, const char *directory,
- const char *identifier, Error *error)
+ const char *identifier, uint64_t expected_frames,
+ Error *error)
{
unsigned stem;
@@ -889,8 +794,8 @@ static bool output_set_create(OutputSet *outputs, const char *directory,
}
(void)snprintf(prefixed_name, identifier_length + stem_length + 2U,
"%s_%s", identifier, STEM_NAMES[stem]);
- if (!wav_writer_open(&outputs->writers[stem], directory, prefixed_name,
- error)) {
+ if (!output_file_open(&outputs->files[stem], directory, prefixed_name,
+ expected_frames, error)) {
free(prefixed_name);
return false;
}
@@ -899,24 +804,23 @@ static bool output_set_create(OutputSet *outputs, const char *directory,
return true;
}
-static bool output_set_commit(OutputSet *outputs, uint64_t expected_frames,
- Error *error)
+static bool output_set_commit(OutputSet *outputs, Error *error)
{
unsigned stem;
for (stem = 0U; stem < MODEL_STEMS; ++stem) {
- if (!wav_writer_finish(&outputs->writers[stem], expected_frames, error)) {
+ if (!output_file_finish(&outputs->files[stem], error)) {
return false;
}
}
for (stem = 0U; stem < MODEL_STEMS; ++stem) {
- if (rename(outputs->writers[stem].partial_path,
- outputs->writers[stem].final_path) != 0) {
+ if (rename(outputs->files[stem].partial_path,
+ outputs->files[stem].final_path) != 0) {
return set_error(error, "cannot finalize output '%s': %s",
- outputs->writers[stem].final_path, strerror(errno));
+ outputs->files[stem].final_path, strerror(errno));
}
- outputs->writers[stem].partial_created = false;
- outputs->writers[stem].renamed = true;
+ outputs->files[stem].partial_created = false;
+ outputs->files[stem].renamed = true;
}
outputs->committed = true;
return true;
@@ -928,7 +832,7 @@ static void output_set_cleanup(OutputSet *outputs)
bool remove_files = !outputs->committed;
for (stem = 0U; stem < MODEL_STEMS; ++stem) {
- wav_writer_cleanup(&outputs->writers[stem], remove_files);
+ output_file_cleanup(&outputs->files[stem], remove_files);
}
if (remove_files && outputs->directory_created && outputs->directory != NULL) {
(void)rmdir(outputs->directory);
@@ -1366,8 +1270,8 @@ static bool write_finalized_audio(OutputSet *outputs, const float *accumulator,
interleaved[frame * MODEL_CHANNELS + channel] = value;
}
}
- if (!wav_writer_write_frames(&outputs->writers[stem], interleaved,
- block, error)) {
+ if (!output_file_write(&outputs->files[stem], interleaved, block,
+ error)) {
return false;
}
offset += block;
@@ -1628,7 +1532,7 @@ int main(int argc, char **argv)
}
if (!checked_mul_u64(resampler.output_frames,
MODEL_CHANNELS * sizeof(float), &output_bytes) ||
- output_bytes > UINT32_MAX - 48U) {
+ output_bytes > UINT32_MAX - 36U) {
(void)set_error(&error,
"resampled audio is too long for standard RIFF WAV output");
exit_code = EXIT_OUTPUT;
@@ -1680,7 +1584,8 @@ int main(int argc, char **argv)
exit_code = EXIT_MODEL;
goto cleanup;
}
- if (!output_set_create(&outputs, output_directory, command.identifier, &error)) {
+ if (!output_set_create(&outputs, output_directory, command.identifier,
+ resampler.output_frames, &error)) {
exit_code = EXIT_OUTPUT;
goto cleanup;
}
@@ -1688,7 +1593,7 @@ int main(int argc, char **argv)
weight, window, &exit_code, &error)) {
goto cleanup;
}
- if (!output_set_commit(&outputs, resampler.output_frames, &error)) {
+ if (!output_set_commit(&outputs, &error)) {
exit_code = EXIT_OUTPUT;
goto cleanup;
}