~ubuntu-dev/ubuntu/lucid/mpd/lucid-201002101854

« back to all changes in this revision

Viewing changes to src/replayGain.c

  • Committer: Bazaar Package Importer
  • Author(s): Charles Majola
  • Date: 2005-02-15 10:43:58 UTC
  • Revision ID: james.westby@ubuntu.com-20050215104358-w8b7yaqqfmsoxj5k
Tags: upstream-0.11.5
ImportĀ upstreamĀ versionĀ 0.11.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* the Music Player Daemon (MPD)
 
2
 * (c)2003-2004 by Warren Dukes (shank@mercury.chem.pitt.edu)
 
3
 * (c)2004 replayGain code by AliasMrJones
 
4
 * This project's homepage is: http://www.musicpd.org
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 */
 
19
 
 
20
#include "replayGain.h"
 
21
 
 
22
#include "log.h"
 
23
#include "conf.h"
 
24
 
 
25
#include <string.h>
 
26
#include <math.h>
 
27
#include <stdlib.h>
 
28
 
 
29
/* Added 4/14/2004 by AliasMrJones */
 
30
static int replayGainState = REPLAYGAIN_OFF;
 
31
 
 
32
static float replayGainPreamp = 1.0;
 
33
 
 
34
void initReplayGainState() {
 
35
        if(!getConf()[CONF_REPLAYGAIN]) return;
 
36
 
 
37
        if(strcmp(getConf()[CONF_REPLAYGAIN],"track")==0) {
 
38
                replayGainState = REPLAYGAIN_TRACK;
 
39
        }
 
40
        else if(strcmp(getConf()[CONF_REPLAYGAIN],"album")==0) {
 
41
                replayGainState = REPLAYGAIN_ALBUM;
 
42
        }
 
43
        else {
 
44
                ERROR("replaygain value \"%s\" is invalid\n",
 
45
                                getConf()[CONF_REPLAYGAIN]);
 
46
                exit(EXIT_FAILURE);
 
47
        }
 
48
 
 
49
        if(getConf()[CONF_REPLAYGAIN_PREAMP]) {
 
50
                char * test;
 
51
                float f = strtod(getConf()[CONF_REPLAYGAIN_PREAMP], &test);
 
52
 
 
53
                if(*test != '\0') {
 
54
                        ERROR("Replaygain preamp \"%s\" is not a number\n",
 
55
                                        getConf()[CONF_REPLAYGAIN_PREAMP]);
 
56
                        exit(EXIT_FAILURE);
 
57
                }
 
58
 
 
59
                if(f < -15 || f > 15) {
 
60
                        ERROR("Replaygain preamp \"%s\" is not between -15 and"
 
61
                                        "15\n", 
 
62
                                        getConf()[CONF_REPLAYGAIN_PREAMP]);
 
63
                        exit(EXIT_FAILURE);
 
64
                }
 
65
 
 
66
                replayGainPreamp = pow(10, f/20.0);
 
67
        }
 
68
}
 
69
 
 
70
int getReplayGainState() {
 
71
        return replayGainState;
 
72
}
 
73
 
 
74
float computeReplayGainScale(float gain, float peak) {
 
75
        float scale;
 
76
 
 
77
        if(gain == 0.0) return(1);
 
78
        scale = pow(10.0, gain/20.0);
 
79
        scale*= replayGainPreamp;
 
80
        if(scale > 15.0) scale = 15.0;
 
81
 
 
82
        if (scale * peak > 1.0) {
 
83
                scale = 1.0 / peak;
 
84
        }
 
85
        return(scale);
 
86
}
 
87
 
 
88
void doReplayGain(char * buffer, int bufferSize, AudioFormat * format, 
 
89
                float scale) 
 
90
{
 
91
        mpd_sint16 * buffer16 = (mpd_sint16 *)buffer;
 
92
        mpd_sint8 * buffer8 = (mpd_sint8 *)buffer;
 
93
        mpd_sint32 temp32;
 
94
 
 
95
        if(scale == 1.0) return;
 
96
        switch(format->bits) {
 
97
                case 16:
 
98
                        while(bufferSize > 0){
 
99
                                temp32 = *buffer16;
 
100
                                temp32 *= scale;
 
101
                                *buffer16 = temp32>32767 ? 32767 : 
 
102
                                        (temp32<-32768 ? -32768 : temp32);
 
103
                                buffer16++;
 
104
                                bufferSize-=2;
 
105
                        }
 
106
                        break;
 
107
                case 8:
 
108
                        while(bufferSize>0){
 
109
                                temp32 = *buffer8;
 
110
                                temp32 *= scale;
 
111
                                *buffer8 = temp32>127 ? 127 : 
 
112
                                        (temp32<-128 ? -128 : temp32);
 
113
                                buffer8++;
 
114
                                bufferSize--;
 
115
                        }
 
116
                        break;
 
117
                default:
 
118
                        ERROR("%i bits not supported by doReplaygain!\n", format->bits);
 
119
        }
 
120
}
 
121
/* End of added code */