]>
code.delx.au - pulseaudio/blob - src/pulsecore/sample-util.c
4 This file is part of PulseAudio.
6 PulseAudio 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 PulseAudio 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 PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <liboil/liboilfuncs.h>
33 #include <pulsecore/log.h>
35 #include "sample-util.h"
36 #include "endianmacros.h"
38 pa_memblock
*pa_silence_memblock_new(pa_mempool
*pool
, const pa_sample_spec
*spec
, size_t length
) {
43 length
= pa_bytes_per_second(spec
)/20; /* 50 ms */
45 return pa_silence_memblock(pa_memblock_new(pool
, length
), spec
);
48 pa_memblock
*pa_silence_memblock(pa_memblock
* b
, const pa_sample_spec
*spec
) {
49 assert(b
&& b
->data
&& spec
);
50 pa_silence_memory(b
->data
, b
->length
, spec
);
54 void pa_silence_memchunk(pa_memchunk
*c
, const pa_sample_spec
*spec
) {
55 assert(c
&& c
->memblock
&& c
->memblock
->data
&& spec
&& c
->length
);
57 pa_silence_memory((uint8_t*) c
->memblock
->data
+c
->index
, c
->length
, spec
);
60 void pa_silence_memory(void *p
, size_t length
, const pa_sample_spec
*spec
) {
62 assert(p
&& length
&& spec
);
64 switch (spec
->format
) {
70 case PA_SAMPLE_FLOAT32
:
85 const pa_mix_info streams
[],
89 const pa_sample_spec
*spec
,
90 const pa_cvolume
*volume
,
93 assert(streams
&& data
&& length
&& spec
);
95 switch (spec
->format
) {
96 case PA_SAMPLE_S16NE
:{
100 for (d
= 0;; d
+= sizeof(int16_t)) {
106 if (!mute
&& volume
->values
[channel
] != PA_VOLUME_MUTED
) {
109 for (i
= 0; i
< nstreams
; i
++) {
111 pa_volume_t cvolume
= streams
[i
].volume
.values
[channel
];
113 if (d
>= streams
[i
].chunk
.length
)
116 if (cvolume
== PA_VOLUME_MUTED
)
119 v
= *((int16_t*) ((uint8_t*) streams
[i
].chunk
.memblock
->data
+ streams
[i
].chunk
.index
+ d
));
121 if (cvolume
!= PA_VOLUME_NORM
)
122 v
= (int32_t) (v
* pa_sw_volume_to_linear(cvolume
));
128 if (volume
->values
[channel
] != PA_VOLUME_NORM
)
129 sum
= (int32_t) (sum
* pa_sw_volume_to_linear(volume
->values
[channel
]));
131 if (sum
< -0x8000) sum
= -0x8000;
132 if (sum
> 0x7FFF) sum
= 0x7FFF;
136 *((int16_t*) data
) = sum
;
137 data
= (uint8_t*) data
+ sizeof(int16_t);
139 if (++channel
>= spec
->channels
)
144 case PA_SAMPLE_S16RE
:{
146 unsigned channel
= 0;
148 for (d
= 0;; d
+= sizeof(int16_t)) {
154 if (!mute
&& volume
->values
[channel
] != PA_VOLUME_MUTED
) {
157 for (i
= 0; i
< nstreams
; i
++) {
159 pa_volume_t cvolume
= streams
[i
].volume
.values
[channel
];
161 if (d
>= streams
[i
].chunk
.length
)
164 if (cvolume
== PA_VOLUME_MUTED
)
167 v
= INT16_SWAP(*((int16_t*) ((uint8_t*) streams
[i
].chunk
.memblock
->data
+ streams
[i
].chunk
.index
+ d
)));
169 if (cvolume
!= PA_VOLUME_NORM
)
170 v
= (int32_t) (v
* pa_sw_volume_to_linear(cvolume
));
176 if (volume
->values
[channel
] != PA_VOLUME_NORM
)
177 sum
= (int32_t) (sum
* pa_sw_volume_to_linear(volume
->values
[channel
]));
179 if (sum
< -0x8000) sum
= -0x8000;
180 if (sum
> 0x7FFF) sum
= 0x7FFF;
184 *((int16_t*) data
) = INT16_SWAP(sum
);
185 data
= (uint8_t*) data
+ sizeof(int16_t);
187 if (++channel
>= spec
->channels
)
194 unsigned channel
= 0;
202 if (!mute
&& volume
->values
[channel
] != PA_VOLUME_MUTED
) {
205 for (i
= 0; i
< nstreams
; i
++) {
207 pa_volume_t cvolume
= streams
[i
].volume
.values
[channel
];
209 if (d
>= streams
[i
].chunk
.length
)
212 if (cvolume
== PA_VOLUME_MUTED
)
215 v
= (int32_t) *((uint8_t*) streams
[i
].chunk
.memblock
->data
+ streams
[i
].chunk
.index
+ d
) - 0x80;
217 if (cvolume
!= PA_VOLUME_NORM
)
218 v
= (int32_t) (v
* pa_sw_volume_to_linear(cvolume
));
224 if (volume
->values
[channel
] != PA_VOLUME_NORM
)
225 sum
= (int32_t) (sum
* pa_sw_volume_to_linear(volume
->values
[channel
]));
227 if (sum
< -0x80) sum
= -0x80;
228 if (sum
> 0x7F) sum
= 0x7F;
232 *((uint8_t*) data
) = (uint8_t) (sum
+ 0x80);
233 data
= (uint8_t*) data
+ 1;
235 if (++channel
>= spec
->channels
)
240 case PA_SAMPLE_FLOAT32NE
: {
242 unsigned channel
= 0;
244 for (d
= 0;; d
+= sizeof(float)) {
250 if (!mute
&& volume
->values
[channel
] != PA_VOLUME_MUTED
) {
253 for (i
= 0; i
< nstreams
; i
++) {
255 pa_volume_t cvolume
= streams
[i
].volume
.values
[channel
];
257 if (d
>= streams
[i
].chunk
.length
)
260 if (cvolume
== PA_VOLUME_MUTED
)
263 v
= *((float*) ((uint8_t*) streams
[i
].chunk
.memblock
->data
+ streams
[i
].chunk
.index
+ d
));
265 if (cvolume
!= PA_VOLUME_NORM
)
266 v
*= pa_sw_volume_to_linear(cvolume
);
272 if (volume
->values
[channel
] != PA_VOLUME_NORM
)
273 sum
*= pa_sw_volume_to_linear(volume
->values
[channel
]);
276 *((float*) data
) = sum
;
277 data
= (uint8_t*) data
+ sizeof(float);
279 if (++channel
>= spec
->channels
)
285 pa_log_error("ERROR: Unable to mix audio data of format %s.", pa_sample_format_to_string(spec
->format
));
291 void pa_volume_memchunk(pa_memchunk
*c
, const pa_sample_spec
*spec
, const pa_cvolume
*volume
) {
292 assert(c
&& spec
&& (c
->length
% pa_frame_size(spec
) == 0));
295 if (pa_cvolume_channels_equal_to(volume
, PA_VOLUME_NORM
))
298 if (pa_cvolume_channels_equal_to(volume
, PA_VOLUME_MUTED
)) {
299 pa_silence_memchunk(c
, spec
);
303 switch (spec
->format
) {
304 case PA_SAMPLE_S16NE
: {
308 double linear
[PA_CHANNELS_MAX
];
310 for (channel
= 0; channel
< spec
->channels
; channel
++)
311 linear
[channel
] = pa_sw_volume_to_linear(volume
->values
[channel
]);
313 for (channel
= 0, d
= (int16_t*) ((uint8_t*) c
->memblock
->data
+c
->index
), n
= c
->length
/sizeof(int16_t); n
> 0; d
++, n
--) {
314 int32_t t
= (int32_t)(*d
);
316 t
= (int32_t) (t
* linear
[channel
]);
318 if (t
< -0x8000) t
= -0x8000;
319 if (t
> 0x7FFF) t
= 0x7FFF;
323 if (++channel
>= spec
->channels
)
329 case PA_SAMPLE_S16RE
: {
333 double linear
[PA_CHANNELS_MAX
];
335 for (channel
= 0; channel
< spec
->channels
; channel
++)
336 linear
[channel
] = pa_sw_volume_to_linear(volume
->values
[channel
]);
338 for (channel
= 0, d
= (int16_t*) ((uint8_t*) c
->memblock
->data
+c
->index
), n
= c
->length
/sizeof(int16_t); n
> 0; d
++, n
--) {
339 int32_t t
= (int32_t)(INT16_SWAP(*d
));
341 t
= (int32_t) (t
* linear
[channel
]);
343 if (t
< -0x8000) t
= -0x8000;
344 if (t
> 0x7FFF) t
= 0x7FFF;
346 *d
= INT16_SWAP((int16_t) t
);
348 if (++channel
>= spec
->channels
)
358 unsigned channel
= 0;
360 for (d
= (uint8_t*) c
->memblock
->data
+ c
->index
, n
= c
->length
; n
> 0; d
++, n
--) {
361 int32_t t
= (int32_t) *d
- 0x80;
363 t
= (int32_t) (t
* pa_sw_volume_to_linear(volume
->values
[channel
]));
365 if (t
< -0x80) t
= -0x80;
366 if (t
> 0x7F) t
= 0x7F;
368 *d
= (uint8_t) (t
+ 0x80);
370 if (++channel
>= spec
->channels
)
376 case PA_SAMPLE_FLOAT32NE
: {
382 d
= (float*) ((uint8_t*) c
->memblock
->data
+ c
->index
);
383 skip
= spec
->channels
* sizeof(float);
384 n
= c
->length
/sizeof(float)/spec
->channels
;
386 for (channel
= 0; channel
< spec
->channels
; channel
++) {
389 if (volume
->values
[channel
] == PA_VOLUME_NORM
)
392 v
= (float) pa_sw_volume_to_linear(volume
->values
[channel
]);
395 oil_scalarmult_f32(t
, skip
, t
, skip
, &v
, n
);
401 pa_log_error("ERROR: Unable to change volume of format %s.",
402 pa_sample_format_to_string(spec
->format
));