add dds to teletone

git-svn-id: http://svn.openzap.org/svn/openzap/trunk@223 a93c3328-9c30-0410-af19-c9cd2b2d52af
This commit is contained in:
Anthony Minessale
2007-06-06 22:58:13 +00:00
parent 886482c168
commit 83d74318e1
7 changed files with 294 additions and 151 deletions

View File

@@ -73,32 +73,37 @@
#ifdef __cplusplus
extern "C" {
#ifdef _doh
}
#endif
#endif
#define TELETONE_MAX_DTMF_DIGITS 128
#define TELETONE_MAX_TONES 6
#define TELETONE_TONE_RANGE 127
typedef double teletone_process_t;
typedef double teletone_process_t;
/*! \file libteletone.h
\brief Top level include file
/*! \file libteletone.h
\brief Top level include file
This file should be included by applications using the library
*/
This file should be included by applications using the library
*/
/*! \brief An abstraction to store a tone mapping */
typedef struct {
/*! An array of tone frequencies */
teletone_process_t freqs[TELETONE_MAX_TONES];
} teletone_tone_map_t;
/*! \brief An abstraction to store a tone mapping */
typedef struct {
/*! An array of tone frequencies */
teletone_process_t freqs[TELETONE_MAX_TONES];
} teletone_tone_map_t;
#if !defined(M_PI)
/* C99 systems may not define M_PI */
/* C99 systems may not define M_PI */
#define M_PI 3.14159265358979323846264338327
#endif
#ifdef _MSC_VER
typedef __int16 int16_t;
typedef __int16 int16_t;
#endif
#include <libteletone_generate.h>

View File

@@ -72,13 +72,30 @@
#define LIBTELETONE_GENERATE_H
#ifdef __cplusplus
extern "C" {
#ifdef _doh
}
#endif
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <math.h>
#if !defined(powf)
extern float powf (float, float);
#endif
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#ifndef _MSC_VER
#include <unistd.h>
#include <stdint.h>
#endif
#include <fcntl.h>
#include <sys/types.h>
@@ -87,115 +104,174 @@ extern "C" {
#include <stdarg.h>
#include <libteletone.h>
#define TELETONE_VOL_DB_MAX 0
#define TELETONE_VOL_DB_MIN -63
struct teletone_dds_state {
uint32_t phase_rate;
uint32_t scale_factor;
uint32_t phase_accumulator;
int16_t sample;
int32_t tx_level;
};
typedef struct teletone_dds_state teletone_dds_state_t;
#define SINE_TABLE_MAX 128
#define SINE_TABLE_LEN (SINE_TABLE_MAX - 1)
#define MAX_PHASE_ACCUMULATOR 0x10000 * 0x10000
/* 3.14 == the max power on ulaw (alaw is 3.17) */
/* 3.02 represents twice the power */
#define DBM0_MAX_POWER (3.14f + 3.02f)
const int16_t TELETONE_SINES[SINE_TABLE_MAX];
static __inline__ int16_t teletone_dds_modulate_sample(teletone_dds_state_t *dds)
{
int32_t bitmask = dds->phase_accumulator, sine_index = (bitmask >>= 23) & SINE_TABLE_LEN;
int16_t sample;
if (bitmask & SINE_TABLE_MAX) {
sine_index = SINE_TABLE_LEN - sine_index;
}
sample = TELETONE_SINES[sine_index];
if (bitmask & (SINE_TABLE_MAX * 2)) {
sample *= -1;
}
dds->phase_accumulator += dds->phase_rate;
return (int16_t) (sample * dds->scale_factor >> 15);
}
static __inline__ void teletone_dds_state_set_tone(teletone_dds_state_t *dds, float tone, uint32_t rate, float tx_level)
{
dds->phase_accumulator = 0;
dds->phase_rate = (int32_t) ((tone * MAX_PHASE_ACCUMULATOR) / rate);
/*! \file libteletone_generate.h
\brief Tone Generation Routines
if (dds->tx_level != tx_level || !dds->scale_factor) {
dds->scale_factor = (int) (powf(10.0f, (tx_level - DBM0_MAX_POWER) / 20.0f) * (32767.0f * 1.414214f));
}
dds->tx_level = tx_level;
}
This module is responsible for tone generation specifics
*/
typedef int16_t teletone_audio_t;
struct teletone_generation_session;
typedef int (*tone_handler)(struct teletone_generation_session *ts, teletone_tone_map_t *map);
/*! \brief An abstraction to store a tone generation session */
struct teletone_generation_session {
/*! An array of tone mappings to character mappings */
teletone_tone_map_t TONES[TELETONE_TONE_RANGE];
/*! The number of channels the output audio should be in */
int channels;
/*! The Rate in hz of the output audio */
int rate;
/*! The duration (in samples) of the output audio */
int duration;
/*! The duration of silence to append after the initial audio is generated */
int wait;
/*! The duration (in samples) of the output audio (takes prescedence over actual duration value) */
int tmp_duration;
/*! The duration of silence to append after the initial audio is generated (takes prescedence over actual wait value)*/
int tmp_wait;
/*! Number of loops to repeat a single instruction*/
int loops;
/*! Number of loops to repeat the entire set of instructions*/
int LOOPS;
/*! Number to mutiply total samples by to determine when to begin ascent or decent e.g. 0=beginning 4=(last 25%) */
int decay_factor;
/*! Direction to perform volume increase/decrease 1/-1*/
int decay_direction;
/*! Number of samples between increase/decrease of volume */
int decay_step;
/*! Volume factor of the tone */
int volume;
/*! Debug on/off */
int debug;
/*! FILE stream to write debug data to */
FILE *debug_stream;
/*! Extra user data to attach to the session*/
void *user_data;
/*! Buffer for storing sample data (dynamic) */
teletone_audio_t *buffer;
/*! Size of the buffer */
int datalen;
/*! In-Use size of the buffer */
int samples;
/*! Callback function called during generation */
int dynamic;
tone_handler handler;
};
typedef struct teletone_generation_session teletone_generation_session_t;
static __inline__ void teletone_dds_state_set_tx_level(teletone_dds_state_t *dds, float tx_level)
{
dds->scale_factor = (int) (powf(10.0f, (tx_level - DBM0_MAX_POWER) / 20.0f) * (32767.0f * 1.414214f));
}
/*!
\brief Assign a set of tones to a tone_session indexed by a paticular index/character
\param ts the tone generation session
\param index the index to map the tone to
\param ... up to TELETONE_MAX_TONES frequencies terminated by 0.0
\return 0
*/
int teletone_set_tone(teletone_generation_session_t *ts, int index, ...);
/*!
\brief Assign a set of tones to a single tone map
\param map the map to assign the tones to
\param ... up to TELETONE_MAX_TONES frequencies terminated by 0.0
\return 0
*/
int teletone_set_map(teletone_tone_map_t *map, ...);
/*! \file libteletone_generate.h
\brief Tone Generation Routines
/*!
\brief Initilize a tone generation session
\param ts the tone generation session to initilize
\param buflen the size of the buffer(in samples) to dynamically allocate
\param handler a callback function to execute when a tone generation instruction is complete
\param user_data optional user data to send
\return 0
*/
int teletone_init_session(teletone_generation_session_t *ts, int buflen, tone_handler handler, void *user_data);
This module is responsible for tone generation specifics
*/
/*!
\brief Free the buffer allocated by a tone generation session
\param ts the tone generation session to destroy
\return 0
*/
int teletone_destroy_session(teletone_generation_session_t *ts);
typedef int16_t teletone_audio_t;
struct teletone_generation_session;
typedef int (*tone_handler)(struct teletone_generation_session *ts, teletone_tone_map_t *map);
/*!
\brief Execute a single tone generation instruction
\param ts the tone generation session to consult for parameters
\param map the tone mapping to use for the frequencies
\return 0
*/
int teletone_mux_tones(teletone_generation_session_t *ts, teletone_tone_map_t *map);
/*! \brief An abstraction to store a tone generation session */
struct teletone_generation_session {
/*! An array of tone mappings to character mappings */
teletone_tone_map_t TONES[TELETONE_TONE_RANGE];
/*! The number of channels the output audio should be in */
int channels;
/*! The Rate in hz of the output audio */
int rate;
/*! The duration (in samples) of the output audio */
int duration;
/*! The duration of silence to append after the initial audio is generated */
int wait;
/*! The duration (in samples) of the output audio (takes prescedence over actual duration value) */
int tmp_duration;
/*! The duration of silence to append after the initial audio is generated (takes prescedence over actual wait value)*/
int tmp_wait;
/*! Number of loops to repeat a single instruction*/
int loops;
/*! Number of loops to repeat the entire set of instructions*/
int LOOPS;
/*! Number to mutiply total samples by to determine when to begin ascent or decent e.g. 0=beginning 4=(last 25%) */
float decay_factor;
/*! Direction to perform volume increase/decrease 1/-1*/
int decay_direction;
/*! Number of samples between increase/decrease of volume */
int decay_step;
/*! Volume factor of the tone */
float volume;
/*! Debug on/off */
int debug;
/*! FILE stream to write debug data to */
FILE *debug_stream;
/*! Extra user data to attach to the session*/
void *user_data;
/*! Buffer for storing sample data (dynamic) */
teletone_audio_t *buffer;
/*! Size of the buffer */
int datalen;
/*! In-Use size of the buffer */
int samples;
/*! Callback function called during generation */
int dynamic;
tone_handler handler;
};
/*!
\brief Execute a tone generation script and call callbacks after each instruction
\param ts the tone generation session to execute on
\param cmd the script to execute
\return 0
*/
int teletone_run(teletone_generation_session_t *ts, char *cmd);
typedef struct teletone_generation_session teletone_generation_session_t;
/*!
\brief Assign a set of tones to a tone_session indexed by a paticular index/character
\param ts the tone generation session
\param index the index to map the tone to
\param ... up to TELETONE_MAX_TONES frequencies terminated by 0.0
\return 0
*/
int teletone_set_tone(teletone_generation_session_t *ts, int index, ...);
/*!
\brief Assign a set of tones to a single tone map
\param map the map to assign the tones to
\param ... up to TELETONE_MAX_TONES frequencies terminated by 0.0
\return 0
*/
int teletone_set_map(teletone_tone_map_t *map, ...);
/*!
\brief Initilize a tone generation session
\param ts the tone generation session to initilize
\param buflen the size of the buffer(in samples) to dynamically allocate
\param handler a callback function to execute when a tone generation instruction is complete
\param user_data optional user data to send
\return 0
*/
int teletone_init_session(teletone_generation_session_t *ts, int buflen, tone_handler handler, void *user_data);
/*!
\brief Free the buffer allocated by a tone generation session
\param ts the tone generation session to destroy
\return 0
*/
int teletone_destroy_session(teletone_generation_session_t *ts);
/*!
\brief Execute a single tone generation instruction
\param ts the tone generation session to consult for parameters
\param map the tone mapping to use for the frequencies
\return 0
*/
int teletone_mux_tones(teletone_generation_session_t *ts, teletone_tone_map_t *map);
/*!
\brief Execute a tone generation script and call callbacks after each instruction
\param ts the tone generation session to execute on
\param cmd the script to execute
\return 0
*/
int teletone_run(teletone_generation_session_t *ts, char *cmd);
#ifdef __cplusplus
}

View File

@@ -34,8 +34,9 @@
#ifndef OPENZAP_H
#define OPENZAP_H
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 500
#define _XOPEN_SOURCE 600
#endif
#ifndef HAVE_STRINGS_H