2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #include <liboil/liboilfuncs.h>
33 #include <liboil/liboil.h>
35 #include <pulse/timeval.h>
37 #include <pulsecore/log.h>
38 #include <pulsecore/macro.h>
39 #include <pulsecore/g711.h>
41 #include "sample-util.h"
42 #include "endianmacros.h"
44 #define PA_SILENCE_MAX (PA_PAGE_SIZE*16)
46 pa_memblock
*pa_silence_memblock(pa_memblock
* b
, const pa_sample_spec
*spec
) {
52 data
= pa_memblock_acquire(b
);
53 pa_silence_memory(data
, pa_memblock_get_length(b
), spec
);
54 pa_memblock_release(b
);
59 pa_memchunk
* pa_silence_memchunk(pa_memchunk
*c
, const pa_sample_spec
*spec
) {
63 pa_assert(c
->memblock
);
66 data
= pa_memblock_acquire(c
->memblock
);
67 pa_silence_memory((uint8_t*) data
+c
->index
, c
->length
, spec
);
68 pa_memblock_release(c
->memblock
);
73 static uint8_t silence_byte(pa_sample_format_t format
) {
81 case PA_SAMPLE_FLOAT32LE
:
82 case PA_SAMPLE_FLOAT32BE
:
89 pa_assert_not_reached();
93 void* pa_silence_memory(void *p
, size_t length
, const pa_sample_spec
*spec
) {
95 pa_assert(length
> 0);
98 memset(p
, silence_byte(spec
->format
), length
);
102 static void calc_linear_integer_volume(int32_t linear
[], const pa_cvolume
*volume
) {
108 for (channel
= 0; channel
< volume
->channels
; channel
++)
109 linear
[channel
] = (int32_t) lrint(pa_sw_volume_to_linear(volume
->values
[channel
]) * 0x10000);
112 static void calc_linear_float_volume(float linear
[], const pa_cvolume
*volume
) {
118 for (channel
= 0; channel
< volume
->channels
; channel
++)
119 linear
[channel
] = (float) pa_sw_volume_to_linear(volume
->values
[channel
]);
122 static void calc_linear_integer_stream_volumes(pa_mix_info streams
[], unsigned nstreams
, const pa_cvolume
*volume
, const pa_sample_spec
*spec
) {
124 float linear
[PA_CHANNELS_MAX
];
130 calc_linear_float_volume(linear
, volume
);
132 for (k
= 0; k
< nstreams
; k
++) {
134 for (channel
= 0; channel
< spec
->channels
; channel
++) {
135 pa_mix_info
*m
= streams
+ k
;
136 m
->linear
[channel
].i
= (int32_t) lrint(pa_sw_volume_to_linear(m
->volume
.values
[channel
]) * linear
[channel
] * 0x10000);
141 static void calc_linear_float_stream_volumes(pa_mix_info streams
[], unsigned nstreams
, const pa_cvolume
*volume
, const pa_sample_spec
*spec
) {
143 float linear
[PA_CHANNELS_MAX
];
149 calc_linear_float_volume(linear
, volume
);
151 for (k
= 0; k
< nstreams
; k
++) {
153 for (channel
= 0; channel
< spec
->channels
; channel
++) {
154 pa_mix_info
*m
= streams
+ k
;
155 m
->linear
[channel
].f
= (float) (pa_sw_volume_to_linear(m
->volume
.values
[channel
]) * linear
[channel
]);
161 pa_mix_info streams
[],
165 const pa_sample_spec
*spec
,
166 const pa_cvolume
*volume
,
169 pa_cvolume full_volume
;
180 volume
= pa_cvolume_reset(&full_volume
, spec
->channels
);
182 if (mute
|| pa_cvolume_is_muted(volume
) || nstreams
<= 0) {
183 pa_silence_memory(data
, length
, spec
);
187 for (k
= 0; k
< nstreams
; k
++)
188 streams
[k
].ptr
= (uint8_t*) pa_memblock_acquire(streams
[k
].chunk
.memblock
) + streams
[k
].chunk
.index
;
190 for (z
= 0; z
< nstreams
; z
++)
191 if (length
> streams
[z
].chunk
.length
)
192 length
= streams
[z
].chunk
.length
;
194 end
= (uint8_t*) data
+ length
;
196 switch (spec
->format
) {
198 case PA_SAMPLE_S16NE
:{
199 unsigned channel
= 0;
201 calc_linear_integer_stream_volumes(streams
, nstreams
, volume
, spec
);
207 for (i
= 0; i
< nstreams
; i
++) {
208 pa_mix_info
*m
= streams
+ i
;
209 int32_t v
, cv
= m
->linear
[channel
].i
;
211 if (PA_UNLIKELY(cv
<= 0))
214 v
= *((int16_t*) m
->ptr
);
215 v
= (v
* cv
) / 0x10000;
218 m
->ptr
= (uint8_t*) m
->ptr
+ sizeof(int16_t);
221 sum
= PA_CLAMP_UNLIKELY(sum
, -0x8000, 0x7FFF);
222 *((int16_t*) data
) = (int16_t) sum
;
224 data
= (uint8_t*) data
+ sizeof(int16_t);
226 if (PA_UNLIKELY(++channel
>= spec
->channels
))
233 case PA_SAMPLE_S16RE
:{
234 unsigned channel
= 0;
236 calc_linear_integer_stream_volumes(streams
, nstreams
, volume
, spec
);
242 for (i
= 0; i
< nstreams
; i
++) {
243 pa_mix_info
*m
= streams
+ i
;
244 int32_t v
, cv
= m
->linear
[channel
].i
;
246 if (PA_UNLIKELY(cv
<= 0))
249 v
= PA_INT16_SWAP(*((int16_t*) m
->ptr
));
250 v
= (v
* cv
) / 0x10000;
253 m
->ptr
= (uint8_t*) m
->ptr
+ sizeof(int16_t);
256 sum
= PA_CLAMP_UNLIKELY(sum
, -0x8000, 0x7FFF);
257 *((int16_t*) data
) = PA_INT16_SWAP((int16_t) sum
);
259 data
= (uint8_t*) data
+ sizeof(int16_t);
261 if (PA_UNLIKELY(++channel
>= spec
->channels
))
268 case PA_SAMPLE_S32NE
:{
269 unsigned channel
= 0;
271 calc_linear_integer_stream_volumes(streams
, nstreams
, volume
, spec
);
277 for (i
= 0; i
< nstreams
; i
++) {
278 pa_mix_info
*m
= streams
+ i
;
279 int32_t cv
= m
->linear
[channel
].i
;
282 if (PA_UNLIKELY(cv
<= 0))
285 v
= *((int32_t*) m
->ptr
);
286 v
= (v
* cv
) / 0x10000;
289 m
->ptr
= (uint8_t*) m
->ptr
+ sizeof(int32_t);
292 sum
= PA_CLAMP_UNLIKELY(sum
, -0x80000000LL
, 0x7FFFFFFFLL
);
293 *((int32_t*) data
) = (int32_t) sum
;
295 data
= (uint8_t*) data
+ sizeof(int32_t);
297 if (PA_UNLIKELY(++channel
>= spec
->channels
))
304 case PA_SAMPLE_S32RE
:{
305 unsigned channel
= 0;
307 calc_linear_integer_stream_volumes(streams
, nstreams
, volume
, spec
);
313 for (i
= 0; i
< nstreams
; i
++) {
314 pa_mix_info
*m
= streams
+ i
;
315 int32_t cv
= m
->linear
[channel
].i
;
318 if (PA_UNLIKELY(cv
<= 0))
321 v
= PA_INT32_SWAP(*((int32_t*) m
->ptr
));
322 v
= (v
* cv
) / 0x10000;
325 m
->ptr
= (uint8_t*) m
->ptr
+ sizeof(int32_t);
328 sum
= PA_CLAMP_UNLIKELY(sum
, -0x80000000LL
, 0x7FFFFFFFLL
);
329 *((int32_t*) data
) = PA_INT32_SWAP((int32_t) sum
);
331 data
= (uint8_t*) data
+ sizeof(int32_t);
333 if (PA_UNLIKELY(++channel
>= spec
->channels
))
341 unsigned channel
= 0;
343 calc_linear_integer_stream_volumes(streams
, nstreams
, volume
, spec
);
349 for (i
= 0; i
< nstreams
; i
++) {
350 pa_mix_info
*m
= streams
+ i
;
351 int32_t v
, cv
= m
->linear
[channel
].i
;
353 if (PA_UNLIKELY(cv
<= 0))
356 v
= (int32_t) *((uint8_t*) m
->ptr
) - 0x80;
357 v
= (v
* cv
) / 0x10000;
360 m
->ptr
= (uint8_t*) m
->ptr
+ 1;
363 sum
= PA_CLAMP_UNLIKELY(sum
, -0x80, 0x7F);
364 *((uint8_t*) data
) = (uint8_t) (sum
+ 0x80);
366 data
= (uint8_t*) data
+ 1;
368 if (PA_UNLIKELY(++channel
>= spec
->channels
))
375 case PA_SAMPLE_ULAW
: {
376 unsigned channel
= 0;
378 calc_linear_integer_stream_volumes(streams
, nstreams
, volume
, spec
);
384 for (i
= 0; i
< nstreams
; i
++) {
385 pa_mix_info
*m
= streams
+ i
;
386 int32_t v
, cv
= m
->linear
[channel
].i
;
388 if (PA_UNLIKELY(cv
<= 0))
391 v
= (int32_t) st_ulaw2linear16(*((uint8_t*) m
->ptr
));
392 v
= (v
* cv
) / 0x10000;
395 m
->ptr
= (uint8_t*) m
->ptr
+ 1;
398 sum
= PA_CLAMP_UNLIKELY(sum
, -0x8000, 0x7FFF);
399 *((uint8_t*) data
) = (uint8_t) st_14linear2ulaw((int16_t) sum
>> 2);
401 data
= (uint8_t*) data
+ 1;
403 if (PA_UNLIKELY(++channel
>= spec
->channels
))
410 case PA_SAMPLE_ALAW
: {
411 unsigned channel
= 0;
413 calc_linear_integer_stream_volumes(streams
, nstreams
, volume
, spec
);
419 for (i
= 0; i
< nstreams
; i
++) {
420 pa_mix_info
*m
= streams
+ i
;
421 int32_t v
, cv
= m
->linear
[channel
].i
;
423 if (PA_UNLIKELY(cv
<= 0))
426 v
= (int32_t) st_alaw2linear16(*((uint8_t*) m
->ptr
));
427 v
= (v
* cv
) / 0x10000;
430 m
->ptr
= (uint8_t*) m
->ptr
+ 1;
433 sum
= PA_CLAMP_UNLIKELY(sum
, -0x8000, 0x7FFF);
434 *((uint8_t*) data
) = (uint8_t) st_13linear2alaw((int16_t) sum
>> 3);
436 data
= (uint8_t*) data
+ 1;
438 if (PA_UNLIKELY(++channel
>= spec
->channels
))
445 case PA_SAMPLE_FLOAT32NE
: {
446 unsigned channel
= 0;
448 calc_linear_float_stream_volumes(streams
, nstreams
, volume
, spec
);
454 for (i
= 0; i
< nstreams
; i
++) {
455 pa_mix_info
*m
= streams
+ i
;
456 float v
, cv
= m
->linear
[channel
].f
;
458 if (PA_UNLIKELY(cv
<= 0))
461 v
= *((float*) m
->ptr
);
465 m
->ptr
= (uint8_t*) m
->ptr
+ sizeof(float);
468 *((float*) data
) = sum
;
470 data
= (uint8_t*) data
+ sizeof(float);
472 if (PA_UNLIKELY(++channel
>= spec
->channels
))
479 case PA_SAMPLE_FLOAT32RE
: {
480 unsigned channel
= 0;
481 float linear
[PA_CHANNELS_MAX
];
483 calc_linear_float_stream_volumes(streams
, nstreams
, volume
, spec
);
489 for (i
= 0; i
< nstreams
; i
++) {
490 pa_mix_info
*m
= streams
+ i
;
491 float v
, cv
= m
->linear
[channel
].f
;
493 if (PA_UNLIKELY(cv
<= 0))
496 v
= PA_FLOAT32_SWAP(*(float*) m
->ptr
);
500 m
->ptr
= (uint8_t*) m
->ptr
+ sizeof(float);
503 *((float*) data
) = PA_FLOAT32_SWAP(sum
);
505 data
= (uint8_t*) data
+ sizeof(float);
507 if (PA_UNLIKELY(++channel
>= spec
->channels
))
515 pa_log_error("Unable to mix audio data of format %s.", pa_sample_format_to_string(spec
->format
));
516 pa_assert_not_reached();
519 for (k
= 0; k
< nstreams
; k
++)
520 pa_memblock_release(streams
[k
].chunk
.memblock
);
526 void pa_volume_memchunk(
528 const pa_sample_spec
*spec
,
529 const pa_cvolume
*volume
) {
535 pa_assert(c
->length
% pa_frame_size(spec
) == 0);
538 if (pa_memblock_is_silence(c
->memblock
))
541 if (pa_cvolume_channels_equal_to(volume
, PA_VOLUME_NORM
))
544 if (pa_cvolume_channels_equal_to(volume
, PA_VOLUME_MUTED
)) {
545 pa_silence_memchunk(c
, spec
);
549 ptr
= (uint8_t*) pa_memblock_acquire(c
->memblock
) + c
->index
;
551 switch (spec
->format
) {
553 case PA_SAMPLE_S16NE
: {
556 int32_t linear
[PA_CHANNELS_MAX
];
558 calc_linear_integer_volume(linear
, volume
);
560 e
= (int16_t*) ptr
+ c
->length
/sizeof(int16_t);
562 for (channel
= 0, d
= ptr
; d
< e
; d
++) {
566 t
= (t
* linear
[channel
]) / 0x10000;
567 t
= PA_CLAMP_UNLIKELY(t
, -0x8000, 0x7FFF);
570 if (PA_UNLIKELY(++channel
>= spec
->channels
))
576 case PA_SAMPLE_S16RE
: {
579 int32_t linear
[PA_CHANNELS_MAX
];
581 calc_linear_integer_volume(linear
, volume
);
583 e
= (int16_t*) ptr
+ c
->length
/sizeof(int16_t);
585 for (channel
= 0, d
= ptr
; d
< e
; d
++) {
588 t
= (int32_t) PA_INT16_SWAP(*d
);
589 t
= (t
* linear
[channel
]) / 0x10000;
590 t
= PA_CLAMP_UNLIKELY(t
, -0x8000, 0x7FFF);
591 *d
= PA_INT16_SWAP((int16_t) t
);
593 if (PA_UNLIKELY(++channel
>= spec
->channels
))
600 case PA_SAMPLE_S32NE
: {
603 int32_t linear
[PA_CHANNELS_MAX
];
605 calc_linear_integer_volume(linear
, volume
);
607 e
= (int32_t*) ptr
+ c
->length
/sizeof(int32_t);
609 for (channel
= 0, d
= ptr
; d
< e
; d
++) {
613 t
= (t
* linear
[channel
]) / 0x10000;
614 t
= PA_CLAMP_UNLIKELY(t
, -0x80000000LL
, 0x7FFFFFFFLL
);
617 if (PA_UNLIKELY(++channel
>= spec
->channels
))
623 case PA_SAMPLE_S32RE
: {
626 int32_t linear
[PA_CHANNELS_MAX
];
628 calc_linear_integer_volume(linear
, volume
);
630 e
= (int32_t*) ptr
+ c
->length
/sizeof(int32_t);
632 for (channel
= 0, d
= ptr
; d
< e
; d
++) {
635 t
= (int64_t) PA_INT32_SWAP(*d
);
636 t
= (t
* linear
[channel
]) / 0x10000;
637 t
= PA_CLAMP_UNLIKELY(t
, -0x80000000LL
, 0x7FFFFFFFLL
);
638 *d
= PA_INT32_SWAP((int32_t) t
);
640 if (PA_UNLIKELY(++channel
>= spec
->channels
))
650 int32_t linear
[PA_CHANNELS_MAX
];
652 calc_linear_integer_volume(linear
, volume
);
654 e
= (uint8_t*) ptr
+ c
->length
;
656 for (channel
= 0, d
= ptr
; d
< e
; d
++) {
659 t
= (int32_t) *d
- 0x80;
660 t
= (t
* linear
[channel
]) / 0x10000;
661 t
= PA_CLAMP_UNLIKELY(t
, -0x80, 0x7F);
662 *d
= (uint8_t) (t
+ 0x80);
664 if (PA_UNLIKELY(++channel
>= spec
->channels
))
670 case PA_SAMPLE_ULAW
: {
673 int32_t linear
[PA_CHANNELS_MAX
];
675 calc_linear_integer_volume(linear
, volume
);
677 e
= (uint8_t*) ptr
+ c
->length
;
679 for (channel
= 0, d
= ptr
; d
< e
; d
++) {
682 t
= (int32_t) st_ulaw2linear16(*d
);
683 t
= (t
* linear
[channel
]) / 0x10000;
684 t
= PA_CLAMP_UNLIKELY(t
, -0x8000, 0x7FFF);
685 *d
= (uint8_t) st_14linear2ulaw((int16_t) t
>> 2);
687 if (PA_UNLIKELY(++channel
>= spec
->channels
))
693 case PA_SAMPLE_ALAW
: {
696 int32_t linear
[PA_CHANNELS_MAX
];
698 calc_linear_integer_volume(linear
, volume
);
700 e
= (uint8_t*) ptr
+ c
->length
;
702 for (channel
= 0, d
= ptr
; d
< e
; d
++) {
705 t
= (int32_t) st_alaw2linear16(*d
);
706 t
= (t
* linear
[channel
]) / 0x10000;
707 t
= PA_CLAMP_UNLIKELY(t
, -0x8000, 0x7FFF);
708 *d
= (uint8_t) st_13linear2alaw((int16_t) t
>> 3);
710 if (PA_UNLIKELY(++channel
>= spec
->channels
))
716 case PA_SAMPLE_FLOAT32NE
: {
723 skip
= (int) (spec
->channels
* sizeof(float));
724 n
= (unsigned) (c
->length
/sizeof(float)/spec
->channels
);
726 for (channel
= 0; channel
< spec
->channels
; channel
++) {
729 if (PA_UNLIKELY(volume
->values
[channel
] == PA_VOLUME_NORM
))
732 v
= (float) pa_sw_volume_to_linear(volume
->values
[channel
]);
734 oil_scalarmult_f32(t
, skip
, t
, skip
, &v
, (int) n
);
739 case PA_SAMPLE_FLOAT32RE
: {
742 float linear
[PA_CHANNELS_MAX
];
744 calc_linear_float_volume(linear
, volume
);
746 e
= (float*) ptr
+ c
->length
/sizeof(float);
748 for (channel
= 0, d
= ptr
; d
< e
; d
++) {
751 t
= PA_FLOAT32_SWAP(*d
);
752 t
*= linear
[channel
];
753 *d
= PA_FLOAT32_SWAP(t
);
755 if (PA_UNLIKELY(++channel
>= spec
->channels
))
764 pa_log_warn(" Unable to change volume of format %s.", pa_sample_format_to_string(spec
->format
));
765 /* If we cannot change the volume, we just don't do it */
768 pa_memblock_release(c
->memblock
);
771 size_t pa_frame_align(size_t l
, const pa_sample_spec
*ss
) {
776 fs
= pa_frame_size(ss
);
781 pa_bool_t
pa_frame_aligned(size_t l
, const pa_sample_spec
*ss
) {
786 fs
= pa_frame_size(ss
);
791 void pa_interleave(const void *src
[], unsigned channels
, void *dst
, size_t ss
, unsigned n
) {
796 pa_assert(channels
> 0);
803 for (c
= 0; c
< channels
; c
++) {
809 d
= (uint8_t*) dst
+ c
* ss
;
811 for (j
= 0; j
< n
; j
++) {
812 oil_memcpy(d
, s
, (int) ss
);
813 s
= (uint8_t*) s
+ ss
;
814 d
= (uint8_t*) d
+ fs
;
819 void pa_deinterleave(const void *src
, void *dst
[], unsigned channels
, size_t ss
, unsigned n
) {
825 pa_assert(channels
> 0);
831 for (c
= 0; c
< channels
; c
++) {
836 s
= (uint8_t*) src
+ c
* ss
;
839 for (j
= 0; j
< n
; j
++) {
840 oil_memcpy(d
, s
, (int) ss
);
841 s
= (uint8_t*) s
+ fs
;
842 d
= (uint8_t*) d
+ ss
;
847 static pa_memblock
*silence_memblock_new(pa_mempool
*pool
, uint8_t c
) {
854 length
= PA_MIN(pa_mempool_block_size_max(pool
), PA_SILENCE_MAX
);
856 b
= pa_memblock_new(pool
, length
);
858 data
= pa_memblock_acquire(b
);
859 memset(data
, c
, length
);
860 pa_memblock_release(b
);
862 pa_memblock_set_is_silence(b
, TRUE
);
867 void pa_silence_cache_init(pa_silence_cache
*cache
) {
870 memset(cache
, 0, sizeof(pa_silence_cache
));
873 void pa_silence_cache_done(pa_silence_cache
*cache
) {
874 pa_sample_format_t f
;
877 for (f
= 0; f
< PA_SAMPLE_MAX
; f
++)
878 if (cache
->blocks
[f
])
879 pa_memblock_unref(cache
->blocks
[f
]);
881 memset(cache
, 0, sizeof(pa_silence_cache
));
884 pa_memchunk
* pa_silence_memchunk_get(pa_silence_cache
*cache
, pa_mempool
*pool
, pa_memchunk
* ret
, const pa_sample_spec
*spec
, size_t length
) {
889 pa_assert(pa_sample_spec_valid(spec
));
891 if (!(b
= cache
->blocks
[spec
->format
]))
893 switch (spec
->format
) {
895 cache
->blocks
[PA_SAMPLE_U8
] = b
= silence_memblock_new(pool
, 0x80);
897 case PA_SAMPLE_S16LE
:
898 case PA_SAMPLE_S16BE
:
899 case PA_SAMPLE_S32LE
:
900 case PA_SAMPLE_S32BE
:
901 case PA_SAMPLE_FLOAT32LE
:
902 case PA_SAMPLE_FLOAT32BE
:
903 cache
->blocks
[PA_SAMPLE_S16LE
] = b
= silence_memblock_new(pool
, 0);
904 cache
->blocks
[PA_SAMPLE_S16BE
] = pa_memblock_ref(b
);
905 cache
->blocks
[PA_SAMPLE_S32LE
] = pa_memblock_ref(b
);
906 cache
->blocks
[PA_SAMPLE_S32BE
] = pa_memblock_ref(b
);
907 cache
->blocks
[PA_SAMPLE_FLOAT32LE
] = pa_memblock_ref(b
);
908 cache
->blocks
[PA_SAMPLE_FLOAT32BE
] = pa_memblock_ref(b
);
911 cache
->blocks
[PA_SAMPLE_ALAW
] = b
= silence_memblock_new(pool
, 0xd5);
914 cache
->blocks
[PA_SAMPLE_ULAW
] = b
= silence_memblock_new(pool
, 0xff);
917 pa_assert_not_reached();
922 ret
->memblock
= pa_memblock_ref(b
);
924 l
= pa_memblock_get_length(b
);
925 if (length
> l
|| length
== 0)
928 ret
->length
= pa_frame_align(length
, spec
);
934 void pa_sample_clamp(pa_sample_format_t format
, void *dst
, size_t dstr
, const void *src
, size_t sstr
, unsigned n
) {
940 if (format
== PA_SAMPLE_FLOAT32NE
) {
942 float minus_one
= -1.0, plus_one
= 1.0;
943 oil_clip_f32(d
, (int) dstr
, s
, (int) sstr
, (int) n
, &minus_one
, &plus_one
);
946 pa_assert(format
== PA_SAMPLE_FLOAT32RE
);
951 f
= PA_FLOAT32_SWAP(*s
);
952 f
= PA_CLAMP_UNLIKELY(f
, -1.0f
, 1.0f
);
953 *d
= PA_FLOAT32_SWAP(f
);
955 s
= (const float*) ((const uint8_t*) s
+ sstr
);
956 d
= (float*) ((uint8_t*) d
+ dstr
);
961 /* Similar to pa_bytes_to_usec() but rounds up, not down */
963 pa_usec_t
pa_bytes_to_usec_round_up(uint64_t length
, const pa_sample_spec
*spec
) {
969 fs
= pa_frame_size(spec
);
970 length
= (length
+ fs
- 1) / fs
;
972 usec
= (pa_usec_t
) length
* PA_USEC_PER_SEC
;
974 return (usec
+ spec
->rate
- 1) / spec
->rate
;
977 /* Similar to pa_usec_to_bytes() but rounds up, not down */
979 size_t pa_usec_to_bytes_round_up(pa_usec_t t
, const pa_sample_spec
*spec
) {
983 u
= (uint64_t) t
* (uint64_t) spec
->rate
;
985 u
= (u
+ PA_USEC_PER_SEC
- 1) / PA_USEC_PER_SEC
;
987 u
*= pa_frame_size(spec
);
992 void pa_memchunk_dump_to_file(pa_memchunk
*c
, const char *fn
) {
999 /* Only for debugging purposes */
1004 pa_log_warn("Failed to open '%s': %s", fn
, pa_cstrerror(errno
));
1008 p
= pa_memblock_acquire(c
->memblock
);
1010 if (fwrite((uint8_t*) p
+ c
->index
, 1, c
->length
, f
) != c
->length
)
1011 pa_log_warn("Failed to write to '%s': %s", fn
, pa_cstrerror(errno
));
1013 pa_memblock_release(c
->memblock
);