You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1058 lines
34KB

  1. /*
  2. Copyright (C) 2006-2011 Nasca Octavian Paul
  3. Author: Nasca Octavian Paul
  4. Copyright (C) 2017 Xenakios
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of version 2 of the GNU General Public License
  7. as published by the Free Software Foundation.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License (version 2) for more details.
  12. You should have received a copy of the GNU General Public License (version 2)
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include "PluginProcessor.h"
  17. #include "PluginEditor.h"
  18. #include <array>
  19. extern String g_plugintitle;
  20. //==============================================================================
  21. PaulstretchpluginAudioProcessorEditor::PaulstretchpluginAudioProcessorEditor(PaulstretchpluginAudioProcessor& p)
  22. : AudioProcessorEditor(&p),
  23. m_wavecomponent(p.m_afm,p.m_thumb.get()),
  24. processor(p), m_perfmeter(&p)
  25. {
  26. addAndMakeVisible(&m_perfmeter);
  27. addAndMakeVisible(&m_import_button);
  28. m_import_button.setButtonText("Import file...");
  29. m_import_button.onClick = [this]() { chooseFile(); };
  30. addAndMakeVisible(&m_settings_button);
  31. m_settings_button.setButtonText("Settings...");
  32. m_settings_button.onClick = [this]() { showSettingsMenu(); };
  33. addAndMakeVisible(&m_info_label);
  34. m_info_label.setJustificationType(Justification::centredRight);
  35. m_wavecomponent.GetFileCallback = [this]() { return processor.getAudioFile(); };
  36. addAndMakeVisible(&m_wavecomponent);
  37. const auto& pars = processor.getParameters();
  38. for (int i=0;i<pars.size();++i)
  39. {
  40. AudioProcessorParameterWithID* parid = dynamic_cast<AudioProcessorParameterWithID*>(pars[i]);
  41. jassert(parid);
  42. bool notifyonlyonrelease = false;
  43. if (parid->paramID.startsWith("fftsize") || parid->paramID.startsWith("numoutchans")
  44. || parid->paramID.startsWith("numinchans"))
  45. notifyonlyonrelease = true;
  46. m_parcomps.push_back(std::make_shared<ParameterComponent>(pars[i],notifyonlyonrelease));
  47. int group_id = -1;
  48. if (i == cpi_harmonicsbw || i == cpi_harmonicsfreq || i == cpi_harmonicsgauss || i == cpi_numharmonics)
  49. group_id = 0;
  50. if (i == cpi_octavesm2 || i == cpi_octavesm1 || i == cpi_octaves0 || i == cpi_octaves1 || i == cpi_octaves15 ||
  51. i == cpi_octaves2)
  52. group_id = 4;
  53. if (i == cpi_tonalvsnoisebw || i == cpi_tonalvsnoisepreserve)
  54. group_id = 1;
  55. if (i == cpi_filter_low || i == cpi_filter_high)
  56. group_id = 6;
  57. if (i == cpi_compress)
  58. group_id = 7;
  59. if (i == cpi_spreadamount)
  60. group_id = 5;
  61. if (i == cpi_frequencyshift)
  62. group_id = 2;
  63. if (i == cpi_pitchshift)
  64. group_id = 3;
  65. m_parcomps.back()->m_group_id = group_id;
  66. addAndMakeVisible(m_parcomps.back().get());
  67. }
  68. //addAndMakeVisible(&m_specvis);
  69. setSize (1000, 30+(pars.size()/2)*25+200);
  70. m_wavecomponent.TimeSelectionChangedCallback = [this](Range<double> range, int which)
  71. {
  72. *processor.getFloatParameter(cpi_soundstart) = range.getStart();
  73. *processor.getFloatParameter(cpi_soundend) = range.getEnd();
  74. };
  75. m_wavecomponent.CursorPosCallback = [this]()
  76. {
  77. return processor.getStretchSource()->getInfilePositionPercent();
  78. };
  79. m_wavecomponent.SeekCallback = [this](double pos)
  80. {
  81. if (processor.getStretchSource()->getPlayRange().contains(pos))
  82. processor.getStretchSource()->seekPercent(pos);
  83. };
  84. m_wavecomponent.ShowFileCacheRange = true;
  85. m_spec_order_ed.setSource(processor.getStretchSource());
  86. addAndMakeVisible(&m_spec_order_ed);
  87. m_spec_order_ed.ModuleSelectedCallback = [this](int id)
  88. {
  89. for (int i = 0; i < m_parcomps.size(); ++i)
  90. {
  91. if (m_parcomps[i]->m_group_id == id)
  92. m_parcomps[i]->setHighLighted(true);
  93. else
  94. m_parcomps[i]->setHighLighted(false);
  95. }
  96. };
  97. m_spec_order_ed.ModuleOrderOrEnabledChangedCallback = [this]()
  98. {
  99. processor.setDirty();
  100. };
  101. startTimer(1, 100);
  102. startTimer(2, 1000);
  103. startTimer(3, 200);
  104. m_wavecomponent.startTimer(100);
  105. }
  106. PaulstretchpluginAudioProcessorEditor::~PaulstretchpluginAudioProcessorEditor()
  107. {
  108. }
  109. void PaulstretchpluginAudioProcessorEditor::paint (Graphics& g)
  110. {
  111. g.fillAll(Colours::darkgrey);
  112. }
  113. void PaulstretchpluginAudioProcessorEditor::resized()
  114. {
  115. m_import_button.setBounds(1, 1, 60, 24);
  116. m_import_button.changeWidthToFitText();
  117. m_settings_button.setBounds(m_import_button.getRight() + 1, 1, 60, 24);
  118. m_settings_button.changeWidthToFitText();
  119. m_perfmeter.setBounds(m_settings_button.getRight() + 1, 1, 150, 24);
  120. m_info_label.setBounds(m_perfmeter.getRight() + 1, m_settings_button.getY(),
  121. getWidth()- m_perfmeter.getRight()-1, 24);
  122. int w = getWidth();
  123. int xoffs = 1;
  124. int yoffs = 30;
  125. int div = w / 4;
  126. //std::vector<std::vector<int>> layout;
  127. //layout.emplace_back(cpi_capture_enabled, cpi_passthrough, cpi_pause_enabled, cpi_freeze);
  128. //layout.emplace_back(cpi_main_volume, cpi_num_inchans, cpi_num_outchans);
  129. m_parcomps[cpi_capture_enabled]->setBounds(xoffs, yoffs, div-1, 24);
  130. //xoffs += div;
  131. //m_parcomps[cpi_max_capture_len]->setBounds(xoffs, yoffs, div - 1, 24);
  132. xoffs += div;
  133. m_parcomps[cpi_passthrough]->setBounds(xoffs, yoffs, div - 1, 24);
  134. xoffs += div;
  135. m_parcomps[cpi_pause_enabled]->setBounds(xoffs, yoffs, div-1, 24);
  136. xoffs += div;
  137. m_parcomps[cpi_freeze]->setBounds(xoffs, yoffs, div - 1, 24);
  138. xoffs = 1;
  139. yoffs += 25;
  140. div = w / 3;
  141. m_parcomps[cpi_main_volume]->setBounds(xoffs, yoffs, div-1, 24);
  142. xoffs += div;
  143. m_parcomps[cpi_num_inchans]->setBounds(xoffs, yoffs, div - 1, 24);
  144. xoffs += div;
  145. m_parcomps[cpi_num_outchans]->setBounds(xoffs, yoffs, div-1, 24);
  146. div = w / 2;
  147. xoffs = 1;
  148. yoffs += 25;
  149. m_parcomps[cpi_fftsize]->setBounds(xoffs, yoffs, div - 1, 24);
  150. xoffs += div;
  151. m_parcomps[cpi_stretchamount]->setBounds(xoffs, yoffs, div - 1, 24);
  152. xoffs = 1;
  153. yoffs += 25;
  154. m_parcomps[cpi_pitchshift]->setBounds(xoffs, yoffs, div - 1, 24);
  155. xoffs += div;
  156. m_parcomps[cpi_frequencyshift]->setBounds(xoffs, yoffs, div - 1, 24);
  157. xoffs = 1;
  158. yoffs += 25;
  159. m_parcomps[cpi_octavesm2]->setBounds(xoffs, yoffs, div - 1, 24);
  160. xoffs += div;
  161. m_parcomps[cpi_octavesm1]->setBounds(xoffs, yoffs, div - 1, 24);
  162. xoffs = 1;
  163. yoffs += 25;
  164. m_parcomps[cpi_octaves0]->setBounds(xoffs, yoffs, div - 1, 24);
  165. xoffs += div;
  166. m_parcomps[cpi_octaves1]->setBounds(xoffs, yoffs, div - 1, 24);
  167. xoffs = 1;
  168. yoffs += 25;
  169. m_parcomps[cpi_octaves15]->setBounds(xoffs, yoffs, div - 1, 24);
  170. xoffs += div;
  171. m_parcomps[cpi_octaves2]->setBounds(xoffs, yoffs, div - 1, 24);
  172. xoffs = 1;
  173. yoffs += 25;
  174. m_parcomps[cpi_numharmonics]->setBounds(xoffs, yoffs, div - 1, 24);
  175. xoffs += div;
  176. m_parcomps[cpi_harmonicsfreq]->setBounds(xoffs, yoffs, div - 1, 24);
  177. xoffs = 1;
  178. yoffs += 25;
  179. m_parcomps[cpi_harmonicsbw]->setBounds(xoffs, yoffs, div - 1, 24);
  180. xoffs += div;
  181. m_parcomps[cpi_harmonicsgauss]->setBounds(xoffs, yoffs, div - 1, 24);
  182. xoffs = 1;
  183. yoffs += 25;
  184. m_parcomps[cpi_spreadamount]->setBounds(xoffs, yoffs, div - 1, 24);
  185. xoffs += div;
  186. m_parcomps[cpi_compress]->setBounds(xoffs, yoffs, div - 1, 24);
  187. xoffs = 1;
  188. yoffs += 25;
  189. m_parcomps[cpi_tonalvsnoisebw]->setBounds(xoffs, yoffs, div - 1, 24);
  190. xoffs += div;
  191. m_parcomps[cpi_tonalvsnoisepreserve]->setBounds(xoffs, yoffs, div - 1, 24);
  192. xoffs = 1;
  193. yoffs += 25;
  194. m_parcomps[cpi_soundstart]->setBounds(xoffs, yoffs, div - 1, 24);
  195. xoffs += div;
  196. m_parcomps[cpi_soundend]->setBounds(xoffs, yoffs, div - 1, 24);
  197. xoffs = 1;
  198. yoffs += 25;
  199. m_parcomps[cpi_filter_low]->setBounds(xoffs, yoffs, div - 1, 24);
  200. xoffs += div;
  201. m_parcomps[cpi_filter_high]->setBounds(xoffs, yoffs, div - 1, 24);
  202. xoffs = 1;
  203. yoffs += 25;
  204. m_parcomps[cpi_loopxfadelen]->setBounds(xoffs, yoffs, div - 1, 24);
  205. xoffs += div;
  206. m_parcomps[cpi_onsetdetection]->setBounds(xoffs, yoffs, div - 1, 24);
  207. yoffs += 25;
  208. int remain_h = getHeight() - 1 - yoffs;
  209. m_spec_order_ed.setBounds(1, yoffs, getWidth() - 2, remain_h / 5 * 1);
  210. m_wavecomponent.setBounds(1, m_spec_order_ed.getBottom()+1, getWidth()-2, remain_h/5*4);
  211. //m_specvis.setBounds(1, yoffs, getWidth() - 2, getHeight() - 1 - yoffs);
  212. }
  213. void PaulstretchpluginAudioProcessorEditor::timerCallback(int id)
  214. {
  215. if (id == 1)
  216. {
  217. for (auto& e : m_parcomps)
  218. e->updateComponent();
  219. if (processor.isRecordingEnabled())
  220. {
  221. m_wavecomponent.setRecordingPosition(processor.getRecordingPositionPercent());
  222. } else
  223. m_wavecomponent.setRecordingPosition(-1.0);
  224. String infotext;
  225. if (processor.m_show_technical_info)
  226. {
  227. infotext += String(processor.getStretchSource()->m_param_change_count);
  228. infotext += " param changes ";
  229. }
  230. infotext += m_last_err + " FFT size " +
  231. String(processor.getStretchSource()->getFFTSize());
  232. if (processor.m_abnormal_output_samples > 0)
  233. infotext += " " + String(processor.m_abnormal_output_samples) + " invalid sample values";
  234. if (processor.isNonRealtime())
  235. infotext += " (offline rendering)";
  236. if (processor.m_playposinfo.isPlaying)
  237. infotext += " "+String(processor.m_playposinfo.timeInSeconds,1);
  238. if (processor.m_show_technical_info)
  239. infotext += " " + String(m_wavecomponent.m_image_init_count) + " " + String(m_wavecomponent.m_image_update_count);
  240. m_info_label.setText(infotext, dontSendNotification);
  241. m_perfmeter.repaint();
  242. }
  243. if (id == 2)
  244. {
  245. m_wavecomponent.setTimeSelection(processor.getTimeSelection());
  246. if (processor.m_state_dirty)
  247. {
  248. m_spec_order_ed.setSource(processor.getStretchSource());
  249. processor.m_state_dirty = false;
  250. }
  251. }
  252. if (id == 3)
  253. {
  254. //m_specvis.setState(processor.getStretchSource()->getProcessParameters(), processor.getStretchSource()->getFFTSize() / 2,
  255. // processor.getSampleRate());
  256. }
  257. }
  258. bool PaulstretchpluginAudioProcessorEditor::isInterestedInFileDrag(const StringArray & files)
  259. {
  260. if (files.size() == 0)
  261. return false;
  262. File f(files[0]);
  263. String extension = f.getFileExtension().toLowerCase();
  264. if (processor.m_afm->getWildcardForAllFormats().containsIgnoreCase(extension))
  265. return true;
  266. return false;
  267. }
  268. void PaulstretchpluginAudioProcessorEditor::filesDropped(const StringArray & files, int x, int y)
  269. {
  270. if (files.size() > 0)
  271. {
  272. File f(files[0]);
  273. processor.setAudioFile(f);
  274. toFront(true);
  275. }
  276. }
  277. void PaulstretchpluginAudioProcessorEditor::chooseFile()
  278. {
  279. String initiallocfn = processor.m_propsfile->m_props_file->getValue("importfilefolder",
  280. File::getSpecialLocation(File::userHomeDirectory).getFullPathName());
  281. File initialloc(initiallocfn);
  282. String filterstring = processor.m_afm->getWildcardForAllFormats();
  283. FileChooser myChooser("Please select audio file...",
  284. initialloc,
  285. filterstring,true);
  286. if (myChooser.browseForFileToOpen())
  287. {
  288. File resu = myChooser.getResult();
  289. String pathname = resu.getFullPathName();
  290. if (pathname.startsWith("/localhost"))
  291. {
  292. pathname = pathname.substring(10);
  293. resu = File(pathname);
  294. }
  295. processor.m_propsfile->m_props_file->setValue("importfilefolder", resu.getParentDirectory().getFullPathName());
  296. m_last_err = processor.setAudioFile(resu);
  297. }
  298. }
  299. void PaulstretchpluginAudioProcessorEditor::showSettingsMenu()
  300. {
  301. PopupMenu menu;
  302. menu.addItem(4, "Reset parameters", true, false);
  303. menu.addItem(5, "Load file with plugin state", true, processor.m_load_file_with_state);
  304. menu.addItem(1, "Play when host transport running", true, processor.m_play_when_host_plays);
  305. menu.addItem(2, "Capture when host transport running", true, processor.m_capture_when_host_plays);
  306. int capturelen = *processor.getFloatParameter(cpi_max_capture_len);
  307. PopupMenu capturelenmenu;
  308. std::vector<int> capturelens{ 2,5,10,30,60,120 };
  309. for (int i=0;i<capturelens.size();++i)
  310. capturelenmenu.addItem(200+i, String(capturelens[i])+" seconds", true, capturelen == capturelens[i]);
  311. menu.addSubMenu("Capture buffer length", capturelenmenu);
  312. menu.addItem(3, "About...", true, false);
  313. #ifdef JUCE_DEBUG
  314. menu.addItem(6, "Dump preset to clipboard", true, false);
  315. #endif
  316. menu.addItem(7, "Show technical info", true, processor.m_show_technical_info);
  317. int r = menu.show();
  318. if (r >= 200 && r < 210)
  319. {
  320. int caplen = capturelens[r - 200];
  321. *processor.getFloatParameter(cpi_max_capture_len) = (float)caplen;
  322. }
  323. if (r == 1)
  324. {
  325. toggleBool(processor.m_play_when_host_plays);
  326. }
  327. if (r == 2)
  328. {
  329. toggleBool(processor.m_capture_when_host_plays);
  330. }
  331. if (r == 4)
  332. {
  333. processor.resetParameters();
  334. }
  335. if (r == 5)
  336. {
  337. toggleBool(processor.m_load_file_with_state);
  338. }
  339. if (r == 3)
  340. {
  341. String fftlib = fftwf_version;
  342. String juceversiontxt = String("JUCE ") + String(JUCE_MAJOR_VERSION) + "." + String(JUCE_MINOR_VERSION);
  343. AlertWindow::showMessageBoxAsync(AlertWindow::InfoIcon,
  344. g_plugintitle,
  345. "Plugin for extreme time stretching and other sound processing\nBuilt on " + String(__DATE__) + " " + String(__TIME__) + "\n"
  346. "Copyright (C) 2006-2011 Nasca Octavian Paul, Tg. Mures, Romania\n"
  347. "(C) 2017-2018 Xenakios\n\n"
  348. "Using " + fftlib + " for FFT\n\n"
  349. + juceversiontxt + " (c) Roli. Used under the GPL license.\n\n"
  350. "GPL licensed source code for this plugin at : https://bitbucket.org/xenakios/paulstretchplugin/overview\n"
  351. , "OK",
  352. this);
  353. }
  354. if (r == 6)
  355. {
  356. ValueTree tree = processor.getStateTree(true,true);
  357. MemoryBlock destData;
  358. MemoryOutputStream stream(destData, true);
  359. tree.writeToStream(stream);
  360. String txt = Base64::toBase64(destData.getData(), destData.getSize());
  361. SystemClipboard::copyTextToClipboard(txt);
  362. }
  363. if (r == 7)
  364. {
  365. toggleBool(processor.m_show_technical_info);
  366. processor.m_propsfile->m_props_file->setValue("showtechnicalinfo", processor.m_show_technical_info);
  367. }
  368. }
  369. WaveformComponent::WaveformComponent(AudioFormatManager* afm, AudioThumbnail* thumb)
  370. {
  371. TimeSelectionChangedCallback = [](Range<double>, int) {};
  372. if (m_use_opengl == true)
  373. m_ogl.attachTo(*this);
  374. m_thumbnail = thumb;
  375. m_thumbnail->addChangeListener(this);
  376. setOpaque(true);
  377. }
  378. WaveformComponent::~WaveformComponent()
  379. {
  380. if (m_use_opengl == true)
  381. m_ogl.detach();
  382. m_thumbnail->removeChangeListener(this);
  383. }
  384. void WaveformComponent::changeListenerCallback(ChangeBroadcaster * /*cb*/)
  385. {
  386. jassert(MessageManager::getInstance()->isThisTheMessageThread());
  387. m_image_dirty = true;
  388. //repaint();
  389. }
  390. void WaveformComponent::updateCachedImage()
  391. {
  392. Graphics tempg(m_waveimage);
  393. tempg.fillAll(Colours::black);
  394. tempg.setColour(Colours::darkgrey);
  395. double thumblen = m_thumbnail->getTotalLength();
  396. m_thumbnail->drawChannels(tempg, { 0,0,getWidth(),getHeight() - m_topmargin },
  397. thumblen*m_view_range.getStart(), thumblen*m_view_range.getEnd(), 1.0f);
  398. m_image_dirty = false;
  399. ++m_image_update_count;
  400. }
  401. void WaveformComponent::paint(Graphics & g)
  402. {
  403. jassert(GetFileCallback);
  404. //Logger::writeToLog("Waveform component paint");
  405. g.fillAll(Colours::black);
  406. g.setColour(Colours::darkgrey);
  407. g.fillRect(0, 0, getWidth(), m_topmargin);
  408. if (m_thumbnail == nullptr || m_thumbnail->getTotalLength() < 0.01)
  409. {
  410. g.setColour(Colours::aqua.darker());
  411. g.drawText("No file loaded", 2, m_topmargin + 2, getWidth(), 20, Justification::topLeft);
  412. return;
  413. }
  414. g.setColour(Colours::lightslategrey);
  415. double thumblen = m_thumbnail->getTotalLength();
  416. double tick_interval = 1.0;
  417. if (thumblen > 60.0)
  418. tick_interval = 5.0;
  419. for (double secs = 0.0; secs < thumblen; secs += tick_interval)
  420. {
  421. float tickxcor = (float)jmap<double>(secs,
  422. thumblen*m_view_range.getStart(), thumblen*m_view_range.getEnd(), 0.0f, (float)getWidth());
  423. g.drawLine(tickxcor, 0.0, tickxcor, (float)m_topmargin, 1.0f);
  424. }
  425. bool m_use_cached_image = true;
  426. if (m_use_cached_image == true)
  427. {
  428. if (m_image_dirty == true || m_waveimage.getWidth() != getWidth()
  429. || m_waveimage.getHeight() != getHeight() - m_topmargin)
  430. {
  431. //Logger::writeToLog("updating cached waveform image");
  432. if (m_waveimage.getWidth() != getWidth()
  433. || m_waveimage.getHeight() != getHeight() - m_topmargin)
  434. {
  435. m_waveimage = Image(Image::ARGB, getWidth(), getHeight() - m_topmargin, true);
  436. ++m_image_init_count;
  437. }
  438. updateCachedImage();
  439. }
  440. g.drawImage(m_waveimage, 0, m_topmargin, getWidth(), getHeight() - m_topmargin, 0, 0, getWidth(), getHeight() - m_topmargin);
  441. }
  442. else
  443. {
  444. //g.fillAll(Colours::black);
  445. g.setColour(Colours::darkgrey);
  446. m_thumbnail->drawChannels(g, { 0,m_topmargin,getWidth(),getHeight() - m_topmargin },
  447. thumblen*m_view_range.getStart(), thumblen*m_view_range.getEnd(), 1.0f);
  448. }
  449. //g.setColour(Colours::darkgrey);
  450. //m_thumb->drawChannels(g, { 0,m_topmargin,getWidth(),getHeight()-m_topmargin },
  451. // 0.0, thumblen, 1.0f);
  452. g.setColour(Colours::white.withAlpha(0.5f));
  453. double sel_len = m_time_sel_end - m_time_sel_start;
  454. //if (sel_len > 0.0 && sel_len < 1.0)
  455. {
  456. int xcorleft = (int)jmap<double>(m_time_sel_start, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  457. int xcorright = (int)jmap<double>(m_time_sel_end, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  458. g.fillRect(xcorleft, m_topmargin, xcorright - xcorleft, getHeight() - m_topmargin);
  459. }
  460. if (m_file_cached.first.getLength() > 0.0 &&
  461. (bool)ShowFileCacheRange.getValue())
  462. {
  463. g.setColour(Colours::red.withAlpha(0.2f));
  464. int xcorleft = (int)jmap<double>(m_file_cached.first.getStart(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  465. int xcorright = (int)jmap<double>(m_file_cached.first.getEnd(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  466. g.fillRect(xcorleft, 0, xcorright - xcorleft, m_topmargin / 2);
  467. xcorleft = (int)jmap<double>(m_file_cached.second.getStart(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  468. xcorright = (int)jmap<double>(m_file_cached.second.getEnd(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  469. if (xcorright - xcorleft>0)
  470. {
  471. g.setColour(Colours::blue.withAlpha(0.2f));
  472. g.fillRect(xcorleft, m_topmargin / 2, xcorright - xcorleft, m_topmargin / 2);
  473. }
  474. }
  475. g.setColour(Colours::white);
  476. if (CursorPosCallback)
  477. {
  478. double pos = jmap<double>(CursorPosCallback(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  479. g.fillRect((int)pos, m_topmargin, 1, getHeight() - m_topmargin);
  480. }
  481. if (m_rec_pos >= 0.0)
  482. {
  483. g.setColour(Colours::lightpink);
  484. double pos = jmap<double>(m_rec_pos, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  485. g.fillRect((int)pos, m_topmargin, 1, getHeight() - m_topmargin);
  486. }
  487. g.setColour(Colours::aqua);
  488. g.drawText(GetFileCallback().getFileName(), 2, m_topmargin + 2, getWidth(), 20, Justification::topLeft);
  489. g.drawText(secondsToString2(thumblen), getWidth() - 200, m_topmargin + 2, 200, 20, Justification::topRight);
  490. }
  491. void WaveformComponent::timerCallback()
  492. {
  493. repaint();
  494. }
  495. void WaveformComponent::setFileCachedRange(std::pair<Range<double>, Range<double>> rng)
  496. {
  497. m_file_cached = rng;
  498. //repaint();
  499. }
  500. void WaveformComponent::setTimerEnabled(bool b)
  501. {
  502. if (b == true)
  503. startTimer(100);
  504. else
  505. stopTimer();
  506. }
  507. void WaveformComponent::setViewRange(Range<double> rng)
  508. {
  509. m_view_range = rng;
  510. m_waveimage = Image();
  511. repaint();
  512. }
  513. void WaveformComponent::mouseDown(const MouseEvent & e)
  514. {
  515. m_mousedown = true;
  516. m_lock_timesel_set = true;
  517. double pos = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  518. if (e.y < m_topmargin || e.mods.isCommandDown())
  519. {
  520. if (SeekCallback)
  521. SeekCallback(pos);
  522. m_didseek = true;
  523. }
  524. else
  525. {
  526. m_time_sel_drag_target = getTimeSelectionEdge(e.x, e.y);
  527. m_drag_time_start = pos;
  528. if (m_time_sel_drag_target == 0)
  529. {
  530. //m_time_sel_start = 0.0;
  531. //m_time_sel_end = 1.0;
  532. }
  533. }
  534. repaint();
  535. }
  536. void WaveformComponent::mouseUp(const MouseEvent & /*e*/)
  537. {
  538. m_lock_timesel_set = false;
  539. m_mousedown = false;
  540. m_didseek = false;
  541. if (m_didchangetimeselection)
  542. {
  543. TimeSelectionChangedCallback(Range<double>(m_time_sel_start, m_time_sel_end), 1);
  544. m_didchangetimeselection = false;
  545. }
  546. }
  547. void WaveformComponent::mouseDrag(const MouseEvent & e)
  548. {
  549. if (m_didseek == true)
  550. return;
  551. if (m_time_sel_drag_target == 0)
  552. {
  553. m_time_sel_start = m_drag_time_start;
  554. m_time_sel_end = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  555. }
  556. if (m_time_sel_drag_target == 1)
  557. {
  558. m_time_sel_start = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  559. }
  560. if (m_time_sel_drag_target == 2)
  561. {
  562. m_time_sel_end = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  563. }
  564. if (m_time_sel_start > m_time_sel_end)
  565. {
  566. std::swap(m_time_sel_start, m_time_sel_end);
  567. if (m_time_sel_drag_target == 1)
  568. m_time_sel_drag_target = 2;
  569. else if (m_time_sel_drag_target == 2)
  570. m_time_sel_drag_target = 1;
  571. }
  572. m_time_sel_start = jlimit(0.0, 1.0, m_time_sel_start);
  573. m_time_sel_end = jlimit(0.0, 1.0, m_time_sel_end);
  574. if (TimeSelectionChangedCallback)
  575. {
  576. if (m_time_sel_end>m_time_sel_start)
  577. TimeSelectionChangedCallback(Range<double>(m_time_sel_start, m_time_sel_end), 0);
  578. else
  579. TimeSelectionChangedCallback(Range<double>(0.0, 1.0), 0);
  580. }
  581. m_didchangetimeselection = true;
  582. repaint();
  583. }
  584. void WaveformComponent::mouseMove(const MouseEvent & e)
  585. {
  586. m_time_sel_drag_target = getTimeSelectionEdge(e.x, e.y);
  587. if (m_time_sel_drag_target == 0)
  588. setMouseCursor(MouseCursor::NormalCursor);
  589. if (m_time_sel_drag_target == 1)
  590. setMouseCursor(MouseCursor::LeftRightResizeCursor);
  591. if (m_time_sel_drag_target == 2)
  592. setMouseCursor(MouseCursor::LeftRightResizeCursor);
  593. }
  594. void WaveformComponent::mouseDoubleClick(const MouseEvent & e)
  595. {
  596. m_time_sel_start = 0.0;
  597. m_time_sel_end = 1.0;
  598. TimeSelectionChangedCallback({ 0.0,1.0 }, 0);
  599. repaint();
  600. }
  601. Range<double> WaveformComponent::getTimeSelection()
  602. {
  603. if (m_time_sel_start >= 0.0 && m_time_sel_end>m_time_sel_start + 0.001)
  604. return { m_time_sel_start, m_time_sel_end };
  605. return { 0.0, 1.0 };
  606. }
  607. void WaveformComponent::setTimeSelection(Range<double> rng)
  608. {
  609. if (m_lock_timesel_set == true)
  610. return;
  611. if (rng.isEmpty())
  612. rng = { -1.0,1.0 };
  613. m_time_sel_start = rng.getStart();
  614. m_time_sel_end = rng.getEnd();
  615. repaint();
  616. }
  617. int WaveformComponent::getTimeSelectionEdge(int x, int y)
  618. {
  619. int xcorleft = (int)jmap<double>(m_time_sel_start, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  620. int xcorright = (int)jmap<double>(m_time_sel_end, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  621. if (juce::Rectangle<int>(xcorleft - 5, m_topmargin, 10, getHeight() - m_topmargin).contains(x, y))
  622. return 1;
  623. if (juce::Rectangle<int>(xcorright - 5, m_topmargin, 10, getHeight() - m_topmargin).contains(x, y))
  624. return 2;
  625. return 0;
  626. }
  627. SpectralVisualizer::SpectralVisualizer()
  628. {
  629. m_img = Image(Image::RGB, 500, 200, true);
  630. }
  631. void SpectralVisualizer::setState(const ProcessParameters & pars, int nfreqs, double samplerate)
  632. {
  633. double t0 = Time::getMillisecondCounterHiRes();
  634. double hz = 440.0;
  635. int numharmonics = 40;
  636. double scaler = 1.0 / numharmonics;
  637. if (m_img.getWidth()!=getWidth() || m_img.getHeight()!=getHeight())
  638. m_img = Image(Image::RGB, getWidth(), getHeight(), true);
  639. if (m_nfreqs == 0 || nfreqs != m_nfreqs)
  640. {
  641. m_nfreqs = nfreqs;
  642. m_insamples = std::vector<REALTYPE>(nfreqs * 2);
  643. m_freqs1 = std::vector<REALTYPE>(nfreqs);
  644. m_freqs2 = std::vector<REALTYPE>(nfreqs);
  645. m_freqs3 = std::vector<REALTYPE>(nfreqs);
  646. m_fft = std::make_unique<FFT>(nfreqs*2);
  647. std::fill(m_insamples.begin(), m_insamples.end(), 0.0f);
  648. for (int i = 0; i < nfreqs; ++i)
  649. {
  650. for (int j = 0; j < numharmonics; ++j)
  651. {
  652. double oscgain = 1.0 - (1.0 / numharmonics)*j;
  653. m_insamples[i] += scaler * oscgain * sin(2 * 3.141592653 / samplerate * i* (hz + hz * j));
  654. }
  655. }
  656. }
  657. //std::fill(m_freqs1.begin(), m_freqs1.end(), 0.0f);
  658. //std::fill(m_freqs2.begin(), m_freqs2.end(), 0.0f);
  659. //std::fill(m_freqs3.begin(), m_freqs3.end(), 0.0f);
  660. //std::fill(m_fft->freq.begin(), m_fft->freq.end(), 0.0f);
  661. for (int i = 0; i < nfreqs; ++i)
  662. {
  663. m_fft->smp[i] = m_insamples[i];
  664. }
  665. m_fft->applywindow(W_HAMMING);
  666. m_fft->smp2freq();
  667. double ratio = pow(2.0f, pars.pitch_shift.cents / 1200.0f);
  668. spectrum_do_pitch_shift(pars, nfreqs, m_fft->freq.data(), m_freqs2.data(), ratio);
  669. spectrum_do_freq_shift(pars, nfreqs, samplerate, m_freqs2.data(), m_freqs1.data());
  670. spectrum_do_compressor(pars, nfreqs, m_freqs1.data(), m_freqs2.data());
  671. spectrum_spread(nfreqs, samplerate, m_freqs3, m_freqs2.data(), m_freqs1.data(), pars.spread.bandwidth);
  672. //if (pars.harmonics.enabled)
  673. // spectrum_do_harmonics(pars, m_freqs3, nfreqs, samplerate, m_freqs1.data(), m_freqs2.data());
  674. //else spectrum_copy(nfreqs, m_freqs1.data(), m_freqs2.data());
  675. Graphics g(m_img);
  676. g.fillAll(Colours::black);
  677. g.setColour(Colours::white);
  678. for (int i = 0; i < nfreqs; ++i)
  679. {
  680. double binfreq = (samplerate / 2 / nfreqs)*i;
  681. double xcor = jmap<double>(binfreq, 0.0, samplerate / 2.0, 0.0, getWidth());
  682. double ycor = getHeight()- jmap<double>(m_freqs2[i], 0.0, nfreqs/128, 0.0, getHeight());
  683. ycor = jlimit<double>(0.0, getHeight(), ycor);
  684. g.drawLine(xcor, getHeight(), xcor, ycor, 1.0);
  685. }
  686. double t1 = Time::getMillisecondCounterHiRes();
  687. m_elapsed = t1 - t0;
  688. repaint();
  689. }
  690. void SpectralVisualizer::paint(Graphics & g)
  691. {
  692. g.drawImage(m_img, 0, 0, getWidth(), getHeight(), 0, 0, m_img.getWidth(), m_img.getHeight());
  693. g.setColour(Colours::yellow);
  694. g.drawText(String(m_elapsed, 1)+" ms", 1, 1, getWidth(), 30, Justification::topLeft);
  695. }
  696. void SpectralChainEditor::paint(Graphics & g)
  697. {
  698. g.fillAll(Colours::black);
  699. if (m_src == nullptr)
  700. return;
  701. int box_w = getWidth() / m_order.size();
  702. int box_h = getHeight();
  703. for (int i = 0; i < m_order.size(); ++i)
  704. {
  705. //if (i!=m_cur_index)
  706. drawBox(g, i, i*box_w, 0, box_w - 30, box_h);
  707. if (i<m_order.size() - 1)
  708. g.drawArrow(juce::Line<float>(i*box_w + (box_w - 30), box_h / 2, i*box_w + box_w, box_h / 2), 2.0f, 15.0f, 15.0f);
  709. }
  710. if (m_drag_x>=0 && m_drag_x<getWidth() && m_cur_index>=0)
  711. drawBox(g, m_cur_index, m_drag_x, 0, box_w - 30, box_h);
  712. }
  713. void SpectralChainEditor::setSource(StretchAudioSource * src)
  714. {
  715. m_src = src;
  716. m_order = m_src->getSpectrumProcessOrder();
  717. repaint();
  718. }
  719. void SpectralChainEditor::mouseDown(const MouseEvent & ev)
  720. {
  721. m_did_drag = false;
  722. int box_w = getWidth() / m_order.size();
  723. int box_h = getHeight();
  724. m_cur_index = ev.x / box_w;
  725. if (m_cur_index >= 0)
  726. {
  727. if (ModuleSelectedCallback)
  728. ModuleSelectedCallback(m_order[m_cur_index].m_index);
  729. juce::Rectangle<int> r(box_w*m_cur_index, 1, 12, 12);
  730. if (r.contains(ev.x, ev.y))
  731. {
  732. m_order[m_cur_index].m_enabled = !m_order[m_cur_index].m_enabled;
  733. m_src->setSpectrumProcessOrder(m_order);
  734. if (ModuleOrderOrEnabledChangedCallback)
  735. ModuleOrderOrEnabledChangedCallback();
  736. repaint();
  737. return;
  738. }
  739. }
  740. m_drag_x = -1;
  741. repaint();
  742. }
  743. void SpectralChainEditor::mouseDrag(const MouseEvent & ev)
  744. {
  745. int box_w = getWidth() / m_order.size();
  746. juce::Rectangle<int> r(box_w*m_cur_index, 1, 12, 12);
  747. if (r.contains(ev.x, ev.y))
  748. return;
  749. if (m_cur_index >= 0 && m_cur_index < m_order.size())
  750. {
  751. int box_h = getHeight();
  752. int new_index = ev.x / box_w;
  753. if (new_index >= 0 && new_index < m_order.size() && new_index != m_cur_index)
  754. {
  755. std::swap(m_order[m_cur_index], m_order[new_index]);
  756. m_cur_index = new_index;
  757. m_did_drag = true;
  758. m_src->setSpectrumProcessOrder(m_order);
  759. if (ModuleOrderOrEnabledChangedCallback)
  760. ModuleOrderOrEnabledChangedCallback();
  761. }
  762. m_drag_x = ev.x;
  763. repaint();
  764. }
  765. }
  766. void SpectralChainEditor::mouseUp(const MouseEvent & ev)
  767. {
  768. m_drag_x = -1;
  769. //m_cur_index = -1;
  770. repaint();
  771. }
  772. void SpectralChainEditor::drawBox(Graphics & g, int index, int x, int y, int w, int h)
  773. {
  774. String txt;
  775. if (m_order[index].m_index == 0)
  776. txt = "Harmonics";
  777. if (m_order[index].m_index == 1)
  778. txt = "Tonal vs Noise";
  779. if (m_order[index].m_index == 2)
  780. txt = "Frequency shift";
  781. if (m_order[index].m_index == 3)
  782. txt = "Pitch shift";
  783. if (m_order[index].m_index == 4)
  784. txt = "Octaves";
  785. if (m_order[index].m_index == 5)
  786. txt = "Spread";
  787. if (m_order[index].m_index == 6)
  788. txt = "Filter";
  789. if (m_order[index].m_index == 7)
  790. txt = "Compressor";
  791. if (index == m_cur_index)
  792. {
  793. g.setColour(Colours::darkgrey);
  794. //g.fillRect(i*box_w, 0, box_w - 30, box_h - 1);
  795. g.fillRect(x, y, w, h);
  796. }
  797. g.setColour(Colours::white);
  798. g.drawRect(x, y, w, h);
  799. g.drawFittedText(txt, x,y,w,h, Justification::centred, 3);
  800. g.setColour(Colours::gold);
  801. g.drawRect(x + 2, y + 2, 12, 12);
  802. if (m_order[index].m_enabled == true)
  803. {
  804. g.drawLine(x+2, y+2, x+14, y+14);
  805. g.drawLine(x+2, y+14, x+14, y+2);
  806. }
  807. g.setColour(Colours::white);
  808. }
  809. ParameterComponent::ParameterComponent(AudioProcessorParameter * par, bool notifyOnlyOnRelease) : m_par(par)
  810. {
  811. addAndMakeVisible(&m_label);
  812. m_labeldefcolor = m_label.findColour(Label::textColourId);
  813. m_label.setText(par->getName(50), dontSendNotification);
  814. AudioParameterFloat* floatpar = dynamic_cast<AudioParameterFloat*>(par);
  815. if (floatpar)
  816. {
  817. m_slider = std::make_unique<MySlider>(&floatpar->range);
  818. m_notify_only_on_release = notifyOnlyOnRelease;
  819. m_slider->setRange(floatpar->range.start, floatpar->range.end, floatpar->range.interval);
  820. m_slider->setValue(*floatpar, dontSendNotification);
  821. m_slider->addListener(this);
  822. m_slider->setDoubleClickReturnValue(true, floatpar->range.convertFrom0to1(par->getDefaultValue()));
  823. addAndMakeVisible(m_slider.get());
  824. }
  825. AudioParameterInt* intpar = dynamic_cast<AudioParameterInt*>(par);
  826. if (intpar)
  827. {
  828. m_slider = std::make_unique<MySlider>();
  829. m_notify_only_on_release = notifyOnlyOnRelease;
  830. m_slider->setRange(intpar->getRange().getStart(), intpar->getRange().getEnd(), 1.0);
  831. m_slider->setValue(*intpar, dontSendNotification);
  832. m_slider->addListener(this);
  833. addAndMakeVisible(m_slider.get());
  834. }
  835. AudioParameterChoice* choicepar = dynamic_cast<AudioParameterChoice*>(par);
  836. if (choicepar)
  837. {
  838. }
  839. AudioParameterBool* boolpar = dynamic_cast<AudioParameterBool*>(par);
  840. if (boolpar)
  841. {
  842. m_togglebut = std::make_unique<ToggleButton>();
  843. m_togglebut->setToggleState(*boolpar, dontSendNotification);
  844. m_togglebut->addListener(this);
  845. m_togglebut->setButtonText(par->getName(50));
  846. addAndMakeVisible(m_togglebut.get());
  847. }
  848. }
  849. void ParameterComponent::resized()
  850. {
  851. if (m_slider)
  852. {
  853. int labw = 200;
  854. if (getWidth() < 400)
  855. labw = 100;
  856. m_label.setBounds(0, 0, labw, 24);
  857. m_slider->setBounds(m_label.getRight() + 1, 0, getWidth() - 2 - m_label.getWidth(), 24);
  858. }
  859. if (m_togglebut)
  860. m_togglebut->setBounds(1, 0, getWidth() - 1, 24);
  861. }
  862. void ParameterComponent::sliderValueChanged(Slider * slid)
  863. {
  864. if (m_notify_only_on_release == true)
  865. return;
  866. AudioParameterFloat* floatpar = dynamic_cast<AudioParameterFloat*>(m_par);
  867. if (floatpar != nullptr)
  868. *floatpar = slid->getValue();
  869. AudioParameterInt* intpar = dynamic_cast<AudioParameterInt*>(m_par);
  870. if (intpar != nullptr)
  871. *intpar = slid->getValue();
  872. }
  873. void ParameterComponent::sliderDragStarted(Slider * slid)
  874. {
  875. m_dragging = true;
  876. }
  877. void ParameterComponent::sliderDragEnded(Slider * slid)
  878. {
  879. m_dragging = false;
  880. if (m_notify_only_on_release == false)
  881. return;
  882. AudioParameterFloat* floatpar = dynamic_cast<AudioParameterFloat*>(m_par);
  883. if (floatpar != nullptr)
  884. *floatpar = slid->getValue();
  885. AudioParameterInt* intpar = dynamic_cast<AudioParameterInt*>(m_par);
  886. if (intpar != nullptr)
  887. *intpar = slid->getValue();
  888. }
  889. void ParameterComponent::buttonClicked(Button * but)
  890. {
  891. AudioParameterBool* boolpar = dynamic_cast<AudioParameterBool*>(m_par);
  892. if (m_togglebut != nullptr && m_togglebut->getToggleState() != *boolpar)
  893. {
  894. *boolpar = m_togglebut->getToggleState();
  895. }
  896. }
  897. void ParameterComponent::updateComponent()
  898. {
  899. AudioParameterFloat* floatpar = dynamic_cast<AudioParameterFloat*>(m_par);
  900. if (floatpar != nullptr && m_slider != nullptr && m_dragging == false && (float)m_slider->getValue() != *floatpar)
  901. {
  902. m_slider->setValue(*floatpar, dontSendNotification);
  903. }
  904. AudioParameterInt* intpar = dynamic_cast<AudioParameterInt*>(m_par);
  905. if (intpar != nullptr && m_slider != nullptr && m_dragging == false && (int)m_slider->getValue() != *intpar)
  906. {
  907. m_slider->setValue(*intpar, dontSendNotification);
  908. }
  909. AudioParameterBool* boolpar = dynamic_cast<AudioParameterBool*>(m_par);
  910. if (m_togglebut != nullptr && m_togglebut->getToggleState() != *boolpar)
  911. {
  912. m_togglebut->setToggleState(*boolpar, dontSendNotification);
  913. }
  914. }
  915. void ParameterComponent::setHighLighted(bool b)
  916. {
  917. if (b == false)
  918. {
  919. m_label.setColour(Label::textColourId, m_labeldefcolor);
  920. if (m_togglebut)
  921. m_togglebut->setColour(ToggleButton::textColourId, m_labeldefcolor);
  922. }
  923. else
  924. {
  925. m_label.setColour(Label::textColourId, Colours::yellow);
  926. if (m_togglebut)
  927. m_togglebut->setColour(ToggleButton::textColourId, Colours::yellow);
  928. }
  929. }
  930. MySlider::MySlider(NormalisableRange<float>* range) : m_range(range)
  931. {
  932. }
  933. double MySlider::proportionOfLengthToValue(double x)
  934. {
  935. if (m_range)
  936. return m_range->convertFrom0to1(x);
  937. return Slider::proportionOfLengthToValue(x);
  938. }
  939. double MySlider::valueToProportionOfLength(double x)
  940. {
  941. if (m_range)
  942. return m_range->convertTo0to1(x);
  943. return Slider::valueToProportionOfLength(x);
  944. }
  945. PerfMeterComponent::PerfMeterComponent(PaulstretchpluginAudioProcessor * p)
  946. : m_proc(p)
  947. {}
  948. void PerfMeterComponent::paint(Graphics & g)
  949. {
  950. g.fillAll(Colours::grey);
  951. double amt = m_proc->getPreBufferingPercent();
  952. g.setColour(Colours::green);
  953. int w = amt * getWidth();
  954. g.fillRect(0, 0, w, getHeight());
  955. g.setColour(Colours::white);
  956. g.drawRect(0, 0, getWidth(), getHeight());
  957. g.setFont(10.0f);
  958. if (m_proc->getPreBufferAmount()>0)
  959. g.drawText("PREBUFFER", 0, 0, getWidth(), getHeight(), Justification::centred);
  960. else
  961. g.drawText("NO PREBUFFER", 0, 0, getWidth(), getHeight(), Justification::centred);
  962. }
  963. void PerfMeterComponent::mouseDown(const MouseEvent & ev)
  964. {
  965. PopupMenu bufferingmenu;
  966. int curbufamount = m_proc->getPreBufferAmount();
  967. bufferingmenu.addItem(100, "None", true, curbufamount == -1);
  968. bufferingmenu.addItem(101, "Small", true, curbufamount == 1);
  969. bufferingmenu.addItem(102, "Medium", true, curbufamount == 2);
  970. bufferingmenu.addItem(103, "Large", true, curbufamount == 3);
  971. bufferingmenu.addItem(104, "Very large", true, curbufamount == 4);
  972. bufferingmenu.addItem(105, "Huge", true, curbufamount == 5);
  973. int r = bufferingmenu.show();
  974. if (r >= 100 && r < 200)
  975. {
  976. if (r == 100)
  977. m_proc->m_use_backgroundbuffering = false;
  978. if (r > 100)
  979. m_proc->setPreBufferAmount(r - 100);
  980. }
  981. }