2009-03-13 18:43:16 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003-2009 The Music Player Daemon Project
|
|
|
|
* http://www.musicpd.org
|
2006-03-16 07:52:46 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Common data structures and functions used by FLAC and OggFLAC
|
2006-03-16 07:52:46 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "_flac_common.h"
|
|
|
|
|
2009-01-01 18:09:24 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
2009-01-15 22:45:28 +01:00
|
|
|
#include <assert.h>
|
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
void
|
|
|
|
flac_data_init(struct flac_data *data, struct decoder * decoder,
|
|
|
|
struct input_stream *input_stream)
|
2006-03-16 07:52:46 +01:00
|
|
|
{
|
2009-11-10 21:46:10 +01:00
|
|
|
pcm_buffer_init(&data->buffer);
|
|
|
|
|
2006-03-16 07:52:46 +01:00
|
|
|
data->time = 0;
|
|
|
|
data->position = 0;
|
2009-01-15 19:50:28 +01:00
|
|
|
data->bit_rate = 0;
|
2008-08-26 08:27:04 +02:00
|
|
|
data->decoder = decoder;
|
2009-01-15 19:50:28 +01:00
|
|
|
data->input_stream = input_stream;
|
|
|
|
data->replay_gain_info = NULL;
|
2006-03-16 07:52:46 +01:00
|
|
|
data->tag = NULL;
|
|
|
|
}
|
|
|
|
|
2009-11-10 21:42:15 +01:00
|
|
|
void
|
|
|
|
flac_data_deinit(struct flac_data *data)
|
|
|
|
{
|
2009-11-10 21:46:10 +01:00
|
|
|
pcm_buffer_deinit(&data->buffer);
|
|
|
|
|
2009-11-10 21:42:15 +01:00
|
|
|
if (data->replay_gain_info != NULL)
|
|
|
|
replay_gain_info_free(data->replay_gain_info);
|
|
|
|
|
|
|
|
if (data->tag != NULL)
|
|
|
|
tag_free(data->tag);
|
|
|
|
}
|
|
|
|
|
2009-07-22 13:31:48 +02:00
|
|
|
static void
|
2009-01-15 19:50:28 +01:00
|
|
|
flac_find_float_comment(const FLAC__StreamMetadata *block,
|
2009-07-22 13:31:48 +02:00
|
|
|
const char *cmnt, float *fl, bool *found_r)
|
2006-03-16 07:52:46 +01:00
|
|
|
{
|
2009-07-22 13:31:46 +02:00
|
|
|
int offset;
|
|
|
|
size_t pos;
|
|
|
|
int len;
|
|
|
|
unsigned char tmp, *p;
|
2006-03-16 07:52:46 +01:00
|
|
|
|
2009-07-22 13:31:46 +02:00
|
|
|
offset = FLAC__metadata_object_vorbiscomment_find_entry_from(block, 0,
|
|
|
|
cmnt);
|
|
|
|
if (offset < 0)
|
2009-07-22 13:31:48 +02:00
|
|
|
return;
|
2006-03-16 07:52:46 +01:00
|
|
|
|
2009-07-22 13:31:46 +02:00
|
|
|
pos = strlen(cmnt) + 1; /* 1 is for '=' */
|
|
|
|
len = block->data.vorbis_comment.comments[offset].length - pos;
|
|
|
|
if (len <= 0)
|
2009-07-22 13:31:48 +02:00
|
|
|
return;
|
2006-03-16 07:52:46 +01:00
|
|
|
|
2009-07-22 13:31:46 +02:00
|
|
|
p = &block->data.vorbis_comment.comments[offset].entry[pos];
|
|
|
|
tmp = p[len];
|
|
|
|
p[len] = '\0';
|
|
|
|
*fl = (float)atof((char *)p);
|
|
|
|
p[len] = tmp;
|
|
|
|
|
2009-07-22 13:31:48 +02:00
|
|
|
*found_r = true;
|
2006-03-16 07:52:46 +01:00
|
|
|
}
|
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
static void
|
|
|
|
flac_parse_replay_gain(const FLAC__StreamMetadata *block,
|
|
|
|
struct flac_data *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-07-22 13:31:48 +02:00
|
|
|
bool found = false;
|
2006-03-16 07:52:46 +01:00
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
if (data->replay_gain_info)
|
|
|
|
replay_gain_info_free(data->replay_gain_info);
|
2006-03-16 07:52:46 +01:00
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
data->replay_gain_info = replay_gain_info_new();
|
2006-03-16 07:52:46 +01:00
|
|
|
|
2009-07-22 13:31:48 +02:00
|
|
|
flac_find_float_comment(block, "replaygain_album_gain",
|
|
|
|
&data->replay_gain_info->tuples[REPLAY_GAIN_ALBUM].gain,
|
|
|
|
&found);
|
|
|
|
flac_find_float_comment(block, "replaygain_album_peak",
|
|
|
|
&data->replay_gain_info->tuples[REPLAY_GAIN_ALBUM].peak,
|
|
|
|
&found);
|
|
|
|
flac_find_float_comment(block, "replaygain_track_gain",
|
|
|
|
&data->replay_gain_info->tuples[REPLAY_GAIN_TRACK].gain,
|
|
|
|
&found);
|
|
|
|
flac_find_float_comment(block, "replaygain_track_peak",
|
|
|
|
&data->replay_gain_info->tuples[REPLAY_GAIN_TRACK].peak,
|
|
|
|
&found);
|
2006-03-16 07:52:46 +01:00
|
|
|
|
|
|
|
if (!found) {
|
2009-01-15 19:50:28 +01:00
|
|
|
replay_gain_info_free(data->replay_gain_info);
|
|
|
|
data->replay_gain_info = NULL;
|
2006-03-16 07:52:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-15 22:44:21 +01:00
|
|
|
/**
|
|
|
|
* Checks if the specified name matches the entry's name, and if yes,
|
|
|
|
* returns the comment value (not null-temrinated).
|
|
|
|
*/
|
|
|
|
static const char *
|
|
|
|
flac_comment_value(const FLAC__StreamMetadata_VorbisComment_Entry *entry,
|
2009-03-15 23:31:07 +01:00
|
|
|
const char *name, const char *char_tnum, size_t *length_r)
|
2009-01-15 22:44:21 +01:00
|
|
|
{
|
|
|
|
size_t name_length = strlen(name);
|
2009-03-17 11:36:09 +01:00
|
|
|
size_t char_tnum_length = 0;
|
2009-01-15 22:44:21 +01:00
|
|
|
const char *comment = (const char*)entry->entry;
|
|
|
|
|
2009-07-22 13:34:33 +02:00
|
|
|
if (entry->length <= name_length ||
|
|
|
|
g_ascii_strncasecmp(comment, name, name_length) != 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (char_tnum != NULL) {
|
|
|
|
char_tnum_length = strlen(char_tnum);
|
|
|
|
if (entry->length > name_length + char_tnum_length + 2 &&
|
|
|
|
comment[name_length] == '[' &&
|
|
|
|
g_ascii_strncasecmp(comment + name_length + 1,
|
|
|
|
char_tnum, char_tnum_length) == 0 &&
|
|
|
|
comment[name_length + char_tnum_length + 1] == ']')
|
|
|
|
name_length = name_length + char_tnum_length + 2;
|
|
|
|
else if (entry->length > name_length + char_tnum_length &&
|
|
|
|
g_ascii_strncasecmp(comment + name_length,
|
|
|
|
char_tnum, char_tnum_length) == 0)
|
|
|
|
name_length = name_length + char_tnum_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (comment[name_length] == '=') {
|
|
|
|
*length_r = entry->length - name_length - 1;
|
|
|
|
return comment + name_length + 1;
|
2009-01-15 22:44:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-01-15 22:45:28 +01:00
|
|
|
/**
|
|
|
|
* Check if the comment's name equals the passed name, and if so, copy
|
|
|
|
* the comment value into the tag.
|
|
|
|
*/
|
2009-01-15 19:57:57 +01:00
|
|
|
static bool
|
2009-01-15 22:45:28 +01:00
|
|
|
flac_copy_comment(struct tag *tag,
|
|
|
|
const FLAC__StreamMetadata_VorbisComment_Entry *entry,
|
2009-03-15 23:31:07 +01:00
|
|
|
const char *name, enum tag_type tag_type,
|
|
|
|
const char *char_tnum)
|
2006-03-16 07:52:46 +01:00
|
|
|
{
|
2009-01-15 22:44:21 +01:00
|
|
|
const char *value;
|
|
|
|
size_t value_length;
|
2006-06-04 13:36:02 +02:00
|
|
|
|
2009-03-15 23:31:07 +01:00
|
|
|
value = flac_comment_value(entry, name, char_tnum, &value_length);
|
2009-01-15 22:44:21 +01:00
|
|
|
if (value != NULL) {
|
2009-01-15 22:45:28 +01:00
|
|
|
tag_add_item_n(tag, tag_type, value, value_length);
|
2009-01-15 19:57:57 +01:00
|
|
|
return true;
|
2006-03-16 07:52:46 +01:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2009-01-15 19:57:57 +01:00
|
|
|
return false;
|
2006-03-16 07:52:46 +01:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2009-01-15 22:45:28 +01:00
|
|
|
/* tracknumber is used in VCs, MPD uses "track" ..., all the other
|
|
|
|
* tag names match */
|
|
|
|
static const char *VORBIS_COMMENT_TRACK_KEY = "tracknumber";
|
|
|
|
static const char *VORBIS_COMMENT_DISC_KEY = "discnumber";
|
|
|
|
|
|
|
|
static void
|
2009-03-15 23:31:07 +01:00
|
|
|
flac_parse_comment(struct tag *tag, const char *char_tnum,
|
2009-01-15 22:45:28 +01:00
|
|
|
const FLAC__StreamMetadata_VorbisComment_Entry *entry)
|
|
|
|
{
|
|
|
|
assert(tag != NULL);
|
|
|
|
|
|
|
|
if (flac_copy_comment(tag, entry, VORBIS_COMMENT_TRACK_KEY,
|
2009-10-13 16:12:45 +02:00
|
|
|
TAG_TRACK, char_tnum) ||
|
2009-01-15 22:45:28 +01:00
|
|
|
flac_copy_comment(tag, entry, VORBIS_COMMENT_DISC_KEY,
|
2009-10-13 16:12:45 +02:00
|
|
|
TAG_DISC, char_tnum) ||
|
2009-01-15 00:42:35 +01:00
|
|
|
flac_copy_comment(tag, entry, "album artist",
|
2009-10-13 16:12:45 +02:00
|
|
|
TAG_ALBUM_ARTIST, char_tnum))
|
2009-01-15 22:45:28 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i)
|
|
|
|
if (flac_copy_comment(tag, entry,
|
2009-03-15 23:31:07 +01:00
|
|
|
tag_item_names[i], i, char_tnum))
|
2009-01-15 22:45:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-11-11 00:05:14 +01:00
|
|
|
static void
|
2009-03-15 23:31:07 +01:00
|
|
|
flac_vorbis_comments_to_tag(struct tag *tag, const char *char_tnum,
|
2009-11-10 21:58:19 +01:00
|
|
|
const FLAC__StreamMetadata_VorbisComment *comment)
|
2006-03-16 07:52:46 +01:00
|
|
|
{
|
2009-11-10 21:58:19 +01:00
|
|
|
for (unsigned i = 0; i < comment->num_comments; ++i)
|
|
|
|
flac_parse_comment(tag, char_tnum, &comment->comments[i]);
|
2006-03-16 07:52:46 +01:00
|
|
|
}
|
|
|
|
|
2009-11-11 00:05:14 +01:00
|
|
|
void
|
|
|
|
flac_tag_apply_metadata(struct tag *tag, const char *track,
|
|
|
|
const FLAC__StreamMetadata *block)
|
|
|
|
{
|
|
|
|
switch (block->type) {
|
|
|
|
case FLAC__METADATA_TYPE_VORBIS_COMMENT:
|
|
|
|
flac_vorbis_comments_to_tag(tag, track,
|
|
|
|
&block->data.vorbis_comment);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FLAC__METADATA_TYPE_STREAMINFO:
|
|
|
|
tag->time = flac_duration(&block->data.stream_info);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void flac_metadata_common_cb(const FLAC__StreamMetadata * block,
|
2009-01-15 19:50:28 +01:00
|
|
|
struct flac_data *data)
|
2006-03-16 07:52:46 +01:00
|
|
|
{
|
|
|
|
const FLAC__StreamMetadata_StreamInfo *si = &(block->data.stream_info);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
switch (block->type) {
|
2006-03-16 07:52:46 +01:00
|
|
|
case FLAC__METADATA_TYPE_STREAMINFO:
|
2009-07-19 17:24:43 +02:00
|
|
|
audio_format_init(&data->audio_format, si->sample_rate,
|
|
|
|
si->bits_per_sample, si->channels);
|
2008-08-26 08:27:05 +02:00
|
|
|
data->total_time = ((float)si->total_samples) / (si->sample_rate);
|
2006-03-16 07:52:46 +01:00
|
|
|
break;
|
|
|
|
case FLAC__METADATA_TYPE_VORBIS_COMMENT:
|
2009-01-15 19:50:28 +01:00
|
|
|
flac_parse_replay_gain(block, data);
|
2009-03-01 14:07:23 +01:00
|
|
|
|
|
|
|
if (data->tag != NULL)
|
2009-11-10 21:58:19 +01:00
|
|
|
flac_vorbis_comments_to_tag(data->tag, NULL,
|
|
|
|
&block->data.vorbis_comment);
|
2009-03-01 14:07:23 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
default:
|
|
|
|
break;
|
2006-03-16 07:52:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void flac_error_common_cb(const char *plugin,
|
|
|
|
const FLAC__StreamDecoderErrorStatus status,
|
2009-01-15 19:50:28 +01:00
|
|
|
struct flac_data *data)
|
2006-03-16 07:52:46 +01:00
|
|
|
{
|
2008-08-26 08:27:07 +02:00
|
|
|
if (decoder_get_command(data->decoder) == DECODE_COMMAND_STOP)
|
2006-07-20 18:02:40 +02:00
|
|
|
return;
|
2006-03-16 07:52:46 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
switch (status) {
|
2006-03-16 07:52:46 +01:00
|
|
|
case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC:
|
2008-11-21 20:15:50 +01:00
|
|
|
g_warning("%s lost sync\n", plugin);
|
2006-03-16 07:52:46 +01:00
|
|
|
break;
|
|
|
|
case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER:
|
2008-11-21 20:15:50 +01:00
|
|
|
g_warning("bad %s header\n", plugin);
|
2006-03-16 07:52:46 +01:00
|
|
|
break;
|
|
|
|
case FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH:
|
2008-11-21 20:15:50 +01:00
|
|
|
g_warning("%s crc mismatch\n", plugin);
|
2006-03-16 07:52:46 +01:00
|
|
|
break;
|
|
|
|
default:
|
2008-11-21 20:15:50 +01:00
|
|
|
g_warning("unknown %s error\n", plugin);
|
2006-03-16 07:52:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-23 23:59:54 +02:00
|
|
|
static void flac_convert_stereo16(int16_t *dest,
|
2008-09-23 23:59:54 +02:00
|
|
|
const FLAC__int32 * const buf[],
|
|
|
|
unsigned int position, unsigned int end)
|
|
|
|
{
|
|
|
|
for (; position < end; ++position) {
|
2008-09-23 23:59:54 +02:00
|
|
|
*dest++ = buf[0][position];
|
|
|
|
*dest++ = buf[1][position];
|
2008-09-23 23:59:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-23 23:59:55 +02:00
|
|
|
static void
|
|
|
|
flac_convert_16(int16_t *dest,
|
|
|
|
unsigned int num_channels,
|
|
|
|
const FLAC__int32 * const buf[],
|
|
|
|
unsigned int position, unsigned int end)
|
|
|
|
{
|
|
|
|
unsigned int c_chan;
|
|
|
|
|
|
|
|
for (; position < end; ++position)
|
|
|
|
for (c_chan = 0; c_chan < num_channels; c_chan++)
|
|
|
|
*dest++ = buf[c_chan][position];
|
|
|
|
}
|
|
|
|
|
2008-09-23 23:59:55 +02:00
|
|
|
/**
|
|
|
|
* Note: this function also handles 24 bit files!
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
flac_convert_32(int32_t *dest,
|
|
|
|
unsigned int num_channels,
|
|
|
|
const FLAC__int32 * const buf[],
|
|
|
|
unsigned int position, unsigned int end)
|
|
|
|
{
|
|
|
|
unsigned int c_chan;
|
|
|
|
|
|
|
|
for (; position < end; ++position)
|
|
|
|
for (c_chan = 0; c_chan < num_channels; c_chan++)
|
|
|
|
*dest++ = buf[c_chan][position];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
flac_convert_8(int8_t *dest,
|
|
|
|
unsigned int num_channels,
|
|
|
|
const FLAC__int32 * const buf[],
|
|
|
|
unsigned int position, unsigned int end)
|
|
|
|
{
|
|
|
|
unsigned int c_chan;
|
|
|
|
|
|
|
|
for (; position < end; ++position)
|
|
|
|
for (c_chan = 0; c_chan < num_channels; c_chan++)
|
|
|
|
*dest++ = buf[c_chan][position];
|
|
|
|
}
|
|
|
|
|
2009-11-10 19:57:58 +01:00
|
|
|
static void
|
|
|
|
flac_convert(void *dest,
|
|
|
|
unsigned int num_channels, unsigned sample_format,
|
|
|
|
const FLAC__int32 *const buf[],
|
|
|
|
unsigned int position, unsigned int end)
|
2008-09-23 23:59:54 +02:00
|
|
|
{
|
2009-11-10 19:57:28 +01:00
|
|
|
switch (sample_format) {
|
|
|
|
case 16:
|
2008-09-23 23:59:55 +02:00
|
|
|
if (num_channels == 2)
|
|
|
|
flac_convert_stereo16((int16_t*)dest, buf,
|
|
|
|
position, end);
|
|
|
|
else
|
|
|
|
flac_convert_16((int16_t*)dest, num_channels, buf,
|
|
|
|
position, end);
|
|
|
|
break;
|
2008-09-23 23:59:54 +02:00
|
|
|
|
2009-11-10 19:57:28 +01:00
|
|
|
case 24:
|
|
|
|
case 32:
|
2008-09-23 23:59:55 +02:00
|
|
|
flac_convert_32((int32_t*)dest, num_channels, buf,
|
|
|
|
position, end);
|
|
|
|
break;
|
|
|
|
|
2009-11-10 19:57:28 +01:00
|
|
|
case 8:
|
2008-09-23 23:59:55 +02:00
|
|
|
flac_convert_8((int8_t*)dest, num_channels, buf,
|
|
|
|
position, end);
|
|
|
|
break;
|
2008-09-23 23:59:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FLAC__StreamDecoderWriteStatus
|
2009-01-15 19:50:28 +01:00
|
|
|
flac_common_write(struct flac_data *data, const FLAC__Frame * frame,
|
2008-09-23 23:59:54 +02:00
|
|
|
const FLAC__int32 *const buf[])
|
|
|
|
{
|
2008-09-23 23:59:55 +02:00
|
|
|
enum decoder_command cmd;
|
2009-11-10 21:46:10 +01:00
|
|
|
size_t buffer_size = frame->header.blocksize *
|
|
|
|
audio_format_frame_size(&data->audio_format);
|
|
|
|
void *buffer;
|
|
|
|
|
|
|
|
buffer = pcm_buffer_get(&data->buffer, buffer_size);
|
|
|
|
|
|
|
|
flac_convert(buffer, data->audio_format.channels,
|
|
|
|
data->audio_format.bits, buf,
|
|
|
|
0, frame->header.blocksize);
|
|
|
|
|
|
|
|
cmd = decoder_data(data->decoder, data->input_stream,
|
|
|
|
buffer, buffer_size,
|
|
|
|
data->time, data->bit_rate,
|
|
|
|
data->replay_gain_info);
|
|
|
|
switch (cmd) {
|
|
|
|
case DECODE_COMMAND_NONE:
|
|
|
|
case DECODE_COMMAND_START:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DECODE_COMMAND_STOP:
|
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
2008-09-23 23:59:54 +02:00
|
|
|
|
2009-11-10 21:46:10 +01:00
|
|
|
case DECODE_COMMAND_SEEK:
|
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
2008-09-23 23:59:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
|
|
|
}
|
2009-03-08 20:16:53 +01:00
|
|
|
|
|
|
|
#if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7
|
|
|
|
|
|
|
|
char*
|
|
|
|
flac_cue_track( const char* pathname,
|
|
|
|
const unsigned int tnum)
|
|
|
|
{
|
2009-08-14 11:51:42 +02:00
|
|
|
FLAC__bool success;
|
|
|
|
FLAC__StreamMetadata* cs;
|
2009-03-08 20:16:53 +01:00
|
|
|
|
2009-08-14 11:51:42 +02:00
|
|
|
success = FLAC__metadata_get_cuesheet(pathname, &cs);
|
|
|
|
if (!success)
|
2009-03-08 20:16:53 +01:00
|
|
|
return NULL;
|
|
|
|
|
2009-08-14 11:51:42 +02:00
|
|
|
assert(cs != NULL);
|
|
|
|
|
2009-03-08 20:16:53 +01:00
|
|
|
if (cs->data.cue_sheet.num_tracks <= 1)
|
|
|
|
{
|
|
|
|
FLAC__metadata_object_delete(cs);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tnum > 0 && tnum < cs->data.cue_sheet.num_tracks)
|
|
|
|
{
|
|
|
|
char* track = g_strdup_printf("track_%03u.flac", tnum);
|
|
|
|
|
|
|
|
FLAC__metadata_object_delete(cs);
|
|
|
|
|
|
|
|
return track;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FLAC__metadata_object_delete(cs);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
flac_vtrack_tnum(const char* fname)
|
|
|
|
{
|
|
|
|
/* find last occurrence of '_' in fname
|
|
|
|
* which is hopefully something like track_xxx.flac
|
|
|
|
* another/better way would be to use tag struct
|
|
|
|
*/
|
|
|
|
char* ptr = strrchr(fname, '_');
|
|
|
|
|
|
|
|
// copy ascii tracknumber to int
|
|
|
|
char vtrack[4];
|
|
|
|
g_strlcpy(vtrack, ++ptr, 4);
|
|
|
|
return (unsigned int)strtol(vtrack, NULL, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* FLAC_API_VERSION_CURRENT >= 7 */
|