1 /* metaflac - Command-line FLAC metadata editor
2 * Copyright (C) 2001,2002 Josh Coalson
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "FLAC/assert.h"
22 #include "share/grabbag.h"
25 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);
26 static FLAC__bool export_cs_to(const char *filename, FLAC__StreamMetadata *cuesheet, const Argument_Filename *cs_filename);
28 FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool cued_seekpoints)
31 FLAC__StreamMetadata *cuesheet = 0, *seektable = 0;
32 FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
33 FLAC__uint64 lead_out_offset;
36 die("out of memory allocating iterator");
38 FLAC__metadata_iterator_init(iterator, chain);
41 FLAC__StreamMetadata *block = FLAC__metadata_iterator_get_block(iterator);
42 if(block->type == FLAC__METADATA_TYPE_STREAMINFO) {
43 lead_out_offset = block->data.stream_info.total_samples;
44 if(lead_out_offset == 0) {
45 fprintf(stderr, "%s: ERROR: FLAC file must have total_samples set in STREAMINFO in order to import/export cuesheet\n", filename);
46 FLAC__metadata_iterator_delete(iterator);
49 if(block->data.stream_info.sample_rate != 44100) {
50 fprintf(stderr, "%s: ERROR: FLAC stream must currently be 44.1kHz in order to import/export cuesheet\n", filename);
51 FLAC__metadata_iterator_delete(iterator);
55 else if(block->type == FLAC__METADATA_TYPE_CUESHEET)
57 else if(block->type == FLAC__METADATA_TYPE_SEEKTABLE)
59 } while(FLAC__metadata_iterator_next(iterator));
61 switch(operation->type) {
62 case OP__IMPORT_CUESHEET_FROM:
64 fprintf(stderr, "%s: ERROR: FLAC file already has CUESHEET block\n", filename);
68 /* create a new SEEKTABLE block if necessary */
69 if(cued_seekpoints && 0 == seektable) {
70 seektable = FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE);
72 die("out of memory allocating SEEKTABLE block");
73 while(FLAC__metadata_iterator_prev(iterator))
75 if(!FLAC__metadata_iterator_insert_block_after(iterator, seektable)) {
76 fprintf(stderr, "%s: ERROR: adding new SEEKTABLE block to metadata, status =\"%s\"\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]);
77 FLAC__metadata_object_delete(seektable);
78 FLAC__metadata_object_delete(cuesheet);
84 ok = import_cs_from(filename, &cuesheet, &operation->argument.filename, needs_write, lead_out_offset, seektable);
85 if(!ok && 0 != seektable)
86 FLAC__metadata_object_delete(seektable);
88 /* append CUESHEET block */
89 while(FLAC__metadata_iterator_next(iterator))
91 if(!FLAC__metadata_iterator_insert_block_after(iterator, cuesheet)) {
92 fprintf(stderr, "%s: ERROR: adding new CUESHEET block to metadata, status =\"%s\"\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]);
93 FLAC__metadata_object_delete(cuesheet);
99 case OP__EXPORT_CUESHEET_TO:
101 fprintf(stderr, "%s: ERROR: FLAC file has no CUESHEET block\n", filename);
105 ok = export_cs_to(filename, cuesheet, &operation->argument.filename);
113 FLAC__metadata_iterator_delete(iterator);
121 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)
124 const char *error_message;
125 unsigned last_line_read;
127 if(0 == cs_filename->value || strlen(cs_filename->value) == 0) {
128 fprintf(stderr, "%s: ERROR: empty import file name\n", filename);
131 if(0 == strcmp(cs_filename->value, "-"))
134 f = fopen(cs_filename->value, "r");
137 fprintf(stderr, "%s: ERROR: can't open import file %s\n", filename, cs_filename->value);
141 *cuesheet = grabbag__cuesheet_parse(f, &error_message, &last_line_read, /*is_cdda=*/true, lead_out_offset);
147 fprintf(stderr, "%s: ERROR: while parsing cuesheet, line %u, %s\n", filename, last_line_read, error_message);
151 /* add seekpoints for each index point if required */
160 FLAC__bool export_cs_to(const char *filename, FLAC__StreamMetadata *cuesheet, const Argument_Filename *cs_filename)
164 if(0 == cs_filename->value || strlen(cs_filename->value) == 0) {
165 fprintf(stderr, "%s: ERROR: empty export file name\n", filename);
168 if(0 == strcmp(cs_filename->value, "-"))
171 f = fopen(cs_filename->value, "w");
174 fprintf(stderr, "%s: ERROR: can't open export file %s\n", filename, cs_filename->value);
178 grabbag__cuesheet_emit(f, cuesheet, "DUMMY.WAV", /*is_cdda=*/true);