114 #define M_PI 3.14159265358979323846 116 #define MAX_FRAME_LENGTH 256 147 static void smb_fft(
float *fft_buffer,
long fft_frame_size,
long sign);
160 .
type =
"pitchshift",
181 shift = datastore->
data;
211 if (!(shift =
ast_calloc(1,
sizeof(*shift)))) {
218 datastore->
data = shift;
222 shift = datastore->
data;
226 if (!strcasecmp(value,
"highest")) {
228 }
else if (!strcasecmp(value,
"higher")) {
230 }
else if (!strcasecmp(value,
"high")) {
232 }
else if (!strcasecmp(value,
"lowest")) {
234 }
else if (!strcasecmp(value,
"lower")) {
236 }
else if (!strcasecmp(value,
"low")) {
239 if (!sscanf(value,
"%30f", &amount) || (amount <= 0) || (amount > 4)) {
244 if (!strcasecmp(data,
"rx")) {
246 }
else if (!strcasecmp(data,
"tx")) {
248 }
else if (!strcasecmp(data,
"both")) {
273 static void smb_fft(
float *fft_buffer,
long fft_frame_size,
long sign)
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;
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) {
287 p1 = fft_buffer + i; p2 = fft_buffer + j;
288 temp = *p1; *(p1++) = *p2;
289 *(p2++) = temp; temp = *p1;
290 *p1 = *p2; *p2 = temp;
293 for (k = 0, le = 2; k < (long) (log(fft_frame_size) / log(2.) + .5); k++) {
298 arg =
M_PI / (le2>>1);
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;
312 tr = ur * wr - ui * wi;
313 ui = ur * wi + ui * wr;
333 double freq_per_bin, expct;
334 long i,k, qpd, index, in_fifo_latency,
step_size, fft_frame_size2;
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;
343 if (fft_data->
gRover == 0) {
344 fft_data->
gRover = in_fifo_latency;
348 for (i = 0; i < num_samps_to_process; i++){
351 in_fifo[fft_data->
gRover] = indata[i];
352 outdata[i] = out_fifo[fft_data->
gRover - in_fifo_latency];
356 if (fft_data->
gRover >= fft_frame_size) {
357 fft_data->
gRover = in_fifo_latency;
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.;
368 smb_fft(fft_worksp, fft_frame_size, -1);
371 for (k = 0; k <= fft_frame_size2; k++) {
374 real = fft_worksp[2*k];
375 imag = fft_worksp[2*k+1];
378 magn = 2. * sqrt(real * real + imag * imag);
379 phase = atan2(imag, real);
382 tmp = phase - last_phase[k];
383 last_phase[k] = phase;
386 tmp -= (double) k * expct;
395 tmp -=
M_PI * (double) qpd;
398 tmp = osamp * tmp / (2. *
M_PI);
401 tmp = (double) k * freq_per_bin + tmp * freq_per_bin;
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;
423 for (k = 0; k <= fft_frame_size2; k++) {
430 tmp -= (double) k * freq_per_bin;
436 tmp = 2. *
M_PI * tmp / osamp;
439 tmp += (double) k * expct;
443 phase = sum_phase[k];
446 fft_worksp[2*k] = magn *
cos(phase);
447 fft_worksp[2*k+1] = magn * sin(phase);
451 for (k = fft_frame_size + 2; k < 2 * fft_frame_size; k++) {
456 smb_fft(fft_worksp, fft_frame_size, 1);
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);
464 out_fifo[k] = output_accum[k];
468 memmove(output_accum, output_accum+step_size, fft_frame_size *
sizeof(
float));
471 for (k = 0; k < in_fifo_latency; k++) {
484 if (!amount || amount == 1 || !fun || (f->
samples % 32)) {
487 for (samples = 0; samples < f->
samples; samples += 32) {
495 .
name =
"PITCH_SHIFT",
#define ast_channel_lock(chan)
Main Channel structure associated with a channel.
Asterisk main include file. File version handling, generic pbx functions.
struct ast_audiohook audiohook
float out_fifo[MAX_FRAME_LENGTH]
float ana_magn[MAX_FRAME_LENGTH]
float output_accum[2 *MAX_FRAME_LENGTH]
static int pitchshift_helper(struct ast_channel *chan, const char *cmd, char *data, const char *value)
float ana_freq[MAX_FRAME_LENGTH]
Structure for a data store type.
static int step_size(struct g726_state *state_ptr)
int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
Attach audiohook to channel.
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.
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.
float in_fifo[MAX_FRAME_LENGTH]
int ast_audiohook_destroy(struct ast_audiohook *audiohook)
Destroys an audiohook structure.
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)
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.
struct ast_frame_subclass subclass
ast_audiohook_manipulate_callback manipulate_callback
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.
float fft_worksp[2 *MAX_FRAME_LENGTH]
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Audio Effects Dialplan Functions")
General Asterisk PBX channel definitions.
Data structure associated with a custom dialplan function.
float syn_freq[MAX_FRAME_LENGTH]
static void destroy_callback(void *data)
static struct ast_custom_function pitch_shift_function
Core PBX routines and definitions.
static int unload_module(void)
static int load_module(void)
#define ast_channel_unlock(chan)
#define ast_calloc(num, len)
A wrapper for calloc()
float sys_magn[MAX_FRAME_LENGTH]
Module has failed to load, may be in an inconsistent state.
static int pitch_shift(struct ast_frame *f, float amount, struct fft_data *fft_data)
Data structure associated with a single frame of data.
enum ast_audiohook_status status
#define ast_datastore_alloc(info, uid)
union ast_frame::@263 data
float last_phase[MAX_FRAME_LENGTH/2+1]
struct ast_format * format
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
static void smb_fft(float *fft_buffer, long fft_frame_size, long sign)
#define ast_custom_function_register(acf)
Register a custom function.