1 /* metaflac - Command-line FLAC metadata editor
2 * Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008,2009 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 along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <stdio.h> /* for snprintf() */
28 #include "FLAC/assert.h"
29 #include "share/grabbag.h"
30 #include "share/compat.h"
31 #include "operations_shorthand.h"
33 static FLAC__bool import_cs_from(const char *filename, FLAC__StreamMetadata **cuesheet, const char *cs_filename, FLAC__bool *needs_write, FLAC__uint64 lead_out_offset, unsigned sample_rate, FLAC__bool is_cdda, Argument_AddSeekpoint *add_seekpoint_link);
34 static FLAC__bool export_cs_to(const char *filename, const FLAC__StreamMetadata *cuesheet, const char *cs_filename);
36 FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write)
39 FLAC__StreamMetadata *cuesheet = 0;
40 FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
41 FLAC__uint64 lead_out_offset = 0;
42 FLAC__bool is_cdda = false;
43 unsigned sample_rate = 0;
46 die("out of memory allocating iterator");
48 FLAC__metadata_iterator_init(iterator, chain);
51 FLAC__StreamMetadata *block = FLAC__metadata_iterator_get_block(iterator);
52 if(block->type == FLAC__METADATA_TYPE_STREAMINFO) {
53 lead_out_offset = block->data.stream_info.total_samples;
54 if(lead_out_offset == 0) {
55 fprintf(stderr, "%s: ERROR: FLAC file must have total_samples set in STREAMINFO in order to import/export cuesheet\n", filename);
56 FLAC__metadata_iterator_delete(iterator);
59 sample_rate = block->data.stream_info.sample_rate;
60 is_cdda = (block->data.stream_info.channels == 1 || block->data.stream_info.channels == 2) && (block->data.stream_info.bits_per_sample == 16) && (sample_rate == 44100);
62 else if(block->type == FLAC__METADATA_TYPE_CUESHEET)
64 } while(FLAC__metadata_iterator_next(iterator));
66 if(lead_out_offset == 0) {
67 fprintf(stderr, "%s: ERROR: FLAC stream has no STREAMINFO block\n", filename);
68 FLAC__metadata_iterator_delete(iterator);
72 switch(operation->type) {
73 case OP__IMPORT_CUESHEET_FROM:
75 fprintf(stderr, "%s: ERROR: FLAC file already has CUESHEET block\n", filename);
79 ok = import_cs_from(filename, &cuesheet, operation->argument.import_cuesheet_from.filename, needs_write, lead_out_offset, sample_rate, is_cdda, operation->argument.import_cuesheet_from.add_seekpoint_link);
81 /* append CUESHEET block */
82 while(FLAC__metadata_iterator_next(iterator))
84 if(!FLAC__metadata_iterator_insert_block_after(iterator, cuesheet)) {
85 print_error_with_chain_status(chain, "%s: ERROR: adding new CUESHEET block to metadata", filename);
86 FLAC__metadata_object_delete(cuesheet);
92 case OP__EXPORT_CUESHEET_TO:
94 fprintf(stderr, "%s: ERROR: FLAC file has no CUESHEET block\n", filename);
98 ok = export_cs_to(filename, cuesheet, operation->argument.filename.value);
106 FLAC__metadata_iterator_delete(iterator);
114 FLAC__bool import_cs_from(const char *filename, FLAC__StreamMetadata **cuesheet, const char *cs_filename, FLAC__bool *needs_write, FLAC__uint64 lead_out_offset, unsigned sample_rate, FLAC__bool is_cdda, Argument_AddSeekpoint *add_seekpoint_link)
117 const char *error_message;
118 char **seekpoint_specification = add_seekpoint_link? &(add_seekpoint_link->specification) : 0;
119 unsigned last_line_read;
121 if(0 == cs_filename || strlen(cs_filename) == 0) {
122 fprintf(stderr, "%s: ERROR: empty import file name\n", filename);
125 if(0 == strcmp(cs_filename, "-"))
128 f = fopen(cs_filename, "r");
131 fprintf(stderr, "%s: ERROR: can't open import file %s: %s\n", filename, cs_filename, strerror(errno));
135 *cuesheet = grabbag__cuesheet_parse(f, &error_message, &last_line_read, sample_rate, is_cdda, lead_out_offset);
141 fprintf(stderr, "%s: ERROR: while parsing cuesheet \"%s\" on line %u: %s\n", filename, cs_filename, last_line_read, error_message);
145 if(!FLAC__format_cuesheet_is_legal(&(*cuesheet)->data.cue_sheet, /*check_cd_da_subset=*/false, &error_message)) {
146 fprintf(stderr, "%s: ERROR parsing cuesheet \"%s\": %s\n", filename, cs_filename, error_message);
150 /* if we're expecting CDDA, warn about non-compliance */
151 if(is_cdda && !FLAC__format_cuesheet_is_legal(&(*cuesheet)->data.cue_sheet, /*check_cd_da_subset=*/true, &error_message)) {
152 fprintf(stderr, "%s: WARNING cuesheet \"%s\" is not audio CD compliant: %s\n", filename, cs_filename, error_message);
153 (*cuesheet)->data.cue_sheet.is_cd = false;
156 /* add seekpoints for each index point if required */
157 if(0 != seekpoint_specification) {
159 unsigned track, index;
160 const FLAC__StreamMetadata_CueSheet *cs = &(*cuesheet)->data.cue_sheet;
161 if(0 == *seekpoint_specification)
162 *seekpoint_specification = local_strdup("");
163 for(track = 0; track < cs->num_tracks; track++) {
164 const FLAC__StreamMetadata_CueSheet_Track *tr = cs->tracks+track;
165 for(index = 0; index < tr->num_indices; index++) {
166 sprintf(spec, "%" PRIu64 ";", (tr->offset + tr->indices[index].offset));
167 local_strcat(seekpoint_specification, spec);
176 FLAC__bool export_cs_to(const char *filename, const FLAC__StreamMetadata *cuesheet, const char *cs_filename)
182 if(0 == cs_filename || strlen(cs_filename) == 0) {
183 fprintf(stderr, "%s: ERROR: empty export file name\n", filename);
186 if(0 == strcmp(cs_filename, "-"))
189 f = fopen(cs_filename, "w");
192 fprintf(stderr, "%s: ERROR: can't open export file %s: %s\n", filename, cs_filename, strerror(errno));
196 reflen = strlen(filename) + 7 + 1;
197 if(0 == (ref = malloc(reflen))) {
198 fprintf(stderr, "%s: ERROR: allocating memory\n", filename);
204 #if defined _MSC_VER || defined __MINGW32__
205 _snprintf(ref, reflen, "\"%s\" FLAC", filename);
207 snprintf(ref, reflen, "\"%s\" FLAC", filename);
210 grabbag__cuesheet_emit(f, cuesheet, ref);