Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
afcf0795
Commit
afcf0795
authored
Dec 22, 2013
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
pcm/Volume: improved dithering
Instead of just adding a rectangular random value before shifting back to the normal scale, use the existing PcmDither library.
parent
394e2815
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
59 additions
and
22 deletions
+59
-22
NEWS
NEWS
+2
-0
PcmDither.cxx
src/pcm/PcmDither.cxx
+13
-0
PcmDither.hxx
src/pcm/PcmDither.hxx
+12
-0
Volume.cxx
src/pcm/Volume.cxx
+27
-20
Volume.hxx
src/pcm/Volume.hxx
+2
-0
test_pcm_volume.cxx
test/test_pcm_volume.cxx
+3
-2
No files found.
NEWS
View file @
afcf0795
...
...
@@ -3,6 +3,8 @@ ver 0.19 (not yet released)
- new commands "addtagid", "cleartagid"
* input
- alsa: new input plugin
* filter
- volume: improved software volume dithering
* new resampler option using libsoxr
ver 0.18.6 (2013/12/24)
...
...
src/pcm/PcmDither.cxx
View file @
afcf0795
...
...
@@ -62,6 +62,19 @@ PcmDither::Dither(T sample)
return
output
>>
scale_bits
;
}
template
<
typename
ST
,
unsigned
SBITS
,
unsigned
DBITS
>
inline
ST
PcmDither
::
DitherShift
(
ST
sample
)
{
static_assert
(
sizeof
(
ST
)
*
8
>
SBITS
,
"Source type too small"
);
static_assert
(
SBITS
>
DBITS
,
"Non-positive scale_bits"
);
static
constexpr
ST
MIN
=
-
(
ST
(
1
)
<<
(
SBITS
-
1
));
static
constexpr
ST
MAX
=
(
ST
(
1
)
<<
(
SBITS
-
1
))
-
1
;
return
Dither
<
ST
,
MIN
,
MAX
,
SBITS
-
DBITS
>
(
sample
);
}
template
<
typename
ST
,
typename
DT
>
inline
typename
DT
::
value_type
PcmDither
::
DitherConvert
(
typename
ST
::
value_type
sample
)
...
...
src/pcm/PcmDither.hxx
View file @
afcf0795
...
...
@@ -32,6 +32,18 @@ public:
constexpr
PcmDither
()
:
error
{
0
,
0
,
0
},
random
(
0
)
{}
/**
* Shift the given sample by #SBITS-#DBITS to the right, and
* apply dithering.
*
* @param ST the input sample type
* @param SBITS the input bit width
* @param DBITS the output bit width
* @param sample the input sample value
*/
template
<
typename
ST
,
unsigned
SBITS
,
unsigned
DBITS
>
ST
DitherShift
(
ST
sample
);
void
Dither24To16
(
int16_t
*
dest
,
const
int32_t
*
src
,
const
int32_t
*
src_end
);
...
...
src/pcm/Volume.cxx
View file @
afcf0795
...
...
@@ -25,62 +25,69 @@
#include "util/ConstBuffer.hxx"
#include "util/Error.hxx"
#include "PcmDither.cxx" // including the .cxx file to get inlined templates
#include <stdint.h>
#include <string.h>
template
<
SampleFormat
F
,
class
Traits
=
SampleTraits
<
F
>>
static
inline
typename
Traits
::
value_type
pcm_volume_sample
(
typename
Traits
::
value_type
_sample
,
pcm_volume_sample
(
PcmDither
&
dither
,
typename
Traits
::
value_type
_sample
,
int
volume
)
{
typename
Traits
::
long_type
sample
(
_sample
);
sample
=
(
sample
*
volume
+
pcm_volume_dither
()
+
PCM_VOLUME_1S
/
2
)
>>
PCM_VOLUME_BITS
;
return
PcmClamp
<
F
,
Traits
>
(
sample
);
return
dither
.
DitherShift
<
typename
Traits
::
long_type
,
Traits
::
BITS
+
PCM_VOLUME_BITS
,
Traits
::
BITS
>
(
sample
*
volume
);
}
template
<
SampleFormat
F
,
class
Traits
=
SampleTraits
<
F
>>
static
void
pcm_volume_change
(
typename
Traits
::
pointer_type
dest
,
pcm_volume_change
(
PcmDither
&
dither
,
typename
Traits
::
pointer_type
dest
,
typename
Traits
::
const_pointer_type
src
,
typename
Traits
::
const_pointer_type
end
,
int
volume
)
{
while
(
src
<
end
)
{
const
auto
sample
=
*
src
++
;
*
dest
++
=
pcm_volume_sample
<
F
,
Traits
>
(
sample
,
volume
);
*
dest
++
=
pcm_volume_sample
<
F
,
Traits
>
(
dither
,
sample
,
volume
);
}
}
static
void
pcm_volume_change_8
(
int8_t
*
dest
,
const
int8_t
*
src
,
const
int8_t
*
end
,
pcm_volume_change_8
(
PcmDither
&
dither
,
int8_t
*
dest
,
const
int8_t
*
src
,
const
int8_t
*
end
,
int
volume
)
{
pcm_volume_change
<
SampleFormat
::
S8
>
(
dest
,
src
,
end
,
volume
);
pcm_volume_change
<
SampleFormat
::
S8
>
(
d
ither
,
d
est
,
src
,
end
,
volume
);
}
static
void
pcm_volume_change_16
(
int16_t
*
dest
,
const
int16_t
*
src
,
const
int16_t
*
end
,
pcm_volume_change_16
(
PcmDither
&
dither
,
int16_t
*
dest
,
const
int16_t
*
src
,
const
int16_t
*
end
,
int
volume
)
{
pcm_volume_change
<
SampleFormat
::
S16
>
(
dest
,
src
,
end
,
volume
);
pcm_volume_change
<
SampleFormat
::
S16
>
(
d
ither
,
d
est
,
src
,
end
,
volume
);
}
static
void
pcm_volume_change_24
(
int32_t
*
dest
,
const
int32_t
*
src
,
const
int32_t
*
end
,
pcm_volume_change_24
(
PcmDither
&
dither
,
int32_t
*
dest
,
const
int32_t
*
src
,
const
int32_t
*
end
,
int
volume
)
{
pcm_volume_change
<
SampleFormat
::
S24_P32
>
(
dest
,
src
,
end
,
volume
);
pcm_volume_change
<
SampleFormat
::
S24_P32
>
(
dither
,
dest
,
src
,
end
,
volume
);
}
static
void
pcm_volume_change_32
(
int32_t
*
dest
,
const
int32_t
*
src
,
const
int32_t
*
end
,
pcm_volume_change_32
(
PcmDither
&
dither
,
int32_t
*
dest
,
const
int32_t
*
src
,
const
int32_t
*
end
,
int
volume
)
{
pcm_volume_change
<
SampleFormat
::
S32
>
(
dest
,
src
,
end
,
volume
);
pcm_volume_change
<
SampleFormat
::
S32
>
(
d
ither
,
d
est
,
src
,
end
,
volume
);
}
static
void
...
...
@@ -143,28 +150,28 @@ PcmVolume::Apply(ConstBuffer<void> src)
gcc_unreachable
();
case
SampleFormat
:
:
S8
:
pcm_volume_change_8
((
int8_t
*
)
data
,
pcm_volume_change_8
(
dither
,
(
int8_t
*
)
data
,
(
const
int8_t
*
)
src
.
data
,
(
const
int8_t
*
)
end
,
volume
);
break
;
case
SampleFormat
:
:
S16
:
pcm_volume_change_16
((
int16_t
*
)
data
,
pcm_volume_change_16
(
dither
,
(
int16_t
*
)
data
,
(
const
int16_t
*
)
src
.
data
,
(
const
int16_t
*
)
end
,
volume
);
break
;
case
SampleFormat
:
:
S24_P32
:
pcm_volume_change_24
((
int32_t
*
)
data
,
pcm_volume_change_24
(
dither
,
(
int32_t
*
)
data
,
(
const
int32_t
*
)
src
.
data
,
(
const
int32_t
*
)
end
,
volume
);
break
;
case
SampleFormat
:
:
S32
:
pcm_volume_change_32
((
int32_t
*
)
data
,
pcm_volume_change_32
(
dither
,
(
int32_t
*
)
data
,
(
const
int32_t
*
)
src
.
data
,
(
const
int32_t
*
)
end
,
volume
);
...
...
src/pcm/Volume.hxx
View file @
afcf0795
...
...
@@ -23,6 +23,7 @@
#include "PcmPrng.hxx"
#include "AudioFormat.hxx"
#include "PcmBuffer.hxx"
#include "PcmDither.hxx"
#include <stdint.h>
#include <stddef.h>
...
...
@@ -87,6 +88,7 @@ class PcmVolume {
unsigned
volume
;
PcmBuffer
buffer
;
PcmDither
dither
;
public
:
PcmVolume
()
...
...
test/test_pcm_volume.cxx
View file @
afcf0795
...
...
@@ -60,8 +60,9 @@ TestVolume(G g=G())
const
auto
_dest
=
ConstBuffer
<
value_type
>::
FromVoid
(
dest
);
for
(
unsigned
i
=
0
;
i
<
N
;
++
i
)
{
CPPUNIT_ASSERT
(
_dest
.
data
[
i
]
>=
(
_src
[
i
]
-
1
)
/
2
);
CPPUNIT_ASSERT
(
_dest
.
data
[
i
]
<=
_src
[
i
]
/
2
+
1
);
const
auto
expected
=
(
_src
[
i
]
+
1
)
/
2
;
CPPUNIT_ASSERT
(
_dest
.
data
[
i
]
>=
expected
-
4
);
CPPUNIT_ASSERT
(
_dest
.
data
[
i
]
<=
expected
+
4
);
}
pv
.
Close
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment