Merge pull request #699 in FS/freeswitch from ~TINKYWINKY/freeswitch:bugfix/FS-8808-avmd-code-refactor to master

* commit '5216adef09990f3b963dc2f9b99c000cf120c885':
  refactor: fix comments
  refactor: fix comments
  refactor: fix comments
This commit is contained in:
Anthony Minessale II 2016-02-05 16:11:57 -06:00
commit 34f02b1c0e
6 changed files with 44 additions and 58 deletions

View File

@ -9,11 +9,11 @@ extern size_t next_power_of_2(size_t v)
v++; v++;
do{ do {
prev = v; prev = v;
v &= ~tmp; v &= ~tmp;
tmp <<= 1; tmp <<= 1;
}while(v != 0); } while (v != 0);
prev <<= 1; prev <<= 1;

View File

@ -29,7 +29,7 @@ extern size_t next_power_of_2(size_t v);
(b)->pos++; \ (b)->pos++; \
(b)->pos &= (b)->mask; \ (b)->pos &= (b)->mask; \
(b)->lpos++; \ (b)->lpos++; \
if((b)->backlog < (b)->buf_len) (b)->backlog++; \ if ((b)->backlog < (b)->buf_len) (b)->backlog++; \
} }
#define DEC_POS(b) \ #define DEC_POS(b) \
@ -37,27 +37,27 @@ extern size_t next_power_of_2(size_t v);
(b)->pos--; \ (b)->pos--; \
(b)->pos &= (b)->mask; \ (b)->pos &= (b)->mask; \
(b)->lpos--; \ (b)->lpos--; \
if(((b)->backlog - 1) < (b)->backlog) (b)->backlog--; \ if (((b)->backlog - 1) < (b)->backlog) (b)->backlog--; \
} }
#define GET_SAMPLE(b, i) ((b)->buf[(i) & (b)->mask]) #define GET_SAMPLE(b, i) ((b)->buf[(i) & (b)->mask])
#define SET_SAMPLE(b, i, v) ((b)->buf[(i) & (b)->mask] = (v)) #define SET_SAMPLE(b, i, v) ((b)->buf[(i) & (b)->mask] = (v))
#define INSERT_FRAME(b, f, l) \ #define INSERT_FRAME(b, f, l) \
do{ \ do { \
for((b)->i = 0; (b)->i < (l); (b)->i++){ \ for ((b)->i = 0; (b)->i < (l); (b)->i++) { \
SET_SAMPLE((b), ((b)->i + (b)->pos), (f)[(b)->i]); \ SET_SAMPLE((b), ((b)->i + (b)->pos), (f)[(b)->i]); \
} \ } \
(b)->pos += (l); \ (b)->pos += (l); \
(b)->lpos += (l); \ (b)->lpos += (l); \
(b)->pos %= (b)->buf_len; \ (b)->pos %= (b)->buf_len; \
(b)->backlog += (l); \ (b)->backlog += (l); \
if((b)->backlog > (b)->buf_len) (b)->backlog = (b)->buf_len; \ if ((b)->backlog > (b)->buf_len) (b)->backlog = (b)->buf_len; \
}while(0) } while (0)
#define INSERT_INT16_FRAME(b, f, l) \ #define INSERT_INT16_FRAME(b, f, l) \
{ \ { \
for((b)->i = 0; (b)->i < (l); (b)->i++){ \ for ((b)->i = 0; (b)->i < (l); (b)->i++) { \
SET_SAMPLE( \ SET_SAMPLE( \
(b), \ (b), \
((b)->i + (b)->pos), \ ((b)->i + (b)->pos), \
@ -72,7 +72,7 @@ extern size_t next_power_of_2(size_t v);
(b)->lpos += (l); \ (b)->lpos += (l); \
(b)->pos &= (b)->mask; \ (b)->pos &= (b)->mask; \
(b)->backlog += (l); \ (b)->backlog += (l); \
if((b)->backlog > (b)->buf_len) (b)->backlog = (b)->buf_len; \ if ((b)->backlog > (b)->buf_len) (b)->backlog = (b)->buf_len; \
} }
@ -95,10 +95,10 @@ extern size_t next_power_of_2(size_t v);
#define GET_CURRENT_SAMPLE(b) GET_SAMPLE((b), GET_CURRENT_POS((b))) #define GET_CURRENT_SAMPLE(b) GET_SAMPLE((b), GET_CURRENT_POS((b)))
#define ADD_SAMPLE(b, s) \ #define ADD_SAMPLE(b, s) \
do{ \ do { \
INC_POS((b)); \ INC_POS((b)); \
SET_SAMPLE((b), GET_CURRENT_POS((b)), (s)); \ SET_SAMPLE((b), GET_CURRENT_POS((b)), (s)); \
}while(0) } while (0)
#endif #endif

View File

@ -35,7 +35,7 @@ extern double desa2(circ_buffer_t *b, size_t i)
x2sq = x2 * x2; x2sq = x2 * x2;
d = 2.0 * ((x2sq) - (x1 * x3)); d = 2.0 * ((x2sq) - (x1 * x3));
if(d == 0.0) return 0.0; if (d == 0.0) return 0.0;
n = ((x2sq) - (x0 * x4)) - ((x1 * x1) - (x0 * x2)) - ((x3 * x3) - (x2 * x4)); n = ((x2sq) - (x0 * x4)) - ((x1 * x1) - (x0 * x2)) - ((x3 * x3) - (x2 * x4));
@ -46,14 +46,10 @@ extern double desa2(circ_buffer_t *b, size_t i)
result = 0.5 * acos(n/d); result = 0.5 * acos(n/d);
#endif #endif
if(ISNAN(result)){ if (ISNAN(result)) result = 0.0;
result = 0.0;
}
return result; return result;
} }
#endif #endif

View File

@ -65,14 +65,12 @@ extern void compute_table(void)
acos_table_file = fopen(ACOS_TABLE_FILENAME, "w"); acos_table_file = fopen(ACOS_TABLE_FILENAME, "w");
for (i = 0; i < (1 << 25); i++) {
for(i = 0; i < (1 << 25); i++){ f = acosf(float_from_index(i));
f = acosf(float_from_index(i)); ret = fwrite(&f, sizeof(f), 1, acos_table_file);
ret = fwrite(&f, sizeof(f), 1, acos_table_file); assert(ret != 0);
assert(ret != 0);
} }
ret = fclose(acos_table_file); ret = fclose(acos_table_file);
assert(ret != EOF); assert(ret != EOF);
} }
@ -82,13 +80,13 @@ extern void init_fast_acosf(void)
{ {
int ret; int ret;
if(acos_table == NULL){ if (acos_table == NULL) {
ret = access(ACOS_TABLE_FILENAME, F_OK); ret = access(ACOS_TABLE_FILENAME, F_OK);
if(ret == 0) compute_table(); if (ret == 0) compute_table();
acos_fd = open(ACOS_TABLE_FILENAME, O_RDONLY); acos_fd = open(ACOS_TABLE_FILENAME, O_RDONLY);
if(acos_fd == -1) perror("Could not open file " ACOS_TABLE_FILENAME); if (acos_fd == -1) perror("Could not open file " ACOS_TABLE_FILENAME);
assert(acos_fd != -1); assert(acos_fd != -1);
acos_table = (float *)mmap( acos_table = (float *)mmap(
NULL, NULL,
ACOS_TABLE_LENGTH * sizeof(float), ACOS_TABLE_LENGTH * sizeof(float),
@ -136,5 +134,3 @@ static float float_from_index(uint32_t d)
#endif #endif

View File

@ -21,11 +21,11 @@ extern double goertzel(circ_buffer_t *b, size_t pos, double f, size_t num)
coeff = 2.0 * cos(2.0 * M_PI * f); coeff = 2.0 * cos(2.0 * M_PI * f);
for(i = 0; i < num; i++){ for (i = 0; i < num; i++) {
/* TODO: optimize to avoid GET_SAMPLE when possible */ /* TODO: optimize to avoid GET_SAMPLE when possible */
s = GET_SAMPLE(b, i + pos) + (coeff * p) - p2; s = GET_SAMPLE(b, i + pos) + (coeff * p) - p2;
p2 = p; p2 = p;
p = s; p = s;
} }
return (p2 * p2) + (p * p) - (coeff * p2 * p); return (p2 * p2) + (p * p) - (coeff * p2 * p);

View File

@ -128,10 +128,10 @@ static switch_bool_t avmd_callback(switch_media_bug_t * bug, void *user_data, sw
static void init_avmd_session_data(avmd_session_t *avmd_session, switch_core_session_t *fs_session); static void init_avmd_session_data(avmd_session_t *avmd_session, switch_core_session_t *fs_session);
/*! \brief The avmd session data initialization function /*! \brief The avmd session data initialization function.
* @author Eric des Courtis * @author Eric des Courtis
* @param avmd_session A reference to a avmd session * @param avmd_session A reference to a avmd session.
* @param fs_session A reference to a FreeSWITCH session * @param fs_session A reference to a FreeSWITCH session.
*/ */
static void init_avmd_session_data(avmd_session_t *avmd_session, switch_core_session_t *fs_session) static void init_avmd_session_data(avmd_session_t *avmd_session, switch_core_session_t *fs_session)
{ {
@ -159,7 +159,7 @@ static void init_avmd_session_data(avmd_session_t *avmd_session, switch_core_se
} }
/*! \brief The callback function that is called when new audio data becomes available /*! \brief The callback function that is called when new audio data becomes available.
* *
* @author Eric des Courtis * @author Eric des Courtis
* @param bug A reference to the media bug. * @param bug A reference to the media bug.
@ -203,7 +203,7 @@ static switch_bool_t avmd_callback(switch_media_bug_t * bug, void *user_data, sw
return SWITCH_TRUE; return SWITCH_TRUE;
} }
/*! \brief FreeSWITCH module loading function /*! \brief FreeSWITCH module loading function.
* *
* @author Eric des Courtis * @author Eric des Courtis
* @return Load success or failure. * @return Load success or failure.
@ -255,7 +255,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_avmd_load)
} }
/*! \brief FreeSWITCH application handler function. /*! \brief FreeSWITCH application handler function.
* This handles calls made from applications such as LUA and the dialplan * This handles calls made from applications such as LUA and the dialplan.
* *
* @author Eric des Courtis * @author Eric des Courtis
* @return Success or failure of the function. * @return Success or failure of the function.
@ -321,7 +321,7 @@ SWITCH_STANDARD_APP(avmd_start_function)
switch_channel_set_private(channel, "_avmd_", bug); switch_channel_set_private(channel, "_avmd_", bug);
} }
/*! \brief Called when the module shuts down /*! \brief Called when the module shuts down.
* *
* @author Eric des Courtis * @author Eric des Courtis
* @return The success or failure of the function. * @return The success or failure of the function.
@ -476,10 +476,10 @@ end:
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
/*! \brief Process one frame of data with avmd algorithm /*! \brief Process one frame of data with avmd algorithm.
* @author Eric des Courtis * @author Eric des Courtis
* @param session An avmd session * @param session An avmd session.
* @param frame A audio frame * @param frame An audio frame.
*/ */
static void avmd_process(avmd_session_t *session, switch_frame_t *frame) static void avmd_process(avmd_session_t *session, switch_frame_t *frame)
{ {
@ -506,9 +506,7 @@ static void avmd_process(avmd_session_t *session, switch_frame_t *frame)
b = &session->b; b = &session->b;
/*! If beep has already been detected skip the CPU heavy stuff */ /*! If beep has already been detected skip the CPU heavy stuff */
if(session->state.beep_state == BEEP_DETECTED){ if (session->state.beep_state == BEEP_DETECTED) return;
return;
}
/*! Precompute values used heavily in the inner loop */ /*! Precompute values used heavily in the inner loop */
sine_len_i = SINE_LEN(session->rate); sine_len_i = SINE_LEN(session->rate);
@ -523,12 +521,12 @@ static void avmd_process(avmd_session_t *session, switch_frame_t *frame)
//switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session->session), SWITCH_LOG_INFO, "<<< AVMD sine_len_i=%d >>>\n", sine_len_i); //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session->session), SWITCH_LOG_INFO, "<<< AVMD sine_len_i=%d >>>\n", sine_len_i);
/*! INNER LOOP -- OPTIMIZATION TARGET */ /*! INNER LOOP -- OPTIMIZATION TARGET */
for(pos = session->pos; pos < (GET_CURRENT_POS(b) - P); pos++){ for (pos = session->pos; pos < (GET_CURRENT_POS(b) - P); pos++) {
if ((pos % sine_len_i) == 0) { if ((pos % sine_len_i) == 0) {
/*! Get a desa2 frequency estimate every sine len */ /*! Get a desa2 frequency estimate every sine len */
f = desa2(b, pos); f = desa2(b, pos);
if(f < MIN_FREQUENCY_R(session->rate) || f > MAX_FREQUENCY_R(session->rate)) { if (f < MIN_FREQUENCY_R(session->rate) || f > MAX_FREQUENCY_R(session->rate)) {
v = 99999.0; v = 99999.0;
RESET_SMA_BUFFER(&session->sma_b); RESET_SMA_BUFFER(&session->sma_b);
RESET_SMA_BUFFER(&session->sqa_b); RESET_SMA_BUFFER(&session->sqa_b);
@ -543,24 +541,20 @@ static void avmd_process(avmd_session_t *session, switch_frame_t *frame)
} }
/*! If variance is less than threshold then we have detection */ /*! If variance is less than threshold then we have detection */
if(v < VARIANCE_THRESHOLD){ if (v < VARIANCE_THRESHOLD) {
switch_channel_set_variable_printf(channel, "avmd_total_time", "%d", (int)(switch_micro_time_now() - session->start_time) / 1000); switch_channel_set_variable_printf(channel, "avmd_total_time", "%d", (int)(switch_micro_time_now() - session->start_time) / 1000);
switch_channel_execute_on(channel, "execute_on_avmd_beep"); switch_channel_execute_on(channel, "execute_on_avmd_beep");
/*! Throw an event to FreeSWITCH */ /*! Throw an event to FreeSWITCH */
status = switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, AVMD_EVENT_BEEP); status = switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, AVMD_EVENT_BEEP);
if(status != SWITCH_STATUS_SUCCESS) { if (status != SWITCH_STATUS_SUCCESS) return;
return;
}
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Beep-Status", "stop"); switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Beep-Status", "stop");
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Unique-ID", switch_core_session_get_uuid(session->session)); switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Unique-ID", switch_core_session_get_uuid(session->session));
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "call-command", "avmd"); switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "call-command", "avmd");
if ((switch_event_dup(&event_copy, event)) != SWITCH_STATUS_SUCCESS) { if ((switch_event_dup(&event_copy, event)) != SWITCH_STATUS_SUCCESS) return;
return;
}
switch_core_session_queue_event(session->session, &event); switch_core_session_queue_event(session->session, &event);
switch_event_fire(&event_copy); switch_event_fire(&event_copy);