mirror of
https://github.com/signalwire/freeswitch.git
synced 2025-02-08 08:51:50 +00:00
Add function pointer typedef.
Switch to use /* */ comments to survive pickier compile flags. Change destroy function to take ** pointer so it can set the pointer back to NULL make code style consistent with the rest of the lib. git-svn-id: http://svn.openzap.org/svn/openzap/trunk@202 a93c3328-9c30-0410-af19-c9cd2b2d52af
This commit is contained in:
parent
2a79a09b8f
commit
e56c64a692
@ -46,7 +46,7 @@
|
|||||||
#define M_PI 3.14159265358979323846
|
#define M_PI 3.14159265358979323846
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// local constants
|
/* local constants */
|
||||||
#define BELL202_MARK 1200
|
#define BELL202_MARK 1200
|
||||||
#define BELL202_SPACE 2200
|
#define BELL202_SPACE 2200
|
||||||
#define BELL202_BAUD 1200
|
#define BELL202_BAUD 1200
|
||||||
@ -58,8 +58,7 @@
|
|||||||
* attributes structure is used.
|
* attributes structure is used.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void
|
void dsp_bell202_attr_init (dsp_bell202_attr_t *attr)
|
||||||
dsp_bell202_attr_init (dsp_bell202_attr_t *attr)
|
|
||||||
{
|
{
|
||||||
memset(attr, 0, sizeof(*attr));
|
memset(attr, 0, sizeof(*attr));
|
||||||
}
|
}
|
||||||
@ -77,48 +76,42 @@ dsp_bell202_attr_init (dsp_bell202_attr_t *attr)
|
|||||||
* zero == ok, -1 == fail.
|
* zero == ok, -1 == fail.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void (*
|
bithandler_func_t dsp_bell202_attr_get_bithandler(dsp_bell202_attr_t *attr, void **bithandler_arg)
|
||||||
dsp_bell202_attr_get_bithandler (dsp_bell202_attr_t *attr, void **bithandler_arg)) (void *, int)
|
|
||||||
{
|
{
|
||||||
*bithandler_arg = attr->bithandler_arg;
|
*bithandler_arg = attr->bithandler_arg;
|
||||||
return (attr -> bithandler);
|
return attr->bithandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void dsp_bell202_attr_set_bithandler(dsp_bell202_attr_t *attr, bithandler_func_t bithandler, void *bithandler_arg)
|
||||||
dsp_bell202_attr_set_bithandler (dsp_bell202_attr_t *attr, void (*bithandler) (void *, int ), void *bithandler_arg)
|
|
||||||
{
|
{
|
||||||
attr->bithandler = bithandler;
|
attr->bithandler = bithandler;
|
||||||
attr->bithandler_arg = bithandler_arg;
|
attr->bithandler_arg = bithandler_arg;
|
||||||
}
|
}
|
||||||
|
|
||||||
void (*
|
bytehandler_func_t dsp_bell202_attr_get_bytehandler(dsp_bell202_attr_t *attr, void **bytehandler_arg)
|
||||||
dsp_bell202_attr_get_bytehandler (dsp_bell202_attr_t *attr, void **bytehandler_arg)) (void *, int)
|
|
||||||
{
|
{
|
||||||
*bytehandler_arg = attr->bytehandler_arg;
|
*bytehandler_arg = attr->bytehandler_arg;
|
||||||
return (attr -> bytehandler);
|
return attr->bytehandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void dsp_bell202_attr_set_bytehandler(dsp_bell202_attr_t *attr, bytehandler_func_t bytehandler, void *bytehandler_arg)
|
||||||
dsp_bell202_attr_set_bytehandler (dsp_bell202_attr_t *attr, void (*bytehandler) (void *, int ), void *bytehandler_arg)
|
|
||||||
{
|
{
|
||||||
attr->bytehandler = bytehandler;
|
attr->bytehandler = bytehandler;
|
||||||
attr->bytehandler_arg = bytehandler_arg;
|
attr->bytehandler_arg = bytehandler_arg;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int dsp_bell202_attr_get_samplerate (dsp_bell202_attr_t *attr)
|
||||||
dsp_bell202_attr_get_samplerate (dsp_bell202_attr_t *attr)
|
|
||||||
{
|
{
|
||||||
return (attr -> sample_rate);
|
return attr->sample_rate;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int dsp_bell202_attr_set_samplerate (dsp_bell202_attr_t *attr, int samplerate)
|
||||||
dsp_bell202_attr_set_samplerate (dsp_bell202_attr_t *attr, int samplerate)
|
|
||||||
{
|
{
|
||||||
if (samplerate <= 0) {
|
if (samplerate <= 0) {
|
||||||
return (-1);
|
return -1;
|
||||||
}
|
}
|
||||||
attr->sample_rate = samplerate;
|
attr->sample_rate = samplerate;
|
||||||
return (0);
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -132,24 +125,23 @@ dsp_bell202_attr_set_samplerate (dsp_bell202_attr_t *attr, int samplerate)
|
|||||||
* Once created, the handle can be used until it is destroyed.
|
* Once created, the handle can be used until it is destroyed.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
dsp_bell202_handle_t *
|
dsp_bell202_handle_t *dsp_bell202_create(dsp_bell202_attr_t *attr)
|
||||||
dsp_bell202_create (dsp_bell202_attr_t *attr)
|
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
double phi_mark, phi_space;
|
double phi_mark, phi_space;
|
||||||
dsp_bell202_handle_t *handle;
|
dsp_bell202_handle_t *handle;
|
||||||
|
|
||||||
handle = malloc(sizeof(*handle));
|
handle = malloc(sizeof(*handle));
|
||||||
if (handle == NULL) {
|
if (!handle) {
|
||||||
return (handle);
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(handle, 0, sizeof(*handle));
|
memset(handle, 0, sizeof(*handle));
|
||||||
|
|
||||||
// fill the attributes member
|
/* fill the attributes member */
|
||||||
memcpy(&handle->attr, attr, sizeof(*attr));
|
memcpy(&handle->attr, attr, sizeof(*attr));
|
||||||
|
|
||||||
// see if we can do downsampling. We only really need 6 samples to "match"
|
/* see if we can do downsampling. We only really need 6 samples to "match" */
|
||||||
if (attr->sample_rate / BELL202_MARK > 6) {
|
if (attr->sample_rate / BELL202_MARK > 6) {
|
||||||
handle->downsampling_count = attr->sample_rate / BELL202_MARK / 6;
|
handle->downsampling_count = attr->sample_rate / BELL202_MARK / 6;
|
||||||
} else {
|
} else {
|
||||||
@ -157,50 +149,44 @@ dsp_bell202_create (dsp_bell202_attr_t *attr)
|
|||||||
}
|
}
|
||||||
handle->current_downsample = 1;
|
handle->current_downsample = 1;
|
||||||
|
|
||||||
// calculate the correlate size (number of samples required for slowest wave)
|
/* calculate the correlate size (number of samples required for slowest wave) */
|
||||||
handle->corrsize = attr->sample_rate / handle->downsampling_count / BELL202_MARK;
|
handle->corrsize = attr->sample_rate / handle->downsampling_count / BELL202_MARK;
|
||||||
|
|
||||||
// allocate the correlation sin/cos arrays and initialize
|
/* allocate the correlation sin/cos arrays and initialize */
|
||||||
for (i = 0; i < 4; i++) {
|
for (i = 0; i < 4; i++) {
|
||||||
handle->correlates[i] = malloc(sizeof(double) * handle->corrsize);
|
handle->correlates[i] = malloc(sizeof(double) * handle->corrsize);
|
||||||
if (handle->correlates[i] == NULL) {
|
if (handle->correlates[i] == NULL) {
|
||||||
break;
|
/* some failed, back out memory allocations */
|
||||||
|
dsp_bell202_destroy(&handle);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (i != 4) { // some failed, back out memory allocations
|
|
||||||
dsp_bell202_destroy (handle);
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
// now initialize them
|
/* now initialize them */
|
||||||
phi_mark = 2. * M_PI / ((double) attr->sample_rate / (double) handle->downsampling_count / (double) BELL202_MARK);
|
phi_mark = 2. * M_PI / ((double) attr->sample_rate / (double) handle->downsampling_count / (double) BELL202_MARK);
|
||||||
phi_space = 2. * M_PI / ((double) attr->sample_rate / (double) handle->downsampling_count / (double) BELL202_SPACE);
|
phi_space = 2. * M_PI / ((double) attr->sample_rate / (double) handle->downsampling_count / (double) BELL202_SPACE);
|
||||||
|
|
||||||
// printf ("phi_mark is %g, phi_space is %g\n", phi_mark, phi_space);
|
|
||||||
for (i = 0; i < handle->corrsize; i++) {
|
for (i = 0; i < handle->corrsize; i++) {
|
||||||
handle->correlates[0][i] = sin(phi_mark * (double) i);
|
handle->correlates[0][i] = sin(phi_mark * (double) i);
|
||||||
handle->correlates[1][i] = cos(phi_mark * (double) i);
|
handle->correlates[1][i] = cos(phi_mark * (double) i);
|
||||||
handle->correlates[2][i] = sin(phi_space * (double) i);
|
handle->correlates[2][i] = sin(phi_space * (double) i);
|
||||||
handle->correlates[3][i] = cos(phi_space * (double) i);
|
handle->correlates[3][i] = cos(phi_space * (double) i);
|
||||||
// printf ("[%2d] MS %10.4f MC %10.4f SS %10.4f SC %10.4f\n", i, handle -> correlates [0][i], handle -> correlates [1][i], handle -> correlates [2][i], handle -> correlates [3][i]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// initialize the ring buffer
|
/* initialize the ring buffer */
|
||||||
handle->buffer = malloc(sizeof(double) * handle->corrsize);
|
handle->buffer = malloc(sizeof(double) * handle->corrsize);
|
||||||
if (handle -> buffer == NULL) { // failed; back out memory allocations
|
if (!handle->buffer) { /* failed; back out memory allocations */
|
||||||
dsp_bell202_destroy (handle);
|
dsp_bell202_destroy(&handle);
|
||||||
return (NULL);
|
return NULL;
|
||||||
}
|
}
|
||||||
memset(handle->buffer, 0, sizeof(double) * handle->corrsize);
|
memset(handle->buffer, 0, sizeof(double) * handle->corrsize);
|
||||||
handle->ringstart = 0;
|
handle->ringstart = 0;
|
||||||
|
|
||||||
// initalize intra-cell position
|
/* initalize intra-cell position */
|
||||||
handle->cellpos = 0;
|
handle->cellpos = 0;
|
||||||
handle->celladj = BELL202_BAUD / (double) attr->sample_rate * (double) handle->downsampling_count;
|
handle->celladj = BELL202_BAUD / (double) attr->sample_rate * (double) handle->downsampling_count;
|
||||||
|
|
||||||
// printf ("corrsize %d celladj %g\n", handle -> corrsize, handle -> celladj);
|
/* if they have provided a byte handler, add a UART to the processing chain */
|
||||||
|
|
||||||
// if they have provided a byte handler, add a UART to the processing chain
|
|
||||||
if (handle->attr.bytehandler) {
|
if (handle->attr.bytehandler) {
|
||||||
dsp_uart_attr_t uart_attr;
|
dsp_uart_attr_t uart_attr;
|
||||||
dsp_uart_handle_t *uart_handle;
|
dsp_uart_handle_t *uart_handle;
|
||||||
@ -209,46 +195,46 @@ dsp_bell202_create (dsp_bell202_attr_t *attr)
|
|||||||
dsp_uart_attr_set_bytehandler(&uart_attr, handle->attr.bytehandler, handle->attr.bytehandler_arg);
|
dsp_uart_attr_set_bytehandler(&uart_attr, handle->attr.bytehandler, handle->attr.bytehandler_arg);
|
||||||
uart_handle = dsp_uart_create(&uart_attr);
|
uart_handle = dsp_uart_create(&uart_attr);
|
||||||
if (uart_handle == NULL) {
|
if (uart_handle == NULL) {
|
||||||
dsp_bell202_destroy (handle);
|
dsp_bell202_destroy(&handle);
|
||||||
return (NULL);
|
return NULL;
|
||||||
}
|
}
|
||||||
handle->attr.bithandler = dsp_uart_bit_handler;
|
handle->attr.bithandler = dsp_uart_bit_handler;
|
||||||
handle->attr.bithandler_arg = uart_handle;
|
handle->attr.bithandler_arg = uart_handle;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (handle);
|
return handle;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* dsp_bell202_destroy
|
* dsp_bell202_destroy
|
||||||
*
|
*
|
||||||
* Destroys a handle, releasing any associated memory. A destroyed handle
|
* Destroys a handle, releasing any associated memory. Sets handle pointer to NULL
|
||||||
* should not be used for anything.
|
* so A destroyed handle can not be used for anything after the destroy.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void
|
void dsp_bell202_destroy(dsp_bell202_handle_t **handle)
|
||||||
dsp_bell202_destroy (dsp_bell202_handle_t *handle)
|
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
// if empty handle, just return
|
/* if empty handle, just return */
|
||||||
if (handle == NULL) {
|
if (*handle == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < 4; i++) {
|
for (i = 0; i < 4; i++) {
|
||||||
if (handle -> correlates [i] != NULL) {
|
if ((*handle)->correlates[i] != NULL) {
|
||||||
free (handle -> correlates [i]);
|
free((*handle)->correlates[i]);
|
||||||
handle -> correlates [i] = NULL;
|
(*handle)->correlates[i] = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (handle -> buffer != NULL) {
|
if ((*handle)->buffer != NULL) {
|
||||||
free (handle -> buffer);
|
free((*handle)->buffer);
|
||||||
handle -> buffer = NULL;
|
(*handle)->buffer = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
free (handle);
|
free(*handle);
|
||||||
|
*handle = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -270,23 +256,23 @@ dsp_bell202_sample (dsp_bell202_handle_t *handle, double normalized_sample)
|
|||||||
double factors[4];
|
double factors[4];
|
||||||
int i, j;
|
int i, j;
|
||||||
|
|
||||||
// if we can avoid processing samples, do so
|
/* if we can avoid processing samples, do so */
|
||||||
if (handle->downsampling_count != 1) {
|
if (handle->downsampling_count != 1) {
|
||||||
if (handle->current_downsample < handle->downsampling_count) {
|
if (handle->current_downsample < handle->downsampling_count) {
|
||||||
handle->current_downsample++;
|
handle->current_downsample++;
|
||||||
return; // throw this sample out
|
return; /* throw this sample out */
|
||||||
}
|
}
|
||||||
handle->current_downsample = 1;
|
handle->current_downsample = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// store sample in buffer
|
/* store sample in buffer */
|
||||||
handle->buffer[handle->ringstart++] = normalized_sample;
|
handle->buffer[handle->ringstart++] = normalized_sample;
|
||||||
if (handle->ringstart >= handle->corrsize) {
|
if (handle->ringstart >= handle->corrsize) {
|
||||||
handle->ringstart = 0;
|
handle->ringstart = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// do the correlation calculation
|
/* do the correlation calculation */
|
||||||
factors [0] = factors [1] = factors [2] = factors [3] = 0; // clear out intermediate sums
|
factors[0] = factors[1] = factors[2] = factors[3] = 0; /* clear out intermediate sums */
|
||||||
j = handle->ringstart;
|
j = handle->ringstart;
|
||||||
for (i = 0; i < handle->corrsize; i++) {
|
for (i = 0; i < handle->corrsize; i++) {
|
||||||
if (j >= handle->corrsize) {
|
if (j >= handle->corrsize) {
|
||||||
@ -300,16 +286,15 @@ dsp_bell202_sample (dsp_bell202_handle_t *handle, double normalized_sample)
|
|||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// store the bit (bit value is comparison of the two sets of correlate factors)
|
/* store the bit (bit value is comparison of the two sets of correlate factors) */
|
||||||
handle->previous_bit = handle->current_bit;
|
handle->previous_bit = handle->current_bit;
|
||||||
handle->current_bit = (factors[0] * factors[0] + factors[1] * factors[1] > factors[2] * factors[2] + factors[3] * factors[3]);
|
handle->current_bit = (factors[0] * factors[0] + factors[1] * factors[1] > factors[2] * factors[2] + factors[3] * factors[3]);
|
||||||
|
|
||||||
// printf ("Sample %10.4f factors %10.4f %10.4f %10.4f %10.4f (sum %10.4f) previous %d current %d cellpos %10.4f\n", normalized_sample, factors [0], factors [1], factors [2], factors [3], factors [0] * factors [0] + factors [1] * factors [1] - factors [2] * factors [2] - factors [3] * factors [3], handle -> previous_bit, handle -> current_bit, handle -> cellpos);
|
/* if there's a transition, we can synchronize the cell position */
|
||||||
// if there's a transition, we can synchronize the cell position
|
|
||||||
if (handle->previous_bit != handle->current_bit) {
|
if (handle->previous_bit != handle->current_bit) {
|
||||||
handle -> cellpos = 0.5; // adjust cell position to be in the middle of the cell
|
handle->cellpos = 0.5; /* adjust cell position to be in the middle of the cell */
|
||||||
}
|
}
|
||||||
handle -> cellpos += handle -> celladj; // walk the cell along
|
handle->cellpos += handle->celladj; /* walk the cell along */
|
||||||
|
|
||||||
if (handle->cellpos > 1.0) {
|
if (handle->cellpos > 1.0) {
|
||||||
handle->cellpos -= 1.0;
|
handle->cellpos -= 1.0;
|
||||||
|
@ -35,12 +35,15 @@
|
|||||||
#ifndef __BELL202_H__
|
#ifndef __BELL202_H__
|
||||||
#define __BELL202_H__
|
#define __BELL202_H__
|
||||||
|
|
||||||
|
typedef void (*bytehandler_func_t) (void *, int);
|
||||||
|
typedef void (*bithandler_func_t) (void *, int);
|
||||||
|
|
||||||
typedef struct dsp_bell202_attr_s
|
typedef struct dsp_bell202_attr_s
|
||||||
{
|
{
|
||||||
int sample_rate; // sample rate in HZ
|
int sample_rate; // sample rate in HZ
|
||||||
void (*bithandler) (void *, int); // bit handler
|
bithandler_func_t bithandler; // bit handler
|
||||||
void *bithandler_arg; // arbitrary ID passed to bithandler as first argument
|
void *bithandler_arg; // arbitrary ID passed to bithandler as first argument
|
||||||
void (*bytehandler) (void *, int); // byte handler
|
bytehandler_func_t bytehandler; // byte handler
|
||||||
void *bytehandler_arg; // arbitrary ID passed to bytehandler as first argument
|
void *bytehandler_arg; // arbitrary ID passed to bytehandler as first argument
|
||||||
} dsp_bell202_attr_t;
|
} dsp_bell202_attr_t;
|
||||||
|
|
||||||
@ -69,19 +72,19 @@ typedef struct
|
|||||||
* d) feed samples through the handler (dsp_bell202_sample)
|
* d) feed samples through the handler (dsp_bell202_sample)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
extern void dsp_bell202_attr_init (dsp_bell202_attr_t *attributes);
|
void dsp_bell202_attr_init(dsp_bell202_attr_t *attributes);
|
||||||
|
|
||||||
extern void (*dsp_bell202_attr_get_bithandler (dsp_bell202_attr_t *attributes, void **bithandler_arg)) (void *, int);
|
bithandler_func_t dsp_bell202_attr_get_bithandler(dsp_bell202_attr_t *attributes, void **bithandler_arg);
|
||||||
extern void dsp_bell202_attr_set_bithandler (dsp_bell202_attr_t *attributes, void (*bithandler) (void *, int ), void *bithandler_arg);
|
void dsp_bell202_attr_set_bithandler(dsp_bell202_attr_t *attributes, bithandler_func_t bithandler, void *bithandler_arg);
|
||||||
extern void (*dsp_bell202_attr_get_bytehandler (dsp_bell202_attr_t *attributes, void **bytehandler_arg)) (void *, int);
|
bytehandler_func_t dsp_bell202_attr_get_bytehandler(dsp_bell202_attr_t *attributes, void **bytehandler_arg);
|
||||||
extern void dsp_bell202_attr_set_bytehandler (dsp_bell202_attr_t *attributes, void (*bytehandler) (void *, int ), void *bytehandler_arg);
|
void dsp_bell202_attr_set_bytehandler(dsp_bell202_attr_t *attributes, bytehandler_func_t bytehandler, void *bytehandler_arg);
|
||||||
extern int dsp_bell202_attr_get_samplerate (dsp_bell202_attr_t *attributes);
|
int dsp_bell202_attr_get_samplerate(dsp_bell202_attr_t *attributes);
|
||||||
extern int dsp_bell202_attr_set_samplerate (dsp_bell202_attr_t *attributes, int samplerate);
|
int dsp_bell202_attr_set_samplerate(dsp_bell202_attr_t *attributes, int samplerate);
|
||||||
|
|
||||||
extern dsp_bell202_handle_t * dsp_bell202_create (dsp_bell202_attr_t *attributes);
|
dsp_bell202_handle_t * dsp_bell202_create(dsp_bell202_attr_t *attributes);
|
||||||
extern void dsp_bell202_destroy (dsp_bell202_handle_t *handle);
|
void dsp_bell202_destroy(dsp_bell202_handle_t **handle);
|
||||||
|
|
||||||
extern void dsp_bell202_sample (dsp_bell202_handle_t *handle, double normalized_sample);
|
void dsp_bell202_sample(dsp_bell202_handle_t *handle, double normalized_sample);
|
||||||
|
|
||||||
#endif // __BELL202_H__
|
#endif // __BELL202_H__
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user