diff options
-rw-r--r-- | libpiano/src/piano.h | 2 | ||||
-rw-r--r-- | libpiano/src/xml.c | 2 | ||||
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/main.c | 1 | ||||
-rw-r--r-- | src/player.c | 27 | ||||
-rw-r--r-- | src/player.h | 1 |
6 files changed, 33 insertions, 2 deletions
diff --git a/libpiano/src/piano.h b/libpiano/src/piano.h index ed3014a..3a7c740 100644 --- a/libpiano/src/piano.h +++ b/libpiano/src/piano.h @@ -80,7 +80,7 @@ struct PianoSong { /* disabled: isSeed */ /* disabled: artistFansURL */ /* disabled: songExplorerUrl */ - //float fileGain; + float fileGain; /* disabled: songDetailURL */ /* disabled: albumDetailURL */ //char *webId; diff --git a/libpiano/src/xml.c b/libpiano/src/xml.c index 5f138ff..55cb5fe 100644 --- a/libpiano/src/xml.c +++ b/libpiano/src/xml.c @@ -265,6 +265,8 @@ void PianoXmlParsePlaylistCb (const char *key, const xmlNode *value, song->stationId = strdup (valueStr); } else if (strcmp ("albumTitle", key) == 0) { song->album = strdup (valueStr); + } else if (strcmp ("fileGain", key) == 0) { + song->fileGain = atof (valueStr); } } diff --git a/src/Makefile.am b/src/Makefile.am index e3a98a4..06c8e59 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -6,5 +6,5 @@ pianobar_CPPFLAGS = ${LIBCURL_CFLAGS} ${LIBAO_CFLAGS} ${LIBXML_CFLAGS} \ -I../libpiano/src -I../libwardrobe/src pianobar_LDADD = ${LIBCURL_LIBS} ${LIBAO_LIBS} ${LIBFAAD_LIBS} \ ${LIBREADLINE_LIBS} ${LIBXML_LIBS} -lpthread \ - ../libpiano/src/libpiano.la ../libwardrobe/src/libwardrobe.la + ../libpiano/src/libpiano.la ../libwardrobe/src/libwardrobe.la -lm dist_man1_MANS = pianobar.1 @@ -438,6 +438,7 @@ int main (int argc, char **argv) { memset (&player, 0, sizeof (player)); player.url = strdup (curSong->audioUrl); + player.gain = curSong->fileGain; /* start player */ pthread_create (&playerThread, NULL, BarPlayerThread, diff --git a/src/player.c b/src/player.c index d72979d..086fcad 100644 --- a/src/player.c +++ b/src/player.c @@ -22,6 +22,8 @@ THE SOFTWARE. #include <unistd.h> #include <string.h> +#include <math.h> +#include <stdint.h> #include "player.h" #include "config.h" @@ -40,6 +42,16 @@ unsigned int BarChangeByteorderUI32 (char buf[4]) { return ret; } +/* compute replaygain scale factor + * algo taken from here: http://www.dsprelated.com/showmessage/29246/1.php + * mpd does the same + * @param apply this gain + * @return this * yourvalue = newgain value + */ +inline float computeReplayGainScale (float applyGain) { + return pow (10.0, applyGain / 20.0); +} + /* our heart: plays streamed music * @param streamed data * @param block size @@ -89,6 +101,21 @@ size_t BarPlayerCurlCb (void *ptr, size_t size, size_t nmemb, void *stream) { NeAACDecGetErrorMessage (frameInfo.error)); break; } + short int *replayBuf = (short int *) aacDecoded; + int tmpReplayBuf; + size_t i; + for (i = 0; i < frameInfo.samples; i++) { + tmpReplayBuf = (float) (replayBuf[i]) * + computeReplayGainScale (player->gain); + /* avoid clipping */ + if (tmpReplayBuf > INT16_MAX) { + replayBuf[i] = INT16_MAX; + } else if (tmpReplayBuf < INT16_MIN) { + replayBuf[i] = INT16_MIN; + } else { + replayBuf[i] = tmpReplayBuf; + } + } /* ao_play needs bytes: 1 sample = 16 bits = 2 bytes */ ao_play (player->audioOutDevice, aacDecoded, frameInfo.samples * 16 / 8); diff --git a/src/player.h b/src/player.h index 9e44bef..9d0908b 100644 --- a/src/player.h +++ b/src/player.h @@ -41,6 +41,7 @@ struct aacPlayer { NeAACDecHandle aacHandle; unsigned long samplerate; unsigned char channels; + float gain; /* audio out */ ao_device *audioOutDevice; char *url; |