CrossFade: reimplement mixramp_interpolate() without strtok()

Don't require a writable string, and don't modify it.
This commit is contained in:
Max Kellermann 2013-10-26 13:13:16 +02:00
parent 7f03f68fcc
commit 067572c6dd
2 changed files with 19 additions and 26 deletions

View File

@ -21,6 +21,7 @@
#include "CrossFade.hxx" #include "CrossFade.hxx"
#include "MusicChunk.hxx" #include "MusicChunk.hxx"
#include "AudioFormat.hxx" #include "AudioFormat.hxx"
#include "util/NumberParser.hxx"
#include "util/Domain.hxx" #include "util/Domain.hxx"
#include "Log.hxx" #include "Log.hxx"
@ -32,20 +33,11 @@
static constexpr Domain cross_fade_domain("cross_fade"); static constexpr Domain cross_fade_domain("cross_fade");
#ifdef WIN32 gcc_pure
static float
static char * mixramp_interpolate(const char *ramp_list, float required_db)
strtok_r(char *str, const char *delim, gcc_unused char **saveptr)
{ {
return strtok(str, delim); float last_db = nan(""), last_secs = 0;
}
#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 = nullptr;
/* ramp_list is a string of pairs of dBs and seconds that describe the /* ramp_list is a string of pairs of dBs and seconds that describe the
* volume profile. Delimiters are semi-colons between pairs and spaces * volume profile. Delimiters are semi-colons between pairs and spaces
@ -53,24 +45,22 @@ static float mixramp_interpolate(char *ramp_list, float required_db)
* The dB values must be monotonically increasing for this to work. */ * The dB values must be monotonically increasing for this to work. */
while (1) { 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 = nullptr;
/* Parse the dB value. */ /* Parse the dB value. */
if (nullptr == ramp_str) char *endptr;
const float db = ParseFloat(ramp_list, &endptr);
if (endptr == ramp_list || *endptr != ' ')
break; break;
db = (float)atof(ramp_str); ramp_list = endptr + 1;
/* Parse the time. */ /* Parse the time. */
ramp_str = strtok_r(nullptr, ";", &save_str); float secs = ParseFloat(ramp_list, &endptr);
if (nullptr == ramp_str) if (endptr == ramp_list || (*endptr != ';' && *endptr != 0))
break; break;
secs = (float)atof(ramp_str); ramp_list = endptr;
if (*ramp_list == ';')
++ramp_list;
/* Check for exact match. */ /* Check for exact match. */
if (db == required_db) { if (db == required_db) {
@ -100,7 +90,7 @@ unsigned
cross_fade_calc(float duration, float total_time, cross_fade_calc(float duration, float total_time,
float mixramp_db, float mixramp_delay, float mixramp_db, float mixramp_delay,
float replay_gain_db, float replay_gain_prev_db, float replay_gain_db, float replay_gain_prev_db,
char *mixramp_start, char *mixramp_prev_end, const char *mixramp_start, const char *mixramp_prev_end,
const AudioFormat af, const AudioFormat af,
const AudioFormat old_format, const AudioFormat old_format,
unsigned max_chunks) unsigned max_chunks)

View File

@ -20,6 +20,8 @@
#ifndef MPD_CROSSFADE_HXX #ifndef MPD_CROSSFADE_HXX
#define MPD_CROSSFADE_HXX #define MPD_CROSSFADE_HXX
#include "Compiler.h"
struct AudioFormat; struct AudioFormat;
struct music_chunk; struct music_chunk;
@ -40,11 +42,12 @@ struct music_chunk;
* @return the number of chunks for crossfading, or 0 if cross fading * @return the number of chunks for crossfading, or 0 if cross fading
* should be disabled for this song change * should be disabled for this song change
*/ */
gcc_pure
unsigned unsigned
cross_fade_calc(float duration, float total_time, cross_fade_calc(float duration, float total_time,
float mixramp_db, float mixramp_delay, float mixramp_db, float mixramp_delay,
float replay_gain_db, float replay_gain_prev_db, float replay_gain_db, float replay_gain_prev_db,
char *mixramp_start, char *mixramp_prev_end, const char *mixramp_start, const char *mixramp_prev_end,
AudioFormat af, AudioFormat old_format, AudioFormat af, AudioFormat old_format,
unsigned max_chunks); unsigned max_chunks);