owntone-server/src/scan-flac.c

192 lines
8.0 KiB
C
Raw Normal View History

2005-02-26 19:42:27 -05:00
/*
* Implementation file for server side format conversion.
*
* Copyright (C) 2005 Timo J. Rinne (tri@iki.fi)
*
* 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
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
2006-11-14 00:29:32 -05:00
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
2005-02-26 19:42:27 -05:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_DIRENT_H
# include <dirent.h> /* why here? For osx 10.2, of course! */
#endif
2009-04-02 05:04:33 -04:00
#include <netinet/in.h> /* htons and friends */
2005-02-26 19:42:27 -05:00
#include <sys/stat.h>
#include "err.h"
2009-04-20 10:53:47 -04:00
#include "ff-dbstruct.h"
2005-02-26 19:42:27 -05:00
#include <FLAC/metadata.h>
2006-02-26 03:46:24 -05:00
#define GET_VORBIS_COMMENT(comment, name, len) (char*) \
2005-05-29 23:41:31 -04:00
(((strncasecmp(name, (char*)(comment).entry, strlen(name)) == 0) && \
2006-02-26 03:46:24 -05:00
((comment).entry[strlen(name)] == '=')) ? \
((*(len) = (comment).length - (strlen(name) + 1)), \
(&((comment).entry[strlen(name) + 1]))) : \
NULL)
/**
2006-11-14 00:29:32 -05:00
* scan a flac file for metainfo.
*
* @param filename file to read metainfo for
* @param pmp3 MP3FILE structure to fill
* @returns TRUE if file should be added to DB, FALSE otherwise
*/
int scan_get_flacinfo(char *filename, MP3FILE *pmp3) {
2005-02-26 19:42:27 -05:00
FLAC__Metadata_Chain *chain;
FLAC__Metadata_Iterator *iterator;
FLAC__StreamMetadata *block;
int found=0;
2005-02-26 19:42:27 -05:00
unsigned int sec, ms;
int i;
char *val;
size_t len;
char tmp;
2005-02-26 19:42:27 -05:00
chain = FLAC__metadata_chain_new();
if (! chain) {
2006-02-26 03:46:24 -05:00
DPRINTF(E_WARN,L_SCAN,"Cannot allocate FLAC metadata chain\n");
return FALSE;
2005-02-26 19:42:27 -05:00
}
if (! FLAC__metadata_chain_read(chain, filename)) {
2006-02-26 03:46:24 -05:00
DPRINTF(E_WARN,L_SCAN,"Cannot read FLAC metadata from %s\n", filename);
FLAC__metadata_chain_delete(chain);
return FALSE;
2005-02-26 19:42:27 -05:00
}
iterator = FLAC__metadata_iterator_new();
if (! iterator) {
2006-02-26 03:46:24 -05:00
DPRINTF(E_WARN,L_SCAN,"Cannot allocate FLAC metadata iterator\n");
FLAC__metadata_chain_delete(chain);
return FALSE;
2005-02-26 19:42:27 -05:00
}
FLAC__metadata_iterator_init(iterator, chain);
do {
2006-02-26 03:46:24 -05:00
block = FLAC__metadata_iterator_get_block(iterator);
if (block->type == FLAC__METADATA_TYPE_STREAMINFO) {
sec = (unsigned int)(block->data.stream_info.total_samples /
block->data.stream_info.sample_rate);
ms = (unsigned int)(((block->data.stream_info.total_samples %
block->data.stream_info.sample_rate) * 1000) /
block->data.stream_info.sample_rate);
if ((sec == 0) && (ms == 0))
break; /* Info is crap, escape div-by-zero. */
pmp3->song_length = (sec * 1000) + ms;
pmp3->bitrate = (uint32_t)((pmp3->file_size) / (((sec * 1000) + ms) / 8));
2006-02-26 03:46:24 -05:00
pmp3->samplerate = block->data.stream_info.sample_rate;
pmp3->bits_per_sample = block->data.stream_info.bits_per_sample;
pmp3->sample_count = block->data.stream_info.total_samples;
2006-02-26 03:46:24 -05:00
found |= 1;
if(found == 3)
break;
}
if (block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
2006-05-31 04:33:23 -04:00
for (i = 0; i < (int)block->data.vorbis_comment.num_comments; i++) {
2006-02-26 03:46:24 -05:00
if ((val = GET_VORBIS_COMMENT(block->data.vorbis_comment.comments[i],
"ARTIST", &len))) {
if ((pmp3->artist = calloc(len + 1, 1)) != NULL)
strncpy(pmp3->artist, val, len);
} else if ((val = GET_VORBIS_COMMENT(block->data.vorbis_comment.comments[i],
"TITLE", &len))) {
if ((pmp3->title = calloc(len + 1, 1)) != NULL)
strncpy(pmp3->title, val, len);
} else if ((val = GET_VORBIS_COMMENT(block->data.vorbis_comment.comments[i],
"ALBUMARTIST", &len))) {
if ((pmp3->album_artist = calloc(len + 1, 1)) != NULL)
strncpy(pmp3->album_artist, val, len);
2006-02-26 03:46:24 -05:00
} else if ((val = GET_VORBIS_COMMENT(block->data.vorbis_comment.comments[i],
"ALBUM", &len))) {
if ((pmp3->album = calloc(len + 1, 1)) != NULL)
strncpy(pmp3->album, val, len);
} else if ((val = GET_VORBIS_COMMENT(block->data.vorbis_comment.comments[i],
"GENRE", &len))) {
if ((pmp3->genre = calloc(len + 1, 1)) != NULL)
strncpy(pmp3->genre, val, len);
} else if ((val = GET_VORBIS_COMMENT(block->data.vorbis_comment.comments[i],
"COMPOSER", &len))) {
if ((pmp3->composer = calloc(len + 1, 1)) != NULL)
strncpy(pmp3->composer, val, len);
} else if ((val = GET_VORBIS_COMMENT(block->data.vorbis_comment.comments[i],
"COMMENT", &len))) {
if(pmp3->comment)
free(pmp3->comment); /* was description */
2006-02-26 03:46:24 -05:00
if ((pmp3->comment = calloc(len + 1, 1)) != NULL)
strncpy(pmp3->comment, val, len);
} else if ((val = GET_VORBIS_COMMENT(block->data.vorbis_comment.comments[i],
"DESCRIPTION", &len))) {
if(!pmp3->comment) {
if ((pmp3->comment = calloc(len + 1, 1)) != NULL)
strncpy(pmp3->comment, val, len);
}
2006-02-26 03:46:24 -05:00
} else if ((val = GET_VORBIS_COMMENT(block->data.vorbis_comment.comments[i],
"TRACKNUMBER", &len))) {
tmp = *(val + len);
*(val + len) = '\0';
pmp3->track = atoi(val);
*(val + len) = tmp;
} else if ((val = GET_VORBIS_COMMENT(block->data.vorbis_comment.comments[i],
"DISCNUMBER", &len))) {
tmp = *(val + len);
*(val + len) = '\0';
pmp3->disc = atoi(val);
*(val + len) = tmp;
} else if ((val = GET_VORBIS_COMMENT(block->data.vorbis_comment.comments[i],
"YEAR", &len))) {
tmp = *(val + len);
*(val + len) = '\0';
pmp3->year = atoi(val);
*(val + len) = tmp;
} else if ((val = GET_VORBIS_COMMENT(block->data.vorbis_comment.comments[i],
"DATE", &len))) {
tmp = *(val + len);
*(val + len) = '\0';
pmp3->year = atoi(val);
*(val + len) = tmp;
}
}
found |= 2;
if(found == 3)
break;
}
2005-02-26 19:42:27 -05:00
} while (FLAC__metadata_iterator_next(iterator));
if (!found) {
2006-02-26 03:46:24 -05:00
DPRINTF(E_WARN,L_SCAN,"Cannot find FLAC metadata in %s\n", filename);
2005-02-26 19:42:27 -05:00
}
FLAC__metadata_iterator_delete(iterator);
FLAC__metadata_chain_delete(chain);
return TRUE;
2005-02-26 19:42:27 -05:00
}