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
22 #include "bcdisplayinfo.h"
37 // It needs to be at least 40Hz yet high enough to have enough precision
38 #define WINDOW_SIZE 2048
41 //#define WINDOW_SIZE 131072
43 REGISTER_PLUGIN(PitchEffect);
49 PitchEffect::PitchEffect(PluginServer *server)
50 : PluginAClient(server)
56 PitchEffect::~PitchEffect()
63 const char* PitchEffect::plugin_title() { return _("Pitch shift"); }
64 int PitchEffect::is_realtime() { return 1; }
68 void PitchEffect::read_data(KeyFrame *keyframe)
71 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
76 result = input.read_tag();
80 if(input.tag.title_is("PITCH"))
82 config.scale = input.tag.get_property("SCALE", config.scale);
83 config.size = input.tag.get_property("SIZE", config.size);
89 void PitchEffect::save_data(KeyFrame *keyframe)
92 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
94 output.tag.set_title("PITCH");
95 output.tag.set_property("SCALE", config.scale);
96 output.tag.set_property("SIZE", config.size);
98 output.append_newline();
100 output.terminate_string();
105 LOAD_CONFIGURATION_MACRO(PitchEffect, PitchConfig)
107 NEW_WINDOW_MACRO(PitchEffect, PitchWindow)
111 void PitchEffect::reset()
116 void PitchEffect::update_gui()
120 if(load_configuration())
122 thread->window->lock_window("PitchEffect::update_gui");
123 ((PitchWindow*)thread->window)->update();
124 thread->window->unlock_window();
131 int PitchEffect::process_buffer(int64_t size,
133 int64_t start_position,
136 //printf("PitchEffect::process_buffer %d\n", __LINE__);
137 load_configuration();
139 if(fft && config.size != fft->window_size)
147 fft = new PitchFFT(this);
148 fft->initialize(config.size);
151 fft->process_buffer(start_position,
167 PitchFFT::PitchFFT(PitchEffect *plugin)
170 this->plugin = plugin;
179 PitchFFT::~PitchFFT()
181 delete [] last_phase;
190 int PitchFFT::signal_process()
193 double scale = plugin->config.scale;
194 double oversample = 1.0;
198 last_phase = new double[window_size];
199 new_freq = new double[window_size];
200 new_magn = new double[window_size];
201 sum_phase = new double[window_size];
202 anal_magn = new double[window_size];
203 anal_freq = new double[window_size];
204 memset (last_phase, 0, window_size * sizeof(double));
205 memset (sum_phase, 0, window_size * sizeof(double));
208 memset(new_freq, 0, window_size * sizeof(double));
209 memset(new_magn, 0, window_size * sizeof(double));
211 // expected phase difference between windows
212 double expected_phase_diff = 2.0 * M_PI / oversample;
214 double freq_per_bin = (double)plugin->PluginAClient::project_sample_rate / window_size;
216 for (int i = 0; i < window_size / 2; i++)
218 // Convert to magnitude and phase
219 double magn = sqrt(freq_real[i] * freq_real[i] + freq_imag[i] * freq_imag[i]);
220 double phase = atan2(freq_imag[i], freq_real[i]);
222 // Remember last phase
223 double temp = phase - last_phase[i];
224 last_phase[i] = phase;
226 // Substract the expected advancement of phase
227 temp -= (double)i * expected_phase_diff;
230 // wrap temp into -/+ PI ... good trick!
231 int qpd = (int)(temp/M_PI);
236 temp -= M_PI * (double)qpd;
238 // Deviation from bin frequency
239 temp = oversample * temp / (2.0 * M_PI);
241 temp = (double)(temp + i) * freq_per_bin;
243 // Now temp is the real freq... move it!
244 int new_bin = (int)(i * scale);
245 if (new_bin >= 0 && new_bin < window_size / 2)
247 new_freq[new_bin] = temp*scale;
248 new_magn[new_bin] += magn;
256 // Synthesize back the fft window
257 for (int i = 0; i < window_size / 2; i++)
259 double magn = new_magn[i];
260 double temp = new_freq[i];
261 // substract the bin frequency
262 temp -= (double)(i) * freq_per_bin;
264 // get bin deviation from freq deviation
265 temp /= freq_per_bin;
268 temp = 2.0 * M_PI * temp / oversample;
270 // add back the expected phase difference (that we substracted in analysis)
271 temp += (double)(i) * expected_phase_diff;
273 // accumulate delta phase, to get bin phase
274 sum_phase[i] += temp;
276 double phase = sum_phase[i];
278 freq_real[i] = magn * cos(phase);
279 freq_imag[i] = magn * sin(phase);
282 for (int i = window_size / 2; i < window_size; i++)
292 1 + (int)(20.0 / ((double)plugin->PluginAClient::project_sample_rate /
293 window_size * 2) + 0.5);
294 if(plugin->config.scale < 1)
296 for(int i = min_freq; i < window_size / 2; i++)
298 double destination = i * plugin->config.scale;
299 int dest_i = (int)(destination + 0.5);
302 if(dest_i <= window_size / 2)
304 freq_real[dest_i] = freq_real[i];
305 freq_imag[dest_i] = freq_imag[i];
313 if(plugin->config.scale > 1)
315 for(int i = window_size / 2 - 1; i >= min_freq; i--)
317 double destination = i * plugin->config.scale;
318 int dest_i = (int)(destination + 0.5);
321 if(dest_i <= window_size / 2)
323 freq_real[dest_i] = freq_real[i];
324 freq_imag[dest_i] = freq_imag[i];
332 symmetry(window_size, freq_real, freq_imag);
339 int PitchFFT::read_samples(int64_t output_sample,
343 return plugin->read_samples(buffer,
345 plugin->get_samplerate(),
356 PitchConfig::PitchConfig()
362 int PitchConfig::equivalent(PitchConfig &that)
364 return EQUIV(scale, that.scale) && size == that.size;
367 void PitchConfig::copy_from(PitchConfig &that)
373 void PitchConfig::interpolate(PitchConfig &prev,
377 int64_t current_frame)
379 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
380 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
381 scale = prev.scale * prev_scale + next.scale * next_scale;
401 PitchWindow::PitchWindow(PitchEffect *plugin)
402 : PluginClientWindow(plugin,
409 this->plugin = plugin;
412 void PitchWindow::create_objects()
414 int x1 = 10, x = 10, y = 10;
417 add_subwindow(title = new BC_Title(x, y, _("Scale:")));
418 x += title->get_w() + plugin->get_theme()->widget_border;
419 add_subwindow(scale = new PitchScale(plugin, x, y));
421 y += scale->get_h() + plugin->get_theme()->widget_border;
422 add_subwindow(title = new BC_Title(x, y, _("Window Size:")));
423 y += title->get_h() + plugin->get_theme()->widget_border;
424 add_subwindow(size = new PitchSize(this, plugin, x, y));
425 size->create_objects();
426 size->update(plugin->config.size);
433 void PitchWindow::update()
435 scale->update(plugin->config.scale);
436 size->update(plugin->config.size);
450 PitchScale::PitchScale(PitchEffect *plugin, int x, int y)
451 : BC_FPot(x, y, (float)plugin->config.scale, .5, 1.5)
453 this->plugin = plugin;
457 int PitchScale::handle_event()
459 plugin->config.scale = get_value();
460 plugin->send_configure_change();
466 PitchSize::PitchSize(PitchWindow *window, PitchEffect *plugin, int x, int y)
467 : BC_PopupMenu(x, y, 100, "4096", 1)
469 this->plugin = plugin;
472 int PitchSize::handle_event()
474 plugin->config.size = atoi(get_text());
475 plugin->send_configure_change();
479 void PitchSize::create_objects()
481 add_item(new BC_MenuItem("2048"));
482 add_item(new BC_MenuItem("4096"));
483 add_item(new BC_MenuItem("8192"));
484 add_item(new BC_MenuItem("16384"));
485 add_item(new BC_MenuItem("32768"));
486 add_item(new BC_MenuItem("65536"));
487 add_item(new BC_MenuItem("131072"));
488 add_item(new BC_MenuItem("262144"));
491 void PitchSize::update(int size)
493 char string[BCTEXTLEN];
494 sprintf(string, "%d", size);