Asterisk - The Open Source Telephony Project GIT-master-a358458
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 135 of file func_pitchshift.c.

◆ HIGHER

#define HIGHER   1.5

Definition at line 134 of file func_pitchshift.c.

◆ HIGHEST

#define HIGHEST   2

Definition at line 133 of file func_pitchshift.c.

◆ LOW

#define LOW   .85

Definition at line 136 of file func_pitchshift.c.

◆ LOWER

#define LOWER   .7

Definition at line 137 of file func_pitchshift.c.

◆ LOWEST

#define LOWEST   .5

Definition at line 138 of file func_pitchshift.c.

◆ M_PI

#define M_PI   3.14159265358979323846

Definition at line 129 of file func_pitchshift.c.

◆ MAX_FRAME_LENGTH

#define MAX_FRAME_LENGTH   256

Definition at line 131 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 166 of file func_pitchshift.c.

167{
168 struct pitchshift_data *shift = data;
169
171 ast_free(shift);
172};
#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 519 of file func_pitchshift.c.

520{
523}
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:1558

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

494{
495 int16_t *fun = (int16_t *) f->data.ptr;
496 int samples;
497
498 /* an amount of 1 has no effect */
499 if (!amount || amount == 1 || !fun || (f->samples % 32)) {
500 return 0;
501 }
502 for (samples = 0; samples < f->samples; samples += 32) {
503 smb_pitch_shift(amount, 32, MAX_FRAME_LENGTH, 32, ast_format_get_sample_rate(f->subclass.format), fun+samples, fun+samples, fft);
504 }
505
506 return 0;
507}
if(!yyg->yy_init)
Definition: ast_expr2f.c:854
short int16_t
Definition: db.h:59
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
struct ast_frame_subclass subclass
union ast_frame::@226 data

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

180{
181 struct ast_datastore *datastore = NULL;
182 struct pitchshift_data *shift = NULL;
183
184
185 if (!f) {
186 return 0;
187 }
189 return -1;
190 }
191
192 if (!(datastore = ast_channel_datastore_find(chan, &pitchshift_datastore, NULL))) {
193 return -1;
194 }
195
196 shift = datastore->data;
197
199 pitch_shift(f, shift->tx.shift_amount, &shift->tx);
200 } else {
201 pitch_shift(f, shift->rx.shift_amount, &shift->rx);
202 }
203
204 return 0;
205}
@ 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:2399
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 207 of file func_pitchshift.c.

208{
209 struct ast_datastore *datastore = NULL;
210 struct pitchshift_data *shift = NULL;
211 int new = 0;
212 float amount = 0;
213
214 if (!chan) {
215 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
216 return -1;
217 }
218
219 ast_channel_lock(chan);
220 if (!(datastore = ast_channel_datastore_find(chan, &pitchshift_datastore, NULL))) {
221 ast_channel_unlock(chan);
222
223 if (!(datastore = ast_datastore_alloc(&pitchshift_datastore, NULL))) {
224 return 0;
225 }
226 if (!(shift = ast_calloc(1, sizeof(*shift)))) {
227 ast_datastore_free(datastore);
228 return 0;
229 }
230
233 datastore->data = shift;
234 new = 1;
235 } else {
236 ast_channel_unlock(chan);
237 shift = datastore->data;
238 }
239
240
241 if (!strcasecmp(value, "highest")) {
242 amount = HIGHEST;
243 } else if (!strcasecmp(value, "higher")) {
244 amount = HIGHER;
245 } else if (!strcasecmp(value, "high")) {
246 amount = HIGH;
247 } else if (!strcasecmp(value, "lowest")) {
248 amount = LOWEST;
249 } else if (!strcasecmp(value, "lower")) {
250 amount = LOWER;
251 } else if (!strcasecmp(value, "low")) {
252 amount = LOW;
253 } else {
254 if (!sscanf(value, "%30f", &amount) || (amount <= 0) || (amount > 4)) {
255 goto cleanup_error;
256 }
257 }
258
259 if (!strcasecmp(data, "rx")) {
260 shift->rx.shift_amount = amount;
261 } else if (!strcasecmp(data, "tx")) {
262 shift->tx.shift_amount = amount;
263 } else if (!strcasecmp(data, "both")) {
264 shift->rx.shift_amount = amount;
265 shift->tx.shift_amount = amount;
266 } else {
267 goto cleanup_error;
268 }
269
270 if (new) {
271 ast_channel_lock(chan);
272 ast_channel_datastore_add(chan, datastore);
273 ast_channel_unlock(chan);
274 ast_audiohook_attach(chan, &shift->audiohook);
275 }
276
277 return 0;
278
279cleanup_error:
280
281 ast_log(LOG_ERROR, "Invalid argument provided to the %s function\n", cmd);
282 if (new) {
283 ast_datastore_free(datastore);
284 }
285 return -1;
286}
#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:484
@ 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:2385
#define ast_channel_lock(chan)
Definition: channel.h:2922
#define ast_channel_unlock(chan)
Definition: channel.h:2923
#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 288 of file func_pitchshift.c.

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

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

Referenced by pitch_shift().

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 514 of file func_pitchshift.c.

515{
517}
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 509 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 174 of file func_pitchshift.c.

Referenced by pitchshift_cb(), and pitchshift_helper().