1*a58d3d2aSXin Li /* Copyright (c) 2018 Mozilla */ 2*a58d3d2aSXin Li /* 3*a58d3d2aSXin Li Redistribution and use in source and binary forms, with or without 4*a58d3d2aSXin Li modification, are permitted provided that the following conditions 5*a58d3d2aSXin Li are met: 6*a58d3d2aSXin Li 7*a58d3d2aSXin Li - Redistributions of source code must retain the above copyright 8*a58d3d2aSXin Li notice, this list of conditions and the following disclaimer. 9*a58d3d2aSXin Li 10*a58d3d2aSXin Li - Redistributions in binary form must reproduce the above copyright 11*a58d3d2aSXin Li notice, this list of conditions and the following disclaimer in the 12*a58d3d2aSXin Li documentation and/or other materials provided with the distribution. 13*a58d3d2aSXin Li 14*a58d3d2aSXin Li THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 15*a58d3d2aSXin Li ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 16*a58d3d2aSXin Li LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 17*a58d3d2aSXin Li A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR 18*a58d3d2aSXin Li CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19*a58d3d2aSXin Li EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20*a58d3d2aSXin Li PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21*a58d3d2aSXin Li PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22*a58d3d2aSXin Li LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 23*a58d3d2aSXin Li NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24*a58d3d2aSXin Li SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25*a58d3d2aSXin Li */ 26*a58d3d2aSXin Li 27*a58d3d2aSXin Li #ifndef LPCNET_H_ 28*a58d3d2aSXin Li #define LPCNET_H_ 29*a58d3d2aSXin Li 30*a58d3d2aSXin Li #include "opus_types.h" 31*a58d3d2aSXin Li 32*a58d3d2aSXin Li #define NB_FEATURES 20 33*a58d3d2aSXin Li #define NB_TOTAL_FEATURES 36 34*a58d3d2aSXin Li 35*a58d3d2aSXin Li /** Number of audio samples in a feature frame (not for encoding/decoding). */ 36*a58d3d2aSXin Li #define LPCNET_FRAME_SIZE (160) 37*a58d3d2aSXin Li 38*a58d3d2aSXin Li typedef struct LPCNetState LPCNetState; 39*a58d3d2aSXin Li 40*a58d3d2aSXin Li typedef struct LPCNetDecState LPCNetDecState; 41*a58d3d2aSXin Li 42*a58d3d2aSXin Li typedef struct LPCNetEncState LPCNetEncState; 43*a58d3d2aSXin Li 44*a58d3d2aSXin Li typedef struct LPCNetPLCState LPCNetPLCState; 45*a58d3d2aSXin Li 46*a58d3d2aSXin Li 47*a58d3d2aSXin Li /** Gets the size of an <code>LPCNetDecState</code> structure. 48*a58d3d2aSXin Li * @returns The size in bytes. 49*a58d3d2aSXin Li */ 50*a58d3d2aSXin Li int lpcnet_decoder_get_size(void); 51*a58d3d2aSXin Li 52*a58d3d2aSXin Li /** Initializes a previously allocated decoder state 53*a58d3d2aSXin Li * The memory pointed to by st must be at least the size returned by lpcnet_decoder_get_size(). 54*a58d3d2aSXin Li * This is intended for applications which use their own allocator instead of malloc. 55*a58d3d2aSXin Li * @see lpcnet_decoder_create(),lpcnet_decoder_get_size() 56*a58d3d2aSXin Li * @param [in] st <tt>LPCNetDecState*</tt>: Decoder state 57*a58d3d2aSXin Li * @retval 0 Success 58*a58d3d2aSXin Li */ 59*a58d3d2aSXin Li int lpcnet_decoder_init(LPCNetDecState *st); 60*a58d3d2aSXin Li 61*a58d3d2aSXin Li void lpcnet_reset(LPCNetState *lpcnet); 62*a58d3d2aSXin Li 63*a58d3d2aSXin Li /** Allocates and initializes a decoder state. 64*a58d3d2aSXin Li * @returns The newly created state 65*a58d3d2aSXin Li */ 66*a58d3d2aSXin Li LPCNetDecState *lpcnet_decoder_create(void); 67*a58d3d2aSXin Li 68*a58d3d2aSXin Li /** Frees an <code>LPCNetDecState</code> allocated by lpcnet_decoder_create(). 69*a58d3d2aSXin Li * @param[in] st <tt>LPCNetDecState*</tt>: State to be freed. 70*a58d3d2aSXin Li */ 71*a58d3d2aSXin Li void lpcnet_decoder_destroy(LPCNetDecState *st); 72*a58d3d2aSXin Li 73*a58d3d2aSXin Li /** Decodes a packet of LPCNET_COMPRESSED_SIZE bytes (currently 8) into LPCNET_PACKET_SAMPLES samples (currently 640). 74*a58d3d2aSXin Li * @param [in] st <tt>LPCNetDecState*</tt>: Decoder state 75*a58d3d2aSXin Li * @param [in] buf <tt>const unsigned char *</tt>: Compressed packet 76*a58d3d2aSXin Li * @param [out] pcm <tt>opus_int16 *</tt>: Decoded audio 77*a58d3d2aSXin Li * @retval 0 Success 78*a58d3d2aSXin Li */ 79*a58d3d2aSXin Li int lpcnet_decode(LPCNetDecState *st, const unsigned char *buf, opus_int16 *pcm); 80*a58d3d2aSXin Li 81*a58d3d2aSXin Li 82*a58d3d2aSXin Li 83*a58d3d2aSXin Li /** Gets the size of an <code>LPCNetEncState</code> structure. 84*a58d3d2aSXin Li * @returns The size in bytes. 85*a58d3d2aSXin Li */ 86*a58d3d2aSXin Li int lpcnet_encoder_get_size(void); 87*a58d3d2aSXin Li 88*a58d3d2aSXin Li /** Initializes a previously allocated encoder state 89*a58d3d2aSXin Li * The memory pointed to by st must be at least the size returned by lpcnet_encoder_get_size(). 90*a58d3d2aSXin Li * This is intended for applications which use their own allocator instead of malloc. 91*a58d3d2aSXin Li * @see lpcnet_encoder_create(),lpcnet_encoder_get_size() 92*a58d3d2aSXin Li * @param [in] st <tt>LPCNetEncState*</tt>: Encoder state 93*a58d3d2aSXin Li * @retval 0 Success 94*a58d3d2aSXin Li */ 95*a58d3d2aSXin Li int lpcnet_encoder_init(LPCNetEncState *st); 96*a58d3d2aSXin Li 97*a58d3d2aSXin Li int lpcnet_encoder_load_model(LPCNetEncState *st, const void *data, int len); 98*a58d3d2aSXin Li 99*a58d3d2aSXin Li /** Allocates and initializes an encoder state. 100*a58d3d2aSXin Li * @returns The newly created state 101*a58d3d2aSXin Li */ 102*a58d3d2aSXin Li LPCNetEncState *lpcnet_encoder_create(void); 103*a58d3d2aSXin Li 104*a58d3d2aSXin Li /** Frees an <code>LPCNetEncState</code> allocated by lpcnet_encoder_create(). 105*a58d3d2aSXin Li * @param[in] st <tt>LPCNetEncState*</tt>: State to be freed. 106*a58d3d2aSXin Li */ 107*a58d3d2aSXin Li void lpcnet_encoder_destroy(LPCNetEncState *st); 108*a58d3d2aSXin Li 109*a58d3d2aSXin Li /** Encodes LPCNET_PACKET_SAMPLES speech samples (currently 640) into a packet of LPCNET_COMPRESSED_SIZE bytes (currently 8). 110*a58d3d2aSXin Li * @param [in] st <tt>LPCNetDecState*</tt>: Encoder state 111*a58d3d2aSXin Li * @param [in] pcm <tt>opus_int16 *</tt>: Input speech to be encoded 112*a58d3d2aSXin Li * @param [out] buf <tt>const unsigned char *</tt>: Compressed packet 113*a58d3d2aSXin Li * @retval 0 Success 114*a58d3d2aSXin Li */ 115*a58d3d2aSXin Li int lpcnet_encode(LPCNetEncState *st, const opus_int16 *pcm, unsigned char *buf); 116*a58d3d2aSXin Li 117*a58d3d2aSXin Li /** Compute features on LPCNET_FRAME_SIZE speech samples (currently 160) and output features for one 10-ms frame. 118*a58d3d2aSXin Li * @param [in] st <tt>LPCNetDecState*</tt>: Encoder state 119*a58d3d2aSXin Li * @param [in] pcm <tt>opus_int16 *</tt>: Input speech to be analyzed 120*a58d3d2aSXin Li * @param [out] features <tt>float[NB_TOTAL_FEATURES]</tt>: Four feature vectors 121*a58d3d2aSXin Li * @retval 0 Success 122*a58d3d2aSXin Li */ 123*a58d3d2aSXin Li int lpcnet_compute_single_frame_features(LPCNetEncState *st, const opus_int16 *pcm, float features[NB_TOTAL_FEATURES], int arch); 124*a58d3d2aSXin Li 125*a58d3d2aSXin Li 126*a58d3d2aSXin Li /** Compute features on LPCNET_FRAME_SIZE speech samples (currently 160) and output features for one 10-ms frame. 127*a58d3d2aSXin Li * @param [in] st <tt>LPCNetDecState*</tt>: Encoder state 128*a58d3d2aSXin Li * @param [in] pcm <tt>float *</tt>: Input speech to be analyzed 129*a58d3d2aSXin Li * @param [out] features <tt>float[NB_TOTAL_FEATURES]</tt>: Four feature vectors 130*a58d3d2aSXin Li * @retval 0 Success 131*a58d3d2aSXin Li */ 132*a58d3d2aSXin Li int lpcnet_compute_single_frame_features_float(LPCNetEncState *st, const float *pcm, float features[NB_TOTAL_FEATURES], int arch); 133*a58d3d2aSXin Li 134*a58d3d2aSXin Li /** Gets the size of an <code>LPCNetState</code> structure. 135*a58d3d2aSXin Li * @returns The size in bytes. 136*a58d3d2aSXin Li */ 137*a58d3d2aSXin Li int lpcnet_get_size(void); 138*a58d3d2aSXin Li 139*a58d3d2aSXin Li /** Initializes a previously allocated synthesis state 140*a58d3d2aSXin Li * The memory pointed to by st must be at least the size returned by lpcnet_get_size(). 141*a58d3d2aSXin Li * This is intended for applications which use their own allocator instead of malloc. 142*a58d3d2aSXin Li * @see lpcnet_create(),lpcnet_get_size() 143*a58d3d2aSXin Li * @param [in] st <tt>LPCNetState*</tt>: Synthesis state 144*a58d3d2aSXin Li * @retval 0 Success 145*a58d3d2aSXin Li */ 146*a58d3d2aSXin Li int lpcnet_init(LPCNetState *st); 147*a58d3d2aSXin Li 148*a58d3d2aSXin Li /** Allocates and initializes a synthesis state. 149*a58d3d2aSXin Li * @returns The newly created state 150*a58d3d2aSXin Li */ 151*a58d3d2aSXin Li LPCNetState *lpcnet_create(void); 152*a58d3d2aSXin Li 153*a58d3d2aSXin Li /** Frees an <code>LPCNetState</code> allocated by lpcnet_create(). 154*a58d3d2aSXin Li * @param[in] st <tt>LPCNetState*</tt>: State to be freed. 155*a58d3d2aSXin Li */ 156*a58d3d2aSXin Li void lpcnet_destroy(LPCNetState *st); 157*a58d3d2aSXin Li 158*a58d3d2aSXin Li /** Synthesizes speech from an LPCNet feature vector. 159*a58d3d2aSXin Li * @param [in] st <tt>LPCNetState*</tt>: Synthesis state 160*a58d3d2aSXin Li * @param [in] features <tt>const float *</tt>: Compressed packet 161*a58d3d2aSXin Li * @param [out] output <tt>opus_int16 **</tt>: Synthesized speech 162*a58d3d2aSXin Li * @param [in] N <tt>int</tt>: Number of samples to generate 163*a58d3d2aSXin Li * @retval 0 Success 164*a58d3d2aSXin Li */ 165*a58d3d2aSXin Li void lpcnet_synthesize(LPCNetState *st, const float *features, opus_int16 *output, int N); 166*a58d3d2aSXin Li 167*a58d3d2aSXin Li 168*a58d3d2aSXin Li 169*a58d3d2aSXin Li int lpcnet_plc_init(LPCNetPLCState *st); 170*a58d3d2aSXin Li void lpcnet_plc_reset(LPCNetPLCState *st); 171*a58d3d2aSXin Li 172*a58d3d2aSXin Li int lpcnet_plc_update(LPCNetPLCState *st, opus_int16 *pcm); 173*a58d3d2aSXin Li 174*a58d3d2aSXin Li int lpcnet_plc_conceal(LPCNetPLCState *st, opus_int16 *pcm); 175*a58d3d2aSXin Li 176*a58d3d2aSXin Li void lpcnet_plc_fec_add(LPCNetPLCState *st, const float *features); 177*a58d3d2aSXin Li 178*a58d3d2aSXin Li void lpcnet_plc_fec_clear(LPCNetPLCState *st); 179*a58d3d2aSXin Li 180*a58d3d2aSXin Li int lpcnet_load_model(LPCNetState *st, const void *data, int len); 181*a58d3d2aSXin Li int lpcnet_plc_load_model(LPCNetPLCState *st, const void *data, int len); 182*a58d3d2aSXin Li 183*a58d3d2aSXin Li #endif 184