Volume.cxx 4.61 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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"
21
#include "Volume.hxx"
22
#include "Domain.hxx"
23
#include "Traits.hxx"
24 25
#include "util/ConstBuffer.hxx"
#include "util/Error.hxx"
26

27 28
#include "PcmDither.cxx" // including the .cxx file to get inlined templates

29 30 31
#include <stdint.h>
#include <string.h>

32 33
template<SampleFormat F, class Traits=SampleTraits<F>>
static inline typename Traits::value_type
34 35
pcm_volume_sample(PcmDither &dither,
		  typename Traits::value_type _sample,
36 37 38 39
		  int volume)
{
	typename Traits::long_type sample(_sample);

40 41 42
	return dither.DitherShift<typename Traits::long_type,
				  Traits::BITS + PCM_VOLUME_BITS,
				  Traits::BITS>(sample * volume);
43 44
}

45
template<SampleFormat F, class Traits=SampleTraits<F>>
46
static void
47 48
pcm_volume_change(PcmDither &dither,
		  typename Traits::pointer_type dest,
49
		  typename Traits::const_pointer_type src,
50
		  size_t n,
51
		  int volume)
52
{
53 54
	for (size_t i = 0; i != n; ++i)
		dest[i] = pcm_volume_sample<F, Traits>(dither, src[i], volume);
55 56 57
}

static void
58
pcm_volume_change_8(PcmDither &dither,
59
		    int8_t *dest, const int8_t *src, size_t n,
60
		    int volume)
61
{
62
	pcm_volume_change<SampleFormat::S8>(dither, dest, src, n, volume);
63 64 65
}

static void
66
pcm_volume_change_16(PcmDither &dither,
67
		     int16_t *dest, const int16_t *src, size_t n,
68
		     int volume)
69
{
70
	pcm_volume_change<SampleFormat::S16>(dither, dest, src, n, volume);
71 72 73
}

static void
74
pcm_volume_change_24(PcmDither &dither,
75
		     int32_t *dest, const int32_t *src, size_t n,
76
		     int volume)
77
{
78
	pcm_volume_change<SampleFormat::S24_P32>(dither, dest, src, n,
79
						 volume);
80 81
}

82
static void
83
pcm_volume_change_32(PcmDither &dither,
84
		     int32_t *dest, const int32_t *src, size_t n,
85
		     int volume)
86
{
87
	pcm_volume_change<SampleFormat::S32>(dither, dest, src, n, volume);
88 89
}

90
static void
91
pcm_volume_change_float(float *dest, const float *src, size_t n,
92
			float volume)
93
{
94 95
	for (size_t i = 0; i != n; ++i)
		dest[i] = src[i] * volume;
96 97
}

Max Kellermann's avatar
Max Kellermann committed
98
bool
99
PcmVolume::Open(SampleFormat _format, Error &error)
100
{
101
	assert(format == SampleFormat::UNDEFINED);
102

103 104 105 106 107 108 109 110 111 112 113 114 115
	switch (_format) {
	case SampleFormat::UNDEFINED:
		error.Format(pcm_domain,
			     "Software volume for %s is not implemented",
			     sample_format_to_string(_format));
		return false;

	case SampleFormat::S8:
	case SampleFormat::S16:
	case SampleFormat::S24_P32:
	case SampleFormat::S32:
	case SampleFormat::FLOAT:
		break;
116 117 118 119

	case SampleFormat::DSD:
		// TODO: implement this; currently, it's a no-op
		break;
120 121
	}

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
	format = _format;
	return true;
}

ConstBuffer<void>
PcmVolume::Apply(ConstBuffer<void> src)
{
	if (volume == PCM_VOLUME_1)
		return src;

	void *data = buffer.Get(src.size);

	if (volume == 0) {
		/* optimized special case: 0% volume = memset(0) */
		/* TODO: is this valid for all sample formats? What
		   about floating point? */
		memset(data, 0, src.size);
		return { data, src.size };
	}

142
	switch (format) {
143
	case SampleFormat::UNDEFINED:
144 145
		assert(false);
		gcc_unreachable();
146

147
	case SampleFormat::S8:
148
		pcm_volume_change_8(dither, (int8_t *)data,
149
				    (const int8_t *)src.data,
150
				    src.size / sizeof(int8_t),
Max Kellermann's avatar
Max Kellermann committed
151
				    volume);
152
		break;
153

154
	case SampleFormat::S16:
155
		pcm_volume_change_16(dither, (int16_t *)data,
156
				     (const int16_t *)src.data,
157
				     src.size / sizeof(int16_t),
Max Kellermann's avatar
Max Kellermann committed
158
				     volume);
159
		break;
160

161
	case SampleFormat::S24_P32:
162
		pcm_volume_change_24(dither, (int32_t *)data,
163
				     (const int32_t *)src.data,
164
				     src.size / sizeof(int32_t),
Max Kellermann's avatar
Max Kellermann committed
165
				     volume);
166
		break;
167

168
	case SampleFormat::S32:
169
		pcm_volume_change_32(dither, (int32_t *)data,
170
				     (const int32_t *)src.data,
171
				     src.size / sizeof(int32_t),
Max Kellermann's avatar
Max Kellermann committed
172
				     volume);
173
		break;
174

175
	case SampleFormat::FLOAT:
176 177
		pcm_volume_change_float((float *)data,
					(const float *)src.data,
178
					src.size / sizeof(float),
179
					pcm_volume_to_float(volume));
180
		break;
181 182 183 184

	case SampleFormat::DSD:
		// TODO: implement this; currently, it's a no-op
		return src;
185
	}
186

187
	return { data, src.size };
188
}