From 9a77ac6e2623eaf9685320b5f37242c1f76af896 Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Mon, 11 Nov 2024 15:29:44 +0100 Subject: [PATCH] Copy metadata Without cover art yet --- include/audiofile.h | 2 ++ src/audiofile.c | 27 +++++++++++++++++++++++++++ src/main.c | 10 ++++++---- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/include/audiofile.h b/include/audiofile.h index 4bc8e7e..667065c 100644 --- a/include/audiofile.h +++ b/include/audiofile.h @@ -2,8 +2,10 @@ #define AUDIOFILE_H #include +#include SNDFILE *audiofile_read(const char *path, SF_INFO *sfinfo); SNDFILE *audiofile_open_for_write(const char *path, SF_INFO *og_info); +int audiofile_copy_metadata(SNDFILE *source, SNDFILE *target); #endif \ No newline at end of file diff --git a/src/audiofile.c b/src/audiofile.c index 97260dd..20727d8 100644 --- a/src/audiofile.c +++ b/src/audiofile.c @@ -38,4 +38,31 @@ SNDFILE *audiofile_open_for_write(const char *path, SF_INFO *og_info) { } return sndfile; +} + +int audiofile_copy_metadata(SNDFILE *source, SNDFILE *target) { + int err; + SF_BROADCAST_INFO binfo; + + if ((err = sf_command(source, SFC_GET_BROADCAST_INFO, &binfo, sizeof(binfo))) != 0) { + printf("Failed reading BROADCAST_INFO: %s\n", sf_error_number(err)); + return 1; + } + if ((err = sf_command(target, SFC_SET_BROADCAST_INFO, &binfo, sizeof(binfo))) != 0) { + printf("Failed writing BROADCAST_INFO: %s\n", sf_error_number(err)); + return 1; + } + + + for (int i=SF_STR_FIRST; i<=SF_STR_LAST; i++) { + const char *data = sf_get_string(source, i); + if (data != NULL) { + if ((err = sf_set_string(target, i, data)) != 0) { + printf("Failed writing metadata %d (%s): %s\n", i, data, sf_error_number(err)); + return 1; + } + } + } + + return 0; } \ No newline at end of file diff --git a/src/main.c b/src/main.c index a564045..07e9164 100644 --- a/src/main.c +++ b/src/main.c @@ -38,8 +38,8 @@ int main(int argc, char *argv[]) { // load audio file SF_INFO sfinfo = {0}; - SNDFILE *sndfile; - if ((sndfile = audiofile_read(input_filename, &sfinfo)) == NULL) { + SNDFILE *in_sndfile; + if ((in_sndfile = audiofile_read(input_filename, &sfinfo)) == NULL) { return 1; } @@ -47,6 +47,8 @@ int main(int argc, char *argv[]) { if ((out_sndfile = audiofile_open_for_write(output_filename, &sfinfo)) == NULL) { return 1; } + + audiofile_copy_metadata(in_sndfile, out_sndfile); // load lilv plugin @@ -90,7 +92,7 @@ int main(int argc, char *argv[]) { float input_buffer[BUFFER_SIZE * 2]; float output_buffer[BUFFER_SIZE * 2]; int frames_read; - while ((frames_read = sf_readf_float(sndfile, input_buffer, BUFFER_SIZE))) { + while ((frames_read = sf_readf_float(in_sndfile, input_buffer, BUFFER_SIZE))) { // a frame is 2 samples and a sample is a number basically and it go left right left right (or how many channels) for (sf_count_t i = 0; i < frames_read; i++) { input_left[i] = input_buffer[i * 2]; @@ -117,7 +119,7 @@ int main(int argc, char *argv[]) { lilv_world_free(world); - sf_close(sndfile); + sf_close(in_sndfile); sf_close(out_sndfile); }