add channels param the the silence generator function

This commit is contained in:
Anthony Minessale 2014-06-13 04:49:37 +05:00
parent 09ae3a9841
commit 579a051867
10 changed files with 34 additions and 46 deletions

View File

@ -152,7 +152,7 @@ SWITCH_DECLARE(void) switch_swap_linear(int16_t *buf, int len);
\param samples the number of 2 byte samples \param samples the number of 2 byte samples
\param divisor the volume factor \param divisor the volume factor
*/ */
SWITCH_DECLARE(void) switch_generate_sln_silence(int16_t *data, uint32_t samples, uint32_t divisor); SWITCH_DECLARE(void) switch_generate_sln_silence(int16_t *data, uint32_t samples, uint32_t channels, uint32_t divisor);
/*! /*!
\brief Change the volume of a signed linear audio frame \brief Change the volume of a signed linear audio frame

View File

@ -4425,7 +4425,7 @@ static void conference_loop_output(conference_member_t *member)
} else { } else {
if (member->conference->comfort_noise_level) { if (member->conference->comfort_noise_level) {
switch_generate_sln_silence(write_frame.data, samples, member->conference->comfort_noise_level); switch_generate_sln_silence(write_frame.data, samples, member->conference->channels, member->conference->comfort_noise_level);
} else { } else {
memset(write_frame.data, 255, bytes); memset(write_frame.data, 255, bytes);
} }

View File

@ -199,7 +199,7 @@ SWITCH_MODULE_RUNTIME_FUNCTION(mod_random_runtime)
int i = 0; int i = 0;
int len = sizeof(data) / 2; int len = sizeof(data) / 2;
switch_generate_sln_silence(data, len, 1); switch_generate_sln_silence(data, len, 1, 1);
random_add_entropy(rfd, data, len); random_add_entropy(rfd, data, len);
while(i < len && !data[i]) i++; while(i < len && !data[i]) i++;

View File

@ -94,7 +94,7 @@ static switch_status_t silence_stream_file_read(switch_file_handle_t *handle, vo
sh->samples -= (int32_t)*len; sh->samples -= (int32_t)*len;
} }
switch_generate_sln_silence((int16_t *) data, (uint32_t)*len, switch_generate_sln_silence((int16_t *) data, (uint32_t)*len, handle->channels,
sh->silence ? sh->silence : (uint32_t)-1); sh->silence ? sh->silence : (uint32_t)-1);
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;

View File

@ -142,6 +142,8 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_sleep(switch_core_session_t *session,
arg_recursion_check_start(args); arg_recursion_check_start(args);
switch_core_session_get_read_impl(session, &imp);
/* /*
if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_INBOUND && !switch_channel_test_flag(channel, CF_PROXY_MODE) && if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_INBOUND && !switch_channel_test_flag(channel, CF_PROXY_MODE) &&
!switch_channel_media_ready(channel) && !switch_channel_test_flag(channel, CF_SERVICE)) { !switch_channel_media_ready(channel) && !switch_channel_test_flag(channel, CF_SERVICE)) {
@ -171,7 +173,6 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_sleep(switch_core_session_t *session,
} }
if (ms > 10 && sval) { if (ms > 10 && sval) {
switch_core_session_get_read_impl(session, &imp);
if (switch_core_codec_init(&codec, if (switch_core_codec_init(&codec,
"L16", "L16",
@ -299,7 +300,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_sleep(switch_core_session_t *session,
} }
if (sval && write_frame.datalen) { if (sval && write_frame.datalen) {
switch_generate_sln_silence((int16_t *) write_frame.data, write_frame.samples, sval); switch_generate_sln_silence((int16_t *) write_frame.data, write_frame.samples, imp.number_of_channels, sval);
switch_core_session_write_frame(session, &write_frame, SWITCH_IO_FLAG_NONE, 0); switch_core_session_write_frame(session, &write_frame, SWITCH_IO_FLAG_NONE, 0);
} else { } else {
switch_core_session_write_frame(session, &cng_frame, SWITCH_IO_FLAG_NONE, 0); switch_core_session_write_frame(session, &cng_frame, SWITCH_IO_FLAG_NONE, 0);
@ -992,7 +993,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_park(switch_core_session_t *session,
} }
if (rate && write_frame.data && sval) { if (rate && write_frame.data && sval) {
switch_generate_sln_silence((int16_t *) write_frame.data, write_frame.samples, sval); switch_generate_sln_silence((int16_t *) write_frame.data, write_frame.samples, read_impl.number_of_channels, sval);
switch_core_session_write_frame(session, &write_frame, SWITCH_IO_FLAG_NONE, 0); switch_core_session_write_frame(session, &write_frame, SWITCH_IO_FLAG_NONE, 0);
} }
@ -1409,7 +1410,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_collect_digits_count(switch_core_sess
} }
if (write_frame.data) { if (write_frame.data) {
switch_generate_sln_silence((int16_t *) write_frame.data, write_frame.samples, sval); switch_generate_sln_silence((int16_t *) write_frame.data, write_frame.samples, imp.number_of_channels, sval);
switch_core_session_write_frame(session, &write_frame, SWITCH_IO_FLAG_NONE, 0); switch_core_session_write_frame(session, &write_frame, SWITCH_IO_FLAG_NONE, 0);
} }

View File

@ -2531,6 +2531,10 @@ static switch_bool_t session_audio_callback(switch_media_bug_t *bug, void *user_
switch_session_audio_t *pvt = (switch_session_audio_t *) user_data; switch_session_audio_t *pvt = (switch_session_audio_t *) user_data;
switch_frame_t *frame = NULL; switch_frame_t *frame = NULL;
int level = 0, mute = 0; int level = 0, mute = 0;
switch_core_session_t *session = switch_core_media_bug_get_session(bug);
switch_codec_implementation_t read_impl = { 0 };
switch_core_session_get_read_impl(session, &read_impl);
if (type == SWITCH_ABC_TYPE_READ_REPLACE || type == SWITCH_ABC_TYPE_WRITE_REPLACE) { if (type == SWITCH_ABC_TYPE_READ_REPLACE || type == SWITCH_ABC_TYPE_WRITE_REPLACE) {
@ -2553,7 +2557,7 @@ static switch_bool_t session_audio_callback(switch_media_bug_t *bug, void *user_
if (frame) { if (frame) {
if (mute) { if (mute) {
if (mute > 1) { if (mute > 1) {
switch_generate_sln_silence(frame->data, frame->datalen / 2, mute); switch_generate_sln_silence(frame->data, frame->datalen / 2, read_impl.number_of_channels, mute);
} else { } else {
memset(frame->data, 0, frame->datalen); memset(frame->data, 0, frame->datalen);
} }

View File

@ -562,7 +562,8 @@ static void *audio_bridge_thread(switch_thread_t *thread, void *obj)
read_frame_count++; read_frame_count++;
if (switch_test_flag(read_frame, SFF_CNG)) { if (switch_test_flag(read_frame, SFF_CNG)) {
if (silence_val) { if (silence_val) {
switch_generate_sln_silence((int16_t *) silence_frame.data, silence_frame.samples, silence_val); switch_generate_sln_silence((int16_t *) silence_frame.data, silence_frame.samples,
read_frame->codec->implementation->number_of_channels, silence_val);
read_frame = &silence_frame; read_frame = &silence_frame;
} else if (!switch_channel_test_flag(chan_b, CF_ACCEPT_CNG)) { } else if (!switch_channel_test_flag(chan_b, CF_ACCEPT_CNG)) {
continue; continue;

View File

@ -1084,7 +1084,8 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_wait_for_answer(switch_core_session_t
} }
} else if (ringback.silence) { } else if (ringback.silence) {
write_frame.datalen = write_frame.codec->implementation->decoded_bytes_per_packet; write_frame.datalen = write_frame.codec->implementation->decoded_bytes_per_packet;
switch_generate_sln_silence((int16_t *) write_frame.data, write_frame.datalen / 2, ringback.silence); switch_generate_sln_silence((int16_t *) write_frame.data, write_frame.datalen / 2,
write_frame.codec->implementation->number_of_channels, ringback.silence);
} }
if ((ringback.fh || ringback.silence || ringback.audio_buffer) && write_frame.codec && write_frame.datalen) { if ((ringback.fh || ringback.silence || ringback.audio_buffer) && write_frame.codec && write_frame.datalen) {
@ -3213,7 +3214,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_originate(switch_core_session_t *sess
if ((ringback.fh || silence || ringback.audio_buffer || oglobals.bridge_early_media > -1) && write_frame.codec && write_frame.datalen) { if ((ringback.fh || silence || ringback.audio_buffer || oglobals.bridge_early_media > -1) && write_frame.codec && write_frame.datalen) {
if (silence) { if (silence) {
write_frame.datalen = read_impl.decoded_bytes_per_packet; write_frame.datalen = read_impl.decoded_bytes_per_packet;
switch_generate_sln_silence((int16_t *) write_frame.data, write_frame.datalen / 2, silence); switch_generate_sln_silence((int16_t *) write_frame.data, write_frame.datalen / 2, write_frame.codec->implementation->number_of_channels, silence);
} }
if (switch_core_session_write_frame(oglobals.session, &write_frame, SWITCH_IO_FLAG_NONE, 0) != SWITCH_STATUS_SUCCESS) { if (switch_core_session_write_frame(oglobals.session, &write_frame, SWITCH_IO_FLAG_NONE, 0) != SWITCH_STATUS_SUCCESS) {

View File

@ -713,9 +713,8 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_record_file(switch_core_session_t *se
double energy = 0; double energy = 0;
for (count = 0; count < samples; count++) { for (count = 0; count < samples * read_impl.number_of_channels; count++) {
energy += abs(fdata[j]); energy += abs(fdata[j++]);
j += read_impl.number_of_channels;
} }
score = (uint32_t) (energy / (samples / divisor)); score = (uint32_t) (energy / (samples / divisor));
@ -730,9 +729,9 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_record_file(switch_core_session_t *se
} }
if (fill_cng) { if (fill_cng) {
switch_generate_sln_silence((int16_t *) write_frame.data, write_frame.samples, fill_cng); switch_generate_sln_silence((int16_t *) write_frame.data, write_frame.samples, read_impl.number_of_channels, fill_cng);
} else if (waste_resources) { } else if (waste_resources) {
switch_generate_sln_silence((int16_t *) write_frame.data, write_frame.samples, waste_resources); switch_generate_sln_silence((int16_t *) write_frame.data, write_frame.samples, read_impl.number_of_channels, waste_resources);
} }
if (!switch_test_flag(fh, SWITCH_FILE_PAUSE) && !switch_test_flag(read_frame, SFF_CNG)) { if (!switch_test_flag(fh, SWITCH_FILE_PAUSE) && !switch_test_flag(read_frame, SFF_CNG)) {

View File

@ -181,14 +181,16 @@ SWITCH_DECLARE(void) switch_swap_linear(int16_t *buf, int len)
} }
} }
#if 1
SWITCH_DECLARE(void) switch_generate_sln_silence(int16_t *data, uint32_t samples, uint32_t divisor) SWITCH_DECLARE(void) switch_generate_sln_silence(int16_t *data, uint32_t samples, uint32_t channels, uint32_t divisor)
{ {
int16_t x; int16_t s;
uint32_t i; uint32_t x, i, j;
int sum_rnd = 0; int sum_rnd = 0;
int16_t rnd2 = (int16_t) switch_micro_time_now() + (int16_t) (intptr_t) data; int16_t rnd2 = (int16_t) switch_micro_time_now() + (int16_t) (intptr_t) data;
if (channels == 0) channels = 1;
assert(divisor); assert(divisor);
if (divisor == (uint32_t)-1) { if (divisor == (uint32_t)-1) {
@ -201,38 +203,18 @@ SWITCH_DECLARE(void) switch_generate_sln_silence(int16_t *data, uint32_t samples
rnd2 = rnd2 * 31821U + 13849U; rnd2 = rnd2 * 31821U + 13849U;
sum_rnd += rnd2; sum_rnd += rnd2;
} }
//switch_normalize_to_16bit(sum_rnd);
*data = (int16_t) ((int16_t) sum_rnd / (int) divisor);
data++; s = (int16_t) ((int16_t) sum_rnd / (int) divisor);
}
}
#else
SWITCH_DECLARE(void) switch_generate_sln_silence(int16_t *data, uint32_t samples, uint32_t divisor) for (j = 0; j < channels; j++) {
{ *data = s;
int16_t rnd = 0, rnd2, x; data++;
uint32_t i;
int sum_rnd = 0;
assert(divisor);
rnd2 = (int16_t) (intptr_t) (&data + switch_epoch_time_now(NULL));
for (i = 0; i < samples; i++, sum_rnd = 0) {
for (x = 0; x < 10; x++) {
rnd = rnd + (int16_t) ((x + i) * rnd2);
sum_rnd += rnd;
} }
switch_normalize_to_16bit(sum_rnd);
*data = (int16_t) ((int16_t) sum_rnd / (int) divisor);
data++;
} }
} }
#endif
SWITCH_DECLARE(uint32_t) switch_merge_sln(int16_t *data, uint32_t samples, int16_t *other_data, uint32_t other_samples) SWITCH_DECLARE(uint32_t) switch_merge_sln(int16_t *data, uint32_t samples, int16_t *other_data, uint32_t other_samples)
{ {
int i; int i;