diff --git a/src/metaflac/Makefile.am b/src/metaflac/Makefile.am index 80eb7238..5e869516 100644 --- a/src/metaflac/Makefile.am +++ b/src/metaflac/Makefile.am @@ -26,6 +26,7 @@ EXTRA_DIST = \ metaflac_SOURCES = \ main.c \ operations.c \ + operations_shorthand_cuesheet.c \ operations_shorthand_seektable.c \ operations_shorthand_streaminfo.c \ operations_shorthand_vorbiscomment.c \ diff --git a/src/metaflac/Makefile.lite b/src/metaflac/Makefile.lite index 1bac6dae..bd67358c 100644 --- a/src/metaflac/Makefile.lite +++ b/src/metaflac/Makefile.lite @@ -28,6 +28,7 @@ LIBS = -lgrabbag -lFLAC -lgain_analysis -lgetopt -lutf8 -lm SRCS_C = \ main.c \ operations.c \ + operations_shorthand_cuesheet.c \ operations_shorthand_seektable.c \ operations_shorthand_streaminfo.c \ operations_shorthand_vorbiscomment.c \ diff --git a/src/metaflac/Makefile.vc b/src/metaflac/Makefile.vc index 6d5060c1..4b7d66a6 100644 --- a/src/metaflac/Makefile.vc +++ b/src/metaflac/Makefile.vc @@ -28,6 +28,7 @@ C_FILES= \ main.c \ operations.c \ + operations_shorthand_cuesheet.c \ operations_shorthand_seektable.c \ operations_shorthand_streaminfo.c \ operations_shorthand_vorbiscomment.c \ diff --git a/src/metaflac/operations.c b/src/metaflac/operations.c index e1dda856..a105c88a 100644 --- a/src/metaflac/operations.c +++ b/src/metaflac/operations.c @@ -51,6 +51,10 @@ extern FLAC__bool do_shorthand_operation__streaminfo(const char *filename, FLAC_ /* from operations_shorthand_vorbiscomment.c */ extern FLAC__bool do_shorthand_operation__vorbis_comment(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool raw); +/* from operations_shorthand_cuesheet.c */ +extern FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool cued_seekpoints); + + FLAC__bool do_operations(const CommandLineOptions *options) { FLAC__bool ok = true; @@ -329,6 +333,10 @@ FLAC__bool do_shorthand_operation(const char *filename, FLAC__bool prefix_with_f case OP__EXPORT_VC_TO: ok = do_shorthand_operation__vorbis_comment(filename, prefix_with_filename, chain, operation, needs_write, !utf8_convert); break; + case OP__IMPORT_CUESHEET_FROM: + case OP__EXPORT_CUESHEET_TO: + ok = do_shorthand_operation__cuesheet(filename, chain, operation, needs_write, /*@@@@cued_seekpoints=*/true); + break; case OP__ADD_SEEKPOINT: ok = do_shorthand_operation__add_seekpoints(filename, chain, operation->argument.add_seekpoint.specification, needs_write); break; @@ -565,6 +573,10 @@ void write_metadata(const char *filename, FLAC__StreamMetadata *block, unsigned write_vc_field(0, &block->data.vorbis_comment.comments[i], raw, stdout); } break; + case FLAC__METADATA_TYPE_CUESHEET: + PPR; printf(" media catalog number: %s\n", block->data.cue_sheet.media_catalog_number); + //@@@@ finish + break; default: PPR; printf("SKIPPING block of unknown type\n"); break; diff --git a/src/metaflac/operations_shorthand_cuesheet.c b/src/metaflac/operations_shorthand_cuesheet.c new file mode 100644 index 00000000..ae17b8c0 --- /dev/null +++ b/src/metaflac/operations_shorthand_cuesheet.c @@ -0,0 +1,184 @@ +/* metaflac - Command-line FLAC metadata editor + * Copyright (C) 2001,2002 Josh Coalson + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "options.h" +#include "utils.h" +#include "FLAC/assert.h" +#include "share/grabbag.h" +#include + +static FLAC__bool import_cs_from(const char *filename, FLAC__StreamMetadata **cuesheet, const Argument_Filename *cs_filename, FLAC__bool *needs_write, FLAC__uint64 lead_out_offset, FLAC__StreamMetadata *seektable); +static FLAC__bool export_cs_to(const char *filename, FLAC__StreamMetadata *cuesheet, const Argument_Filename *cs_filename); + +FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool cued_seekpoints) +{ + FLAC__bool ok = true; + FLAC__StreamMetadata *cuesheet = 0, *seektable = 0; + FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new(); + FLAC__uint64 lead_out_offset; + + if(0 == iterator) + die("out of memory allocating iterator"); + + FLAC__metadata_iterator_init(iterator, chain); + + do { + FLAC__StreamMetadata *block = FLAC__metadata_iterator_get_block(iterator); + if(block->type == FLAC__METADATA_TYPE_STREAMINFO) { + lead_out_offset = block->data.stream_info.total_samples; + if(lead_out_offset == 0) { + fprintf(stderr, "%s: ERROR: FLAC file must have total_samples set in STREAMINFO in order to import/export cuesheet\n", filename); + FLAC__metadata_iterator_delete(iterator); + return false; + } + if(block->data.stream_info.sample_rate != 44100) { + fprintf(stderr, "%s: ERROR: FLAC stream must currently be 44.1kHz in order to import/export cuesheet\n", filename); + FLAC__metadata_iterator_delete(iterator); + return false; + } + } + else if(block->type == FLAC__METADATA_TYPE_CUESHEET) + cuesheet = block; + else if(block->type == FLAC__METADATA_TYPE_SEEKTABLE) + seektable = block; + } while(FLAC__metadata_iterator_next(iterator)); + + switch(operation->type) { + case OP__IMPORT_CUESHEET_FROM: + if(0 != cuesheet) { + fprintf(stderr, "%s: ERROR: FLAC file already has CUESHEET block\n", filename); + ok = false; + } + else { + /* create a new SEEKTABLE block if necessary */ + if(cued_seekpoints && 0 == seektable) { + seektable = FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE); + if(0 == seektable) + die("out of memory allocating SEEKTABLE block"); + while(FLAC__metadata_iterator_prev(iterator)) + ; + if(!FLAC__metadata_iterator_insert_block_after(iterator, seektable)) { + fprintf(stderr, "%s: ERROR: adding new SEEKTABLE block to metadata, status =\"%s\"\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]); + FLAC__metadata_object_delete(seektable); + FLAC__metadata_object_delete(cuesheet); + ok = false; + } + } + else + seektable = 0; + ok = import_cs_from(filename, &cuesheet, &operation->argument.filename, needs_write, lead_out_offset, seektable); + if(!ok && 0 != seektable) + FLAC__metadata_object_delete(seektable); + else { + /* append CUESHEET block */ + while(FLAC__metadata_iterator_next(iterator)) + ; + if(!FLAC__metadata_iterator_insert_block_after(iterator, cuesheet)) { + fprintf(stderr, "%s: ERROR: adding new CUESHEET block to metadata, status =\"%s\"\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]); + FLAC__metadata_object_delete(cuesheet); + ok = false; + } + } + } + break; + case OP__EXPORT_CUESHEET_TO: + if(0 == cuesheet) { + fprintf(stderr, "%s: ERROR: FLAC file has no CUESHEET block\n", filename); + ok = false; + } + else + ok = export_cs_to(filename, cuesheet, &operation->argument.filename); + break; + default: + ok = false; + FLAC__ASSERT(0); + break; + }; + + FLAC__metadata_iterator_delete(iterator); + return ok; +} + +/* + * local routines + */ + +FLAC__bool import_cs_from(const char *filename, FLAC__StreamMetadata **cuesheet, const Argument_Filename *cs_filename, FLAC__bool *needs_write, FLAC__uint64 lead_out_offset, FLAC__StreamMetadata *seektable) +{ + FILE *f; + const char *error_message; + unsigned last_line_read; + + if(0 == cs_filename->value || strlen(cs_filename->value) == 0) { + fprintf(stderr, "%s: ERROR: empty import file name\n", filename); + return false; + } + if(0 == strcmp(cs_filename->value, "-")) + f = stdin; + else + f = fopen(cs_filename->value, "r"); + + if(0 == f) { + fprintf(stderr, "%s: ERROR: can't open import file %s\n", filename, cs_filename->value); + return false; + } + + *cuesheet = grabbag__cuesheet_parse(f, &error_message, &last_line_read, /*is_cdda=*/true, lead_out_offset); + + if(f != stdin) + fclose(f); + + if(0 == *cuesheet) { + fprintf(stderr, "%s: ERROR: while parsing cuesheet, line %u, %s\n", filename, last_line_read, error_message); + return false; + } + + /* add seekpoints for each index point if required */ + if(0 != seektable) { + //@@@@ + } + + *needs_write = true; + return true; +} + +FLAC__bool export_cs_to(const char *filename, FLAC__StreamMetadata *cuesheet, const Argument_Filename *cs_filename) +{ + FILE *f; + + if(0 == cs_filename->value || strlen(cs_filename->value) == 0) { + fprintf(stderr, "%s: ERROR: empty export file name\n", filename); + return false; + } + if(0 == strcmp(cs_filename->value, "-")) + f = stdout; + else + f = fopen(cs_filename->value, "w"); + + if(0 == f) { + fprintf(stderr, "%s: ERROR: can't open export file %s\n", filename, cs_filename->value); + return false; + } + + grabbag__cuesheet_emit(f, cuesheet, "DUMMY.WAV", /*is_cdda=*/true); + + if(f != stdout) + fclose(f); + + return true; +} diff --git a/src/metaflac/operations_shorthand_vorbiscomment.c b/src/metaflac/operations_shorthand_vorbiscomment.c index 85170e95..fc0d1d64 100644 --- a/src/metaflac/operations_shorthand_vorbiscomment.c +++ b/src/metaflac/operations_shorthand_vorbiscomment.c @@ -27,8 +27,8 @@ static FLAC__bool remove_vc_all(const char *filename, FLAC__StreamMetadata *bloc static FLAC__bool remove_vc_field(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write); static FLAC__bool remove_vc_firstfield(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write); static FLAC__bool set_vc_field(const char *filename, FLAC__StreamMetadata *block, const Argument_VcField *field, FLAC__bool *needs_write, FLAC__bool raw); -static FLAC__bool import_vc_from(const char *filename, FLAC__StreamMetadata *block, const Argument_VcFilename *vc_filename, FLAC__bool *needs_write, FLAC__bool raw); -static FLAC__bool export_vc_to(const char *filename, FLAC__StreamMetadata *block, const Argument_VcFilename *vc_filename, FLAC__bool raw); +static FLAC__bool import_vc_from(const char *filename, FLAC__StreamMetadata *block, const Argument_Filename *vc_filename, FLAC__bool *needs_write, FLAC__bool raw); +static FLAC__bool export_vc_to(const char *filename, FLAC__StreamMetadata *block, const Argument_Filename *vc_filename, FLAC__bool raw); FLAC__bool do_shorthand_operation__vorbis_comment(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool raw) { @@ -91,10 +91,10 @@ FLAC__bool do_shorthand_operation__vorbis_comment(const char *filename, FLAC__bo ok = set_vc_field(filename, block, &operation->argument.vc_field, needs_write, raw); break; case OP__IMPORT_VC_FROM: - ok = import_vc_from(filename, block, &operation->argument.vc_filename, needs_write, raw); + ok = import_vc_from(filename, block, &operation->argument.filename, needs_write, raw); break; case OP__EXPORT_VC_TO: - ok = export_vc_to(filename, block, &operation->argument.vc_filename, raw); + ok = export_vc_to(filename, block, &operation->argument.filename, raw); break; default: ok = false; @@ -206,7 +206,7 @@ FLAC__bool set_vc_field(const char *filename, FLAC__StreamMetadata *block, const } } -FLAC__bool import_vc_from(const char *filename, FLAC__StreamMetadata *block, const Argument_VcFilename *vc_filename, FLAC__bool *needs_write, FLAC__bool raw) +FLAC__bool import_vc_from(const char *filename, FLAC__StreamMetadata *block, const Argument_Filename *vc_filename, FLAC__bool *needs_write, FLAC__bool raw) { FILE *f; char line[65536]; @@ -263,7 +263,7 @@ FLAC__bool import_vc_from(const char *filename, FLAC__StreamMetadata *block, con return ret; } -FLAC__bool export_vc_to(const char *filename, FLAC__StreamMetadata *block, const Argument_VcFilename *vc_filename, FLAC__bool raw) +FLAC__bool export_vc_to(const char *filename, FLAC__StreamMetadata *block, const Argument_Filename *vc_filename, FLAC__bool raw) { FILE *f; FLAC__bool ret; diff --git a/src/metaflac/options.c b/src/metaflac/options.c index 6363b009..5155719c 100644 --- a/src/metaflac/options.c +++ b/src/metaflac/options.c @@ -63,6 +63,8 @@ struct share__option long_options_[] = { { "set-vc-field", 1, 0, 0 }, { "import-vc-from", 1, 0, 0 }, { "export-vc-to", 1, 0, 0 }, + { "import-cuesheet-from", 1, 0, 0 }, + { "export-cuesheet-to", 1, 0, 0 }, { "add-seekpoint", 1, 0, 0 }, { "add-replay-gain", 0, 0, 0 }, { "add-padding", 1, 0, 0 }, @@ -188,6 +190,8 @@ FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options) } } + //@@@@ check for only one FLAC file used with --import-cuesheet-from/--export-cuesheet-to + if(options->args.checks.has_block_type && options->args.checks.has_except_block_type) { fprintf(stderr, "ERROR: you may not specify both '--block-type' and '--except-block-type'\n"); had_error = true; @@ -226,8 +230,10 @@ void free_options(CommandLineOptions *options) break; case OP__IMPORT_VC_FROM: case OP__EXPORT_VC_TO: - if(0 != op->argument.vc_filename.value) - free(op->argument.vc_filename.value); + case OP__IMPORT_CUESHEET_FROM: + case OP__EXPORT_CUESHEET_TO: + if(0 != op->argument.filename.value) + free(op->argument.filename.value); break; case OP__ADD_SEEKPOINT: if(0 != op->argument.add_seekpoint.specification) @@ -452,7 +458,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi else if(0 == strcmp(opt, "import-vc-from")) { op = append_shorthand_operation(options, OP__IMPORT_VC_FROM); FLAC__ASSERT(0 != option_argument); - if(!parse_filename(option_argument, &(op->argument.vc_filename.value))) { + if(!parse_filename(option_argument, &(op->argument.filename.value))) { fprintf(stderr, "ERROR (--%s): missing filename\n", opt); ok = false; } @@ -460,7 +466,23 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi else if(0 == strcmp(opt, "export-vc-to")) { op = append_shorthand_operation(options, OP__EXPORT_VC_TO); FLAC__ASSERT(0 != option_argument); - if(!parse_filename(option_argument, &(op->argument.vc_filename.value))) { + if(!parse_filename(option_argument, &(op->argument.filename.value))) { + fprintf(stderr, "ERROR (--%s): missing filename\n", opt); + ok = false; + } + } + else if(0 == strcmp(opt, "import-cuesheet-from")) { + op = append_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM); + FLAC__ASSERT(0 != option_argument); + if(!parse_filename(option_argument, &(op->argument.filename.value))) { + fprintf(stderr, "ERROR (--%s): missing filename\n", opt); + ok = false; + } + } + else if(0 == strcmp(opt, "export-cuesheet-to")) { + op = append_shorthand_operation(options, OP__EXPORT_CUESHEET_TO); + FLAC__ASSERT(0 != option_argument); + if(!parse_filename(option_argument, &(op->argument.filename.value))) { fprintf(stderr, "ERROR (--%s): missing filename\n", opt); ok = false; } diff --git a/src/metaflac/options.h b/src/metaflac/options.h index 42fef8f6..f92d00ab 100644 --- a/src/metaflac/options.h +++ b/src/metaflac/options.h @@ -58,6 +58,8 @@ typedef enum { OP__SET_VC_FIELD, OP__IMPORT_VC_FROM, OP__EXPORT_VC_TO, + OP__IMPORT_CUESHEET_FROM, + OP__EXPORT_CUESHEET_TO, OP__ADD_SEEKPOINT, OP__ADD_REPLAY_GAIN, OP__ADD_PADDING, @@ -103,7 +105,7 @@ typedef struct { typedef struct { char *value; -} Argument_VcFilename; +} Argument_Filename; typedef struct { unsigned num_entries; @@ -145,7 +147,7 @@ typedef struct { Argument_StreaminfoUInt64 streaminfo_uint64; Argument_VcFieldName vc_field_name; Argument_VcField vc_field; - Argument_VcFilename vc_filename; + Argument_Filename filename; Argument_AddSeekpoint add_seekpoint; Argument_AddPadding add_padding; } argument; diff --git a/src/metaflac/usage.c b/src/metaflac/usage.c index eb239543..e8a7d11f 100644 --- a/src/metaflac/usage.c +++ b/src/metaflac/usage.c @@ -117,12 +117,10 @@ int long_usage(const char *message, ...) fprintf(out, "--show-vc-vendor Show the vendor string from the VORBIS_COMMENT block.\n"); fprintf(out, "--show-vc-field=name Show all Vorbis comment fields where the the field name\n"); fprintf(out, " matches 'name'.\n"); - fprintf(out, "--remove-vc-field=name\n"); - fprintf(out, " Remove all Vorbis comment fields whose field name is\n"); - fprintf(out, " 'name'.\n"); - fprintf(out, "--remove-vc-firstfield=name\n"); - fprintf(out, " Remove first Vorbis comment field whose field name is\n"); - fprintf(out, " 'name'.\n"); + fprintf(out, "--remove-vc-field=name Remove all Vorbis comment fields whose field name\n"); + fprintf(out, " is 'name'.\n"); + fprintf(out, "--remove-vc-firstfield=name Remove first Vorbis comment field whose field\n"); + fprintf(out, " name is 'name'.\n"); fprintf(out, "--remove-vc-all Remove all Vorbis comment fields, leaving only the\n"); fprintf(out, " vendor string in the VORBIS_COMMENT block.\n"); fprintf(out, "--set-vc-field=field Add a Vorbis comment field. The field must comply with\n"); @@ -137,6 +135,13 @@ int long_usage(const char *message, ...) fprintf(out, "--export-vc-to=file Export Vorbis comments to a file. Use '-' for stdin.\n"); fprintf(out, " Each line will be of the form NAME=VALUE. Specify\n"); fprintf(out, " --no-utf8-convert if necessary.\n"); + fprintf(out, "--import-cuesheet-from=file Import a cuesheet from a file. Only one FLAC\n"); + fprintf(out, " file may be specified. Seekpoints for all indices in\n"); + fprintf(out, " the cuesheet will be added unless --no-cued-seekpoints\n"); + fprintf(out, " is specified.\n"); + fprintf(out, "--export-cuesheet-to=file Export CUESHEET block to a cuesheet file, suitable\n"); + fprintf(out, " for use by CD authoring software. Use '-' for stdin.\n"); + fprintf(out, " Only one FLAC file may be specified on the command line.\n"); fprintf(out, "--add-replay-gain Calculates the title and album gains/peaks of the given\n"); fprintf(out, " FLAC files as if all the files were part of one album,\n"); fprintf(out, " then stores them in the VORBIS_COMMENT block. The tags\n");