[svn-commits] russell: branch russell/jack r93156 - /team/russell/jack/apps/app_jack.c

SVN commits to the Digium repositories svn-commits at lists.digium.com
Sat Dec 15 14:04:35 CST 2007


Author: russell
Date: Sat Dec 15 14:04:34 2007
New Revision: 93156

URL: http://svn.digium.com/view/asterisk?view=rev&rev=93156
Log:
Allocate a resampler for the audio coming in from jack

Modified:
    team/russell/jack/apps/app_jack.c

Modified: team/russell/jack/apps/app_jack.c
URL: http://svn.digium.com/view/asterisk/team/russell/jack/apps/app_jack.c?view=diff&rev=93156&r1=93155&r2=93156
==============================================================================
--- team/russell/jack/apps/app_jack.c (original)
+++ team/russell/jack/apps/app_jack.c Sat Dec 15 14:04:34 2007
@@ -65,7 +65,9 @@
 	jack_ringbuffer_t *rb;
 	struct ast_smoother *smoother;
 	void *output_resampler;
-	double resample_factor;
+	double output_resample_factor;
+	void *input_resampler;
+	double input_resample_factor;
 	unsigned int stop:1;
 };
 
@@ -117,11 +119,56 @@
 	ast_log(LOG_NOTICE, "%s: %s\n", prefix, str->str);
 }
 
+static int alloc_resampler(struct jack_data *jack_data, int input)
+{
+	double from_srate, to_srate, jack_srate;
+	void **resampler;
+	double *resample_factor;
+
+	if (input && jack_data->input_resampler)
+		return 0;
+
+	if (!input && jack_data->output_resampler)
+		return 0;
+
+	jack_srate = jack_get_sample_rate(jack_data->client);
+
+	/* XXX Hard coded 8 kHz */
+
+	to_srate = input ? 8000.0 : jack_srate; 
+	from_srate = input ? jack_srate : 8000.0;
+
+	resample_factor = input ? &jack_data->input_resample_factor : 
+		&jack_data->output_resample_factor;
+
+	if (from_srate == to_srate) {
+		/* Awesome!  The jack sample rate is the same as ours.
+		 * Resampling isn't needed. */
+		*resample_factor = 1.0;
+		return 0;
+	}
+
+	*resample_factor = to_srate / from_srate;
+
+	resampler = input ? &jack_data->input_resampler :
+		&jack_data->output_resampler;
+
+	if (!(*resampler = resample_open(RESAMPLE_QUALITY, 
+		*resample_factor, *resample_factor))) {
+		ast_log(LOG_ERROR, "Failed to open %s resampler\n", 
+			input ? "input" : "output");
+		return -1;
+	}
+
+	return 0;
+}
+
 static int jack_process(jack_nframes_t nframes, void *arg)
 {
 	struct jack_data *jack_data = arg;
 
-	(void)(jack_data);
+	if (!jack_data->input_resample_factor)
+		alloc_resampler(jack_data, 1);
 
 	/* XXX Here we need to attempt to read nframes number of samples from the
 	 * ringbuffer and write it to the output port.  Then, we need to read nframes
@@ -169,6 +216,11 @@
 	if (jack_data->output_resampler) {
 		resample_close(jack_data->output_resampler);
 		jack_data->output_resampler = NULL;
+	}
+	
+	if (jack_data->input_resampler) {
+		resample_close(jack_data->input_resampler);
+		jack_data->input_resampler = NULL;
 	}
 }
 
@@ -240,29 +292,8 @@
 	uint16_t *s_buf = f->data;
 	jack_ringbuffer_data_t write_vector[2];
 
-	do {
-		double from_srate, to_srate;
-
-		if (jack_data->output_resampler)
-			break;
-
-		from_srate = 8000.0; /* XXX Hard coded 8 kHz */
-		to_srate = jack_get_sample_rate(jack_data->client);
-	
-		if (from_srate == to_srate) {
-			/* Awesome!  The jack sample rate is the same as ours.
-			 * Resampling isn't needed. */
-			break;
-		}
-
-		jack_data->resample_factor = to_srate / from_srate;
-
-		if (!(jack_data->output_resampler = resample_open(RESAMPLE_QUALITY, 
-			jack_data->resample_factor, jack_data->resample_factor))) {
-			ast_log(LOG_ERROR, "Failed to open output_resampler\n");
-			return -1;
-		}
-	} while (0);
+	if (!jack_data->output_resample_factor)
+		alloc_resampler(jack_data, 0);
 
 	if (jack_data->output_resampler) {
 		float in_buf[f->samples];
@@ -276,7 +307,8 @@
 		while (in_buf_used < sizeof(in_buf)) {
 			int res;
 
-			res = resample_process(jack_data->output_resampler, jack_data->resample_factor,
+			res = resample_process(jack_data->output_resampler, 
+				jack_data->output_resample_factor,
 				&in_buf[in_buf_used / sizeof(in_buf[0])], sizeof(in_buf) - in_buf_used, 
 				0, &in_buf_used, 
 				&f_buf[out_buf_used / sizeof(f_buf[0])], sizeof(f_buf) - out_buf_used);
@@ -292,7 +324,7 @@
 			}
 		}
 
-		f_buf_used = sizeof(f_buf[0]) * jack_data->resample_factor;
+		f_buf_used = sizeof(f_buf[0]) * jack_data->output_resample_factor;
 		if (f_buf_used > sizeof(f_buf))
 			f_buf_used = sizeof(f_buf);
 	} else {




More information about the svn-commits mailing list