table_type get_window_samples(LVAL window, sample_type **samples, long *len) { table_type result = NULL; if (soundp(window)) { sound_type window_sound = getsound(window); xlprot1(window); /* maybe not necessary */ result = sound_to_table(window_sound); xlpop(); *samples = result->samples; *len = (long) (result->length + 0.5); } return result; }
sound_type snd_make_convolve(sound_type x_snd, sound_type h_snd) { register convolve_susp_type susp; rate_type sr = x_snd->sr; time_type t0 = x_snd->t0; sample_type scale_factor = 1.0F; time_type t0_min = t0; table_type table; double log_len; falloc_generic(susp, convolve_susp_node, "snd_make_convolve"); table = sound_to_table(h_snd); susp->h_len = table->length; log_len = log(table->length) / M_LN2; /* compute log-base-2(length) */ susp->M = (int) log_len; if (susp->M != log_len) susp->M++; /* round up */ susp->N = 1 << susp->M; /* size of data blocks */ susp->M++; /* M = log2(2 * N) */ susp->H = (sample_type *) calloc(2 * susp->N, sizeof(susp->H[0])); if (!susp->H) { xlabort("memory allocation failure in convolve"); } memcpy(susp->H, table->samples, sizeof(susp->H[0]) * susp->N); table_unref(table); /* don't need table now */ /* remaining N samples are already zero-filled */ if (fftInit(susp->M)) { free(susp->H); xlabort("fft initialization error in convolve"); } rffts(susp->H, susp->M, 1); susp->X = (sample_type *) calloc(2 * susp->N, sizeof(susp->X[0])); susp->R = (sample_type *) calloc(2 * susp->N, sizeof(susp->R[0])); if (!susp->X || !susp->R) { free(susp->H); if (susp->X) free(susp->X); if (susp->R) free(susp->R); xlabort("memory allocation failed in convolve"); } susp->R_current = susp->R + susp->N; susp->susp.fetch = &convolve_s_fetch; susp->terminate_cnt = UNKNOWN; /* handle unequal start times, if any */ if (t0 < x_snd->t0) sound_prepend_zeros(x_snd, t0); /* minimum start time over all inputs: */ t0_min = min(x_snd->t0, t0); /* how many samples to toss before t0: */ susp->susp.toss_cnt = (long) ((t0 - t0_min) * sr + 0.5); if (susp->susp.toss_cnt > 0) { susp->susp.keep_fetch = susp->susp.fetch; susp->susp.fetch = convolve_toss_fetch; } /* initialize susp state */ susp->susp.free = convolve_free; susp->susp.sr = sr; susp->susp.t0 = t0; susp->susp.mark = convolve_mark; susp->susp.print_tree = convolve_print_tree; susp->susp.name = "convolve"; susp->logically_stopped = false; susp->susp.log_stop_cnt = logical_stop_cnt_cvt(x_snd); susp->susp.current = 0; susp->x_snd = x_snd; susp->x_snd_cnt = 0; return sound_create((snd_susp_type)susp, t0, sr, scale_factor); }