root/src/include/private/libteletone_generate.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. teletone_dds_phase_rate
  2. teletone_dds_state_modulate_sample
  3. teletone_dds_state_set_tx_level
  4. teletone_dds_state_reset_accum
  5. teletone_dds_state_set_tone

   1 /* 
   2  * libteletone
   3  * Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
   4  *
   5  * Copyright (c) 2007, Anthony Minessale II
   6  * All rights reserved.
   7  * 
   8  * Redistribution and use in source and binary forms, with or without
   9  * modification, are permitted provided that the following conditions
  10  * are met:
  11  * 
  12  * * Redistributions of source code must retain the above copyright
  13  * notice, this list of conditions and the following disclaimer.
  14  * 
  15  * * Redistributions in binary form must reproduce the above copyright
  16  * notice, this list of conditions and the following disclaimer in the
  17  * documentation and/or other materials provided with the distribution.
  18  * 
  19  * * Neither the name of the original author; nor the names of any contributors
  20  * may be used to endorse or promote products derived from this software
  21  * without specific prior written permission.
  22  * 
  23  * 
  24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER
  28  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  30  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  31  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35  */
  36 #ifndef LIBTELETONE_GENERATE_H
  37 #define LIBTELETONE_GENERATE_H
  38 #ifdef __cplusplus
  39 extern "C" {
  40 #ifdef _doh
  41 }
  42 #endif
  43 #endif
  44 
  45 #include <stdio.h>
  46 #include <stdlib.h>
  47 
  48 #if  defined(__SUNPRO_C) || defined(__SUNPRO_CC)
  49 #ifndef __inline__
  50 #define __inline__ inline
  51 #endif
  52 #endif
  53 
  54 #ifdef _MSC_VER
  55 #ifndef __inline__
  56 #define __inline__ __inline
  57 #endif
  58 
  59 typedef unsigned __int64 uint64_t;
  60 typedef unsigned __int32 uint32_t;
  61 typedef unsigned __int16 uint16_t;
  62 typedef unsigned __int8 uint8_t;
  63 typedef __int64 int64_t;
  64 typedef __int32 int32_t;
  65 typedef __int16 int16_t;
  66 typedef __int8 int8_t;
  67 #else
  68 #include <stdint.h>
  69 #endif
  70 #include <sys/types.h>
  71 #include <sys/stat.h>
  72 #include <fcntl.h>
  73 #include <math.h>
  74 #if !defined(powf) && !defined(_WIN64)
  75 extern float powf (float, float);
  76 #endif
  77 #include <string.h>
  78 #include <errno.h>
  79 #ifndef _MSC_VER
  80 #include <unistd.h>
  81 #endif
  82 #include <assert.h>
  83 #include <stdarg.h>
  84 #include "libteletone.h"
  85 
  86 #define TELETONE_VOL_DB_MAX 0
  87 #define TELETONE_VOL_DB_MIN -63
  88 #define MAX_PHASE_TONES 4
  89 
  90 struct teletone_dds_state {
  91         uint32_t phase_rate[MAX_PHASE_TONES];
  92         uint32_t scale_factor;
  93         uint32_t phase_accumulator;
  94         teletone_process_t tx_level;
  95 };
  96 typedef struct teletone_dds_state teletone_dds_state_t;
  97 
  98 #define SINE_TABLE_MAX 128
  99 #define SINE_TABLE_LEN (SINE_TABLE_MAX - 1)
 100 #define MAX_PHASE_ACCUMULATOR 0x10000 * 0x10000
 101 /* 3.14 == the max power on ulaw (alaw is 3.17) */
 102 /* 3.02 represents twice the power */
 103 #define DBM0_MAX_POWER (3.14f + 3.02f)
 104 
 105 TELETONE_API_DATA extern int16_t TELETONE_SINES[SINE_TABLE_MAX];
 106 
 107 static __inline__ int32_t teletone_dds_phase_rate(teletone_process_t tone, uint32_t rate)
 108 {
 109         return (int32_t) ((tone * MAX_PHASE_ACCUMULATOR) / rate);
 110 }
 111 
 112 static __inline__ int16_t teletone_dds_state_modulate_sample(teletone_dds_state_t *dds, uint32_t pindex)
 113 {
 114         int32_t bitmask = dds->phase_accumulator, sine_index = (bitmask >>= 23) & SINE_TABLE_LEN;
 115         int16_t sample;
 116 
 117         if (pindex >= MAX_PHASE_TONES)  {
 118                 pindex = 0;
 119         }
 120 
 121         if (bitmask & SINE_TABLE_MAX) {
 122                 sine_index = SINE_TABLE_LEN - sine_index;
 123         }
 124 
 125         sample = TELETONE_SINES[sine_index];
 126         
 127         if (bitmask & (SINE_TABLE_MAX * 2)) {
 128                 sample *= -1;
 129         }
 130 
 131         dds->phase_accumulator += dds->phase_rate[pindex];
 132         return (int16_t) (sample * dds->scale_factor >> 15);
 133 }
 134 
 135 static __inline__ void teletone_dds_state_set_tx_level(teletone_dds_state_t *dds, float tx_level)
 136 {
 137         dds->scale_factor = (int) (powf(10.0f, (tx_level - DBM0_MAX_POWER) / 20.0f) * (32767.0f * 1.414214f));
 138         dds->tx_level = tx_level;
 139 }
 140 
 141 static __inline__ void teletone_dds_state_reset_accum(teletone_dds_state_t *dds)
 142 {
 143         dds->phase_accumulator = 0;
 144 }
 145 
 146 static __inline__ int teletone_dds_state_set_tone(teletone_dds_state_t *dds, teletone_process_t tone, uint32_t rate, uint32_t pindex)
 147 {
 148         if (pindex < MAX_PHASE_TONES)  {
 149                 dds->phase_rate[pindex] = teletone_dds_phase_rate(tone, rate);
 150                 return 0;
 151         }
 152         
 153         return -1;
 154 }
 155 
 156 
 157 
 158 /*! \file libteletone_generate.h
 159   \brief Tone Generation Routines
 160 
 161   This module is responsible for tone generation specifics
 162 */
 163 
 164 typedef int16_t teletone_audio_t;
 165 struct teletone_generation_session;
 166 typedef int (*tone_handler)(struct teletone_generation_session *ts, teletone_tone_map_t *map);
 167 
 168 /*! \brief An abstraction to store a tone generation session */
 169 struct teletone_generation_session {
 170         /*! An array of tone mappings to character mappings */
 171         teletone_tone_map_t TONES[TELETONE_TONE_RANGE];
 172         /*! The number of channels the output audio should be in */
 173         int channels;
 174         /*! The Rate in hz of the output audio */
 175         int rate;
 176         /*! The duration (in samples) of the output audio */
 177         int duration;
 178         /*! The duration of silence to append after the initial audio is generated */
 179         int wait;
 180         /*! The duration (in samples) of the output audio (takes prescedence over actual duration value) */
 181         int tmp_duration;
 182         /*! The duration of silence to append after the initial audio is generated (takes prescedence over actual wait value)*/
 183         int tmp_wait;
 184         /*! Number of loops to repeat a single instruction*/
 185         int loops;
 186         /*! Number of loops to repeat the entire set of instructions*/
 187         int LOOPS;
 188         /*! Number to mutiply total samples by to determine when to begin ascent or decent e.g. 0=beginning 4=(last 25%) */
 189         float decay_factor;
 190         /*! Direction to perform volume increase/decrease 1/-1*/
 191         int decay_direction;
 192         /*! Number of samples between increase/decrease of volume */
 193         int decay_step;
 194         /*! Volume factor of the tone */
 195         float volume;
 196         /*! Debug on/off */
 197         int debug;
 198         /*! FILE stream to write debug data to */
 199         FILE *debug_stream;
 200         /*! Extra user data to attach to the session*/
 201         void *user_data;
 202         /*! Buffer for storing sample data (dynamic) */
 203         teletone_audio_t *buffer;
 204         /*! Size of the buffer */
 205         int datalen;
 206         /*! In-Use size of the buffer */
 207         int samples;
 208         /*! Callback function called during generation */
 209         int dynamic;
 210         tone_handler handler;
 211 };
 212 
 213 typedef struct teletone_generation_session teletone_generation_session_t;
 214 
 215 
 216 /*! 
 217   \brief Assign a set of tones to a tone_session indexed by a paticular index/character
 218   \param ts the tone generation session
 219   \param index the index to map the tone to
 220   \param ... up to TELETONE_MAX_TONES frequencies terminated by 0.0
 221   \return 0
 222 */
 223 TELETONE_API(int) teletone_set_tone(teletone_generation_session_t *ts, int index, ...);
 224 
 225 /*! 
 226   \brief Assign a set of tones to a single tone map
 227   \param map the map to assign the tones to
 228   \param ... up to TELETONE_MAX_TONES frequencies terminated by 0.0
 229   \return 0
 230 */
 231 TELETONE_API(int) teletone_set_map(teletone_tone_map_t *map, ...);
 232 
 233 /*! 
 234   \brief Initilize a tone generation session
 235   \param ts the tone generation session to initilize
 236   \param buflen the size of the buffer(in samples) to dynamically allocate
 237   \param handler a callback function to execute when a tone generation instruction is complete
 238   \param user_data optional user data to send
 239   \return 0
 240 */
 241 TELETONE_API(int) teletone_init_session(teletone_generation_session_t *ts, int buflen, tone_handler handler, void *user_data);
 242 
 243 /*! 
 244   \brief Free the buffer allocated by a tone generation session
 245   \param ts the tone generation session to destroy
 246   \return 0
 247 */
 248 TELETONE_API(int) teletone_destroy_session(teletone_generation_session_t *ts);
 249 
 250 /*! 
 251   \brief Execute a single tone generation instruction
 252   \param ts the tone generation session to consult for parameters
 253   \param map the tone mapping to use for the frequencies
 254   \return 0
 255 */
 256 TELETONE_API(int) teletone_mux_tones(teletone_generation_session_t *ts, teletone_tone_map_t *map);
 257 
 258 /*! 
 259   \brief Execute a tone generation script and call callbacks after each instruction
 260   \param ts the tone generation session to execute on
 261   \param cmd the script to execute
 262   \return 0
 263 */
 264 TELETONE_API(int) teletone_run(teletone_generation_session_t *ts, const char *cmd);
 265 
 266 #ifdef __cplusplus
 267 }
 268 #endif
 269 
 270 #endif
 271 
 272 /* For Emacs:
 273  * Local Variables:
 274  * mode:c
 275  * indent-tabs-mode:t
 276  * tab-width:4
 277  * c-basic-offset:4
 278  * End:
 279  * For VIM:
 280  * vim:set softtabstop=4 shiftwidth=4 tabstop=4:
 281  */

/* [<][>][^][v][top][bottom][index][help] */