From 9a71845700e132b3e3fe5920aac24a5420c5d7b3 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 4 Jan 2013 10:31:59 +0100 Subject: crossfade: convert to C++ --- src/CrossFade.cxx | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/CrossFade.hxx | 51 +++++++++++++++++++ src/PlayerThread.cxx | 2 +- src/crossfade.c | 136 --------------------------------------------------- src/crossfade.h | 51 ------------------- 5 files changed, 188 insertions(+), 188 deletions(-) create mode 100644 src/CrossFade.cxx create mode 100644 src/CrossFade.hxx delete mode 100644 src/crossfade.c delete mode 100644 src/crossfade.h (limited to 'src') diff --git a/src/CrossFade.cxx b/src/CrossFade.cxx new file mode 100644 index 000000000..fe937e4ca --- /dev/null +++ b/src/CrossFade.cxx @@ -0,0 +1,136 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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. + */ + +#include "config.h" +#include "CrossFade.hxx" +#include "MusicChunk.hxx" +#include "audio_format.h" +#include "tag.h" + +#include +#include +#include +#include + +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "crossfade" + +#ifdef G_OS_WIN32 +static char * +strtok_r(char *str, const char *delim, G_GNUC_UNUSED char **saveptr) +{ + return strtok(str, delim); +} +#endif + +static float mixramp_interpolate(char *ramp_list, float required_db) +{ + float db, secs, last_db = nan(""), last_secs = 0; + char *ramp_str, *save_str = NULL; + + /* ramp_list is a string of pairs of dBs and seconds that describe the + * volume profile. Delimiters are semi-colons between pairs and spaces + * between the dB and seconds of a pair. + * The dB values must be monotonically increasing for this to work. */ + + while (1) { + /* Parse the dB tokens out of the input string. */ + ramp_str = strtok_r(ramp_list, " ", &save_str); + + /* Tell strtok to continue next time round. */ + ramp_list = NULL; + + /* Parse the dB value. */ + if (NULL == ramp_str) { + return nan(""); + } + db = (float)atof(ramp_str); + + /* Parse the time. */ + ramp_str = strtok_r(NULL, ";", &save_str); + if (NULL == ramp_str) { + return nan(""); + } + secs = (float)atof(ramp_str); + + /* Check for exact match. */ + if (db == required_db) { + return secs; + } + + /* Save if too quiet. */ + if (db < required_db) { + last_db = db; + last_secs = secs; + continue; + } + + /* If required db < any stored value, use the least. */ + if (isnan(last_db)) { + return secs; + } + + /* Finally, interpolate linearly. */ + secs = last_secs + (required_db - last_db) * (secs - last_secs) / (db - last_db); + return secs; + } +} + +unsigned cross_fade_calc(float duration, float total_time, + float mixramp_db, float mixramp_delay, + float replay_gain_db, float replay_gain_prev_db, + char *mixramp_start, char *mixramp_prev_end, + const struct audio_format *af, + const struct audio_format *old_format, + unsigned max_chunks) +{ + unsigned int chunks = 0; + float chunks_f; + float mixramp_overlap; + + if (duration < 0 || duration >= total_time || + /* we can't crossfade when the audio formats are different */ + !audio_format_equals(af, old_format)) + return 0; + + assert(duration >= 0); + assert(audio_format_valid(af)); + + chunks_f = (float)audio_format_time_to_size(af) / (float)CHUNK_SIZE; + + if (isnan(mixramp_delay) || !(mixramp_start) || !(mixramp_prev_end)) { + chunks = (chunks_f * duration + 0.5); + } else { + /* Calculate mixramp overlap. */ + mixramp_overlap = mixramp_interpolate(mixramp_start, mixramp_db - replay_gain_db) + + mixramp_interpolate(mixramp_prev_end, mixramp_db - replay_gain_prev_db); + if (!isnan(mixramp_overlap) && (mixramp_delay <= mixramp_overlap)) { + chunks = (chunks_f * (mixramp_overlap - mixramp_delay)); + g_debug("will overlap %d chunks, %fs", chunks, + mixramp_overlap - mixramp_delay); + } + } + + if (chunks > max_chunks) { + chunks = max_chunks; + g_warning("audio_buffer_size too small for computed MixRamp overlap"); + } + + return chunks; +} diff --git a/src/CrossFade.hxx b/src/CrossFade.hxx new file mode 100644 index 000000000..1c4670758 --- /dev/null +++ b/src/CrossFade.hxx @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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. + */ + +#ifndef MPD_CROSSFADE_HXX +#define MPD_CROSSFADE_HXX + +struct audio_format; +struct music_chunk; + +/** + * Calculate how many music pipe chunks should be used for crossfading. + * + * @param duration the requested crossfade duration + * @param total_time total_time the duration of the new song + * @param mixramp_db the current mixramp_db setting + * @param mixramp_delay the current mixramp_delay setting + * @param replay_gain_db the ReplayGain adjustment used for this song + * @param replay_gain_prev_db the ReplayGain adjustment used on the last song + * @param mixramp_start the next songs mixramp_start tag + * @param mixramp_prev_end the last songs mixramp_end setting + * @param af the audio format of the new song + * @param old_format the audio format of the current song + * @param max_chunks the maximum number of chunks + * @return the number of chunks for crossfading, or 0 if cross fading + * should be disabled for this song change + */ +unsigned cross_fade_calc(float duration, float total_time, + float mixramp_db, float mixramp_delay, + float replay_gain_db, float replay_gain_prev_db, + char *mixramp_start, char *mixramp_prev_end, + const struct audio_format *af, + const struct audio_format *old_format, + unsigned max_chunks); + +#endif diff --git a/src/PlayerThread.cxx b/src/PlayerThread.cxx index cb641bbfb..a394b8445 100644 --- a/src/PlayerThread.cxx +++ b/src/PlayerThread.cxx @@ -27,12 +27,12 @@ #include "song.h" #include "Main.hxx" #include "mpd_error.h" +#include "CrossFade.hxx" extern "C" { #include "player_control.h" #include "output_all.h" #include "event_pipe.h" -#include "crossfade.h" #include "tag.h" #include "idle.h" } diff --git a/src/crossfade.c b/src/crossfade.c deleted file mode 100644 index cf3943314..000000000 --- a/src/crossfade.c +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * 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. - */ - -#include "config.h" -#include "crossfade.h" -#include "MusicChunk.hxx" -#include "audio_format.h" -#include "tag.h" - -#include -#include -#include -#include - -#undef G_LOG_DOMAIN -#define G_LOG_DOMAIN "crossfade" - -#ifdef G_OS_WIN32 -static char * -strtok_r(char *str, const char *delim, G_GNUC_UNUSED char **saveptr) -{ - return strtok(str, delim); -} -#endif - -static float mixramp_interpolate(char *ramp_list, float required_db) -{ - float db, secs, last_db = nan(""), last_secs = 0; - char *ramp_str, *save_str = NULL; - - /* ramp_list is a string of pairs of dBs and seconds that describe the - * volume profile. Delimiters are semi-colons between pairs and spaces - * between the dB and seconds of a pair. - * The dB values must be monotonically increasing for this to work. */ - - while (1) { - /* Parse the dB tokens out of the input string. */ - ramp_str = strtok_r(ramp_list, " ", &save_str); - - /* Tell strtok to continue next time round. */ - ramp_list = NULL; - - /* Parse the dB value. */ - if (NULL == ramp_str) { - return nan(""); - } - db = (float)atof(ramp_str); - - /* Parse the time. */ - ramp_str = strtok_r(NULL, ";", &save_str); - if (NULL == ramp_str) { - return nan(""); - } - secs = (float)atof(ramp_str); - - /* Check for exact match. */ - if (db == required_db) { - return secs; - } - - /* Save if too quiet. */ - if (db < required_db) { - last_db = db; - last_secs = secs; - continue; - } - - /* If required db < any stored value, use the least. */ - if (isnan(last_db)) { - return secs; - } - - /* Finally, interpolate linearly. */ - secs = last_secs + (required_db - last_db) * (secs - last_secs) / (db - last_db); - return secs; - } -} - -unsigned cross_fade_calc(float duration, float total_time, - float mixramp_db, float mixramp_delay, - float replay_gain_db, float replay_gain_prev_db, - char *mixramp_start, char *mixramp_prev_end, - const struct audio_format *af, - const struct audio_format *old_format, - unsigned max_chunks) -{ - unsigned int chunks = 0; - float chunks_f; - float mixramp_overlap; - - if (duration < 0 || duration >= total_time || - /* we can't crossfade when the audio formats are different */ - !audio_format_equals(af, old_format)) - return 0; - - assert(duration >= 0); - assert(audio_format_valid(af)); - - chunks_f = (float)audio_format_time_to_size(af) / (float)CHUNK_SIZE; - - if (isnan(mixramp_delay) || !(mixramp_start) || !(mixramp_prev_end)) { - chunks = (chunks_f * duration + 0.5); - } else { - /* Calculate mixramp overlap. */ - mixramp_overlap = mixramp_interpolate(mixramp_start, mixramp_db - replay_gain_db) - + mixramp_interpolate(mixramp_prev_end, mixramp_db - replay_gain_prev_db); - if (!isnan(mixramp_overlap) && (mixramp_delay <= mixramp_overlap)) { - chunks = (chunks_f * (mixramp_overlap - mixramp_delay)); - g_debug("will overlap %d chunks, %fs", chunks, - mixramp_overlap - mixramp_delay); - } - } - - if (chunks > max_chunks) { - chunks = max_chunks; - g_warning("audio_buffer_size too small for computed MixRamp overlap"); - } - - return chunks; -} diff --git a/src/crossfade.h b/src/crossfade.h deleted file mode 100644 index d581dbfe0..000000000 --- a/src/crossfade.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * 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. - */ - -#ifndef MPD_CROSSFADE_H -#define MPD_CROSSFADE_H - -struct audio_format; -struct music_chunk; - -/** - * Calculate how many music pipe chunks should be used for crossfading. - * - * @param duration the requested crossfade duration - * @param total_time total_time the duration of the new song - * @param mixramp_db the current mixramp_db setting - * @param mixramp_delay the current mixramp_delay setting - * @param replay_gain_db the ReplayGain adjustment used for this song - * @param replay_gain_prev_db the ReplayGain adjustment used on the last song - * @param mixramp_start the next songs mixramp_start tag - * @param mixramp_prev_end the last songs mixramp_end setting - * @param af the audio format of the new song - * @param old_format the audio format of the current song - * @param max_chunks the maximum number of chunks - * @return the number of chunks for crossfading, or 0 if cross fading - * should be disabled for this song change - */ -unsigned cross_fade_calc(float duration, float total_time, - float mixramp_db, float mixramp_delay, - float replay_gain_db, float replay_gain_prev_db, - char *mixramp_start, char *mixramp_prev_end, - const struct audio_format *af, - const struct audio_format *old_format, - unsigned max_chunks); - -#endif -- cgit v1.2.3