]>
code.delx.au - pulseaudio/blob - polyp/sample-util.c
d521afe4a15f68ef5f6fe0fdd9be70cecad6f73c
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 Lesser 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 Lesser 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
31 #include "sample-util.h"
33 struct pa_memblock
*pa_silence_memblock(struct pa_memblock
* b
, const struct pa_sample_spec
*spec
) {
34 assert(b
&& b
->data
&& spec
);
35 pa_silence_memory(b
->data
, b
->length
, spec
);
39 void pa_silence_memchunk(struct pa_memchunk
*c
, const struct pa_sample_spec
*spec
) {
40 assert(c
&& c
->memblock
&& c
->memblock
->data
&& spec
&& c
->length
);
41 pa_silence_memory((uint8_t*) c
->memblock
->data
+c
->index
, c
->length
, spec
);
44 void pa_silence_memory(void *p
, size_t length
, const struct pa_sample_spec
*spec
) {
46 assert(p
&& length
&& spec
);
48 switch (spec
->format
) {
54 case PA_SAMPLE_FLOAT32
:
68 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
);
71 if (spec
->format
== PA_SAMPLE_S16NE
) {
74 for (d
= 0;; d
+= sizeof(int16_t)) {
81 for (c
= 0; c
< nchannels
; c
++) {
83 pa_volume_t cvolume
= channels
[c
].volume
;
85 if (d
>= channels
[c
].chunk
.length
)
88 if (cvolume
== PA_VOLUME_MUTED
)
91 v
= *((int16_t*) ((uint8_t*) channels
[c
].chunk
.memblock
->data
+ channels
[c
].chunk
.index
+ d
));
93 if (cvolume
!= PA_VOLUME_NORM
) {
102 if (volume
== PA_VOLUME_MUTED
)
104 else if (volume
!= PA_VOLUME_NORM
) {
106 sum
/= PA_VOLUME_NORM
;
109 if (sum
< -0x8000) sum
= -0x8000;
110 if (sum
> 0x7FFF) sum
= 0x7FFF;
112 *((int16_t*) data
) = sum
;
113 data
= (uint8_t*) data
+ sizeof(int16_t);
115 } else if (spec
->format
== PA_SAMPLE_U8
) {
125 for (c
= 0; c
< nchannels
; c
++) {
127 pa_volume_t cvolume
= channels
[c
].volume
;
129 if (d
>= channels
[c
].chunk
.length
)
132 if (cvolume
== PA_VOLUME_MUTED
)
135 v
= (int32_t) *((uint8_t*) channels
[c
].chunk
.memblock
->data
+ channels
[c
].chunk
.index
+ d
) - 0x80;
137 if (cvolume
!= PA_VOLUME_NORM
) {
146 if (volume
== PA_VOLUME_MUTED
)
148 else if (volume
!= PA_VOLUME_NORM
) {
150 sum
/= PA_VOLUME_NORM
;
153 if (sum
< -0x80) sum
= -0x80;
154 if (sum
> 0x7F) sum
= 0x7F;
156 *((uint8_t*) data
) = (uint8_t) (sum
+ 0x80);
157 data
= (uint8_t*) data
+ 1;
160 } else if (spec
->format
== PA_SAMPLE_FLOAT32NE
) {
163 for (d
= 0;; d
+= sizeof(float)) {
170 for (c
= 0; c
< nchannels
; c
++) {
172 pa_volume_t cvolume
= channels
[c
].volume
;
174 if (d
>= channels
[c
].chunk
.length
)
177 if (cvolume
== PA_VOLUME_MUTED
)
180 v
= *((float*) ((uint8_t*) channels
[c
].chunk
.memblock
->data
+ channels
[c
].chunk
.index
+ d
));
182 if (cvolume
!= PA_VOLUME_NORM
)
183 v
= v
*cvolume
/PA_VOLUME_NORM
;
189 if (volume
== PA_VOLUME_MUTED
)
191 else if (volume
!= PA_VOLUME_NORM
)
192 sum
= sum
*volume
/PA_VOLUME_NORM
;
194 if (sum
< -1) sum
= -1;
195 if (sum
> 1) sum
= 1;
197 *((float*) data
) = sum
;
198 data
= (uint8_t*) data
+ sizeof(float);
206 void pa_volume_memchunk(struct pa_memchunk
*c
, const struct pa_sample_spec
*spec
, pa_volume_t volume
) {
207 assert(c
&& spec
&& (c
->length
% pa_frame_size(spec
) == 0));
209 if (volume
== PA_VOLUME_NORM
)
212 if (volume
== PA_VOLUME_MUTED
) {
213 pa_silence_memchunk(c
, spec
);
217 if (spec
->format
== PA_SAMPLE_S16NE
) {
221 for (d
= (int16_t*) ((uint8_t*) c
->memblock
->data
+c
->index
), n
= c
->length
/sizeof(int16_t); n
> 0; d
++, n
--) {
222 int32_t t
= (int32_t)(*d
);
227 if (t
< -0x8000) t
= -0x8000;
228 if (t
> 0x7FFF) t
= 0x7FFF;
232 } else if (spec
->format
== PA_SAMPLE_U8
) {
236 for (d
= (uint8_t*) c
->memblock
->data
+ c
->index
, n
= c
->length
; n
> 0; d
++, n
--) {
237 int32_t t
= (int32_t) *d
- 0x80;
242 if (t
< -0x80) t
= -0x80;
243 if (t
> 0x7F) t
= 0x7F;
245 *d
= (uint8_t) (t
+ 0x80);
248 } else if (spec
->format
== PA_SAMPLE_FLOAT32NE
) {
252 for (d
= (float*) ((uint8_t*) c
->memblock
->data
+c
->index
), n
= c
->length
/sizeof(float); n
> 0; d
++, n
--) {