| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2005-09-15 15:44:26 +00:00
										 |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Written by Steve Underwood <steveu@coppice.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (C) 2004 Steve Underwood | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-15 15:44:26 +00:00
										 |  |  |  * See http://www.asterisk.org for more information about
 | 
					
						
							|  |  |  |  * the Asterisk project. Please do not directly contact | 
					
						
							|  |  |  |  * any of the maintainers of this project for assistance; | 
					
						
							|  |  |  |  * the project provides a web site, mailing lists and IRC | 
					
						
							|  |  |  |  * channels for your use. | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-15 15:44:26 +00:00
										 |  |  |  * This program is free software, distributed under the terms of | 
					
						
							|  |  |  |  * the GNU General Public License Version 2. See the LICENSE file | 
					
						
							|  |  |  |  * at the top of the source tree. | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This version may be optionally licenced under the GNU LGPL licence. | 
					
						
							| 
									
										
										
										
											2005-09-15 15:44:26 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2006-06-14 14:12:56 +00:00
										 |  |  |  * A license has been granted to Digium (via disclaimer) for the use of | 
					
						
							|  |  |  |  * this code. | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  | /*! \file
 | 
					
						
							| 
									
										
										
										
											2005-09-15 15:44:26 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  |  * \brief SpanDSP - a series of DSP components for telephony | 
					
						
							| 
									
										
										
										
											2005-09-15 15:44:26 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-12-30 21:18:06 +00:00
										 |  |  |  * \author Steve Underwood <steveu@coppice.org> | 
					
						
							| 
									
										
										
										
											2005-09-15 15:44:26 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-15 16:20:16 +00:00
										 |  |  | /*** MODULEINFO
 | 
					
						
							|  |  |  | 	<support_level>core</support_level> | 
					
						
							|  |  |  |  ***/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 18:54:56 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ASTERISK_FILE_VERSION(__FILE__, "$Revision$") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | #include <math.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-21 06:02:45 +00:00
										 |  |  | #include "asterisk/plc.h"
 | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if !defined(FALSE)
 | 
					
						
							|  |  |  | #define FALSE 0
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | #if !defined(TRUE)
 | 
					
						
							|  |  |  | #define TRUE (!FALSE)
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-03-20 02:53:48 +00:00
										 |  |  | #if !defined(INT16_MAX)
 | 
					
						
							|  |  |  | #define INT16_MAX	(32767)
 | 
					
						
							|  |  |  | #define INT16_MIN	(-32767-1)
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | /* We do a straight line fade to zero volume in 50ms when we are filling in for missing data. */ | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | #define ATTENUATION_INCREMENT       0.0025			      /* Attenuation per sample */
 | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-03-29 02:12:31 +00:00
										 |  |  | #define ms_to_samples(t)	    (((t)*DEFAULT_SAMPLE_RATE)/1000)
 | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static inline int16_t fsaturate(double damp) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 	if (damp > 32767.0) | 
					
						
							|  |  |  | 		return  INT16_MAX; | 
					
						
							|  |  |  | 	if (damp < -32768.0) | 
					
						
							|  |  |  | 		return  INT16_MIN; | 
					
						
							|  |  |  | 	return (int16_t) rint(damp); | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void save_history(plc_state_t *s, int16_t *buf, int len) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 	if (len >= PLC_HISTORY_LEN) { | 
					
						
							|  |  |  | 		/* Just keep the last part of the new data, starting at the beginning of the buffer */ | 
					
						
							| 
									
										
										
										
											2006-07-10 19:05:48 +00:00
										 |  |  | 		 memcpy(s->history, buf + len - PLC_HISTORY_LEN, sizeof(int16_t) * PLC_HISTORY_LEN); | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 		s->buf_ptr = 0; | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (s->buf_ptr + len > PLC_HISTORY_LEN) { | 
					
						
							|  |  |  | 		/* Wraps around - must break into two sections */ | 
					
						
							| 
									
										
										
										
											2006-07-10 19:05:48 +00:00
										 |  |  | 		memcpy(s->history + s->buf_ptr, buf, sizeof(int16_t) * (PLC_HISTORY_LEN - s->buf_ptr)); | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 		len -= (PLC_HISTORY_LEN - s->buf_ptr); | 
					
						
							|  |  |  | 		memcpy(s->history, buf + (PLC_HISTORY_LEN - s->buf_ptr), sizeof(int16_t)*len); | 
					
						
							|  |  |  | 		s->buf_ptr = len; | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	/* Can use just one section */ | 
					
						
							|  |  |  | 	memcpy(s->history + s->buf_ptr, buf, sizeof(int16_t)*len); | 
					
						
							|  |  |  | 	s->buf_ptr += len; | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | /*- End of function --------------------------------------------------------*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void normalise_history(plc_state_t *s) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 	int16_t tmp[PLC_HISTORY_LEN]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (s->buf_ptr == 0) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	memcpy(tmp, s->history, sizeof(int16_t)*s->buf_ptr); | 
					
						
							| 
									
										
										
										
											2006-07-10 19:05:48 +00:00
										 |  |  | 	memcpy(s->history, s->history + s->buf_ptr, sizeof(int16_t) * (PLC_HISTORY_LEN - s->buf_ptr)); | 
					
						
							|  |  |  | 	memcpy(s->history + PLC_HISTORY_LEN - s->buf_ptr, tmp, sizeof(int16_t) * s->buf_ptr); | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 	s->buf_ptr = 0; | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | /*- End of function --------------------------------------------------------*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int __inline__ amdf_pitch(int min_pitch, int max_pitch, int16_t amp[], int len) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 	int i; | 
					
						
							|  |  |  | 	int j; | 
					
						
							|  |  |  | 	int acc; | 
					
						
							|  |  |  | 	int min_acc; | 
					
						
							|  |  |  | 	int pitch; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pitch = min_pitch; | 
					
						
							|  |  |  | 	min_acc = INT_MAX; | 
					
						
							| 
									
										
										
										
											2006-07-10 19:05:48 +00:00
										 |  |  | 	for (i = max_pitch; i <= min_pitch; i++) { | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 		acc = 0; | 
					
						
							| 
									
										
										
										
											2006-07-10 19:05:48 +00:00
										 |  |  | 		for (j = 0; j < len; j++) | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 			acc += abs(amp[i + j] - amp[j]); | 
					
						
							|  |  |  | 		if (acc < min_acc) { | 
					
						
							|  |  |  | 			min_acc = acc; | 
					
						
							|  |  |  | 			pitch = i; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return pitch; | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | /*- End of function --------------------------------------------------------*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int plc_rx(plc_state_t *s, int16_t amp[], int len) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 	int i; | 
					
						
							|  |  |  | 	int pitch_overlap; | 
					
						
							|  |  |  | 	float old_step; | 
					
						
							|  |  |  | 	float new_step; | 
					
						
							|  |  |  | 	float old_weight; | 
					
						
							|  |  |  | 	float new_weight; | 
					
						
							|  |  |  | 	float gain; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	if (s->missing_samples) { | 
					
						
							|  |  |  | 		/* Although we have a real signal, we need to smooth it to fit well
 | 
					
						
							|  |  |  | 		with the synthetic signal we used for the previous block */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* The start of the real data is overlapped with the next 1/4 cycle
 | 
					
						
							|  |  |  | 		   of the synthetic data. */ | 
					
						
							|  |  |  | 		pitch_overlap = s->pitch >> 2; | 
					
						
							|  |  |  | 		if (pitch_overlap > len) | 
					
						
							|  |  |  | 			pitch_overlap = len; | 
					
						
							|  |  |  | 		gain = 1.0 - s->missing_samples*ATTENUATION_INCREMENT; | 
					
						
							|  |  |  | 		if (gain < 0.0) | 
					
						
							|  |  |  | 			gain = 0.0; | 
					
						
							|  |  |  | 		new_step = 1.0/pitch_overlap; | 
					
						
							|  |  |  | 		old_step = new_step*gain; | 
					
						
							|  |  |  | 		new_weight = new_step; | 
					
						
							|  |  |  | 		old_weight = (1.0 - new_step)*gain; | 
					
						
							| 
									
										
										
										
											2006-07-10 19:05:48 +00:00
										 |  |  | 		for (i = 0; i < pitch_overlap; i++) { | 
					
						
							|  |  |  | 			amp[i] = fsaturate(old_weight * s->pitchbuf[s->pitch_offset] + new_weight * amp[i]); | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 			if (++s->pitch_offset >= s->pitch) | 
					
						
							|  |  |  | 				s->pitch_offset = 0; | 
					
						
							|  |  |  | 			new_weight += new_step; | 
					
						
							|  |  |  | 			old_weight -= old_step; | 
					
						
							|  |  |  | 			if (old_weight < 0.0) | 
					
						
							|  |  |  | 				old_weight = 0.0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		s->missing_samples = 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	save_history(s, amp, len); | 
					
						
							|  |  |  | 	return len; | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | /*- End of function --------------------------------------------------------*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int plc_fillin(plc_state_t *s, int16_t amp[], int len) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 	int i; | 
					
						
							|  |  |  | 	int pitch_overlap; | 
					
						
							|  |  |  | 	float old_step; | 
					
						
							|  |  |  | 	float new_step; | 
					
						
							|  |  |  | 	float old_weight; | 
					
						
							|  |  |  | 	float new_weight; | 
					
						
							|  |  |  | 	float gain; | 
					
						
							|  |  |  | 	int orig_len; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	orig_len = len; | 
					
						
							|  |  |  | 	if (s->missing_samples == 0) { | 
					
						
							|  |  |  | 		/* As the gap in real speech starts we need to assess the last known pitch,
 | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 		and prepare the synthetic data we will use for fill-in */ | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 		normalise_history(s); | 
					
						
							|  |  |  | 		s->pitch = amdf_pitch(PLC_PITCH_MIN, PLC_PITCH_MAX, s->history + PLC_HISTORY_LEN - CORRELATION_SPAN - PLC_PITCH_MIN, CORRELATION_SPAN); | 
					
						
							|  |  |  | 		/* We overlap a 1/4 wavelength */ | 
					
						
							|  |  |  | 		pitch_overlap = s->pitch >> 2; | 
					
						
							|  |  |  | 		/* Cook up a single cycle of pitch, using a single of the real signal with 1/4
 | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 		cycle OLA'ed to make the ends join up nicely */ | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 		/* The first 3/4 of the cycle is a simple copy */ | 
					
						
							|  |  |  | 		for (i = 0;  i < s->pitch - pitch_overlap;  i++) | 
					
						
							|  |  |  | 			s->pitchbuf[i] = s->history[PLC_HISTORY_LEN - s->pitch + i]; | 
					
						
							|  |  |  | 		/* The last 1/4 of the cycle is overlapped with the end of the previous cycle */ | 
					
						
							|  |  |  | 		new_step = 1.0/pitch_overlap; | 
					
						
							|  |  |  | 		new_weight = new_step; | 
					
						
							| 
									
										
										
										
											2006-07-10 19:05:48 +00:00
										 |  |  | 		for ( ; i < s->pitch; i++) { | 
					
						
							|  |  |  | 			s->pitchbuf[i] = s->history[PLC_HISTORY_LEN - s->pitch + i] * (1.0 - new_weight) + s->history[PLC_HISTORY_LEN - 2 * s->pitch + i]*new_weight; | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 			new_weight += new_step; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		/* We should now be ready to fill in the gap with repeated, decaying cycles
 | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 		of what is in pitchbuf */ | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/* We need to OLA the first 1/4 wavelength of the synthetic data, to smooth
 | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 		it into the previous real data. To avoid the need to introduce a delay | 
					
						
							|  |  |  | 		in the stream, reverse the last 1/4 wavelength, and OLA with that. */ | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 		gain = 1.0; | 
					
						
							| 
									
										
										
										
											2006-07-10 19:05:48 +00:00
										 |  |  | 		new_step = 1.0 / pitch_overlap; | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 		old_step = new_step; | 
					
						
							|  |  |  | 		new_weight = new_step; | 
					
						
							|  |  |  | 		old_weight = 1.0 - new_step; | 
					
						
							| 
									
										
										
										
											2006-07-10 19:05:48 +00:00
										 |  |  | 		for (i = 0; i < pitch_overlap; i++) { | 
					
						
							|  |  |  | 			amp[i] = fsaturate(old_weight * s->history[PLC_HISTORY_LEN - 1 - i] + new_weight * s->pitchbuf[i]); | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 			new_weight += new_step; | 
					
						
							|  |  |  | 			old_weight -= old_step; | 
					
						
							|  |  |  | 			if (old_weight < 0.0) | 
					
						
							|  |  |  | 				old_weight = 0.0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		s->pitch_offset = i; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		gain = 1.0 - s->missing_samples*ATTENUATION_INCREMENT; | 
					
						
							|  |  |  | 		i = 0; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-07-10 19:05:48 +00:00
										 |  |  | 	for ( ; gain > 0.0 && i < len; i++) { | 
					
						
							|  |  |  | 		amp[i] = s->pitchbuf[s->pitch_offset] * gain; | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 		gain -= ATTENUATION_INCREMENT; | 
					
						
							|  |  |  | 		if (++s->pitch_offset >= s->pitch) | 
					
						
							|  |  |  | 			s->pitch_offset = 0; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-07-10 19:05:48 +00:00
										 |  |  | 	for ( ; i < len; i++) | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 		amp[i] = 0; | 
					
						
							|  |  |  | 	s->missing_samples += orig_len; | 
					
						
							|  |  |  | 	save_history(s, amp, len); | 
					
						
							|  |  |  | 	return len; | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | /*- End of function --------------------------------------------------------*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | plc_state_t *plc_init(plc_state_t *s) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2005-07-25 15:23:47 +00:00
										 |  |  | 	memset(s, 0, sizeof(*s)); | 
					
						
							|  |  |  | 	return s; | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | } | 
					
						
							|  |  |  | /*- End of function --------------------------------------------------------*/ | 
					
						
							|  |  |  | /*- End of file ------------------------------------------------------------*/ |