]>
code.delx.au - pulseaudio/blob - polyp/sample-util.c
51b22fbe1f69d3cc619843e3114034cfda524813
4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include "sample-util.h"
32 struct pa_memblock
*pa_silence_memblock(struct pa_memblock
* b
, const struct pa_sample_spec
*spec
) {
33 assert(b
&& b
->data
&& spec
);
34 pa_silence_memory(b
->data
, b
->length
, spec
);
38 void pa_silence_memchunk(struct pa_memchunk
*c
, const struct pa_sample_spec
*spec
) {
39 assert(c
&& c
->memblock
&& c
->memblock
->data
&& spec
&& c
->length
);
40 pa_silence_memory((uint8_t*) c
->memblock
->data
+c
->index
, c
->length
, spec
);
43 void pa_silence_memory(void *p
, size_t length
, const struct pa_sample_spec
*spec
) {
45 assert(p
&& length
&& spec
);
47 switch (spec
->format
) {
53 case PA_SAMPLE_FLOAT32
:
67 size_t pa_mix(struct pa_mix_info channels
[], unsigned nchannels
, void *data
, size_t length
, const struct pa_sample_spec
*spec
, pa_volume_t volume
) {
69 assert(channels
&& data
&& length
&& spec
);
70 assert(spec
->format
== PA_SAMPLE_S16NE
);
72 for (d
= 0;; d
+= sizeof(int16_t)) {
78 for (c
= 0; c
< nchannels
; c
++) {
80 uint32_t volume
= channels
[c
].volume
;
82 if (d
>= channels
[c
].chunk
.length
)
85 if (volume
== PA_VOLUME_MUTED
)
88 v
= *((int16_t*) ((uint8_t*) channels
[c
].chunk
.memblock
->data
+ channels
[c
].chunk
.index
+ d
));
90 if (volume
!= PA_VOLUME_NORM
)
91 v
= (int32_t) ((float)v
*volume
/PA_VOLUME_NORM
);
97 if (volume
== PA_VOLUME_MUTED
)
99 else if (volume
!= PA_VOLUME_NORM
)
100 sum
= (int32_t) ((float) sum
*volume
/PA_VOLUME_NORM
);
102 if (sum
< -0x8000) sum
= -0x8000;
103 if (sum
> 0x7FFF) sum
= 0x7FFF;
105 *((int16_t*) data
) = sum
;
106 data
= (uint8_t*) data
+ sizeof(int16_t);
111 void pa_volume_memchunk(struct pa_memchunk
*c
, const struct pa_sample_spec
*spec
, pa_volume_t volume
) {
114 assert(c
&& spec
&& (c
->length
% pa_frame_size(spec
) == 0));
115 assert(spec
->format
== PA_SAMPLE_S16NE
);
117 if (volume
== PA_VOLUME_NORM
)
120 if (volume
== PA_VOLUME_MUTED
) {
121 pa_silence_memchunk(c
, spec
);
125 for (d
= (int16_t*) ((uint8_t*) c
->memblock
->data
+c
->index
), n
= c
->length
/sizeof(int16_t); n
> 0; d
++, n
--) {
126 int32_t t
= (int32_t)(*d
);
131 if (t
< -0x8000) t
= -0x8000;
132 if (t
> 0x7FFF) t
= 0x7FFF;