]>
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
:
71 case PA_SAMPLE_FLOAT32RE
:
86 const pa_mix_info streams
[],
90 const pa_sample_spec
*spec
,
91 const pa_cvolume
*volume
,
94 assert(streams
&& data
&& length
&& spec
);
96 switch (spec
->format
) {
97 case PA_SAMPLE_S16NE
:{
101 for (d
= 0;; d
+= sizeof(int16_t)) {
107 if (!mute
&& volume
->values
[channel
] != PA_VOLUME_MUTED
) {
110 for (i
= 0; i
< nstreams
; i
++) {
112 pa_volume_t cvolume
= streams
[i
].volume
.values
[channel
];
114 if (d
>= streams
[i
].chunk
.length
)
117 if (cvolume
== PA_VOLUME_MUTED
)
120 v
= *((int16_t*) ((uint8_t*) streams
[i
].chunk
.memblock
->data
+ streams
[i
].chunk
.index
+ d
));
122 if (cvolume
!= PA_VOLUME_NORM
)
123 v
= (int32_t) (v
* pa_sw_volume_to_linear(cvolume
));
129 if (volume
->values
[channel
] != PA_VOLUME_NORM
)
130 sum
= (int32_t) (sum
* pa_sw_volume_to_linear(volume
->values
[channel
]));
132 if (sum
< -0x8000) sum
= -0x8000;
133 if (sum
> 0x7FFF) sum
= 0x7FFF;
137 *((int16_t*) data
) = sum
;
138 data
= (uint8_t*) data
+ sizeof(int16_t);
140 if (++channel
>= spec
->channels
)
145 case PA_SAMPLE_S16RE
:{
147 unsigned channel
= 0;
149 for (d
= 0;; d
+= sizeof(int16_t)) {
155 if (!mute
&& volume
->values
[channel
] != PA_VOLUME_MUTED
) {
158 for (i
= 0; i
< nstreams
; i
++) {
160 pa_volume_t cvolume
= streams
[i
].volume
.values
[channel
];
162 if (d
>= streams
[i
].chunk
.length
)
165 if (cvolume
== PA_VOLUME_MUTED
)
168 v
= INT16_SWAP(*((int16_t*) ((uint8_t*) streams
[i
].chunk
.memblock
->data
+ streams
[i
].chunk
.index
+ d
)));
170 if (cvolume
!= PA_VOLUME_NORM
)
171 v
= (int32_t) (v
* pa_sw_volume_to_linear(cvolume
));
177 if (volume
->values
[channel
] != PA_VOLUME_NORM
)
178 sum
= (int32_t) (sum
* pa_sw_volume_to_linear(volume
->values
[channel
]));
180 if (sum
< -0x8000) sum
= -0x8000;
181 if (sum
> 0x7FFF) sum
= 0x7FFF;
185 *((int16_t*) data
) = INT16_SWAP(sum
);
186 data
= (uint8_t*) data
+ sizeof(int16_t);
188 if (++channel
>= spec
->channels
)
195 unsigned channel
= 0;
203 if (!mute
&& volume
->values
[channel
] != PA_VOLUME_MUTED
) {
206 for (i
= 0; i
< nstreams
; i
++) {
208 pa_volume_t cvolume
= streams
[i
].volume
.values
[channel
];
210 if (d
>= streams
[i
].chunk
.length
)
213 if (cvolume
== PA_VOLUME_MUTED
)
216 v
= (int32_t) *((uint8_t*) streams
[i
].chunk
.memblock
->data
+ streams
[i
].chunk
.index
+ d
) - 0x80;
218 if (cvolume
!= PA_VOLUME_NORM
)
219 v
= (int32_t) (v
* pa_sw_volume_to_linear(cvolume
));
225 if (volume
->values
[channel
] != PA_VOLUME_NORM
)
226 sum
= (int32_t) (sum
* pa_sw_volume_to_linear(volume
->values
[channel
]));
228 if (sum
< -0x80) sum
= -0x80;
229 if (sum
> 0x7F) sum
= 0x7F;
233 *((uint8_t*) data
) = (uint8_t) (sum
+ 0x80);
234 data
= (uint8_t*) data
+ 1;
236 if (++channel
>= spec
->channels
)
241 case PA_SAMPLE_FLOAT32NE
: {
243 unsigned channel
= 0;
245 for (d
= 0;; d
+= sizeof(float)) {
251 if (!mute
&& volume
->values
[channel
] != PA_VOLUME_MUTED
) {
254 for (i
= 0; i
< nstreams
; i
++) {
256 pa_volume_t cvolume
= streams
[i
].volume
.values
[channel
];
258 if (d
>= streams
[i
].chunk
.length
)
261 if (cvolume
== PA_VOLUME_MUTED
)
264 v
= *((float*) ((uint8_t*) streams
[i
].chunk
.memblock
->data
+ streams
[i
].chunk
.index
+ d
));
266 if (cvolume
!= PA_VOLUME_NORM
)
267 v
*= pa_sw_volume_to_linear(cvolume
);
273 if (volume
->values
[channel
] != PA_VOLUME_NORM
)
274 sum
*= pa_sw_volume_to_linear(volume
->values
[channel
]);
277 *((float*) data
) = sum
;
278 data
= (uint8_t*) data
+ sizeof(float);
280 if (++channel
>= spec
->channels
)
286 pa_log_error("ERROR: Unable to mix audio data of format %s.", pa_sample_format_to_string(spec
->format
));
292 void pa_volume_memchunk(pa_memchunk
*c
, const pa_sample_spec
*spec
, const pa_cvolume
*volume
) {
293 assert(c
&& spec
&& (c
->length
% pa_frame_size(spec
) == 0));
296 if (pa_cvolume_channels_equal_to(volume
, PA_VOLUME_NORM
))
299 if (pa_cvolume_channels_equal_to(volume
, PA_VOLUME_MUTED
)) {
300 pa_silence_memchunk(c
, spec
);
304 switch (spec
->format
) {
305 case PA_SAMPLE_S16NE
: {
309 double linear
[PA_CHANNELS_MAX
];
311 for (channel
= 0; channel
< spec
->channels
; channel
++)
312 linear
[channel
] = pa_sw_volume_to_linear(volume
->values
[channel
]);
314 for (channel
= 0, d
= (int16_t*) ((uint8_t*) c
->memblock
->data
+c
->index
), n
= c
->length
/sizeof(int16_t); n
> 0; d
++, n
--) {
315 int32_t t
= (int32_t)(*d
);
317 t
= (int32_t) (t
* linear
[channel
]);
319 if (t
< -0x8000) t
= -0x8000;
320 if (t
> 0x7FFF) t
= 0x7FFF;
324 if (++channel
>= spec
->channels
)
330 case PA_SAMPLE_S16RE
: {
334 double linear
[PA_CHANNELS_MAX
];
336 for (channel
= 0; channel
< spec
->channels
; channel
++)
337 linear
[channel
] = pa_sw_volume_to_linear(volume
->values
[channel
]);
339 for (channel
= 0, d
= (int16_t*) ((uint8_t*) c
->memblock
->data
+c
->index
), n
= c
->length
/sizeof(int16_t); n
> 0; d
++, n
--) {
340 int32_t t
= (int32_t)(INT16_SWAP(*d
));
342 t
= (int32_t) (t
* linear
[channel
]);
344 if (t
< -0x8000) t
= -0x8000;
345 if (t
> 0x7FFF) t
= 0x7FFF;
347 *d
= INT16_SWAP((int16_t) t
);
349 if (++channel
>= spec
->channels
)
359 unsigned channel
= 0;
361 for (d
= (uint8_t*) c
->memblock
->data
+ c
->index
, n
= c
->length
; n
> 0; d
++, n
--) {
362 int32_t t
= (int32_t) *d
- 0x80;
364 t
= (int32_t) (t
* pa_sw_volume_to_linear(volume
->values
[channel
]));
366 if (t
< -0x80) t
= -0x80;
367 if (t
> 0x7F) t
= 0x7F;
369 *d
= (uint8_t) (t
+ 0x80);
371 if (++channel
>= spec
->channels
)
377 case PA_SAMPLE_FLOAT32NE
: {
383 d
= (float*) ((uint8_t*) c
->memblock
->data
+ c
->index
);
384 skip
= spec
->channels
* sizeof(float);
385 n
= c
->length
/sizeof(float)/spec
->channels
;
387 for (channel
= 0; channel
< spec
->channels
; channel
++) {
390 if (volume
->values
[channel
] == PA_VOLUME_NORM
)
393 v
= (float) pa_sw_volume_to_linear(volume
->values
[channel
]);
396 oil_scalarmult_f32(t
, skip
, t
, skip
, &v
, n
);
402 pa_log_error("ERROR: Unable to change volume of format %s.",
403 pa_sample_format_to_string(spec
->format
));