4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "samples.inc"
33 static uint8_t rev_bytes[256];
34 static unsigned int reverse_bits(unsigned int index, unsigned int bits);
35 static void bit_reverse(unsigned int samples,
36 double *real_in, double *imag_in,
37 double *real_out, double *imag_out);
41 // can be in place, but imag_out must exist, imag_in optional
42 int do_fft(int samples, // must be a power of 2
43 int inverse, // 0 = forward FFT, 1 = inverse
44 double *real_in, double *imag_in, // complex input
45 double *real_out, double *imag_out); // complex output
46 int do_fft(int samples, int inverse, double *real, double *imag) {
47 return do_fft(samples, inverse, real, imag, real, imag);
49 static unsigned int samples_to_bits(unsigned int samples);
50 static int symmetry(int size, double *freq_real, double *freq_imag);
51 virtual int update_progress(int current_position);
54 class CrossfadeFFT : public FFT
58 virtual ~CrossfadeFFT();
61 int initialize(int window_size, int bands = 1);
62 long get_delay(); // Number of samples fifo is delayed
64 int fix_window_size();
68 // Read enough samples from input to generate the requested number of samples.
69 // output_sample - tells it if we've seeked and the output overflow is invalid.
70 // return - 0 on success 1 on failure
71 // output_sample - start of samples if forward. End of samples if reverse.
72 // It's always contiguous.
73 // output_ptr - if nonzero, output is put here
74 // direction - PLAY_FORWARD or PLAY_REVERSE
75 int process_buffer(int64_t output_sample,
76 long size, Samples *output_ptr, int direction);
77 // multiband processing
78 int process_buffer(int64_t output_sample,
79 long size, Samples **output_ptr, int direction);
81 // Called by process_buffer to read samples from input.
82 // Returns 1 on error or 0 on success.
83 virtual int read_samples(int64_t output_sample,
84 int samples, Samples *buffer);
86 // Process a window in the frequency domain
87 virtual int signal_process();
88 // Process a window in the time domain after the frequency domain
89 virtual int post_process();
91 virtual int signal_process(int band);
92 virtual int post_process(int band);
94 // Size of a window. Automatically fixed to a power of 2
97 // Time domane input of complete windows
98 Samples *input_buffer;
99 // Frequency domain output of FFT
102 // Time domain output of FFT
107 void allocate_output(int new_allocation);
109 // output of crossfaded windows with overflow. 1 buffer for each band
110 double **output_buffer;
112 // backup frequency domain for multiband
116 // samples in input_buffer including the tail
119 long input_allocation;
120 // Samples in output buffer less window border
122 // Space in output buffer including window border
123 long output_allocation;
124 // Starting sample of output buffer in project
125 int64_t output_sample;
126 // Starting sample of input buffer in project
127 int64_t input_sample;
128 // Don't crossfade the first window