153a5a1b3Sopenharmony_ci#ifndef foosampleutilhfoo
253a5a1b3Sopenharmony_ci#define foosampleutilhfoo
353a5a1b3Sopenharmony_ci
453a5a1b3Sopenharmony_ci/***
553a5a1b3Sopenharmony_ci  This file is part of PulseAudio.
653a5a1b3Sopenharmony_ci
753a5a1b3Sopenharmony_ci  Copyright 2004-2006 Lennart Poettering
853a5a1b3Sopenharmony_ci  Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
953a5a1b3Sopenharmony_ci
1053a5a1b3Sopenharmony_ci  PulseAudio is free software; you can redistribute it and/or modify
1153a5a1b3Sopenharmony_ci  it under the terms of the GNU Lesser General Public License as published
1253a5a1b3Sopenharmony_ci  by the Free Software Foundation; either version 2.1 of the License,
1353a5a1b3Sopenharmony_ci  or (at your option) any later version.
1453a5a1b3Sopenharmony_ci
1553a5a1b3Sopenharmony_ci  PulseAudio is distributed in the hope that it will be useful, but
1653a5a1b3Sopenharmony_ci  WITHOUT ANY WARRANTY; without even the implied warranty of
1753a5a1b3Sopenharmony_ci  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1853a5a1b3Sopenharmony_ci  General Public License for more details.
1953a5a1b3Sopenharmony_ci
2053a5a1b3Sopenharmony_ci  You should have received a copy of the GNU Lesser General Public License
2153a5a1b3Sopenharmony_ci  along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
2253a5a1b3Sopenharmony_ci***/
2353a5a1b3Sopenharmony_ci
2453a5a1b3Sopenharmony_ci#include <inttypes.h>
2553a5a1b3Sopenharmony_ci#include <limits.h>
2653a5a1b3Sopenharmony_ci
2753a5a1b3Sopenharmony_ci#include <pulse/gccmacro.h>
2853a5a1b3Sopenharmony_ci#include <pulse/sample.h>
2953a5a1b3Sopenharmony_ci#include <pulse/volume.h>
3053a5a1b3Sopenharmony_ci#include <pulse/channelmap.h>
3153a5a1b3Sopenharmony_ci
3253a5a1b3Sopenharmony_ci#include <pulsecore/memblock.h>
3353a5a1b3Sopenharmony_ci#include <pulsecore/memchunk.h>
3453a5a1b3Sopenharmony_ci
3553a5a1b3Sopenharmony_citypedef struct pa_silence_cache {
3653a5a1b3Sopenharmony_ci    pa_memblock* blocks[PA_SAMPLE_MAX];
3753a5a1b3Sopenharmony_ci} pa_silence_cache;
3853a5a1b3Sopenharmony_ci
3953a5a1b3Sopenharmony_civoid pa_silence_cache_init(pa_silence_cache *cache);
4053a5a1b3Sopenharmony_civoid pa_silence_cache_done(pa_silence_cache *cache);
4153a5a1b3Sopenharmony_ci
4253a5a1b3Sopenharmony_civoid *pa_silence_memory(void *p, size_t length, const pa_sample_spec *spec);
4353a5a1b3Sopenharmony_cipa_memchunk* pa_silence_memchunk(pa_memchunk *c, const pa_sample_spec *spec);
4453a5a1b3Sopenharmony_cipa_memblock* pa_silence_memblock(pa_memblock *b, const pa_sample_spec *spec);
4553a5a1b3Sopenharmony_ci
4653a5a1b3Sopenharmony_cipa_memchunk* pa_silence_memchunk_get(pa_silence_cache *cache, pa_mempool *pool, pa_memchunk* ret, const pa_sample_spec *spec, size_t length);
4753a5a1b3Sopenharmony_ci
4853a5a1b3Sopenharmony_cisize_t pa_frame_align(size_t l, const pa_sample_spec *ss) PA_GCC_PURE;
4953a5a1b3Sopenharmony_ci
5053a5a1b3Sopenharmony_cibool pa_frame_aligned(size_t l, const pa_sample_spec *ss) PA_GCC_PURE;
5153a5a1b3Sopenharmony_ci
5253a5a1b3Sopenharmony_civoid pa_interleave(const void *src[], unsigned channels, void *dst, size_t ss, unsigned n);
5353a5a1b3Sopenharmony_civoid pa_deinterleave(const void *src, void *dst[], unsigned channels, size_t ss, unsigned n);
5453a5a1b3Sopenharmony_ci
5553a5a1b3Sopenharmony_civoid pa_sample_clamp(pa_sample_format_t format, void *dst, size_t dstr, const void *src, size_t sstr, unsigned n);
5653a5a1b3Sopenharmony_ci
5753a5a1b3Sopenharmony_cistatic inline int32_t pa_mult_s16_volume(int16_t v, int32_t cv) {
5853a5a1b3Sopenharmony_ci#ifdef HAVE_FAST_64BIT_OPERATIONS
5953a5a1b3Sopenharmony_ci    /* Multiply with 64 bit integers on 64 bit platforms */
6053a5a1b3Sopenharmony_ci    return (v * (int64_t) cv) >> 16;
6153a5a1b3Sopenharmony_ci#else
6253a5a1b3Sopenharmony_ci    /* Multiplying the 32 bit volume factor with the
6353a5a1b3Sopenharmony_ci     * 16 bit sample might result in an 48 bit value. We
6453a5a1b3Sopenharmony_ci     * want to do without 64 bit integers and hence do
6553a5a1b3Sopenharmony_ci     * the multiplication independently for the HI and
6653a5a1b3Sopenharmony_ci     * LO part of the volume. */
6753a5a1b3Sopenharmony_ci
6853a5a1b3Sopenharmony_ci    int32_t hi = cv >> 16;
6953a5a1b3Sopenharmony_ci    int32_t lo = cv & 0xFFFF;
7053a5a1b3Sopenharmony_ci    return ((v * lo) >> 16) + (v * hi);
7153a5a1b3Sopenharmony_ci#endif
7253a5a1b3Sopenharmony_ci}
7353a5a1b3Sopenharmony_ci
7453a5a1b3Sopenharmony_cipa_usec_t pa_bytes_to_usec_round_up(uint64_t length, const pa_sample_spec *spec);
7553a5a1b3Sopenharmony_cisize_t pa_usec_to_bytes_round_up(pa_usec_t t, const pa_sample_spec *spec);
7653a5a1b3Sopenharmony_ci
7753a5a1b3Sopenharmony_civoid pa_memchunk_dump_to_file(pa_memchunk *c, const char *fn);
7853a5a1b3Sopenharmony_ci
7953a5a1b3Sopenharmony_civoid pa_memchunk_sine(pa_memchunk *c, pa_mempool *pool, unsigned rate, unsigned freq);
8053a5a1b3Sopenharmony_ci
8153a5a1b3Sopenharmony_citypedef void (*pa_do_volume_func_t) (void *samples, const void *volumes, unsigned channels, unsigned length);
8253a5a1b3Sopenharmony_ci
8353a5a1b3Sopenharmony_cipa_do_volume_func_t pa_get_volume_func(pa_sample_format_t f);
8453a5a1b3Sopenharmony_civoid pa_set_volume_func(pa_sample_format_t f, pa_do_volume_func_t func);
8553a5a1b3Sopenharmony_ci
8653a5a1b3Sopenharmony_cisize_t pa_convert_size(size_t size, const pa_sample_spec *from, const pa_sample_spec *to);
8753a5a1b3Sopenharmony_ci
8853a5a1b3Sopenharmony_ci#define PA_CHANNEL_POSITION_MASK_LEFT                                   \
8953a5a1b3Sopenharmony_ci    (PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_LEFT)           \
9053a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_REAR_LEFT)          \
9153a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER) \
9253a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_SIDE_LEFT)          \
9353a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_LEFT)     \
9453a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_LEFT))     \
9553a5a1b3Sopenharmony_ci
9653a5a1b3Sopenharmony_ci#define PA_CHANNEL_POSITION_MASK_RIGHT                                  \
9753a5a1b3Sopenharmony_ci    (PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_RIGHT)          \
9853a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_REAR_RIGHT)         \
9953a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER) \
10053a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_SIDE_RIGHT)         \
10153a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_RIGHT)    \
10253a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_RIGHT))
10353a5a1b3Sopenharmony_ci
10453a5a1b3Sopenharmony_ci#define PA_CHANNEL_POSITION_MASK_CENTER                                 \
10553a5a1b3Sopenharmony_ci    (PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_CENTER)         \
10653a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_REAR_CENTER)        \
10753a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_CENTER)         \
10853a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_CENTER)   \
10953a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_CENTER))
11053a5a1b3Sopenharmony_ci
11153a5a1b3Sopenharmony_ci#define PA_CHANNEL_POSITION_MASK_FRONT                                  \
11253a5a1b3Sopenharmony_ci    (PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_LEFT)           \
11353a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_RIGHT)        \
11453a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_CENTER)       \
11553a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER) \
11653a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER) \
11753a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_LEFT)     \
11853a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_RIGHT)    \
11953a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_CENTER))
12053a5a1b3Sopenharmony_ci
12153a5a1b3Sopenharmony_ci#define PA_CHANNEL_POSITION_MASK_REAR                                   \
12253a5a1b3Sopenharmony_ci    (PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_REAR_LEFT)            \
12353a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_REAR_RIGHT)         \
12453a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_REAR_CENTER)        \
12553a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_LEFT)      \
12653a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_RIGHT)     \
12753a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_CENTER))
12853a5a1b3Sopenharmony_ci
12953a5a1b3Sopenharmony_ci#define PA_CHANNEL_POSITION_MASK_LFE                                    \
13053a5a1b3Sopenharmony_ci    PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_LFE)
13153a5a1b3Sopenharmony_ci
13253a5a1b3Sopenharmony_ci#define PA_CHANNEL_POSITION_MASK_HFE                                    \
13353a5a1b3Sopenharmony_ci    (PA_CHANNEL_POSITION_MASK_REAR | PA_CHANNEL_POSITION_MASK_FRONT     \
13453a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK_LEFT | PA_CHANNEL_POSITION_MASK_RIGHT   \
13553a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK_CENTER)
13653a5a1b3Sopenharmony_ci
13753a5a1b3Sopenharmony_ci#define PA_CHANNEL_POSITION_MASK_SIDE_OR_TOP_CENTER                     \
13853a5a1b3Sopenharmony_ci    (PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_SIDE_LEFT)            \
13953a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_SIDE_RIGHT)         \
14053a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_CENTER))
14153a5a1b3Sopenharmony_ci
14253a5a1b3Sopenharmony_ci#define PA_CHANNEL_POSITION_MASK_TOP                                    \
14353a5a1b3Sopenharmony_ci    (PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_CENTER)           \
14453a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_LEFT)     \
14553a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_RIGHT)    \
14653a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_CENTER)   \
14753a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_LEFT)      \
14853a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_RIGHT)     \
14953a5a1b3Sopenharmony_ci     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_CENTER))
15053a5a1b3Sopenharmony_ci
15153a5a1b3Sopenharmony_ci#define PA_CHANNEL_POSITION_MASK_ALL            \
15253a5a1b3Sopenharmony_ci    ((pa_channel_position_mask_t) (PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_MAX)-1))
15353a5a1b3Sopenharmony_ci
15453a5a1b3Sopenharmony_ci#endif
155