Asterisk - The Open Source Telephony Project GIT-master-754dea3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
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 138 of file func_pitchshift.c.

◆ HIGHER

#define HIGHER   1.5

Definition at line 137 of file func_pitchshift.c.

◆ HIGHEST

#define HIGHEST   2

Definition at line 136 of file func_pitchshift.c.

◆ LOW

#define LOW   .85

Definition at line 139 of file func_pitchshift.c.

◆ LOWER

#define LOWER   .7

Definition at line 140 of file func_pitchshift.c.

◆ LOWEST

#define LOWEST   .5

Definition at line 141 of file func_pitchshift.c.

◆ M_PI

#define M_PI   3.14159265358979323846

Definition at line 132 of file func_pitchshift.c.

◆ MAX_FRAME_LENGTH

#define MAX_FRAME_LENGTH   256

Definition at line 134 of file func_pitchshift.c.

Function Documentation

◆ AST_MODULE_INFO_STANDARD_EXTENDED()

AST_MODULE_INFO_STANDARD_EXTENDED ( ASTERISK_GPL_KEY  ,
"Audio Effects Dialplan Functions"   
)

◆ destroy_callback()

static void destroy_callback ( void *  data)
static

Definition at line 169 of file func_pitchshift.c.

170{
171 struct pitchshift_data *shift = data;
172
174 ast_free(shift);
175};
#define ast_free(a)
Definition: astmm.h:180
int ast_audiohook_destroy(struct ast_audiohook *audiohook)
Destroys an audiohook structure.
Definition: audiohook.c:124
struct ast_audiohook audiohook

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

◆ load_module()

static int load_module ( void  )
static

Definition at line 522 of file func_pitchshift.c.

523{
526}
static struct ast_custom_function pitch_shift_function
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
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:1559

References ast_custom_function_register, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, and pitch_shift_function.

◆ pitch_shift()

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

Definition at line 496 of file func_pitchshift.c.

497{
498 int16_t *fun = (int16_t *) f->data.ptr;
499 int samples;
500
501 /* an amount of 1 has no effect */
502 if (!amount || amount == 1 || !fun || (f->samples % 32)) {
503 return 0;
504 }
505 for (samples = 0; samples < f->samples; samples += 32) {
506 smb_pitch_shift(amount, 32, MAX_FRAME_LENGTH, 32, ast_format_get_sample_rate(f->subclass.format), fun+samples, fun+samples, fft);
507 }
508
509 return 0;
510}
if(!yyg->yy_init)
Definition: ast_expr2f.c:854
unsigned int ast_format_get_sample_rate(const struct ast_format *format)
Get the sample rate of a media format.
Definition: format.c:379
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 MAX_FRAME_LENGTH
struct ast_format * format
union ast_frame::@228 data
struct ast_frame_subclass subclass

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().

◆ 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 182 of file func_pitchshift.c.

183{
184 struct ast_datastore *datastore = NULL;
185 struct pitchshift_data *shift = NULL;
186
187
188 if (!f) {
189 return 0;
190 }
192 return -1;
193 }
194
195 if (!(datastore = ast_channel_datastore_find(chan, &pitchshift_datastore, NULL))) {
196 return -1;
197 }
198
199 shift = datastore->data;
200
202 pitch_shift(f, shift->tx.shift_amount, &shift->tx);
203 } else {
204 pitch_shift(f, shift->rx.shift_amount, &shift->rx);
205 }
206
207 return 0;
208}
@ AST_AUDIOHOOK_DIRECTION_WRITE
Definition: audiohook.h:50
@ AST_AUDIOHOOK_STATUS_DONE
Definition: audiohook.h:45
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:2428
direction
static const struct ast_datastore_info pitchshift_datastore
static int pitch_shift(struct ast_frame *f, float amount, struct fft_data *fft_data)
#define NULL
Definition: resample.c:96
enum ast_audiohook_status status
Definition: audiohook.h:108
Structure for a data store object.
Definition: datastore.h:64
void * data
Definition: datastore.h:66
float shift_amount
struct fft_data tx
struct fft_data rx

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

Referenced by pitchshift_helper().

◆ pitchshift_helper()

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

Definition at line 210 of file func_pitchshift.c.

211{
212 struct ast_datastore *datastore = NULL;
213 struct pitchshift_data *shift = NULL;
214 int new = 0;
215 float amount = 0;
216
217 if (!chan) {
218 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
219 return -1;
220 }
221
222 ast_channel_lock(chan);
223 if (!(datastore = ast_channel_datastore_find(chan, &pitchshift_datastore, NULL))) {
224 ast_channel_unlock(chan);
225
226 if (!(datastore = ast_datastore_alloc(&pitchshift_datastore, NULL))) {
227 return 0;
228 }
229 if (!(shift = ast_calloc(1, sizeof(*shift)))) {
230 ast_datastore_free(datastore);
231 return 0;
232 }
233
236 datastore->data = shift;
237 new = 1;
238 } else {
239 ast_channel_unlock(chan);
240 shift = datastore->data;
241 }
242
243
244 if (!strcasecmp(value, "highest")) {
245 amount = HIGHEST;
246 } else if (!strcasecmp(value, "higher")) {
247 amount = HIGHER;
248 } else if (!strcasecmp(value, "high")) {
249 amount = HIGH;
250 } else if (!strcasecmp(value, "lowest")) {
251 amount = LOWEST;
252 } else if (!strcasecmp(value, "lower")) {
253 amount = LOWER;
254 } else if (!strcasecmp(value, "low")) {
255 amount = LOW;
256 } else {
257 if (!sscanf(value, "%30f", &amount) || (amount <= 0) || (amount > 4)) {
258 goto cleanup_error;
259 }
260 }
261
262 if (!strcasecmp(data, "rx")) {
263 shift->rx.shift_amount = amount;
264 } else if (!strcasecmp(data, "tx")) {
265 shift->tx.shift_amount = amount;
266 } else if (!strcasecmp(data, "both")) {
267 shift->rx.shift_amount = amount;
268 shift->tx.shift_amount = amount;
269 } else {
270 goto cleanup_error;
271 }
272
273 if (new) {
274 ast_channel_lock(chan);
275 ast_channel_datastore_add(chan, datastore);
276 ast_channel_unlock(chan);
277 ast_audiohook_attach(chan, &shift->audiohook);
278 }
279
280 return 0;
281
282cleanup_error:
283
284 ast_log(LOG_ERROR, "Invalid argument provided to the %s function\n", cmd);
285 if (new) {
286 ast_datastore_free(datastore);
287 }
288 return -1;
289}
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_log
Definition: astobj2.c:42
@ AST_AUDIOHOOK_MANIPULATE_ALL_RATES
Definition: audiohook.h:75
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:100
int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
Attach audiohook to channel.
Definition: audiohook.c:512
@ AST_AUDIOHOOK_TYPE_MANIPULATE
Definition: audiohook.h:38
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2414
#define ast_channel_lock(chan)
Definition: channel.h:2970
#define ast_channel_unlock(chan)
Definition: channel.h:2971
#define ast_datastore_alloc(info, uid)
Definition: datastore.h:85
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:68
#define LOWEST
#define HIGHER
#define HIGH
#define HIGHEST
#define LOWER
#define LOW
static int pitchshift_cb(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *f, enum ast_audiohook_direction direction)
#define LOG_ERROR
#define LOG_WARNING
ast_audiohook_manipulate_callback manipulate_callback
Definition: audiohook.h:118
int value
Definition: syslog.c:37

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_datastore, pitchshift_data::rx, fft_data::shift_amount, pitchshift_data::tx, and value.

◆ smb_fft()

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

Definition at line 291 of file func_pitchshift.c.

292{
293 float wr, wi, arg, *p1, *p2, temp;
294 float tr, ti, ur, ui, *p1r, *p1i, *p2r, *p2i;
295 long i, bitm, j, le, le2, k;
296
297 for (i = 2; i < 2 * fft_frame_size - 2; i += 2) {
298 for (bitm = 2, j = 0; bitm < 2 * fft_frame_size; bitm <<= 1) {
299 if (i & bitm) {
300 j++;
301 }
302 j <<= 1;
303 }
304 if (i < j) {
305 p1 = fft_buffer + i; p2 = fft_buffer + j;
306 temp = *p1; *(p1++) = *p2;
307 *(p2++) = temp; temp = *p1;
308 *p1 = *p2; *p2 = temp;
309 }
310 }
311 for (k = 0, le = 2; k < (long) (log(fft_frame_size) / log(2.) + .5); k++) {
312 le <<= 1;
313 le2 = le>>1;
314 ur = 1.0;
315 ui = 0.0;
316 arg = M_PI / (le2>>1);
317 wr = cos(arg);
318 wi = sign * sin(arg);
319 for (j = 0; j < le2; j += 2) {
320 p1r = fft_buffer+j; p1i = p1r + 1;
321 p2r = p1r + le2; p2i = p2r + 1;
322 for (i = j; i < 2 * fft_frame_size; i += le) {
323 tr = *p2r * ur - *p2i * ui;
324 ti = *p2r * ui + *p2i * ur;
325 *p2r = *p1r - tr; *p2i = *p1i - ti;
326 *p1r += tr; *p1i += ti;
327 p1r += le; p1i += le;
328 p2r += le; p2i += le;
329 }
330 tr = ur * wr - ui * wi;
331 ui = ur * wi + ui * wr;
332 ur = tr;
333 }
334 }
335}
unsigned int cos
Definition: chan_iax2.c:380
#define M_PI

References cos, and M_PI.

Referenced by smb_pitch_shift().

◆ 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 337 of file func_pitchshift.c.

338{
339 float *in_fifo = fft_data->in_fifo;
340 float *out_fifo = fft_data->out_fifo;
341 float *fft_worksp = fft_data->fft_worksp;
342 float *last_phase = fft_data->last_phase;
343 float *sum_phase = fft_data->sum_phase;
344 float *output_accum = fft_data->output_accum;
345 float *ana_freq = fft_data->ana_freq;
346 float *ana_magn = fft_data->ana_magn;
347 float *syn_freq = fft_data->syn_freq;
348 float *sys_magn = fft_data->sys_magn;
349
350 double magn, phase, tmp, window, real, imag;
351 double freq_per_bin, expect;
352 long i,k, qpd, index, in_fifo_latency, step_size, fft_frame_size2;
353
354 /* set up some handy variables */
355 fft_frame_size2 = fft_frame_size / 2;
356 step_size = fft_frame_size / osamp;
357 freq_per_bin = sample_rate / (double) fft_frame_size;
358 expect = 2. * M_PI * (double) step_size / (double) fft_frame_size;
359 in_fifo_latency = fft_frame_size-step_size;
360
361 if (fft_data->gRover == 0) {
362 fft_data->gRover = in_fifo_latency;
363 }
364
365 /* main processing loop */
366 for (i = 0; i < num_samps_to_process; i++){
367
368 /* As long as we have not yet collected enough data just read in */
369 in_fifo[fft_data->gRover] = indata[i];
370 outdata[i] = out_fifo[fft_data->gRover - in_fifo_latency];
371 fft_data->gRover++;
372
373 /* now we have enough data for processing */
374 if (fft_data->gRover >= fft_frame_size) {
375 fft_data->gRover = in_fifo_latency;
376
377 /* do windowing and re,im interleave */
378 for (k = 0; k < fft_frame_size;k++) {
379 window = -.5 * cos(2. * M_PI * (double) k / (double) fft_frame_size) + .5;
380 fft_worksp[2*k] = in_fifo[k] * window;
381 fft_worksp[2*k+1] = 0.;
382 }
383
384 /* ***************** ANALYSIS ******************* */
385 /* do transform */
386 smb_fft(fft_worksp, fft_frame_size, -1);
387
388 /* this is the analysis step */
389 for (k = 0; k <= fft_frame_size2; k++) {
390
391 /* de-interlace FFT buffer */
392 real = fft_worksp[2*k];
393 imag = fft_worksp[2*k+1];
394
395 /* compute magnitude and phase */
396 magn = 2. * sqrt(real * real + imag * imag);
397 phase = atan2(imag, real);
398
399 /* compute phase difference */
400 tmp = phase - last_phase[k];
401 last_phase[k] = phase;
402
403 /* subtract expected phase difference */
404 tmp -= (double) k * expect;
405
406 /* map delta phase into +/- Pi interval */
407 qpd = tmp / M_PI;
408 if (qpd >= 0) {
409 qpd += qpd & 1;
410 } else {
411 qpd -= qpd & 1;
412 }
413 tmp -= M_PI * (double) qpd;
414
415 /* get deviation from bin frequency from the +/- Pi interval */
416 tmp = osamp * tmp / (2. * M_PI);
417
418 /* compute the k-th partials' true frequency */
419 tmp = (double) k * freq_per_bin + tmp * freq_per_bin;
420
421 /* store magnitude and true frequency in analysis arrays */
422 ana_magn[k] = magn;
423 ana_freq[k] = tmp;
424
425 }
426
427 /* ***************** PROCESSING ******************* */
428 /* this does the actual pitch shifting */
429 memset(sys_magn, 0, fft_frame_size * sizeof(float));
430 memset(syn_freq, 0, fft_frame_size * sizeof(float));
431 for (k = 0; k <= fft_frame_size2; k++) {
432 index = k * pitchShift;
433 if (index <= fft_frame_size2) {
434 sys_magn[index] += ana_magn[k];
435 syn_freq[index] = ana_freq[k] * pitchShift;
436 }
437 }
438
439 /* ***************** SYNTHESIS ******************* */
440 /* this is the synthesis step */
441 for (k = 0; k <= fft_frame_size2; k++) {
442
443 /* get magnitude and true frequency from synthesis arrays */
444 magn = sys_magn[k];
445 tmp = syn_freq[k];
446
447 /* subtract bin mid frequency */
448 tmp -= (double) k * freq_per_bin;
449
450 /* get bin deviation from freq deviation */
451 tmp /= freq_per_bin;
452
453 /* take osamp into account */
454 tmp = 2. * M_PI * tmp / osamp;
455
456 /* add the overlap phase advance back in */
457 tmp += (double) k * expect;
458
459 /* accumulate delta phase to get bin phase */
460 sum_phase[k] += tmp;
461 phase = sum_phase[k];
462
463 /* get real and imag part and re-interleave */
464 fft_worksp[2*k] = magn * cos(phase);
465 fft_worksp[2*k+1] = magn * sin(phase);
466 }
467
468 /* zero negative frequencies */
469 for (k = fft_frame_size + 2; k < 2 * fft_frame_size; k++) {
470 fft_worksp[k] = 0.;
471 }
472
473 /* do inverse transform */
474 smb_fft(fft_worksp, fft_frame_size, 1);
475
476 /* do windowing and add to output accumulator */
477 for (k = 0; k < fft_frame_size; k++) {
478 window = -.5 * cos(2. * M_PI * (double) k / (double) fft_frame_size) + .5;
479 output_accum[k] += 2. * window * fft_worksp[2*k] / (fft_frame_size2 * osamp);
480 }
481 for (k = 0; k < step_size; k++) {
482 out_fifo[k] = output_accum[k];
483 }
484
485 /* shift accumulator */
486 memmove(output_accum, output_accum+step_size, fft_frame_size * sizeof(float));
487
488 /* move input FIFO */
489 for (k = 0; k < in_fifo_latency; k++) {
490 in_fifo[k] = in_fifo[k+step_size];
491 }
492 }
493 }
494}
static int step_size(struct g726_state *state_ptr)
Definition: codec_g726.c:247
char window[WINSIZE]
Definition: eagi_proxy.c:69
static void smb_fft(float *fft_buffer, long fft_frame_size, long sign)
float real
Definition: lpc10.h:79
float output_accum[2 *MAX_FRAME_LENGTH]
float out_fifo[MAX_FRAME_LENGTH]
float ana_freq[MAX_FRAME_LENGTH]
float sum_phase[MAX_FRAME_LENGTH/2+1]
float in_fifo[MAX_FRAME_LENGTH]
float sys_magn[MAX_FRAME_LENGTH]
float fft_worksp[2 *MAX_FRAME_LENGTH]
float ana_magn[MAX_FRAME_LENGTH]
float syn_freq[MAX_FRAME_LENGTH]
float last_phase[MAX_FRAME_LENGTH/2+1]

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, and window.

Referenced by pitch_shift().

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 517 of file func_pitchshift.c.

518{
520}
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.

References ast_custom_function_unregister(), and 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 512 of file func_pitchshift.c.

Referenced by load_module(), and unload_module().

◆ 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 177 of file func_pitchshift.c.

Referenced by pitchshift_cb(), and pitchshift_helper().