2009-07-07 08:58:51 +02:00
|
|
|
/*
|
2013-07-28 12:42:06 +02:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-07-07 08:58:51 +02:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* 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.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-07-28 12:42:06 +02:00
|
|
|
#include "SndfileDecoderPlugin.hxx"
|
2009-07-07 08:58:51 +02:00
|
|
|
#include "decoder_api.h"
|
2009-11-10 19:01:38 +01:00
|
|
|
#include "audio_check.h"
|
2012-02-11 19:12:02 +01:00
|
|
|
#include "tag_handler.h"
|
2009-07-07 08:58:51 +02:00
|
|
|
|
|
|
|
#include <sndfile.h>
|
|
|
|
|
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "sndfile"
|
|
|
|
|
|
|
|
static sf_count_t
|
|
|
|
sndfile_vio_get_filelen(void *user_data)
|
|
|
|
{
|
2013-07-28 12:42:06 +02:00
|
|
|
const struct input_stream *is = (const struct input_stream *)user_data;
|
2009-07-07 08:58:51 +02:00
|
|
|
|
2013-01-24 19:14:40 +01:00
|
|
|
return input_stream_get_size(is);
|
2009-07-07 08:58:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static sf_count_t
|
|
|
|
sndfile_vio_seek(sf_count_t offset, int whence, void *user_data)
|
|
|
|
{
|
2013-07-28 12:42:06 +02:00
|
|
|
struct input_stream *is = (struct input_stream *)user_data;
|
2009-07-07 08:58:51 +02:00
|
|
|
bool success;
|
|
|
|
|
2013-07-28 12:42:06 +02:00
|
|
|
success = input_stream_lock_seek(is, offset, whence, nullptr);
|
2009-07-07 08:58:51 +02:00
|
|
|
if (!success)
|
|
|
|
return -1;
|
|
|
|
|
2013-01-24 19:14:40 +01:00
|
|
|
return input_stream_get_offset(is);
|
2009-07-07 08:58:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static sf_count_t
|
|
|
|
sndfile_vio_read(void *ptr, sf_count_t count, void *user_data)
|
|
|
|
{
|
2013-07-28 12:42:06 +02:00
|
|
|
struct input_stream *is = (struct input_stream *)user_data;
|
|
|
|
GError *error = nullptr;
|
2009-07-07 08:58:51 +02:00
|
|
|
size_t nbytes;
|
|
|
|
|
2011-09-14 21:46:41 +02:00
|
|
|
nbytes = input_stream_lock_read(is, ptr, count, &error);
|
2013-07-28 12:42:06 +02:00
|
|
|
if (nbytes == 0 && error != nullptr) {
|
2009-11-14 23:53:04 +01:00
|
|
|
g_warning("%s", error->message);
|
|
|
|
g_error_free(error);
|
2009-07-07 08:58:51 +02:00
|
|
|
return -1;
|
2009-11-14 23:53:04 +01:00
|
|
|
}
|
2009-07-07 08:58:51 +02:00
|
|
|
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sf_count_t
|
|
|
|
sndfile_vio_write(G_GNUC_UNUSED const void *ptr,
|
|
|
|
G_GNUC_UNUSED sf_count_t count,
|
|
|
|
G_GNUC_UNUSED void *user_data)
|
|
|
|
{
|
|
|
|
/* no writing! */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sf_count_t
|
|
|
|
sndfile_vio_tell(void *user_data)
|
|
|
|
{
|
2013-07-28 12:42:06 +02:00
|
|
|
const struct input_stream *is = (const struct input_stream *)user_data;
|
2009-07-07 08:58:51 +02:00
|
|
|
|
2013-01-24 19:14:40 +01:00
|
|
|
return input_stream_get_offset(is);
|
2009-07-07 08:58:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This SF_VIRTUAL_IO implementation wraps MPD's #input_stream to a
|
|
|
|
* libsndfile stream.
|
|
|
|
*/
|
|
|
|
static SF_VIRTUAL_IO vio = {
|
2013-07-28 12:42:06 +02:00
|
|
|
sndfile_vio_get_filelen,
|
|
|
|
sndfile_vio_seek,
|
|
|
|
sndfile_vio_read,
|
|
|
|
sndfile_vio_write,
|
|
|
|
sndfile_vio_tell,
|
2009-07-07 08:58:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a frame number to a timestamp (in seconds).
|
|
|
|
*/
|
|
|
|
static float
|
|
|
|
frame_to_time(sf_count_t frame, const struct audio_format *audio_format)
|
|
|
|
{
|
|
|
|
return (float)frame / (float)audio_format->sample_rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a timestamp (in seconds) to a frame number.
|
|
|
|
*/
|
|
|
|
static sf_count_t
|
|
|
|
time_to_frame(float t, const struct audio_format *audio_format)
|
|
|
|
{
|
|
|
|
return (sf_count_t)(t * audio_format->sample_rate);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sndfile_stream_decode(struct decoder *decoder, struct input_stream *is)
|
|
|
|
{
|
2013-07-28 12:42:06 +02:00
|
|
|
GError *error = nullptr;
|
2009-07-07 08:58:51 +02:00
|
|
|
SNDFILE *sf;
|
|
|
|
SF_INFO info;
|
|
|
|
struct audio_format audio_format;
|
|
|
|
size_t frame_size;
|
2009-12-25 19:47:33 +01:00
|
|
|
sf_count_t read_frames, num_frames;
|
2009-07-07 08:58:51 +02:00
|
|
|
int buffer[4096];
|
|
|
|
enum decoder_command cmd;
|
|
|
|
|
|
|
|
info.format = 0;
|
|
|
|
|
|
|
|
sf = sf_open_virtual(&vio, SFM_READ, &info, is);
|
2013-07-28 12:42:06 +02:00
|
|
|
if (sf == nullptr) {
|
2009-07-07 08:58:51 +02:00
|
|
|
g_warning("sf_open_virtual() failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* for now, always read 32 bit samples. Later, we could lower
|
|
|
|
MPD's CPU usage by reading 16 bit samples with
|
|
|
|
sf_readf_short() on low-quality source files. */
|
2009-11-10 17:11:34 +01:00
|
|
|
if (!audio_format_init_checked(&audio_format, info.samplerate,
|
|
|
|
SAMPLE_FORMAT_S32,
|
2009-11-10 19:01:38 +01:00
|
|
|
info.channels, &error)) {
|
|
|
|
g_warning("%s", error->message);
|
|
|
|
g_error_free(error);
|
2009-07-07 08:58:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder_initialized(decoder, &audio_format, info.seekable,
|
|
|
|
frame_to_time(info.frames, &audio_format));
|
|
|
|
|
|
|
|
frame_size = audio_format_frame_size(&audio_format);
|
|
|
|
read_frames = sizeof(buffer) / frame_size;
|
|
|
|
|
|
|
|
do {
|
|
|
|
num_frames = sf_readf_int(sf, buffer, read_frames);
|
|
|
|
if (num_frames <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
cmd = decoder_data(decoder, is,
|
|
|
|
buffer, num_frames * frame_size,
|
2010-01-03 22:44:23 +01:00
|
|
|
0);
|
2009-07-07 08:58:51 +02:00
|
|
|
if (cmd == DECODE_COMMAND_SEEK) {
|
|
|
|
sf_count_t c =
|
|
|
|
time_to_frame(decoder_seek_where(decoder),
|
|
|
|
&audio_format);
|
|
|
|
c = sf_seek(sf, c, SEEK_SET);
|
|
|
|
if (c < 0)
|
|
|
|
decoder_seek_error(decoder);
|
|
|
|
else
|
|
|
|
decoder_command_finished(decoder);
|
|
|
|
cmd = DECODE_COMMAND_NONE;
|
|
|
|
}
|
|
|
|
} while (cmd == DECODE_COMMAND_NONE);
|
|
|
|
|
|
|
|
sf_close(sf);
|
|
|
|
}
|
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
static bool
|
|
|
|
sndfile_scan_file(const char *path_fs,
|
|
|
|
const struct tag_handler *handler, void *handler_ctx)
|
2009-07-07 08:58:51 +02:00
|
|
|
{
|
|
|
|
SNDFILE *sf;
|
|
|
|
SF_INFO info;
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
info.format = 0;
|
|
|
|
|
|
|
|
sf = sf_open(path_fs, SFM_READ, &info);
|
2013-07-28 12:42:06 +02:00
|
|
|
if (sf == nullptr)
|
2012-02-11 19:12:02 +01:00
|
|
|
return false;
|
2009-07-07 08:58:51 +02:00
|
|
|
|
|
|
|
if (!audio_valid_sample_rate(info.samplerate)) {
|
|
|
|
sf_close(sf);
|
|
|
|
g_warning("Invalid sample rate in %s\n", path_fs);
|
2012-02-11 19:12:02 +01:00
|
|
|
return false;
|
2009-07-07 08:58:51 +02:00
|
|
|
}
|
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
tag_handler_invoke_duration(handler, handler_ctx,
|
|
|
|
info.frames / info.samplerate);
|
2009-07-07 08:58:51 +02:00
|
|
|
|
|
|
|
p = sf_get_string(sf, SF_STR_TITLE);
|
2013-07-28 12:42:06 +02:00
|
|
|
if (p != nullptr)
|
2012-02-11 19:12:02 +01:00
|
|
|
tag_handler_invoke_tag(handler, handler_ctx,
|
|
|
|
TAG_TITLE, p);
|
2009-07-07 08:58:51 +02:00
|
|
|
|
|
|
|
p = sf_get_string(sf, SF_STR_ARTIST);
|
2013-07-28 12:42:06 +02:00
|
|
|
if (p != nullptr)
|
2012-02-11 19:12:02 +01:00
|
|
|
tag_handler_invoke_tag(handler, handler_ctx,
|
|
|
|
TAG_ARTIST, p);
|
2009-07-07 08:58:51 +02:00
|
|
|
|
|
|
|
p = sf_get_string(sf, SF_STR_DATE);
|
2013-07-28 12:42:06 +02:00
|
|
|
if (p != nullptr)
|
2012-02-11 19:12:02 +01:00
|
|
|
tag_handler_invoke_tag(handler, handler_ctx,
|
|
|
|
TAG_DATE, p);
|
2009-07-07 08:58:51 +02:00
|
|
|
|
|
|
|
sf_close(sf);
|
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
return true;
|
2009-07-07 08:58:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *const sndfile_suffixes[] = {
|
|
|
|
"wav", "aiff", "aif", /* Microsoft / SGI / Apple */
|
|
|
|
"au", "snd", /* Sun / DEC / NeXT */
|
|
|
|
"paf", /* Paris Audio File */
|
|
|
|
"iff", "svx", /* Commodore Amiga IFF / SVX */
|
|
|
|
"sf", /* IRCAM */
|
|
|
|
"voc", /* Creative */
|
|
|
|
"w64", /* Soundforge */
|
|
|
|
"pvf", /* Portable Voice Format */
|
|
|
|
"xi", /* Fasttracker */
|
|
|
|
"htk", /* HMM Tool Kit */
|
|
|
|
"caf", /* Apple */
|
|
|
|
"sd2", /* Sound Designer II */
|
|
|
|
|
|
|
|
/* libsndfile also supports FLAC and Ogg Vorbis, but only by
|
|
|
|
linking with libFLAC and libvorbis - we can do better, we
|
|
|
|
have native plugins for these libraries */
|
|
|
|
|
2013-07-28 12:42:06 +02:00
|
|
|
nullptr
|
2009-07-07 08:58:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const char *const sndfile_mime_types[] = {
|
|
|
|
"audio/x-wav",
|
|
|
|
"audio/x-aiff",
|
|
|
|
|
|
|
|
/* what are the MIME types of the other supported formats? */
|
|
|
|
|
2013-07-28 12:42:06 +02:00
|
|
|
nullptr
|
2009-07-07 08:58:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const struct decoder_plugin sndfile_decoder_plugin = {
|
2013-07-28 12:42:06 +02:00
|
|
|
"sndfile",
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
sndfile_stream_decode,
|
|
|
|
nullptr,
|
|
|
|
sndfile_scan_file,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
sndfile_suffixes,
|
|
|
|
sndfile_mime_types,
|
2009-07-07 08:58:51 +02:00
|
|
|
};
|