00001 /* 00002 * SpanDSP - a series of DSP components for telephony 00003 * 00004 * tone_detect.h - General telephony tone detection. 00005 * 00006 * Written by Steve Underwood <steveu@coppice.org> 00007 * 00008 * Copyright (C) 2001, 2005 Steve Underwood 00009 * 00010 * All rights reserved. 00011 * 00012 * This program is free software; you can redistribute it and/or modify 00013 * it under the terms of the GNU General Public License version 2, as 00014 * published by the Free Software Foundation. 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU General Public License 00022 * along with this program; if not, write to the Free Software 00023 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00024 * 00025 * $Id: tone_detect.h,v 1.33 2007/08/13 13:08:19 steveu Exp $ 00026 */ 00027 00028 #if !defined(_SPANDSP_TONE_DETECT_H_) 00029 #define _SPANDSP_TONE_DETECT_H_ 00030 00031 /*! 00032 Floating point Goertzel filter descriptor. 00033 */ 00034 typedef struct 00035 { 00036 #if defined(SPANDSP_USE_FIXED_POINT_EXPERIMENTAL) 00037 int32_t fac; 00038 #else 00039 float fac; 00040 #endif 00041 int samples; 00042 } goertzel_descriptor_t; 00043 00044 /*! 00045 Floating point Goertzel filter state descriptor. 00046 */ 00047 typedef struct 00048 { 00049 #if defined(SPANDSP_USE_FIXED_POINT_EXPERIMENTAL) 00050 int32_t v2; 00051 int32_t v3; 00052 int32_t fac; 00053 #else 00054 float v2; 00055 float v3; 00056 float fac; 00057 #endif 00058 int samples; 00059 int current_sample; 00060 } goertzel_state_t; 00061 00062 #if defined(__cplusplus) 00063 extern "C" 00064 { 00065 #endif 00066 00067 /*! \brief Create a descriptor for use with either a Goertzel transform */ 00068 void make_goertzel_descriptor(goertzel_descriptor_t *t, 00069 float freq, 00070 int samples); 00071 00072 /*! \brief Initialise the state of a Goertzel transform. 00073 \param s The Goertzel context. If NULL, a context is allocated with malloc. 00074 \param t The Goertzel descriptor. 00075 \return A pointer to the Goertzel state. */ 00076 goertzel_state_t *goertzel_init(goertzel_state_t *s, 00077 goertzel_descriptor_t *t); 00078 00079 /*! \brief Reset the state of a Goertzel transform. 00080 \param s The Goertzel context. 00081 \param t The Goertzel descriptor. 00082 \return A pointer to the Goertzel state. */ 00083 void goertzel_reset(goertzel_state_t *s); 00084 00085 /*! \brief Update the state of a Goertzel transform. 00086 \param s The Goertzel context. 00087 \param amp The samples to be transformed. 00088 \param samples The number of samples. 00089 \return The number of samples unprocessed */ 00090 int goertzel_update(goertzel_state_t *s, 00091 const int16_t amp[], 00092 int samples); 00093 00094 /*! \brief Evaluate the final result of a Goertzel transform. 00095 \param s The Goertzel context. 00096 \return The result of the transform. */ 00097 float goertzel_result(goertzel_state_t *s); 00098 00099 /*! \brief Update the state of a Goertzel transform. 00100 \param s The Goertzel context. 00101 \param amp The sample to be transformed. */ 00102 static __inline__ void goertzel_sample(goertzel_state_t *s, int16_t amp) 00103 { 00104 #if defined(SPANDSP_USE_FIXED_POINT_EXPERIMENTAL) 00105 int32_t v1; 00106 #else 00107 float v1; 00108 #endif 00109 00110 v1 = s->v2; 00111 s->v2 = s->v3; 00112 s->v3 = s->fac*s->v2 - v1 + amp; 00113 s->current_sample++; 00114 } 00115 /*- End of function --------------------------------------------------------*/ 00116 00117 #if defined(__cplusplus) 00118 } 00119 #endif 00120 00121 #endif 00122 /*- End of file ------------------------------------------------------------*/