]>
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(const pa_sample_spec
*spec
, size_t length
, pa_memblock_stat
*s
) {
42 length
= pa_bytes_per_second(spec
)/10; /* 100 ms */
44 return pa_silence_memblock(pa_memblock_new(length
, s
), spec
);
47 pa_memblock
*pa_silence_memblock(pa_memblock
* b
, const pa_sample_spec
*spec
) {
48 assert(b
&& b
->data
&& spec
);
49 pa_silence_memory(b
->data
, b
->length
, spec
);
53 void pa_silence_memchunk(pa_memchunk
*c
, const pa_sample_spec
*spec
) {
54 assert(c
&& c
->memblock
&& c
->memblock
->data
&& spec
&& c
->length
);
56 pa_silence_memory((uint8_t*) c
->memblock
->data
+c
->index
, c
->length
, spec
);
59 void pa_silence_memory(void *p
, size_t length
, const pa_sample_spec
*spec
) {
61 assert(p
&& length
&& spec
);
63 switch (spec
->format
) {
69 case PA_SAMPLE_FLOAT32
:
84 const pa_mix_info streams
[],
88 const pa_sample_spec
*spec
,
89 const pa_cvolume
*volume
,
92 assert(streams
&& data
&& length
&& spec
);
94 switch (spec
->format
) {
95 case PA_SAMPLE_S16NE
:{
99 for (d
= 0;; d
+= sizeof(int16_t)) {
105 if (!mute
&& volume
->values
[channel
] != PA_VOLUME_MUTED
) {
108 for (i
= 0; i
< nstreams
; i
++) {
110 pa_volume_t cvolume
= streams
[i
].volume
.values
[channel
];
112 if (d
>= streams
[i
].chunk
.length
)
115 if (cvolume
== PA_VOLUME_MUTED
)
118 v
= *((int16_t*) ((uint8_t*) streams
[i
].chunk
.memblock
->data
+ streams
[i
].chunk
.index
+ d
));
120 if (cvolume
!= PA_VOLUME_NORM
)
121 v
= (int32_t) (v
* pa_sw_volume_to_linear(cvolume
));
127 if (volume
->values
[channel
] != PA_VOLUME_NORM
)
128 sum
= (int32_t) (sum
* pa_sw_volume_to_linear(volume
->values
[channel
]));
130 if (sum
< -0x8000) sum
= -0x8000;
131 if (sum
> 0x7FFF) sum
= 0x7FFF;
135 *((int16_t*) data
) = sum
;
136 data
= (uint8_t*) data
+ sizeof(int16_t);
138 if (++channel
>= spec
->channels
)
143 case PA_SAMPLE_S16RE
:{
145 unsigned channel
= 0;
147 for (d
= 0;; d
+= sizeof(int16_t)) {
153 if (!mute
&& volume
->values
[channel
] != PA_VOLUME_MUTED
) {
156 for (i
= 0; i
< nstreams
; i
++) {
158 pa_volume_t cvolume
= streams
[i
].volume
.values
[channel
];
160 if (d
>= streams
[i
].chunk
.length
)
163 if (cvolume
== PA_VOLUME_MUTED
)
166 v
= INT16_SWAP(*((int16_t*) ((uint8_t*) streams
[i
].chunk
.memblock
->data
+ streams
[i
].chunk
.index
+ d
)));
168 if (cvolume
!= PA_VOLUME_NORM
)
169 v
= (int32_t) (v
* pa_sw_volume_to_linear(cvolume
));
175 if (volume
->values
[channel
] != PA_VOLUME_NORM
)
176 sum
= (int32_t) (sum
* pa_sw_volume_to_linear(volume
->values
[channel
]));
178 if (sum
< -0x8000) sum
= -0x8000;
179 if (sum
> 0x7FFF) sum
= 0x7FFF;
183 *((int16_t*) data
) = INT16_SWAP(sum
);
184 data
= (uint8_t*) data
+ sizeof(int16_t);
186 if (++channel
>= spec
->channels
)
193 unsigned channel
= 0;
201 if (!mute
&& volume
->values
[channel
] != PA_VOLUME_MUTED
) {
204 for (i
= 0; i
< nstreams
; i
++) {
206 pa_volume_t cvolume
= streams
[i
].volume
.values
[channel
];
208 if (d
>= streams
[i
].chunk
.length
)
211 if (cvolume
== PA_VOLUME_MUTED
)
214 v
= (int32_t) *((uint8_t*) streams
[i
].chunk
.memblock
->data
+ streams
[i
].chunk
.index
+ d
) - 0x80;
216 if (cvolume
!= PA_VOLUME_NORM
)
217 v
= (int32_t) (v
* pa_sw_volume_to_linear(cvolume
));
223 if (volume
->values
[channel
] != PA_VOLUME_NORM
)
224 sum
= (int32_t) (sum
* pa_sw_volume_to_linear(volume
->values
[channel
]));
226 if (sum
< -0x80) sum
= -0x80;
227 if (sum
> 0x7F) sum
= 0x7F;
231 *((uint8_t*) data
) = (uint8_t) (sum
+ 0x80);
232 data
= (uint8_t*) data
+ 1;
234 if (++channel
>= spec
->channels
)
239 case PA_SAMPLE_FLOAT32NE
: {
241 unsigned channel
= 0;
243 for (d
= 0;; d
+= sizeof(float)) {
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
= *((float*) ((uint8_t*) streams
[i
].chunk
.memblock
->data
+ streams
[i
].chunk
.index
+ d
));
264 if (cvolume
!= PA_VOLUME_NORM
)
265 v
*= pa_sw_volume_to_linear(cvolume
);
271 if (volume
->values
[channel
] != PA_VOLUME_NORM
)
272 sum
*= pa_sw_volume_to_linear(volume
->values
[channel
]);
275 *((float*) data
) = sum
;
276 data
= (uint8_t*) data
+ sizeof(float);
278 if (++channel
>= spec
->channels
)
284 pa_log_error(__FILE__
": ERROR: Unable to mix audio data of format %s.", pa_sample_format_to_string(spec
->format
));
290 void pa_volume_memchunk(pa_memchunk
*c
, const pa_sample_spec
*spec
, const pa_cvolume
*volume
) {
291 assert(c
&& spec
&& (c
->length
% pa_frame_size(spec
) == 0));
294 if (pa_cvolume_channels_equal_to(volume
, PA_VOLUME_NORM
))
297 if (pa_cvolume_channels_equal_to(volume
, PA_VOLUME_MUTED
)) {
298 pa_silence_memchunk(c
, spec
);
302 switch (spec
->format
) {
303 case PA_SAMPLE_S16NE
: {
307 double linear
[PA_CHANNELS_MAX
];
309 for (channel
= 0; channel
< spec
->channels
; channel
++)
310 linear
[channel
] = pa_sw_volume_to_linear(volume
->values
[channel
]);
312 for (channel
= 0, d
= (int16_t*) ((uint8_t*) c
->memblock
->data
+c
->index
), n
= c
->length
/sizeof(int16_t); n
> 0; d
++, n
--) {
313 int32_t t
= (int32_t)(*d
);
315 t
= (int32_t) (t
* linear
[channel
]);
317 if (t
< -0x8000) t
= -0x8000;
318 if (t
> 0x7FFF) t
= 0x7FFF;
322 if (++channel
>= spec
->channels
)
328 case PA_SAMPLE_S16RE
: {
332 double linear
[PA_CHANNELS_MAX
];
334 for (channel
= 0; channel
< spec
->channels
; channel
++)
335 linear
[channel
] = pa_sw_volume_to_linear(volume
->values
[channel
]);
337 for (channel
= 0, d
= (int16_t*) ((uint8_t*) c
->memblock
->data
+c
->index
), n
= c
->length
/sizeof(int16_t); n
> 0; d
++, n
--) {
338 int32_t t
= (int32_t)(INT16_SWAP(*d
));
340 t
= (int32_t) (t
* linear
[channel
]);
342 if (t
< -0x8000) t
= -0x8000;
343 if (t
> 0x7FFF) t
= 0x7FFF;
345 *d
= INT16_SWAP((int16_t) t
);
347 if (++channel
>= spec
->channels
)
357 unsigned channel
= 0;
359 for (d
= (uint8_t*) c
->memblock
->data
+ c
->index
, n
= c
->length
; n
> 0; d
++, n
--) {
360 int32_t t
= (int32_t) *d
- 0x80;
362 t
= (int32_t) (t
* pa_sw_volume_to_linear(volume
->values
[channel
]));
364 if (t
< -0x80) t
= -0x80;
365 if (t
> 0x7F) t
= 0x7F;
367 *d
= (uint8_t) (t
+ 0x80);
369 if (++channel
>= spec
->channels
)
375 case PA_SAMPLE_FLOAT32NE
: {
381 d
= (float*) ((uint8_t*) c
->memblock
->data
+ c
->index
);
382 skip
= spec
->channels
* sizeof(float);
383 n
= c
->length
/sizeof(float)/spec
->channels
;
385 for (channel
= 0; channel
< spec
->channels
; channel
++) {
388 if (volume
->values
[channel
] == PA_VOLUME_NORM
)
391 v
= (float) pa_sw_volume_to_linear(volume
->values
[channel
]);
394 oil_scalarmult_f32(t
, skip
, t
, skip
, &v
, n
);
400 pa_log_error(__FILE__
": ERROR: Unable to change volume of format %s.",
401 pa_sample_format_to_string(spec
->format
));