Updated license from GPL version 2 or later to GPL version 3 or later.
[debian/gnuradio] / gr-cvsd-vocoder / src / lib / cvsd_decode_bs.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 #ifndef INCLUDED_CVSD_DECODE_BS_H
23 #define INCLUDED_CVSD_DECODE_BS_H
24
25 #include <gr_sync_interpolator.h>
26
27 class cvsd_decode_bs;
28
29 typedef boost::shared_ptr<cvsd_decode_bs> cvsd_decode_bs_sptr;
30
31  /*!
32   * \brief Constructor parameters to initialize the CVSD decoder.  The default
33   * values are modeled after the Bluetooth standard and should not be changed,
34   * except by an advanced user
35   *
36   * \param min_step       Minimum step size used to update the internal reference.  Default: "10"
37   * \param max_step       Maximum step size used to update the internal reference.  Default: "1280"
38   * \param step_decay     Decay factor applied to step size when there is not a run of J output 1s or 0s.  Default: "0.9990234375"  (i.e. 1-1/1024)
39   * \param accum_decay    Decay factor applied to the internal reference during every interation of the codec.  Default: "0.96875"  (i.e. 1-1/32)
40   * \param K;             Size of shift register; the number of output bits remembered by codec (must be less or equal to 32).  Default: "32"
41   * \param J;             Number of bits in the shift register that are equal; i.e. the size of a run of 1s, 0s. Default: "4"
42   * \param pos_accum_max  Maximum integer value allowed for the internal reference.  Default: "32767" (2^15 - 1 or MAXSHORT)
43   * \param neg_accum_max  Minimum integer value allowed for the internal reference.  Default: "-32767" (-2^15 + 1 or MINSHORT+1)
44   *
45   */
46
47 cvsd_decode_bs_sptr cvsd_make_decode_bs (short min_step=10,
48                                            short max_step=1280,
49                                            double step_decay=0.9990234375,
50                                            double accum_decay= 0.96875,
51                                            int K=32,
52                                            int J=4,
53                                            short pos_accum_max=32767,
54                                            short neg_accum_max=-32767);
55
56 /*!
57  * \brief This block performs CVSD audio decoding.  Its design and implementation
58  * is modeled after the CVSD encoder/decoder specifications defined in the
59  * Bluetooth standard.
60  *
61  * \ingroup block
62  *
63  * CVSD is a method for encoding speech that seeks to reduce the
64  * bandwidth required for digital voice transmission.  CVSD takes
65  * advantage of strong correlation between samples, quantizing the
66  * difference in amplitude between two consecutive samples.  This
67  * difference requires fewer quantization levels as compared to other
68  * methods that quantize the actual amplitude level, reducing the
69  * bandwidth.  CVSD employs a two level quantizer (one bit) and an
70  * adaptive algorithm that allows for continuous step size adjustment.
71  *
72  * The coder can represent low amplitude signals with accuracy without
73  * sacrificing performance on large amplitude signals, a trade off that
74  * occurs in some non-adaptive modulations.
75  *
76  * The CVSD decoder effectively provides 1-to-8 decompression.  More
77  * specifically, for each incoming input bit, the decoder outputs one
78  * audio sample.  If the input is a "1" bit, the internal reference is
79  * increased appropriately and then outputted as the next estimated audio
80  * sample.  If the input is a "0" bit, the internal reference is
81  * decreased appropriately and then likewise outputted as the next estimated
82  * audio sample.  Grouping 8 input bits together, the encoder essentially
83  * produces 8 output audio samples for everyone one input byte.
84  *
85  * This decoder requires that output audio samples are 2-byte short signed
86  * integers.  The result bandwidth conversion, therefore, is 1 byte of
87  * encoded audio data to 16 output bytes of raw audio data.
88  *
89  * The CVSD decoder module must be post-fixed by a down-converter to
90  * under-sample the audio data after decoding.  The Bluetooth standard
91  * specifically calls for a 8-to-1 decimating down-converter.  This is
92  * required so that so that output sampling rate equals the original input
93  * sampling rate present before the encoder.  In all cases, the output
94  * down-converter rate must be the inverse of the input up-converter rate
95  * before the CVSD encoder.
96  *
97  * References:
98  * 1.  Continuously Variable Slope Delta Modulation (CVSD) A Tutorial,
99  *         Available: http://www.eetkorea.com/ARTICLES/2003AUG/A/2003AUG29_NTEK_RFD_AN02.PDF.
100  * 2.  Specification of The Bluetooth System
101  *         Available: http://grouper.ieee.org/groups/802/15/Bluetooth/core_10_b.pdf.
102  * 3.  McGarrity, S., Bluetooth Full Duplex Voice and Data Transmission. 2002.
103  *         Bluetooth Voice Simulink® Model, Available:
104  *         http://www.mathworks.com/company/newsletters/digest/nov01/bluetooth.html
105  *
106  */
107
108 class cvsd_decode_bs : public gr_sync_interpolator
109 {
110 private:
111   friend cvsd_decode_bs_sptr cvsd_make_decode_bs (short min_step,
112                                                   short max_step,
113                                                   double step_decay,
114                                                   double accum_decay,
115                                                   int K,
116                                                   int J,
117                                                   short pos_accum_max,
118                                                   short neg_accum_max);
119
120   cvsd_decode_bs (short min_step, short max_step, double step_decay,
121                   double accum_decay, int K, int J,
122                   short pos_accum_max, short neg_accum_max);
123
124   //! Member functions required by the encoder/decoder
125   //! \brief Rounding function specific to CVSD
126   //! \return the input value rounded to the nearest integer
127   int cvsd_round(double input);
128   
129   //! \brief A power function specific to CVSD data formats
130   //! \return (radix)^power, where radix and power are short integers
131   unsigned int cvsd_pow (short radix, short power);
132  
133   //! \brief Sums number of 1's in the input
134   //! \return the number of 1s in the four bytes of an input unsigned integer
135   unsigned char cvsd_bitwise_sum (unsigned int input);
136
137   short d_min_step;
138   short d_max_step;
139   double d_step_decay;
140   double d_accum_decay;
141
142   int d_K;  //!< \brief Size of shift register; the number of output bits remembered in shift register
143   int d_J;  //!< \brief Number of bits in the shift register that are equal; size of run of 1s, 0s
144
145   short d_pos_accum_max;
146   short d_neg_accum_max;
147
148   int d_accum;                //!< \brief Current value of internal reference
149   int d_loop_counter;         //!< \brief Current value of the loop counter
150   unsigned int d_runner;      //!< \brief Current value of the shift register
151   unsigned int d_runner_mask; //!< \brief Value of the mask to access the last J bits of the shift register
152   short d_stepsize;           //!< \brief Current value of the step sizer
153
154  public:
155   ~cvsd_decode_bs ();   // public destructor
156
157   short min_step() { return d_min_step; }
158   short max_step() { return d_max_step; }
159   double step_decay() { return d_step_decay; }
160   double accum_decay() { return d_accum_decay; }
161   int K() { return d_K; }
162   int J() { return d_J; }
163   short pos_accum_max() { return d_pos_accum_max; }
164   short neg_accum_max() { return d_neg_accum_max; }
165
166   int work (int noutput_items,
167             gr_vector_const_void_star &input_items,
168             gr_vector_void_star &output_items);
169 };
170
171 #endif /* INCLUDED_CVSD_DECODE_BS_H */