| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
#include "asterisk.h" |
| 66 |
|
| 67 |
#include "asterisk/module.h" |
| 68 |
#include "asterisk/channel.h" |
| 69 |
#include "asterisk/pbx.h" |
| 70 |
#include "asterisk/utils.h" |
| 71 |
#include "asterisk/audiohook.h" |
| 72 |
#include <math.h> |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
#ifndef M_PI |
| 132 |
#define M_PI 3.14159265358979323846 |
| 133 |
#endif |
| 134 |
#define MAX_FRAME_LENGTH 256 |
| 135 |
|
| 136 |
#define HIGHEST 2 |
| 137 |
#define HIGHER 1.5 |
| 138 |
#define HIGH 1.25 |
| 139 |
#define LOW .85 |
| 140 |
#define LOWER .7 |
| 141 |
#define LOWEST .5 |
| 142 |
|
| 143 |
struct fft_data { |
| 144 |
float in_fifo[MAX_FRAME_LENGTH]; |
| 145 |
float out_fifo[MAX_FRAME_LENGTH]; |
| 146 |
float fft_worksp[2*MAX_FRAME_LENGTH]; |
| 147 |
float last_phase[MAX_FRAME_LENGTH/2+1]; |
| 148 |
float sum_phase[MAX_FRAME_LENGTH/2+1]; |
| 149 |
float output_accum[2*MAX_FRAME_LENGTH]; |
| 150 |
float ana_freq[MAX_FRAME_LENGTH]; |
| 151 |
float ana_magn[MAX_FRAME_LENGTH]; |
| 152 |
float syn_freq[MAX_FRAME_LENGTH]; |
| 153 |
float sys_magn[MAX_FRAME_LENGTH]; |
| 154 |
long gRover; |
| 155 |
float shift_amount; |
| 156 |
}; |
| 157 |
|
| 158 |
struct pitchshift_data { |
| 159 |
struct ast_audiohook audiohook; |
| 160 |
|
| 161 |
struct fft_data rx; |
| 162 |
struct fft_data tx; |
| 163 |
}; |
| 164 |
|
| 165 |
static void smb_fft(float *fft_buffer, long fft_frame_size, long sign); |
| 166 |
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); |
| 167 |
static int pitch_shift(struct ast_frame *f, float amount, struct fft_data *fft_data); |
| 168 |
|
| 169 |
static void destroy_callback(void *data) |
| 170 |
{ |
| 171 |
struct pitchshift_data *shift = data; |
| 172 |
|
| 173 |
ast_audiohook_destroy(&shift->audiohook); |
| 174 |
ast_free(shift); |
| 175 |
}; |
| 176 |
|
| 177 |
static const struct ast_datastore_info pitchshift_datastore = { |
| 178 |
.type = "pitchshift", |
| 179 |
.destroy = destroy_callback |
| 180 |
}; |
| 181 |
|
| 182 |
static int pitchshift_cb(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *f, enum ast_audiohook_direction direction) |
| 183 |
{ |
| 184 |
struct ast_datastore *datastore = NULL; |
| 185 |
struct pitchshift_data *shift = NULL; |
| 186 |
|
| 187 |
|
| 188 |
if (!f) { |
| 189 |
return 0; |
| 190 |
} |
| 191 |
if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE) { |
| 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 |
|
| 201 |
if (direction == AST_AUDIOHOOK_DIRECTION_WRITE) { |
| 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 |
} |
| 209 |
|
| 210 |
static int pitchshift_helper(struct ast_channel *chan, const char *cmd, char *data, const char *value) |
| 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 |
|
| 234 |
ast_audiohook_init(&shift->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "pitch_shift", AST_AUDIOHOOK_MANIPULATE_ALL_RATES); |
| 235 |
shift->audiohook.manipulate_callback = pitchshift_cb; |
| 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 |
|
| 282 |
cleanup_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 |
} |
| 290 |
|
| 291 |
static void smb_fft(float *fft_buffer, long fft_frame_size, long sign) |
| 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 |
} |
| 336 |
|
| 337 |
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) |
| 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 |
|
| 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 |
|
| 366 |
for (i = 0; i < num_samps_to_process; i++){ |
| 367 |
|
| 368 |
|
| 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 |
|
| 374 |
if (fft_data->gRover >= fft_frame_size) { |
| 375 |
fft_data->gRover = in_fifo_latency; |
| 376 |
|
| 377 |
|
| 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 |
|
| 385 |
|
| 386 |
smb_fft(fft_worksp, fft_frame_size, -1); |
| 387 |
|
| 388 |
|
| 389 |
for (k = 0; k <= fft_frame_size2; k++) { |
| 390 |
|
| 391 |
|
| 392 |
real = fft_worksp[2*k]; |
| 393 |
imag = fft_worksp[2*k+1]; |
| 394 |
|
| 395 |
|
| 396 |
magn = 2. * sqrt(real * real + imag * imag); |
| 397 |
phase = atan2(imag, real); |
| 398 |
|
| 399 |
|
| 400 |
tmp = phase - last_phase[k]; |
| 401 |
last_phase[k] = phase; |
| 402 |
|
| 403 |
|
| 404 |
tmp -= (double) k * expect; |
| 405 |
|
| 406 |
|
| 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 |
|
| 416 |
tmp = osamp * tmp / (2. * M_PI); |
| 417 |
|
| 418 |
|
| 419 |
tmp = (double) k * freq_per_bin + tmp * freq_per_bin; |
| 420 |
|
| 421 |
|
| 422 |
ana_magn[k] = magn; |
| 423 |
ana_freq[k] = tmp; |
| 424 |
|
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 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 |
|
| 440 |
|
| 441 |
for (k = 0; k <= fft_frame_size2; k++) { |
| 442 |
|
| 443 |
|
| 444 |
magn = sys_magn[k]; |
| 445 |
tmp = syn_freq[k]; |
| 446 |
|
| 447 |
|
| 448 |
tmp -= (double) k * freq_per_bin; |
| 449 |
|
| 450 |
|
| 451 |
tmp /= freq_per_bin; |
| 452 |
|
| 453 |
|
| 454 |
tmp = 2. * M_PI * tmp / osamp; |
| 455 |
|
| 456 |
|
| 457 |
tmp += (double) k * expect; |
| 458 |
|
| 459 |
|
| 460 |
sum_phase[k] += tmp; |
| 461 |
phase = sum_phase[k]; |
| 462 |
|
| 463 |
|
| 464 |
fft_worksp[2*k] = magn * cos(phase); |
| 465 |
fft_worksp[2*k+1] = magn * sin(phase); |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
for (k = fft_frame_size + 2; k < 2 * fft_frame_size; k++) { |
| 470 |
fft_worksp[k] = 0.; |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
smb_fft(fft_worksp, fft_frame_size, 1); |
| 475 |
|
| 476 |
|
| 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 |
|
| 486 |
memmove(output_accum, output_accum+step_size, fft_frame_size * sizeof(float)); |
| 487 |
|
| 488 |
|
| 489 |
for (k = 0; k < in_fifo_latency; k++) { |
| 490 |
in_fifo[k] = in_fifo[k+step_size]; |
| 491 |
} |
| 492 |
} |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
static int pitch_shift(struct ast_frame *f, float amount, struct fft_data *fft) |
| 497 |
{ |
| 498 |
int16_t *fun = (int16_t *) f->data.ptr; |
| 499 |
int samples; |
| 500 |
|
| 501 |
|
| 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 |
} |
| 511 |
|
| 512 |
static struct ast_custom_function pitch_shift_function = { |
| 513 |
.name = "PITCH_SHIFT", |
| 514 |
.write = pitchshift_helper, |
| 515 |
}; |
| 516 |
|
| 517 |
static int unload_module(void) |
| 518 |
{ |
| 519 |
return ast_custom_function_unregister(&pitch_shift_function); |
| 520 |
} |
| 521 |
|
| 522 |
static int load_module(void) |
| 523 |
{ |
| 524 |
int res = ast_custom_function_register(&pitch_shift_function); |
| 525 |
return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS; |
| 526 |
} |
| 527 |
|
| 528 |
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Audio Effects Dialplan Functions"); |