]>
code.delx.au - pulseaudio/blob - src/pulsecore/sample-util.c
4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 #include <liboil/liboilfuncs.h>
35 #include <pulsecore/log.h>
36 #include <pulsecore/macro.h>
38 #include "sample-util.h"
39 #include "endianmacros.h"
41 #define PA_SILENCE_MAX (1024*1024*1)
43 pa_memblock
*pa_silence_memblock_new(pa_mempool
*pool
, const pa_sample_spec
*spec
, size_t length
) {
49 length
= pa_bytes_per_second(spec
)/20; /* 50 ms */
51 if (length
> PA_SILENCE_MAX
)
52 length
= PA_SILENCE_MAX
;
54 fs
= pa_frame_size(spec
);
56 length
= (length
+fs
-1)/fs
;
63 return pa_silence_memblock(pa_memblock_new(pool
, length
), spec
);
66 pa_memblock
*pa_silence_memblock(pa_memblock
* b
, const pa_sample_spec
*spec
) {
72 data
= pa_memblock_acquire(b
);
73 pa_silence_memory(data
, pa_memblock_get_length(b
), spec
);
74 pa_memblock_release(b
);
78 void pa_silence_memchunk(pa_memchunk
*c
, const pa_sample_spec
*spec
) {
82 pa_assert(c
->memblock
);
85 data
= pa_memblock_acquire(c
->memblock
);
86 pa_silence_memory((uint8_t*) data
+c
->index
, c
->length
, spec
);
87 pa_memblock_release(c
->memblock
);
90 void pa_silence_memory(void *p
, size_t length
, const pa_sample_spec
*spec
) {
93 pa_assert(length
> 0);
96 switch (spec
->format
) {
100 case PA_SAMPLE_S16LE
:
101 case PA_SAMPLE_S16BE
:
102 case PA_SAMPLE_FLOAT32
:
103 case PA_SAMPLE_FLOAT32RE
:
111 pa_assert_not_reached();
114 memset(p
, c
, length
);
118 pa_mix_info streams
[],
122 const pa_sample_spec
*spec
,
123 const pa_cvolume
*volume
,
126 pa_cvolume full_volume
;
136 volume
= pa_cvolume_reset(&full_volume
, spec
->channels
);
138 for (k
= 0; k
< nstreams
; k
++)
139 streams
[k
].internal
= pa_memblock_acquire(streams
[k
].chunk
.memblock
);
141 switch (spec
->format
) {
142 case PA_SAMPLE_S16NE
:{
143 unsigned channel
= 0;
145 for (d
= 0;; d
+= sizeof(int16_t)) {
151 if (!mute
&& volume
->values
[channel
] != PA_VOLUME_MUTED
) {
154 for (i
= 0; i
< nstreams
; i
++) {
156 pa_volume_t cvolume
= streams
[i
].volume
.values
[channel
];
158 if (d
>= streams
[i
].chunk
.length
)
161 if (cvolume
== PA_VOLUME_MUTED
)
164 v
= *((int16_t*) ((uint8_t*) streams
[i
].internal
+ streams
[i
].chunk
.index
+ d
));
166 if (cvolume
!= PA_VOLUME_NORM
)
167 v
= (int32_t) (v
* pa_sw_volume_to_linear(cvolume
));
173 if (volume
->values
[channel
] != PA_VOLUME_NORM
)
174 sum
= (int32_t) (sum
* pa_sw_volume_to_linear(volume
->values
[channel
]));
176 if (sum
< -0x8000) sum
= -0x8000;
177 if (sum
> 0x7FFF) sum
= 0x7FFF;
181 *((int16_t*) data
) = sum
;
182 data
= (uint8_t*) data
+ sizeof(int16_t);
184 if (++channel
>= spec
->channels
)
191 case PA_SAMPLE_S16RE
:{
192 unsigned channel
= 0;
194 for (d
= 0;; d
+= sizeof(int16_t)) {
200 if (!mute
&& volume
->values
[channel
] != PA_VOLUME_MUTED
) {
203 for (i
= 0; i
< nstreams
; i
++) {
205 pa_volume_t cvolume
= streams
[i
].volume
.values
[channel
];
207 if (d
>= streams
[i
].chunk
.length
)
210 if (cvolume
== PA_VOLUME_MUTED
)
213 v
= INT16_SWAP(*((int16_t*) ((uint8_t*) streams
[i
].internal
+ streams
[i
].chunk
.index
+ d
)));
215 if (cvolume
!= PA_VOLUME_NORM
)
216 v
= (int32_t) (v
* pa_sw_volume_to_linear(cvolume
));
222 if (volume
->values
[channel
] != PA_VOLUME_NORM
)
223 sum
= (int32_t) (sum
* pa_sw_volume_to_linear(volume
->values
[channel
]));
225 if (sum
< -0x8000) sum
= -0x8000;
226 if (sum
> 0x7FFF) sum
= 0x7FFF;
230 *((int16_t*) data
) = INT16_SWAP(sum
);
231 data
= (uint8_t*) data
+ sizeof(int16_t);
233 if (++channel
>= spec
->channels
)
241 unsigned channel
= 0;
249 if (!mute
&& volume
->values
[channel
] != PA_VOLUME_MUTED
) {
252 for (i
= 0; i
< nstreams
; i
++) {
254 pa_volume_t cvolume
= streams
[i
].volume
.values
[channel
];
256 if (d
>= streams
[i
].chunk
.length
)
259 if (cvolume
== PA_VOLUME_MUTED
)
262 v
= (int32_t) *((uint8_t*) streams
[i
].internal
+ streams
[i
].chunk
.index
+ d
) - 0x80;
264 if (cvolume
!= PA_VOLUME_NORM
)
265 v
= (int32_t) (v
* pa_sw_volume_to_linear(cvolume
));
271 if (volume
->values
[channel
] != PA_VOLUME_NORM
)
272 sum
= (int32_t) (sum
* pa_sw_volume_to_linear(volume
->values
[channel
]));
274 if (sum
< -0x80) sum
= -0x80;
275 if (sum
> 0x7F) sum
= 0x7F;
279 *((uint8_t*) data
) = (uint8_t) (sum
+ 0x80);
280 data
= (uint8_t*) data
+ 1;
282 if (++channel
>= spec
->channels
)
289 case PA_SAMPLE_FLOAT32NE
: {
290 unsigned channel
= 0;
292 for (d
= 0;; d
+= sizeof(float)) {
298 if (!mute
&& volume
->values
[channel
] != PA_VOLUME_MUTED
) {
301 for (i
= 0; i
< nstreams
; i
++) {
303 pa_volume_t cvolume
= streams
[i
].volume
.values
[channel
];
305 if (d
>= streams
[i
].chunk
.length
)
308 if (cvolume
== PA_VOLUME_MUTED
)
311 v
= *((float*) ((uint8_t*) streams
[i
].internal
+ streams
[i
].chunk
.index
+ d
));
313 if (cvolume
!= PA_VOLUME_NORM
)
314 v
*= pa_sw_volume_to_linear(cvolume
);
320 if (volume
->values
[channel
] != PA_VOLUME_NORM
)
321 sum
*= pa_sw_volume_to_linear(volume
->values
[channel
]);
324 *((float*) data
) = sum
;
325 data
= (uint8_t*) data
+ sizeof(float);
327 if (++channel
>= spec
->channels
)
335 pa_log_error("ERROR: Unable to mix audio data of format %s.", pa_sample_format_to_string(spec
->format
));
341 for (k
= 0; k
< nstreams
; k
++)
342 pa_memblock_release(streams
[k
].chunk
.memblock
);
348 void pa_volume_memchunk(
350 const pa_sample_spec
*spec
,
351 const pa_cvolume
*volume
) {
357 pa_assert(c
->length
% pa_frame_size(spec
) == 0);
360 if (pa_cvolume_channels_equal_to(volume
, PA_VOLUME_NORM
))
363 if (pa_cvolume_channels_equal_to(volume
, PA_VOLUME_MUTED
)) {
364 pa_silence_memchunk(c
, spec
);
368 ptr
= pa_memblock_acquire(c
->memblock
);
370 switch (spec
->format
) {
371 case PA_SAMPLE_S16NE
: {
375 double linear
[PA_CHANNELS_MAX
];
377 for (channel
= 0; channel
< spec
->channels
; channel
++)
378 linear
[channel
] = pa_sw_volume_to_linear(volume
->values
[channel
]);
380 for (channel
= 0, d
= (int16_t*) ((uint8_t*) ptr
+ c
->index
), n
= c
->length
/sizeof(int16_t); n
> 0; d
++, n
--) {
381 int32_t t
= (int32_t)(*d
);
383 t
= (int32_t) (t
* linear
[channel
]);
385 if (t
< -0x8000) t
= -0x8000;
386 if (t
> 0x7FFF) t
= 0x7FFF;
390 if (++channel
>= spec
->channels
)
396 case PA_SAMPLE_S16RE
: {
400 double linear
[PA_CHANNELS_MAX
];
402 for (channel
= 0; channel
< spec
->channels
; channel
++)
403 linear
[channel
] = pa_sw_volume_to_linear(volume
->values
[channel
]);
405 for (channel
= 0, d
= (int16_t*) ((uint8_t*) ptr
+ c
->index
), n
= c
->length
/sizeof(int16_t); n
> 0; d
++, n
--) {
406 int32_t t
= (int32_t)(INT16_SWAP(*d
));
408 t
= (int32_t) (t
* linear
[channel
]);
410 if (t
< -0x8000) t
= -0x8000;
411 if (t
> 0x7FFF) t
= 0x7FFF;
413 *d
= INT16_SWAP((int16_t) t
);
415 if (++channel
>= spec
->channels
)
425 unsigned channel
= 0;
427 for (d
= (uint8_t*) ptr
+ c
->index
, n
= c
->length
; n
> 0; d
++, n
--) {
428 int32_t t
= (int32_t) *d
- 0x80;
430 t
= (int32_t) (t
* pa_sw_volume_to_linear(volume
->values
[channel
]));
432 if (t
< -0x80) t
= -0x80;
433 if (t
> 0x7F) t
= 0x7F;
435 *d
= (uint8_t) (t
+ 0x80);
437 if (++channel
>= spec
->channels
)
443 case PA_SAMPLE_FLOAT32NE
: {
449 d
= (float*) ((uint8_t*) ptr
+ c
->index
);
450 skip
= spec
->channels
* sizeof(float);
451 n
= c
->length
/sizeof(float)/spec
->channels
;
453 for (channel
= 0; channel
< spec
->channels
; channel
++) {
456 if (volume
->values
[channel
] == PA_VOLUME_NORM
)
459 v
= (float) pa_sw_volume_to_linear(volume
->values
[channel
]);
462 oil_scalarmult_f32(t
, skip
, t
, skip
, &v
, n
);
468 pa_log_error("ERROR: Unable to change volume of format %s.",
469 pa_sample_format_to_string(spec
->format
));
473 pa_memblock_release(c
->memblock
);