xref: /btstack/src/classic/btstack_cvsd_plc.h (revision 3d45f2d56b54773708226b637840df928a7cb3af)
17e6b1e83SMilanka Ringwald /*
27e6b1e83SMilanka Ringwald  * Copyright (C) 2016 BlueKitchen GmbH
37e6b1e83SMilanka Ringwald  *
47e6b1e83SMilanka Ringwald  * Redistribution and use in source and binary forms, with or without
57e6b1e83SMilanka Ringwald  * modification, are permitted provided that the following conditions
67e6b1e83SMilanka Ringwald  * are met:
77e6b1e83SMilanka Ringwald  *
87e6b1e83SMilanka Ringwald  * 1. Redistributions of source code must retain the above copyright
97e6b1e83SMilanka Ringwald  *    notice, this list of conditions and the following disclaimer.
107e6b1e83SMilanka Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
117e6b1e83SMilanka Ringwald  *    notice, this list of conditions and the following disclaimer in the
127e6b1e83SMilanka Ringwald  *    documentation and/or other materials provided with the distribution.
137e6b1e83SMilanka Ringwald  * 3. Neither the name of the copyright holders nor the names of
147e6b1e83SMilanka Ringwald  *    contributors may be used to endorse or promote products derived
157e6b1e83SMilanka Ringwald  *    from this software without specific prior written permission.
167e6b1e83SMilanka Ringwald  * 4. Any redistribution, use, or modification is done solely for
177e6b1e83SMilanka Ringwald  *    personal benefit and not for any commercial purpose or for
187e6b1e83SMilanka Ringwald  *    monetary gain.
197e6b1e83SMilanka Ringwald  *
207e6b1e83SMilanka Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
217e6b1e83SMilanka Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
227e6b1e83SMilanka Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
237e6b1e83SMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
247e6b1e83SMilanka Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
257e6b1e83SMilanka Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
267e6b1e83SMilanka Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
277e6b1e83SMilanka Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
287e6b1e83SMilanka Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
297e6b1e83SMilanka Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
307e6b1e83SMilanka Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
317e6b1e83SMilanka Ringwald  * SUCH DAMAGE.
327e6b1e83SMilanka Ringwald  *
337e6b1e83SMilanka Ringwald  * Please inquire about commercial licensing options at
347e6b1e83SMilanka Ringwald  * [email protected]
357e6b1e83SMilanka Ringwald  *
367e6b1e83SMilanka Ringwald  */
377e6b1e83SMilanka Ringwald 
387e6b1e83SMilanka Ringwald /*
397e6b1e83SMilanka Ringwald  * btstack_cvsd_plc.h
407e6b1e83SMilanka Ringwald  *
417e6b1e83SMilanka Ringwald  */
427e6b1e83SMilanka Ringwald 
437e6b1e83SMilanka Ringwald #ifndef __BTSTACK_CVSD_PLC_H
447e6b1e83SMilanka Ringwald #define __BTSTACK_CVSD_PLC_H
457e6b1e83SMilanka Ringwald 
467e6b1e83SMilanka Ringwald #include <stdint.h>
477e6b1e83SMilanka Ringwald 
487e6b1e83SMilanka Ringwald #if defined __cplusplus
497e6b1e83SMilanka Ringwald extern "C" {
507e6b1e83SMilanka Ringwald #endif
517e6b1e83SMilanka Ringwald 
52*3d45f2d5SMilanka Ringwald #define BTSTACK_CVSD_PLC_SAMPLE_FORMAT int16_t
53cf8890c4SMilanka Ringwald 
54eb1deeddSMilanka Ringwald #define CVSD_FS_MAX 60       /* CVSD Frame Size */
55cf8890c4SMilanka Ringwald #define CVSD_N 256           /* Window Length for pattern matching */
567e6b1e83SMilanka Ringwald #define CVSD_M 10            /* Template for matching */
57eb1deeddSMilanka Ringwald #define CVSD_LHIST (CVSD_N+CVSD_FS_MAX-1)  /* Length of history buffer required */
587e6b1e83SMilanka Ringwald #define CVSD_RT 5            /*  Reconvergence Time (samples) */
597e6b1e83SMilanka Ringwald #define CVSD_OLAL 16         /*  OverLap-Add Length (samples) */
607e6b1e83SMilanka Ringwald 
617e6b1e83SMilanka Ringwald /* PLC State Information */
627e6b1e83SMilanka Ringwald typedef struct cvsd_plc_state {
63eb1deeddSMilanka Ringwald     int16_t hist[CVSD_LHIST+CVSD_FS_MAX+CVSD_RT+CVSD_OLAL];
647e6b1e83SMilanka Ringwald     int16_t bestlag;
657e6b1e83SMilanka Ringwald     int     nbf;
6626463303SMilanka Ringwald 
6726463303SMilanka Ringwald     // summary of processed good and bad frames
6826463303SMilanka Ringwald     int good_frames_nr;
6926463303SMilanka Ringwald     int bad_frames_nr;
70202da317SMilanka Ringwald     int frame_count;
71eb1deeddSMilanka Ringwald     uint16_t cvsd_fs;
727e6b1e83SMilanka Ringwald } btstack_cvsd_plc_state_t;
737e6b1e83SMilanka Ringwald 
747cd567c8SMatthias Ringwald // All int16 audio samples are in host endiness
757e6b1e83SMilanka Ringwald void btstack_cvsd_plc_init(btstack_cvsd_plc_state_t *plc_state);
76fac2e2feSMatthias Ringwald void btstack_cvsd_plc_bad_frame(btstack_cvsd_plc_state_t *plc_state, int16_t *out);
77fac2e2feSMatthias Ringwald void btstack_cvsd_plc_good_frame(btstack_cvsd_plc_state_t *plc_state, int16_t *in, int16_t *out);
78fac2e2feSMatthias Ringwald void btstack_cvsd_plc_process_data(btstack_cvsd_plc_state_t * state, int16_t * in, uint16_t size, int16_t * out);
79202da317SMilanka Ringwald void btstack_cvsd_dump_statistics(btstack_cvsd_plc_state_t * state);
807e6b1e83SMilanka Ringwald 
81*3d45f2d5SMilanka Ringwald // testing only
82*3d45f2d5SMilanka Ringwald int   btstack_cvsd_plc_pattern_match(BTSTACK_CVSD_PLC_SAMPLE_FORMAT *y);
83*3d45f2d5SMilanka Ringwald float btstack_cvsd_plc_amplitude_match(btstack_cvsd_plc_state_t *plc_state, BTSTACK_CVSD_PLC_SAMPLE_FORMAT *y, BTSTACK_CVSD_PLC_SAMPLE_FORMAT bestmatch);
84*3d45f2d5SMilanka Ringwald BTSTACK_CVSD_PLC_SAMPLE_FORMAT btstack_cvsd_plc_crop_sample(float val);
85*3d45f2d5SMilanka Ringwald float btstack_cvsd_plc_rcos(int index);
86*3d45f2d5SMilanka Ringwald 
877e6b1e83SMilanka Ringwald #if defined __cplusplus
887e6b1e83SMilanka Ringwald }
897e6b1e83SMilanka Ringwald #endif
907e6b1e83SMilanka Ringwald 
917e6b1e83SMilanka Ringwald #endif // __BTSTACK_CVSD_PLC_H
92