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

Defines

#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 struct ast_datastore_info pitchshift_datastore


Detailed Description

Pitch Shift Audio Effect.

Author:
David Vossel <dvossel@digium.com>

Definition in file func_pitchshift.c.


Define Documentation

#define HIGH   1.25

Definition at line 122 of file func_pitchshift.c.

Referenced by pitchshift_helper().

#define HIGHER   1.5

Definition at line 121 of file func_pitchshift.c.

Referenced by pitchshift_helper().

#define HIGHEST   2

Definition at line 120 of file func_pitchshift.c.

Referenced by pitchshift_helper().

#define LOW   .85

Definition at line 123 of file func_pitchshift.c.

Referenced by pitchshift_helper().

#define LOWER   .7

Definition at line 124 of file func_pitchshift.c.

Referenced by pitchshift_helper().

#define LOWEST   .5

Definition at line 125 of file func_pitchshift.c.

Referenced by pitchshift_helper().

#define M_PI   3.14159265358979323846

Definition at line 116 of file func_pitchshift.c.

#define MAX_FRAME_LENGTH   256

Definition at line 118 of file func_pitchshift.c.

Referenced by pitch_shift().


Function Documentation

AST_MODULE_INFO_STANDARD_EXTENDED ( ASTERISK_GPL_KEY  ,
"Audio Effects Dialplan Functions"   
)

static void destroy_callback ( void *  data  )  [static]

Definition at line 153 of file func_pitchshift.c.

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

00154 {
00155    struct pitchshift_data *shift = data;
00156 
00157    ast_audiohook_destroy(&shift->audiohook);
00158    ast_free(shift);
00159 };

static int load_module ( void   )  [static]

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

Definition at line 480 of file func_pitchshift.c.

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

Referenced by pitchshift_cb().

00481 {
00482    int16_t *fun = (int16_t *) f->data.ptr;
00483    int samples;
00484 
00485    /* an amount of 1 has no effect */
00486    if (!amount || amount == 1 || !fun || (f->samples % 32)) {
00487       return 0;
00488    }
00489    for (samples = 0; samples < f->samples; samples += 32) {
00490       smb_pitch_shift(amount, 32, MAX_FRAME_LENGTH, 32, ast_format_get_sample_rate(f->subclass.format), fun+samples, fun+samples, fft);
00491    }
00492 
00493    return 0;
00494 }

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

00167 {
00168    struct ast_datastore *datastore = NULL;
00169    struct pitchshift_data *shift = NULL;
00170 
00171 
00172    if (!f) {
00173       return 0;
00174    }
00175    if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE) {
00176       return -1;
00177    }
00178 
00179    if (!(datastore = ast_channel_datastore_find(chan, &pitchshift_datastore, NULL))) {
00180       return -1;
00181    }
00182 
00183    shift = datastore->data;
00184 
00185    if (direction == AST_AUDIOHOOK_DIRECTION_WRITE) {
00186       pitch_shift(f, shift->tx.shift_amount, &shift->tx);
00187    } else {
00188       pitch_shift(f, shift->rx.shift_amount, &shift->rx);
00189    }
00190 
00191    return 0;
00192 }

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

Definition at line 194 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.

00195 {
00196    struct ast_datastore *datastore = NULL;
00197    struct pitchshift_data *shift = NULL;
00198    int new = 0;
00199    float amount = 0;
00200 
00201    if (!chan) {
00202       ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
00203       return -1;
00204    }
00205 
00206    ast_channel_lock(chan);
00207    if (!(datastore = ast_channel_datastore_find(chan, &pitchshift_datastore, NULL))) {
00208       ast_channel_unlock(chan);
00209 
00210       if (!(datastore = ast_datastore_alloc(&pitchshift_datastore, NULL))) {
00211          return 0;
00212       }
00213       if (!(shift = ast_calloc(1, sizeof(*shift)))) {
00214          ast_datastore_free(datastore);
00215          return 0;
00216       }
00217 
00218       ast_audiohook_init(&shift->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "pitch_shift", AST_AUDIOHOOK_MANIPULATE_ALL_RATES);
00219       shift->audiohook.manipulate_callback = pitchshift_cb;
00220       datastore->data = shift;
00221       new = 1;
00222    } else {
00223       ast_channel_unlock(chan);
00224       shift = datastore->data;
00225    }
00226 
00227 
00228    if (!strcasecmp(value, "highest")) {
00229       amount = HIGHEST;
00230    } else if (!strcasecmp(value, "higher")) {
00231       amount = HIGHER;
00232    } else if (!strcasecmp(value, "high")) {
00233       amount = HIGH;
00234    } else if (!strcasecmp(value, "lowest")) {
00235       amount = LOWEST;
00236    } else if (!strcasecmp(value, "lower")) {
00237       amount = LOWER;
00238    } else if (!strcasecmp(value, "low")) {
00239       amount = LOW;
00240    } else {
00241       if (!sscanf(value, "%30f", &amount) || (amount <= 0) || (amount > 4)) {
00242          goto cleanup_error;
00243       }
00244    }
00245 
00246    if (!strcasecmp(data, "rx")) {
00247       shift->rx.shift_amount = amount;
00248    } else if (!strcasecmp(data, "tx")) {
00249       shift->tx.shift_amount = amount;
00250    } else if (!strcasecmp(data, "both")) {
00251       shift->rx.shift_amount = amount;
00252       shift->tx.shift_amount = amount;
00253    } else {
00254       goto cleanup_error;
00255    }
00256 
00257    if (new) {
00258       ast_channel_lock(chan);
00259       ast_channel_datastore_add(chan, datastore);
00260       ast_channel_unlock(chan);
00261       ast_audiohook_attach(chan, &shift->audiohook);
00262    }
00263 
00264    return 0;
00265 
00266 cleanup_error:
00267 
00268    ast_log(LOG_ERROR, "Invalid argument provided to the %s function\n", cmd);
00269    if (new) {
00270       ast_datastore_free(datastore);
00271    }
00272    return -1;
00273 }

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

Definition at line 275 of file func_pitchshift.c.

References cos, and M_PI.

Referenced by smb_pitch_shift().

00276 {
00277    float wr, wi, arg, *p1, *p2, temp;
00278    float tr, ti, ur, ui, *p1r, *p1i, *p2r, *p2i;
00279    long i, bitm, j, le, le2, k;
00280 
00281    for (i = 2; i < 2 * fft_frame_size - 2; i += 2) {
00282       for (bitm = 2, j = 0; bitm < 2 * fft_frame_size; bitm <<= 1) {
00283          if (i & bitm) {
00284             j++;
00285          }
00286          j <<= 1;
00287       }
00288       if (i < j) {
00289          p1 = fft_buffer + i; p2 = fft_buffer + j;
00290          temp = *p1; *(p1++) = *p2;
00291          *(p2++) = temp; temp = *p1;
00292          *p1 = *p2; *p2 = temp;
00293       }
00294    }
00295    for (k = 0, le = 2; k < (long) (log(fft_frame_size) / log(2.) + .5); k++) {
00296       le <<= 1;
00297       le2 = le>>1;
00298       ur = 1.0;
00299       ui = 0.0;
00300       arg = M_PI / (le2>>1);
00301       wr = cos(arg);
00302       wi = sign * sin(arg);
00303       for (j = 0; j < le2; j += 2) {
00304          p1r = fft_buffer+j; p1i = p1r + 1;
00305          p2r = p1r + le2; p2i = p2r + 1;
00306          for (i = j; i < 2 * fft_frame_size; i += le) {
00307             tr = *p2r * ur - *p2i * ui;
00308             ti = *p2r * ui + *p2i * ur;
00309             *p2r = *p1r - tr; *p2i = *p1i - ti;
00310             *p1r += tr; *p1i += ti;
00311             p1r += le; p1i += le;
00312             p2r += le; p2i += le;
00313          }
00314          tr = ur * wr - ui * wi;
00315          ui = ur * wi + ui * wr;
00316          ur = tr;
00317       }
00318    }
00319 }

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

00322 {
00323    float *in_fifo = fft_data->in_fifo;
00324    float *out_fifo = fft_data->out_fifo;
00325    float *fft_worksp = fft_data->fft_worksp;
00326    float *last_phase = fft_data->last_phase;
00327    float *sum_phase = fft_data->sum_phase;
00328    float *output_accum = fft_data->output_accum;
00329    float *ana_freq = fft_data->ana_freq;
00330    float *ana_magn = fft_data->ana_magn;
00331    float *syn_freq = fft_data->syn_freq;
00332    float *sys_magn = fft_data->sys_magn;
00333 
00334    double magn, phase, tmp, window, real, imag;
00335    double freq_per_bin, expct;
00336    long i,k, qpd, index, in_fifo_latency, step_size, fft_frame_size2;
00337 
00338    /* set up some handy variables */
00339    fft_frame_size2 = fft_frame_size / 2;
00340    step_size = fft_frame_size / osamp;
00341    freq_per_bin = sample_rate / (double) fft_frame_size;
00342    expct = 2. * M_PI * (double) step_size / (double) fft_frame_size;
00343    in_fifo_latency = fft_frame_size-step_size;
00344 
00345    if (fft_data->gRover == 0) {
00346       fft_data->gRover = in_fifo_latency;
00347    }
00348 
00349    /* main processing loop */
00350    for (i = 0; i < num_samps_to_process; i++){
00351 
00352       /* As long as we have not yet collected enough data just read in */
00353       in_fifo[fft_data->gRover] = indata[i];
00354       outdata[i] = out_fifo[fft_data->gRover - in_fifo_latency];
00355       fft_data->gRover++;
00356 
00357       /* now we have enough data for processing */
00358       if (fft_data->gRover >= fft_frame_size) {
00359          fft_data->gRover = in_fifo_latency;
00360 
00361          /* do windowing and re,im interleave */
00362          for (k = 0; k < fft_frame_size;k++) {
00363             window = -.5 * cos(2. * M_PI * (double) k / (double) fft_frame_size) + .5;
00364             fft_worksp[2*k] = in_fifo[k] * window;
00365             fft_worksp[2*k+1] = 0.;
00366          }
00367 
00368          /* ***************** ANALYSIS ******************* */
00369          /* do transform */
00370          smb_fft(fft_worksp, fft_frame_size, -1);
00371 
00372          /* this is the analysis step */
00373          for (k = 0; k <= fft_frame_size2; k++) {
00374 
00375             /* de-interlace FFT buffer */
00376             real = fft_worksp[2*k];
00377             imag = fft_worksp[2*k+1];
00378 
00379             /* compute magnitude and phase */
00380             magn = 2. * sqrt(real * real + imag * imag);
00381             phase = atan2(imag, real);
00382 
00383             /* compute phase difference */
00384             tmp = phase - last_phase[k];
00385             last_phase[k] = phase;
00386 
00387             /* subtract expected phase difference */
00388             tmp -= (double) k * expct;
00389 
00390             /* map delta phase into +/- Pi interval */
00391             qpd = tmp / M_PI;
00392             if (qpd >= 0) {
00393                qpd += qpd & 1;
00394             } else {
00395                qpd -= qpd & 1;
00396             }
00397             tmp -= M_PI * (double) qpd;
00398 
00399             /* get deviation from bin frequency from the +/- Pi interval */
00400             tmp = osamp * tmp / (2. * M_PI);
00401 
00402             /* compute the k-th partials' true frequency */
00403             tmp = (double) k * freq_per_bin + tmp * freq_per_bin;
00404 
00405             /* store magnitude and true frequency in analysis arrays */
00406             ana_magn[k] = magn;
00407             ana_freq[k] = tmp;
00408 
00409          }
00410 
00411          /* ***************** PROCESSING ******************* */
00412          /* this does the actual pitch shifting */
00413          memset(sys_magn, 0, fft_frame_size * sizeof(float));
00414          memset(syn_freq, 0, fft_frame_size * sizeof(float));
00415          for (k = 0; k <= fft_frame_size2; k++) {
00416             index = k * pitchShift;
00417             if (index <= fft_frame_size2) {
00418                sys_magn[index] += ana_magn[k];
00419                syn_freq[index] = ana_freq[k] * pitchShift;
00420             }
00421          }
00422 
00423          /* ***************** SYNTHESIS ******************* */
00424          /* this is the synthesis step */
00425          for (k = 0; k <= fft_frame_size2; k++) {
00426 
00427             /* get magnitude and true frequency from synthesis arrays */
00428             magn = sys_magn[k];
00429             tmp = syn_freq[k];
00430 
00431             /* subtract bin mid frequency */
00432             tmp -= (double) k * freq_per_bin;
00433 
00434             /* get bin deviation from freq deviation */
00435             tmp /= freq_per_bin;
00436 
00437             /* take osamp into account */
00438             tmp = 2. * M_PI * tmp / osamp;
00439 
00440             /* add the overlap phase advance back in */
00441             tmp += (double) k * expct;
00442 
00443             /* accumulate delta phase to get bin phase */
00444             sum_phase[k] += tmp;
00445             phase = sum_phase[k];
00446 
00447             /* get real and imag part and re-interleave */
00448             fft_worksp[2*k] = magn * cos(phase);
00449             fft_worksp[2*k+1] = magn * sin(phase);
00450          }
00451 
00452          /* zero negative frequencies */
00453          for (k = fft_frame_size + 2; k < 2 * fft_frame_size; k++) {
00454             fft_worksp[k] = 0.;
00455          }
00456 
00457          /* do inverse transform */
00458          smb_fft(fft_worksp, fft_frame_size, 1);
00459 
00460          /* do windowing and add to output accumulator */
00461          for (k = 0; k < fft_frame_size; k++) {
00462             window = -.5 * cos(2. * M_PI * (double) k / (double) fft_frame_size) + .5;
00463             output_accum[k] += 2. * window * fft_worksp[2*k] / (fft_frame_size2 * osamp);
00464          }
00465          for (k = 0; k < step_size; k++) {
00466             out_fifo[k] = output_accum[k];
00467          }
00468 
00469          /* shift accumulator */
00470          memmove(output_accum, output_accum+step_size, fft_frame_size * sizeof(float));
00471 
00472          /* move input FIFO */
00473          for (k = 0; k < in_fifo_latency; k++) {
00474             in_fifo[k] = in_fifo[k+step_size];
00475          }
00476       }
00477    }
00478 }

static int unload_module ( void   )  [static]

Definition at line 501 of file func_pitchshift.c.

References ast_custom_function_unregister().

00502 {
00503    return ast_custom_function_unregister(&pitch_shift_function);
00504 }


Variable Documentation

Initial value:

 {
   .name = "PITCH_SHIFT",
   .write = pitchshift_helper,
}

Definition at line 496 of file func_pitchshift.c.

Initial value:

 {
   .type = "pitchshift",
   .destroy = destroy_callback
}

Definition at line 161 of file func_pitchshift.c.


Generated on Thu Apr 16 06:31:21 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6