Asterisk - The Open Source Telephony Project  18.5.0
func_pitchshift.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2010, Digium, Inc.
5  *
6  * David Vossel <[email protected]>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*! \file
20  *
21  * \brief Pitch Shift Audio Effect
22  *
23  * \author David Vossel <[email protected]>
24  *
25  * \ingroup functions
26  */
27 
28 /************************* SMB FUNCTION LICENSE *********************************
29 *
30 * SYNOPSIS: Routine for doing pitch shifting while maintaining
31 * duration using the Short Time Fourier Transform.
32 *
33 * DESCRIPTION: The routine takes a pitchShift factor value which is between 0.5
34 * (one octave down) and 2. (one octave up). A value of exactly 1 does not change
35 * the pitch. num_samps_to_process tells the routine how many samples in indata[0...
36 * num_samps_to_process-1] should be pitch shifted and moved to outdata[0 ...
37 * num_samps_to_process-1]. The two buffers can be identical (ie. it can process the
38 * data in-place). fft_frame_size defines the FFT frame size used for the
39 * processing. Typical values are 1024, 2048 and 4096. It may be any value <=
40 * MAX_FRAME_LENGTH but it MUST be a power of 2. osamp is the STFT
41 * oversampling factor which also determines the overlap between adjacent STFT
42 * frames. It should at least be 4 for moderate scaling ratios. A value of 32 is
43 * recommended for best quality. sampleRate takes the sample rate for the signal
44 * in unit Hz, ie. 44100 for 44.1 kHz audio. The data passed to the routine in
45 * indata[] should be in the range [-1.0, 1.0), which is also the output range
46 * for the data, make sure you scale the data accordingly (for 16bit signed integers
47 * you would have to divide (and multiply) by 32768).
48 *
49 * COPYRIGHT 1999-2009 Stephan M. Bernsee <smb [AT] dspdimension [DOT] com>
50 *
51 * The Wide Open License (WOL)
52 *
53 * Permission to use, copy, modify, distribute and sell this software and its
54 * documentation for any purpose is hereby granted without fee, provided that
55 * the above copyright notice and this license appear in all source copies.
56 * THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF
57 * ANY KIND. See http://www.dspguru.com/wol.htm for more information.
58 *
59 *****************************************************************************/
60 
61 /*** MODULEINFO
62  <support_level>extended</support_level>
63  ***/
64 
65 #include "asterisk.h"
66 
67 #include "asterisk/module.h"
68 #include "asterisk/channel.h"
69 #include "asterisk/pbx.h"
70 #include "asterisk/utils.h"
71 #include "asterisk/audiohook.h"
72 #include <math.h>
73 
74 /*** DOCUMENTATION
75  <function name="PITCH_SHIFT" language="en_US">
76  <synopsis>
77  Pitch shift both tx and rx audio streams on a channel.
78  </synopsis>
79  <syntax>
80  <parameter name="channel direction" required="true">
81  <para>Direction can be either <literal>rx</literal>, <literal>tx</literal>, or
82  <literal>both</literal>. The direction can either be set to a valid floating
83  point number between 0.1 and 4.0 or one of the enum values listed below. A value
84  of 1.0 has no effect. Greater than 1 raises the pitch. Lower than 1 lowers
85  the pitch.</para>
86 
87  <para>The pitch amount can also be set by the following values</para>
88  <enumlist>
89  <enum name = "highest" />
90  <enum name = "higher" />
91  <enum name = "high" />
92  <enum name = "low" />
93  <enum name = "lower" />
94  <enum name = "lowest" />
95  </enumlist>
96  </parameter>
97  </syntax>
98  <description>
99  <para>Examples:</para>
100  <para>exten => 1,1,Set(PITCH_SHIFT(tx)=highest); raises pitch an octave </para>
101  <para>exten => 1,1,Set(PITCH_SHIFT(rx)=higher) ; raises pitch more </para>
102  <para>exten => 1,1,Set(PITCH_SHIFT(both)=high) ; raises pitch </para>
103  <para>exten => 1,1,Set(PITCH_SHIFT(rx)=low) ; lowers pitch </para>
104  <para>exten => 1,1,Set(PITCH_SHIFT(tx)=lower) ; lowers pitch more </para>
105  <para>exten => 1,1,Set(PITCH_SHIFT(both)=lowest) ; lowers pitch an octave </para>
106 
107  <para>exten => 1,1,Set(PITCH_SHIFT(rx)=0.8) ; lowers pitch </para>
108  <para>exten => 1,1,Set(PITCH_SHIFT(tx)=1.5) ; raises pitch </para>
109  </description>
110  </function>
111  ***/
112 
113 #ifndef M_PI
114 #define M_PI 3.14159265358979323846
115 #endif
116 #define MAX_FRAME_LENGTH 256
117 
118 #define HIGHEST 2
119 #define HIGHER 1.5
120 #define HIGH 1.25
121 #define LOW .85
122 #define LOWER .7
123 #define LOWEST .5
124 
125 struct fft_data {
136  long gRover;
138 };
139 
141  struct ast_audiohook audiohook;
142 
143  struct fft_data rx;
144  struct fft_data tx;
145 };
146 
147 static void smb_fft(float *fft_buffer, long fft_frame_size, long sign);
148 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);
149 static int pitch_shift(struct ast_frame *f, float amount, struct fft_data *fft_data);
150 
151 static void destroy_callback(void *data)
152 {
153  struct pitchshift_data *shift = data;
154 
156  ast_free(shift);
157 };
158 
160  .type = "pitchshift",
161  .destroy = destroy_callback
162 };
163 
164 static int pitchshift_cb(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *f, enum ast_audiohook_direction direction)
165 {
166  struct ast_datastore *datastore = NULL;
167  struct pitchshift_data *shift = NULL;
168 
169 
170  if (!f) {
171  return 0;
172  }
173  if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE) {
174  return -1;
175  }
176 
177  if (!(datastore = ast_channel_datastore_find(chan, &pitchshift_datastore, NULL))) {
178  return -1;
179  }
180 
181  shift = datastore->data;
182 
183  if (direction == AST_AUDIOHOOK_DIRECTION_WRITE) {
184  pitch_shift(f, shift->tx.shift_amount, &shift->tx);
185  } else {
186  pitch_shift(f, shift->rx.shift_amount, &shift->rx);
187  }
188 
189  return 0;
190 }
191 
192 static int pitchshift_helper(struct ast_channel *chan, const char *cmd, char *data, const char *value)
193 {
194  struct ast_datastore *datastore = NULL;
195  struct pitchshift_data *shift = NULL;
196  int new = 0;
197  float amount = 0;
198 
199  if (!chan) {
200  ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
201  return -1;
202  }
203 
204  ast_channel_lock(chan);
205  if (!(datastore = ast_channel_datastore_find(chan, &pitchshift_datastore, NULL))) {
206  ast_channel_unlock(chan);
207 
208  if (!(datastore = ast_datastore_alloc(&pitchshift_datastore, NULL))) {
209  return 0;
210  }
211  if (!(shift = ast_calloc(1, sizeof(*shift)))) {
212  ast_datastore_free(datastore);
213  return 0;
214  }
215 
218  datastore->data = shift;
219  new = 1;
220  } else {
221  ast_channel_unlock(chan);
222  shift = datastore->data;
223  }
224 
225 
226  if (!strcasecmp(value, "highest")) {
227  amount = HIGHEST;
228  } else if (!strcasecmp(value, "higher")) {
229  amount = HIGHER;
230  } else if (!strcasecmp(value, "high")) {
231  amount = HIGH;
232  } else if (!strcasecmp(value, "lowest")) {
233  amount = LOWEST;
234  } else if (!strcasecmp(value, "lower")) {
235  amount = LOWER;
236  } else if (!strcasecmp(value, "low")) {
237  amount = LOW;
238  } else {
239  if (!sscanf(value, "%30f", &amount) || (amount <= 0) || (amount > 4)) {
240  goto cleanup_error;
241  }
242  }
243 
244  if (!strcasecmp(data, "rx")) {
245  shift->rx.shift_amount = amount;
246  } else if (!strcasecmp(data, "tx")) {
247  shift->tx.shift_amount = amount;
248  } else if (!strcasecmp(data, "both")) {
249  shift->rx.shift_amount = amount;
250  shift->tx.shift_amount = amount;
251  } else {
252  goto cleanup_error;
253  }
254 
255  if (new) {
256  ast_channel_lock(chan);
257  ast_channel_datastore_add(chan, datastore);
258  ast_channel_unlock(chan);
259  ast_audiohook_attach(chan, &shift->audiohook);
260  }
261 
262  return 0;
263 
264 cleanup_error:
265 
266  ast_log(LOG_ERROR, "Invalid argument provided to the %s function\n", cmd);
267  if (new) {
268  ast_datastore_free(datastore);
269  }
270  return -1;
271 }
272 
273 static void smb_fft(float *fft_buffer, long fft_frame_size, long sign)
274 {
275  float wr, wi, arg, *p1, *p2, temp;
276  float tr, ti, ur, ui, *p1r, *p1i, *p2r, *p2i;
277  long i, bitm, j, le, le2, k;
278 
279  for (i = 2; i < 2 * fft_frame_size - 2; i += 2) {
280  for (bitm = 2, j = 0; bitm < 2 * fft_frame_size; bitm <<= 1) {
281  if (i & bitm) {
282  j++;
283  }
284  j <<= 1;
285  }
286  if (i < j) {
287  p1 = fft_buffer + i; p2 = fft_buffer + j;
288  temp = *p1; *(p1++) = *p2;
289  *(p2++) = temp; temp = *p1;
290  *p1 = *p2; *p2 = temp;
291  }
292  }
293  for (k = 0, le = 2; k < (long) (log(fft_frame_size) / log(2.) + .5); k++) {
294  le <<= 1;
295  le2 = le>>1;
296  ur = 1.0;
297  ui = 0.0;
298  arg = M_PI / (le2>>1);
299  wr = cos(arg);
300  wi = sign * sin(arg);
301  for (j = 0; j < le2; j += 2) {
302  p1r = fft_buffer+j; p1i = p1r + 1;
303  p2r = p1r + le2; p2i = p2r + 1;
304  for (i = j; i < 2 * fft_frame_size; i += le) {
305  tr = *p2r * ur - *p2i * ui;
306  ti = *p2r * ui + *p2i * ur;
307  *p2r = *p1r - tr; *p2i = *p1i - ti;
308  *p1r += tr; *p1i += ti;
309  p1r += le; p1i += le;
310  p2r += le; p2i += le;
311  }
312  tr = ur * wr - ui * wi;
313  ui = ur * wi + ui * wr;
314  ur = tr;
315  }
316  }
317 }
318 
319 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)
320 {
321  float *in_fifo = fft_data->in_fifo;
322  float *out_fifo = fft_data->out_fifo;
323  float *fft_worksp = fft_data->fft_worksp;
324  float *last_phase = fft_data->last_phase;
325  float *sum_phase = fft_data->sum_phase;
326  float *output_accum = fft_data->output_accum;
327  float *ana_freq = fft_data->ana_freq;
328  float *ana_magn = fft_data->ana_magn;
329  float *syn_freq = fft_data->syn_freq;
330  float *sys_magn = fft_data->sys_magn;
331 
332  double magn, phase, tmp, window, real, imag;
333  double freq_per_bin, expct;
334  long i,k, qpd, index, in_fifo_latency, step_size, fft_frame_size2;
335 
336  /* set up some handy variables */
337  fft_frame_size2 = fft_frame_size / 2;
338  step_size = fft_frame_size / osamp;
339  freq_per_bin = sample_rate / (double) fft_frame_size;
340  expct = 2. * M_PI * (double) step_size / (double) fft_frame_size;
341  in_fifo_latency = fft_frame_size-step_size;
342 
343  if (fft_data->gRover == 0) {
344  fft_data->gRover = in_fifo_latency;
345  }
346 
347  /* main processing loop */
348  for (i = 0; i < num_samps_to_process; i++){
349 
350  /* As long as we have not yet collected enough data just read in */
351  in_fifo[fft_data->gRover] = indata[i];
352  outdata[i] = out_fifo[fft_data->gRover - in_fifo_latency];
353  fft_data->gRover++;
354 
355  /* now we have enough data for processing */
356  if (fft_data->gRover >= fft_frame_size) {
357  fft_data->gRover = in_fifo_latency;
358 
359  /* do windowing and re,im interleave */
360  for (k = 0; k < fft_frame_size;k++) {
361  window = -.5 * cos(2. * M_PI * (double) k / (double) fft_frame_size) + .5;
362  fft_worksp[2*k] = in_fifo[k] * window;
363  fft_worksp[2*k+1] = 0.;
364  }
365 
366  /* ***************** ANALYSIS ******************* */
367  /* do transform */
368  smb_fft(fft_worksp, fft_frame_size, -1);
369 
370  /* this is the analysis step */
371  for (k = 0; k <= fft_frame_size2; k++) {
372 
373  /* de-interlace FFT buffer */
374  real = fft_worksp[2*k];
375  imag = fft_worksp[2*k+1];
376 
377  /* compute magnitude and phase */
378  magn = 2. * sqrt(real * real + imag * imag);
379  phase = atan2(imag, real);
380 
381  /* compute phase difference */
382  tmp = phase - last_phase[k];
383  last_phase[k] = phase;
384 
385  /* subtract expected phase difference */
386  tmp -= (double) k * expct;
387 
388  /* map delta phase into +/- Pi interval */
389  qpd = tmp / M_PI;
390  if (qpd >= 0) {
391  qpd += qpd & 1;
392  } else {
393  qpd -= qpd & 1;
394  }
395  tmp -= M_PI * (double) qpd;
396 
397  /* get deviation from bin frequency from the +/- Pi interval */
398  tmp = osamp * tmp / (2. * M_PI);
399 
400  /* compute the k-th partials' true frequency */
401  tmp = (double) k * freq_per_bin + tmp * freq_per_bin;
402 
403  /* store magnitude and true frequency in analysis arrays */
404  ana_magn[k] = magn;
405  ana_freq[k] = tmp;
406 
407  }
408 
409  /* ***************** PROCESSING ******************* */
410  /* this does the actual pitch shifting */
411  memset(sys_magn, 0, fft_frame_size * sizeof(float));
412  memset(syn_freq, 0, fft_frame_size * sizeof(float));
413  for (k = 0; k <= fft_frame_size2; k++) {
414  index = k * pitchShift;
415  if (index <= fft_frame_size2) {
416  sys_magn[index] += ana_magn[k];
417  syn_freq[index] = ana_freq[k] * pitchShift;
418  }
419  }
420 
421  /* ***************** SYNTHESIS ******************* */
422  /* this is the synthesis step */
423  for (k = 0; k <= fft_frame_size2; k++) {
424 
425  /* get magnitude and true frequency from synthesis arrays */
426  magn = sys_magn[k];
427  tmp = syn_freq[k];
428 
429  /* subtract bin mid frequency */
430  tmp -= (double) k * freq_per_bin;
431 
432  /* get bin deviation from freq deviation */
433  tmp /= freq_per_bin;
434 
435  /* take osamp into account */
436  tmp = 2. * M_PI * tmp / osamp;
437 
438  /* add the overlap phase advance back in */
439  tmp += (double) k * expct;
440 
441  /* accumulate delta phase to get bin phase */
442  sum_phase[k] += tmp;
443  phase = sum_phase[k];
444 
445  /* get real and imag part and re-interleave */
446  fft_worksp[2*k] = magn * cos(phase);
447  fft_worksp[2*k+1] = magn * sin(phase);
448  }
449 
450  /* zero negative frequencies */
451  for (k = fft_frame_size + 2; k < 2 * fft_frame_size; k++) {
452  fft_worksp[k] = 0.;
453  }
454 
455  /* do inverse transform */
456  smb_fft(fft_worksp, fft_frame_size, 1);
457 
458  /* do windowing and add to output accumulator */
459  for (k = 0; k < fft_frame_size; k++) {
460  window = -.5 * cos(2. * M_PI * (double) k / (double) fft_frame_size) + .5;
461  output_accum[k] += 2. * window * fft_worksp[2*k] / (fft_frame_size2 * osamp);
462  }
463  for (k = 0; k < step_size; k++) {
464  out_fifo[k] = output_accum[k];
465  }
466 
467  /* shift accumulator */
468  memmove(output_accum, output_accum+step_size, fft_frame_size * sizeof(float));
469 
470  /* move input FIFO */
471  for (k = 0; k < in_fifo_latency; k++) {
472  in_fifo[k] = in_fifo[k+step_size];
473  }
474  }
475  }
476 }
477 
478 static int pitch_shift(struct ast_frame *f, float amount, struct fft_data *fft)
479 {
480  int16_t *fun = (int16_t *) f->data.ptr;
481  int samples;
482 
483  /* an amount of 1 has no effect */
484  if (!amount || amount == 1 || !fun || (f->samples % 32)) {
485  return 0;
486  }
487  for (samples = 0; samples < f->samples; samples += 32) {
488  smb_pitch_shift(amount, 32, MAX_FRAME_LENGTH, 32, ast_format_get_sample_rate(f->subclass.format), fun+samples, fun+samples, fft);
489  }
490 
491  return 0;
492 }
493 
495  .name = "PITCH_SHIFT",
496  .write = pitchshift_helper,
497 };
498 
499 static int unload_module(void)
500 {
501  return ast_custom_function_unregister(&pitch_shift_function);
502 }
503 
504 static int load_module(void)
505 {
506  int res = ast_custom_function_register(&pitch_shift_function);
508 }
509 
510 AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Audio Effects Dialplan Functions");
const char * name
Definition: pbx.h:119
#define MAX_FRAME_LENGTH
const char * type
Definition: datastore.h:32
#define ast_channel_lock(chan)
Definition: channel.h:2945
Main Channel structure associated with a channel.
unsigned int cos
Definition: chan_iax2.c:352
Asterisk main include file. File version handling, generic pbx functions.
struct ast_audiohook audiohook
short int16_t
Definition: db.h:59
float out_fifo[MAX_FRAME_LENGTH]
float ana_magn[MAX_FRAME_LENGTH]
float output_accum[2 *MAX_FRAME_LENGTH]
#define LOG_WARNING
Definition: logger.h:274
Audiohooks Architecture.
static int tmp()
Definition: bt_open.c:389
static int pitchshift_helper(struct ast_channel *chan, const char *cmd, char *data, const char *value)
float ana_freq[MAX_FRAME_LENGTH]
if(!yyg->yy_init)
Definition: ast_expr2f.c:868
Structure for a data store type.
Definition: datastore.h:31
static int step_size(struct g726_state *state_ptr)
Definition: codec_g726.c:247
int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
Attach audiohook to channel.
Definition: audiohook.c:501
static int pitchshift_cb(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *f, enum ast_audiohook_direction direction)
static const struct ast_datastore_info pitchshift_datastore
Structure for a data store object.
Definition: datastore.h:68
float sum_phase[MAX_FRAME_LENGTH/2+1]
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
Definition: channel.c:2404
float in_fifo[MAX_FRAME_LENGTH]
#define NULL
Definition: resample.c:96
int value
Definition: syslog.c:37
int ast_audiohook_destroy(struct ast_audiohook *audiohook)
Destroys an audiohook structure.
Definition: audiohook.c:133
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)
#define HIGH
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:68
struct ast_frame_subclass subclass
Utility functions.
#define M_PI
ast_audiohook_manipulate_callback manipulate_callback
Definition: audiohook.h:117
int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source, enum ast_audiohook_init_flags flags)
Initialize an audiohook structure.
Definition: audiohook.c:108
float fft_worksp[2 *MAX_FRAME_LENGTH]
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Audio Effects Dialplan Functions")
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
float syn_freq[MAX_FRAME_LENGTH]
static void destroy_callback(void *data)
static struct ast_custom_function pitch_shift_function
float shift_amount
Core PBX routines and definitions.
#define LOW
#define HIGHER
#define HIGHEST
#define LOG_ERROR
Definition: logger.h:285
struct fft_data tx
#define LOWER
float real
Definition: lpc10.h:79
#define LOWEST
static int unload_module(void)
static int load_module(void)
#define ast_channel_unlock(chan)
Definition: channel.h:2946
#define ast_free(a)
Definition: astmm.h:182
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
float sys_magn[MAX_FRAME_LENGTH]
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
ast_audiohook_direction
Definition: audiohook.h:48
static int pitch_shift(struct ast_frame *f, float amount, struct fft_data *fft_data)
void * data
Definition: datastore.h:70
char window[WINSIZE]
Definition: eagi_proxy.c:69
unsigned int ast_format_get_sample_rate(const struct ast_format *format)
Get the sample rate of a media format.
Definition: format.c:379
Data structure associated with a single frame of data.
enum ast_audiohook_status status
Definition: audiohook.h:107
#define ast_datastore_alloc(info, uid)
Definition: datastore.h:89
union ast_frame::@263 data
float last_phase[MAX_FRAME_LENGTH/2+1]
struct ast_format * format
struct fft_data rx
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
direction
Asterisk module definitions.
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2390
static void smb_fft(float *fft_buffer, long fft_frame_size, long sign)
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1508