PcmVolume.cxx 4.31 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
21 22
#include "PcmVolume.hxx"
#include "PcmUtils.hxx"
23
#include "AudioFormat.hxx"
24 25 26 27 28

#include <stdint.h>
#include <string.h>

static void
29
pcm_volume_change_8(int8_t *buffer, const int8_t *end, int volume)
30
{
31
	while (buffer < end) {
32 33
		int32_t sample = *buffer;

34 35
		sample = (sample * volume + pcm_volume_dither() +
			  PCM_VOLUME_1 / 2)
36 37
			/ PCM_VOLUME_1;

38
		*buffer++ = PcmClamp<int8_t, int16_t, 8>(sample);
39 40 41 42
	}
}

static void
43
pcm_volume_change_16(int16_t *buffer, const int16_t *end, int volume)
44
{
45
	while (buffer < end) {
46 47
		int32_t sample = *buffer;

48 49
		sample = (sample * volume + pcm_volume_dither() +
			  PCM_VOLUME_1 / 2)
50 51
			/ PCM_VOLUME_1;

52
		*buffer++ = PcmClamp<int16_t, int32_t, 16>(sample);
53 54 55
	}
}

56
#ifdef __i386__
57 58 59 60 61
/**
 * Optimized volume function for i386.  Use the EDX:EAX 2*32 bit
 * multiplication result instead of emulating 64 bit multiplication.
 */
static inline int32_t
62
pcm_volume_sample_24(int32_t sample, int32_t volume, gcc_unused int32_t dither)
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
{
	int32_t result;

	asm(/* edx:eax = sample * volume */
	    "imul %2\n"

	    /* "add %3, %1\n"  dithering disabled for now, because we
	       have no overflow check - is dithering really important
	       here? */

	    /* eax = edx:eax / PCM_VOLUME_1 */
	    "sal $22, %%edx\n"
	    "shr $10, %1\n"
	    "or %%edx, %1\n"

	    : "=a"(result)
	    : "0"(sample), "r"(volume) /* , "r"(dither) */
	    : "edx"
	    );

	return result;
}
#endif

87
static void
88
pcm_volume_change_24(int32_t *buffer, const int32_t *end, int volume)
89
{
90
	while (buffer < end) {
91
#ifdef __i386__
92 93 94 95 96 97 98
		/* assembly version for i386 */
		int32_t sample = *buffer;

		sample = pcm_volume_sample_24(sample, volume,
					      pcm_volume_dither());
#else
		/* portable version */
99 100
		int64_t sample = *buffer;

101 102
		sample = (sample * volume + pcm_volume_dither() +
			  PCM_VOLUME_1 / 2)
103
			/ PCM_VOLUME_1;
104
#endif
105
		*buffer++ = PcmClamp<int32_t, int32_t, 24>(sample);
106 107 108
	}
}

109
static void
110
pcm_volume_change_32(int32_t *buffer, const int32_t *end, int volume)
111
{
112
	while (buffer < end) {
113 114 115 116 117 118 119 120 121 122 123 124
#ifdef __i386__
		/* assembly version for i386 */
		int32_t sample = *buffer;

		*buffer++ = pcm_volume_sample_24(sample, volume, 0);
#else
		/* portable version */
		int64_t sample = *buffer;

		sample = (sample * volume + pcm_volume_dither() +
			  PCM_VOLUME_1 / 2)
			/ PCM_VOLUME_1;
125
		*buffer++ = PcmClamp<int32_t, int64_t, 32>(sample);
126 127 128 129
#endif
	}
}

130 131 132 133 134 135 136 137 138 139
static void
pcm_volume_change_float(float *buffer, const float *end, float volume)
{
	while (buffer < end) {
		float sample = *buffer;
		sample *= volume;
		*buffer++ = sample;
	}
}

Max Kellermann's avatar
Max Kellermann committed
140
bool
141
pcm_volume(void *buffer, size_t length,
142
	   SampleFormat format,
143 144 145
	   int volume)
{
	if (volume == PCM_VOLUME_1)
Max Kellermann's avatar
Max Kellermann committed
146
		return true;
147 148

	if (volume <= 0) {
149
		memset(buffer, 0, length);
Max Kellermann's avatar
Max Kellermann committed
150
		return true;
151 152
	}

153
	const void *end = pcm_end_pointer(buffer, length);
154
	switch (format) {
155 156
	case SampleFormat::UNDEFINED:
	case SampleFormat::DSD:
157 158 159
		/* not implemented */
		return false;

160
	case SampleFormat::S8:
Max Kellermann's avatar
Max Kellermann committed
161 162
		pcm_volume_change_8((int8_t *)buffer, (const int8_t *)end,
				    volume);
Max Kellermann's avatar
Max Kellermann committed
163
		return true;
164

165
	case SampleFormat::S16:
Max Kellermann's avatar
Max Kellermann committed
166 167
		pcm_volume_change_16((int16_t *)buffer, (const int16_t *)end,
				     volume);
Max Kellermann's avatar
Max Kellermann committed
168
		return true;
169

170
	case SampleFormat::S24_P32:
Max Kellermann's avatar
Max Kellermann committed
171 172
		pcm_volume_change_24((int32_t *)buffer, (const int32_t *)end,
				     volume);
Max Kellermann's avatar
Max Kellermann committed
173
		return true;
174

175
	case SampleFormat::S32:
Max Kellermann's avatar
Max Kellermann committed
176 177
		pcm_volume_change_32((int32_t *)buffer, (const int32_t *)end,
				     volume);
178
		return true;
179

180
	case SampleFormat::FLOAT:
Max Kellermann's avatar
Max Kellermann committed
181
		pcm_volume_change_float((float *)buffer, (const float *)end,
182 183
					pcm_volume_to_float(volume));
		return true;
184
	}
185 186

	assert(false);
187
	gcc_unreachable();
188
}