[asterisk-commits] dvossel: branch dvossel/funk_effects r248531 - /team/dvossel/funk_effects/funcs/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Feb 23 20:59:10 CST 2010


Author: dvossel
Date: Tue Feb 23 20:59:08 2010
New Revision: 248531

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=248531
Log:
adds some error checking to function input

Modified:
    team/dvossel/funk_effects/funcs/func_pitchshift.c

Modified: team/dvossel/funk_effects/funcs/func_pitchshift.c
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/funk_effects/funcs/func_pitchshift.c?view=diff&rev=248531&r1=248530&r2=248531
==============================================================================
--- team/dvossel/funk_effects/funcs/func_pitchshift.c (original)
+++ team/dvossel/funk_effects/funcs/func_pitchshift.c Tue Feb 23 20:59:08 2010
@@ -32,10 +32,10 @@
 *
 * DESCRIPTION: The routine takes a pitchShift factor value which is between 0.5
 * (one octave down) and 2. (one octave up). A value of exactly 1 does not change
-* the pitch. numSampsToProcess tells the routine how many samples in indata[0...
-* numSampsToProcess-1] should be pitch shifted and moved to outdata[0 ...
-* numSampsToProcess-1]. The two buffers can be identical (ie. it can process the
-* data in-place). fftFrameSize defines the FFT frame size used for the
+* the pitch. num_samps_to_process tells the routine how many samples in indata[0...
+* num_samps_to_process-1] should be pitch shifted and moved to outdata[0 ...
+* num_samps_to_process-1]. The two buffers can be identical (ie. it can process the
+* data in-place). fft_frame_size defines the FFT frame size used for the
 * processing. Typical values are 1024, 2048 and 4096. It may be any value <=
 * MAX_FRAME_LENGTH but it MUST be a power of 2. osamp is the STFT
 * oversampling factor which also determines the overlap between adjacent STFT
@@ -87,8 +87,6 @@
 			<para>exten => 1,1,Set(PITCH_SHIFT(rx)=0.8) ; lowers pitch</para>
 			<para>exten => 2,1,Set(PITCH_SHIFT(tx)=1.5) ; raises pitch</para>
 		</description>
-
-
 	</function>
  ***/
 
@@ -117,8 +115,8 @@
 	struct fft_data tx;
 };
 
-static void smb_fft(float *fftBuffer, long fftFrameSize, long sign);
-static void smb_pitch_shift(float pitchShift, long numSampsToProcess, long fftFrameSize, long osamp, float sample_rate, int16_t *indata, int16_t *outdata, struct fft_data *fft_data);
+static void smb_fft(float *fftBuffer, long fft_frame_size, long sign);
+static void smb_pitch_shift(float pitchShift, long num_samps_to_process, long fft_frame_size, long osamp, float sample_rate, int16_t *indata, int16_t *outdata, struct fft_data *fft_data);
 static int pitch_shift(struct ast_frame *f, float amount, struct fft_data *fft_data);
 
 static void destroy_callback(void *data)
@@ -140,7 +138,9 @@
 	struct pitchshift_data *shift = NULL;
 
 	if ((audiohook->status == AST_AUDIOHOOK_STATUS_DONE) ||
-		(f->frametype != AST_FRAME_VOICE)) {
+		(f->frametype != AST_FRAME_VOICE) ||
+		((f->subclass.codec != AST_FORMAT_SLINEAR) &&
+		(f->subclass.codec != AST_FORMAT_SLINEAR16))) {
 		return -1;
 	}
 
@@ -187,7 +187,7 @@
 		shift = datastore->data;
 	}
 
-	if (!sscanf(value, "%30f", &amount)) {
+	if (!sscanf(value, "%30f", &amount) || amount <= 0) {
 		goto cleanup_error;
 	}
 
@@ -218,14 +218,14 @@
 	return -1;
 }
 
-static void smb_fft(float *fftBuffer, long fftFrameSize, long sign)
+static void smb_fft(float *fftBuffer, long fft_frame_size, long sign)
 {
 	float wr, wi, arg, *p1, *p2, temp;
 	float tr, ti, ur, ui, *p1r, *p1i, *p2r, *p2i;
 	long i, bitm, j, le, le2, k;
 
-	for (i = 2; i < 2*fftFrameSize-2; i += 2) {
-		for (bitm = 2, j = 0; bitm < 2*fftFrameSize; bitm <<= 1) {
+	for (i = 2; i < 2*fft_frame_size-2; i += 2) {
+		for (bitm = 2, j = 0; bitm < 2*fft_frame_size; bitm <<= 1) {
 			if (i & bitm) j++;
 			j <<= 1;
 		}
@@ -236,7 +236,7 @@
 			*p1 = *p2; *p2 = temp;
 		}
 	}
-	for (k = 0, le = 2; k < (long)(log(fftFrameSize)/log(2.)+.5); k++) {
+	for (k = 0, le = 2; k < (long)(log(fft_frame_size)/log(2.)+.5); k++) {
 		le <<= 1;
 		le2 = le>>1;
 		ur = 1.0;
@@ -247,7 +247,7 @@
 		for (j = 0; j < le2; j += 2) {
 			p1r = fftBuffer+j; p1i = p1r+1;
 			p2r = p1r+le2; p2i = p2r+1;
-			for (i = j; i < 2*fftFrameSize; i += le) {
+			for (i = j; i < 2*fft_frame_size; i += le) {
 				tr = *p2r * ur - *p2i * ui;
 				ti = *p2r * ui + *p2i * ur;
 				*p2r = *p1r - tr; *p2i = *p1i - ti;
@@ -262,7 +262,7 @@
 	}
 }
 
-static void smb_pitch_shift(float pitchShift, long numSampsToProcess, long fftFrameSize, long osamp, float sample_rate, int16_t *indata, int16_t *outdata, struct fft_data *fft_data)
+static void smb_pitch_shift(float pitchShift, long num_samps_to_process, long fft_frame_size, long osamp, float sample_rate, int16_t *indata, int16_t *outdata, struct fft_data *fft_data)
 {
 	float *in_fifo = fft_data->in_fifo;
 	float *out_fifo = fft_data->out_fifo;
@@ -280,15 +280,15 @@
 	long i,k, qpd, index, in_fifo_latency, step_size, fft_frame_size2;
 
 	/* set up some handy variables */
-	fft_frame_size2 = fftFrameSize/2;
-	step_size = fftFrameSize/osamp;
-	freq_per_bin = sample_rate/(double)fftFrameSize;
-	expct = 2.*M_PI*(double)step_size/(double)fftFrameSize;
-	in_fifo_latency = fftFrameSize-step_size;
+	fft_frame_size2 = fft_frame_size/2;
+	step_size = fft_frame_size/osamp;
+	freq_per_bin = sample_rate/(double)fft_frame_size;
+	expct = 2.*M_PI*(double)step_size/(double)fft_frame_size;
+	in_fifo_latency = fft_frame_size-step_size;
 	if (fft_data->gRover == 0) fft_data->gRover = in_fifo_latency;
 
 	/* main processing loop */
-	for (i = 0; i < numSampsToProcess; i++){
+	for (i = 0; i < num_samps_to_process; i++){
 
 		/* As long as we have not yet collected enough data just read in */
 		in_fifo[fft_data->gRover] = indata[i];
@@ -296,19 +296,19 @@
 		fft_data->gRover++;
 
 		/* now we have enough data for processing */
-		if (fft_data->gRover >= fftFrameSize) {
+		if (fft_data->gRover >= fft_frame_size) {
 			fft_data->gRover = in_fifo_latency;
 
 			/* do windowing and re,im interleave */
-			for (k = 0; k < fftFrameSize;k++) {
-				window = -.5*cos(2.*M_PI*(double)k/(double)fftFrameSize)+.5;
+			for (k = 0; k < fft_frame_size;k++) {
+				window = -.5*cos(2.*M_PI*(double)k/(double)fft_frame_size)+.5;
 				fft_worksp[2*k] = in_fifo[k] * window;
 				fft_worksp[2*k+1] = 0.;
 			}
 
 			/* ***************** ANALYSIS ******************* */
 			/* do transform */
-			smb_fft(fft_worksp, fftFrameSize, -1);
+			smb_fft(fft_worksp, fft_frame_size, -1);
 
 			/* this is the analysis step */
 			for (k = 0; k <= fft_frame_size2; k++) {
@@ -348,8 +348,8 @@
 
 			/* ***************** PROCESSING ******************* */
 			/* this does the actual pitch shifting */
-			memset(sys_magn, 0, fftFrameSize*sizeof(float));
-			memset(syn_freq, 0, fftFrameSize*sizeof(float));
+			memset(sys_magn, 0, fft_frame_size*sizeof(float));
+			memset(syn_freq, 0, fft_frame_size*sizeof(float));
 			for (k = 0; k <= fft_frame_size2; k++) {
 				index = k*pitchShift;
 				if (index <= fft_frame_size2) {
@@ -388,20 +388,20 @@
 			}
 
 			/* zero negative frequencies */
-			for (k = fftFrameSize+2; k < 2*fftFrameSize; k++) fft_worksp[k] = 0.;
+			for (k = fft_frame_size+2; k < 2*fft_frame_size; k++) fft_worksp[k] = 0.;
 
 			/* do inverse transform */
-			smb_fft(fft_worksp, fftFrameSize, 1);
+			smb_fft(fft_worksp, fft_frame_size, 1);
 
 			/* do windowing and add to output accumulator */
-			for(k=0; k < fftFrameSize; k++) {
-				window = -.5*cos(2.*M_PI*(double)k/(double)fftFrameSize)+.5;
+			for(k=0; k < fft_frame_size; k++) {
+				window = -.5*cos(2.*M_PI*(double)k/(double)fft_frame_size)+.5;
 				output_accum[k] += 2.*window*fft_worksp[2*k]/(fft_frame_size2*osamp);
 			}
 			for (k = 0; k < step_size; k++) out_fifo[k] = output_accum[k];
 
 			/* shift accumulator */
-			memmove(output_accum, output_accum+step_size, fftFrameSize*sizeof(float));
+			memmove(output_accum, output_accum+step_size, fft_frame_size*sizeof(float));
 
 			/* move input FIFO */
 			for (k = 0; k < in_fifo_latency; k++) in_fifo[k] = in_fifo[k+step_size];
@@ -413,8 +413,13 @@
 {
 	int16_t *fun = (int16_t *) f->data.ptr;
 	int samples;
-		for (samples = 0; samples < f->samples; samples += 32) {
-		smb_pitch_shift(amount, 32, 128, 32, 8000, fun+samples, fun+samples, fft);
+
+	/* an amount of 1 has no effect */
+	if (amount == 1) {
+		return 0;
+	}
+	for (samples = 0; samples < f->samples; samples += 32) {
+		smb_pitch_shift(amount, 32, 128, 32, ast_format_rate(f->subclass.codec), fun+samples, fun+samples, fft);
 	}
 
 	return 0;




More information about the asterisk-commits mailing list