Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | Functions | Variables
func_pitchshift.c File Reference

Pitch Shift Audio Effect. More...

#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"
#include "asterisk/audiohook.h"
#include <math.h>
Include dependency graph for func_pitchshift.c:

Go to the source code of this file.

Data Structures

struct  fft_data
 
struct  pitchshift_data
 

Macros

#define HIGH   1.25
 
#define HIGHER   1.5
 
#define HIGHEST   2
 
#define LOW   .85
 
#define LOWER   .7
 
#define LOWEST   .5
 
#define M_PI   3.14159265358979323846
 
#define MAX_FRAME_LENGTH   256
 

Functions

 AST_MODULE_INFO_STANDARD_EXTENDED (ASTERISK_GPL_KEY, "Audio Effects Dialplan Functions")
 
static void destroy_callback (void *data)
 
static int load_module (void)
 
static int pitch_shift (struct ast_frame *f, float amount, struct fft_data *fft_data)
 
static int pitchshift_cb (struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *f, enum ast_audiohook_direction direction)
 
static int pitchshift_helper (struct ast_channel *chan, const char *cmd, char *data, const char *value)
 
static void smb_fft (float *fft_buffer, 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 unload_module (void)
 

Variables

static struct ast_custom_function pitch_shift_function
 
static const struct ast_datastore_info pitchshift_datastore
 

Detailed Description

Pitch Shift Audio Effect.

Author
David Vossel dvoss.nosp@m.el@d.nosp@m.igium.nosp@m..com

Definition in file func_pitchshift.c.

Macro Definition Documentation

◆ HIGH

#define HIGH   1.25

Definition at line 120 of file func_pitchshift.c.

Referenced by pitchshift_helper().

◆ HIGHER

#define HIGHER   1.5

Definition at line 119 of file func_pitchshift.c.

Referenced by pitchshift_helper().

◆ HIGHEST

#define HIGHEST   2

Definition at line 118 of file func_pitchshift.c.

Referenced by pitchshift_helper().

◆ LOW

#define LOW   .85

Definition at line 121 of file func_pitchshift.c.

Referenced by pitchshift_helper().

◆ LOWER

#define LOWER   .7

Definition at line 122 of file func_pitchshift.c.

Referenced by pitchshift_helper().

◆ LOWEST

#define LOWEST   .5

Definition at line 123 of file func_pitchshift.c.

Referenced by pitchshift_helper().

◆ M_PI

#define M_PI   3.14159265358979323846

Definition at line 114 of file func_pitchshift.c.

Referenced by smb_fft(), and smb_pitch_shift().

◆ MAX_FRAME_LENGTH

#define MAX_FRAME_LENGTH   256

Definition at line 116 of file func_pitchshift.c.

Referenced by pitch_shift().

Function Documentation

◆ AST_MODULE_INFO_STANDARD_EXTENDED()

AST_MODULE_INFO_STANDARD_EXTENDED ( ASTERISK_GPL_KEY  ,
"Audio Effects Dialplan Functions"   
)

Referenced by load_module().

◆ destroy_callback()

static void destroy_callback ( void *  data)
static

Definition at line 151 of file func_pitchshift.c.

References ast_audiohook_destroy(), ast_free, and pitchshift_data::audiohook.

152 {
153  struct pitchshift_data *shift = data;
154 
156  ast_free(shift);
157 };
struct ast_audiohook audiohook
int ast_audiohook_destroy(struct ast_audiohook *audiohook)
Destroys an audiohook structure.
Definition: audiohook.c:133
#define ast_free(a)
Definition: astmm.h:182

◆ load_module()

static int load_module ( void  )
static

Definition at line 504 of file func_pitchshift.c.

References ast_custom_function_register, AST_MODULE_INFO_STANDARD_EXTENDED(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, and ASTERISK_GPL_KEY.

505 {
508 }
static struct ast_custom_function pitch_shift_function
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1508

◆ pitch_shift()

static int pitch_shift ( struct ast_frame f,
float  amount,
struct fft_data fft_data 
)
static

Definition at line 478 of file func_pitchshift.c.

References ast_format_get_sample_rate(), ast_frame::data, ast_frame_subclass::format, if(), MAX_FRAME_LENGTH, ast_frame::ptr, ast_frame::samples, smb_pitch_shift(), and ast_frame::subclass.

Referenced by pitchshift_cb().

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 }
#define MAX_FRAME_LENGTH
short int16_t
Definition: db.h:59
if(!yyg->yy_init)
Definition: ast_expr2f.c:868
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)
struct ast_frame_subclass subclass
unsigned int ast_format_get_sample_rate(const struct ast_format *format)
Get the sample rate of a media format.
Definition: format.c:379
union ast_frame::@263 data
struct ast_format * format

◆ pitchshift_cb()

static int pitchshift_cb ( struct ast_audiohook audiohook,
struct ast_channel chan,
struct ast_frame f,
enum ast_audiohook_direction  direction 
)
static

Definition at line 164 of file func_pitchshift.c.

References AST_AUDIOHOOK_DIRECTION_WRITE, AST_AUDIOHOOK_STATUS_DONE, ast_channel_datastore_find(), ast_datastore::data, NULL, pitch_shift(), pitchshift_data::rx, fft_data::shift_amount, ast_audiohook::status, and pitchshift_data::tx.

Referenced by pitchshift_helper().

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 
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 }
static const struct ast_datastore_info pitchshift_datastore
Structure for a data store object.
Definition: datastore.h:68
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
#define NULL
Definition: resample.c:96
float shift_amount
struct fft_data tx
static int pitch_shift(struct ast_frame *f, float amount, struct fft_data *fft_data)
void * data
Definition: datastore.h:70
enum ast_audiohook_status status
Definition: audiohook.h:107
struct fft_data rx
direction

◆ pitchshift_helper()

static int pitchshift_helper ( struct ast_channel chan,
const char *  cmd,
char *  data,
const char *  value 
)
static

Definition at line 192 of file func_pitchshift.c.

References ast_audiohook_attach(), ast_audiohook_init(), AST_AUDIOHOOK_MANIPULATE_ALL_RATES, AST_AUDIOHOOK_TYPE_MANIPULATE, ast_calloc, ast_channel_datastore_add(), ast_channel_datastore_find(), ast_channel_lock, ast_channel_unlock, ast_datastore_alloc, ast_datastore_free(), ast_log, pitchshift_data::audiohook, ast_datastore::data, HIGH, HIGHER, HIGHEST, LOG_ERROR, LOG_WARNING, LOW, LOWER, LOWEST, ast_audiohook::manipulate_callback, NULL, pitchshift_cb(), pitchshift_data::rx, fft_data::shift_amount, and pitchshift_data::tx.

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 }
#define ast_channel_lock(chan)
Definition: channel.h:2945
struct ast_audiohook audiohook
#define LOG_WARNING
Definition: logger.h:274
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
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
#define NULL
Definition: resample.c:96
int value
Definition: syslog.c:37
#define HIGH
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:68
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
#define ast_log
Definition: astobj2.c:42
float shift_amount
#define LOW
#define HIGHER
#define HIGHEST
#define LOG_ERROR
Definition: logger.h:285
struct fft_data tx
#define LOWER
#define LOWEST
#define ast_channel_unlock(chan)
Definition: channel.h:2946
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
void * data
Definition: datastore.h:70
#define ast_datastore_alloc(info, uid)
Definition: datastore.h:89
struct fft_data rx
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2390

◆ smb_fft()

static void smb_fft ( float *  fft_buffer,
long  fft_frame_size,
long  sign 
)
static

Definition at line 273 of file func_pitchshift.c.

References cos, and M_PI.

Referenced by smb_pitch_shift().

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 }
unsigned int cos
Definition: chan_iax2.c:352
#define M_PI

◆ smb_pitch_shift()

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

Definition at line 319 of file func_pitchshift.c.

References fft_data::ana_freq, fft_data::ana_magn, cos, fft_data::fft_worksp, fft_data::gRover, fft_data::in_fifo, fft_data::last_phase, M_PI, fft_data::out_fifo, fft_data::output_accum, smb_fft(), step_size(), fft_data::sum_phase, fft_data::syn_freq, fft_data::sys_magn, tmp(), and window.

Referenced by pitch_shift().

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 }
unsigned int cos
Definition: chan_iax2.c:352
float out_fifo[MAX_FRAME_LENGTH]
float ana_magn[MAX_FRAME_LENGTH]
float output_accum[2 *MAX_FRAME_LENGTH]
static int tmp()
Definition: bt_open.c:389
float ana_freq[MAX_FRAME_LENGTH]
static int step_size(struct g726_state *state_ptr)
Definition: codec_g726.c:247
float sum_phase[MAX_FRAME_LENGTH/2+1]
float in_fifo[MAX_FRAME_LENGTH]
#define M_PI
float fft_worksp[2 *MAX_FRAME_LENGTH]
float syn_freq[MAX_FRAME_LENGTH]
float real
Definition: lpc10.h:79
float sys_magn[MAX_FRAME_LENGTH]
char window[WINSIZE]
Definition: eagi_proxy.c:69
float last_phase[MAX_FRAME_LENGTH/2+1]
static void smb_fft(float *fft_buffer, long fft_frame_size, long sign)

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 499 of file func_pitchshift.c.

References ast_custom_function_unregister().

500 {
502 }
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
static struct ast_custom_function pitch_shift_function

Variable Documentation

◆ pitch_shift_function

struct ast_custom_function pitch_shift_function
static
Initial value:
= {
.name = "PITCH_SHIFT",
}
static int pitchshift_helper(struct ast_channel *chan, const char *cmd, char *data, const char *value)

Definition at line 494 of file func_pitchshift.c.

◆ pitchshift_datastore

const struct ast_datastore_info pitchshift_datastore
static
Initial value:
= {
.type = "pitchshift",
.destroy = destroy_callback
}
static void destroy_callback(void *data)

Definition at line 159 of file func_pitchshift.c.