129#define M_PI 3.14159265358979323846
131#define MAX_FRAME_LENGTH 256
162static void smb_fft(
float *fft_buffer,
long fft_frame_size,
long sign);
175 .
type =
"pitchshift",
196 shift = datastore->
data;
226 if (!(shift =
ast_calloc(1,
sizeof(*shift)))) {
233 datastore->
data = shift;
237 shift = datastore->
data;
241 if (!strcasecmp(
value,
"highest")) {
243 }
else if (!strcasecmp(
value,
"higher")) {
245 }
else if (!strcasecmp(
value,
"high")) {
247 }
else if (!strcasecmp(
value,
"lowest")) {
249 }
else if (!strcasecmp(
value,
"lower")) {
251 }
else if (!strcasecmp(
value,
"low")) {
254 if (!sscanf(
value,
"%30f", &amount) || (amount <= 0) || (amount > 4)) {
259 if (!strcasecmp(data,
"rx")) {
261 }
else if (!strcasecmp(data,
"tx")) {
263 }
else if (!strcasecmp(data,
"both")) {
288static void smb_fft(
float *fft_buffer,
long fft_frame_size,
long sign)
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;
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) {
302 p1 = fft_buffer + i; p2 = fft_buffer + j;
303 temp = *p1; *(p1++) = *p2;
304 *(p2++) = temp; temp = *p1;
305 *p1 = *p2; *p2 = temp;
308 for (k = 0, le = 2; k < (long) (log(fft_frame_size) / log(2.) + .5); k++) {
313 arg =
M_PI / (le2>>1);
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;
327 tr = ur * wr - ui * wi;
328 ui = ur * wi + ui * wr;
348 double freq_per_bin, expect;
349 long i,k, qpd, index, in_fifo_latency,
step_size, fft_frame_size2;
352 fft_frame_size2 = fft_frame_size / 2;
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;
363 for (i = 0; i < num_samps_to_process; i++){
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.;
383 smb_fft(fft_worksp, fft_frame_size, -1);
386 for (k = 0; k <= fft_frame_size2; k++) {
389 real = fft_worksp[2*k];
390 imag = fft_worksp[2*k+1];
393 magn = 2. * sqrt(
real *
real + imag * imag);
394 phase = atan2(imag,
real);
397 tmp = phase - last_phase[k];
398 last_phase[k] = phase;
401 tmp -= (double) k * expect;
416 tmp = (double) k * freq_per_bin +
tmp * freq_per_bin;
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;
438 for (k = 0; k <= fft_frame_size2; k++) {
445 tmp -= (double) k * freq_per_bin;
454 tmp += (double) k * expect;
458 phase = sum_phase[k];
461 fft_worksp[2*k] = magn *
cos(phase);
462 fft_worksp[2*k+1] = magn * sin(phase);
466 for (k = fft_frame_size + 2; k < 2 * fft_frame_size; k++) {
471 smb_fft(fft_worksp, fft_frame_size, 1);
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);
479 out_fifo[k] = output_accum[k];
483 memmove(output_accum, output_accum+
step_size, fft_frame_size *
sizeof(
float));
486 for (k = 0; k < in_fifo_latency; k++) {
499 if (!amount || amount == 1 || !fun || (f->
samples % 32)) {
502 for (samples = 0; samples < f->
samples; samples += 32) {
510 .
name =
"PITCH_SHIFT",
Asterisk main include file. File version handling, generic pbx functions.
#define ast_calloc(num, len)
A wrapper for calloc()
@ AST_AUDIOHOOK_MANIPULATE_ALL_RATES
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.
@ AST_AUDIOHOOK_DIRECTION_WRITE
int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
Attach audiohook to channel.
int ast_audiohook_destroy(struct ast_audiohook *audiohook)
Destroys an audiohook structure.
@ AST_AUDIOHOOK_TYPE_MANIPULATE
@ AST_AUDIOHOOK_STATUS_DONE
General Asterisk PBX channel definitions.
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
#define ast_channel_lock(chan)
#define ast_channel_unlock(chan)
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.
static int step_size(struct g726_state *state_ptr)
#define ast_datastore_alloc(info, uid)
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
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 struct ast_custom_function pitch_shift_function
static const struct ast_datastore_info pitchshift_datastore
static void smb_fft(float *fft_buffer, long fft_frame_size, long sign)
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 unload_module(void)
static int pitchshift_cb(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *f, enum ast_audiohook_direction direction)
static int pitch_shift(struct ast_frame *f, float amount, struct fft_data *fft_data)
static int pitchshift_helper(struct ast_channel *chan, const char *cmd, char *data, const char *value)
Asterisk module definitions.
#define ASTERISK_GPL_KEY
The text the key() function should return.
@ AST_MODULE_LOAD_SUCCESS
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Core PBX routines and definitions.
#define ast_custom_function_register(acf)
Register a custom function.
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
ast_audiohook_manipulate_callback manipulate_callback
enum ast_audiohook_status status
Main Channel structure associated with a channel.
Data structure associated with a custom dialplan function.
Structure for a data store type.
Structure for a data store object.
struct ast_format * format
Data structure associated with a single frame of data.
struct ast_frame_subclass subclass
union ast_frame::@226 data
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]
struct ast_audiohook audiohook