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.

1052 lines
33KB

  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. if (m_waveimage.getWidth() != getWidth()
  432. || m_waveimage.getHeight() != getHeight() - m_topmargin)
  433. {
  434. m_waveimage = Image(Image::ARGB, getWidth(), getHeight() - m_topmargin, true);
  435. ++m_image_init_count;
  436. }
  437. updateCachedImage();
  438. }
  439. g.drawImage(m_waveimage, 0, m_topmargin, getWidth(), getHeight() - m_topmargin, 0, 0, getWidth(), getHeight() - m_topmargin);
  440. }
  441. else
  442. {
  443. g.setColour(Colours::darkgrey);
  444. m_thumbnail->drawChannels(g, { 0,m_topmargin,getWidth(),getHeight() - m_topmargin },
  445. thumblen*m_view_range.getStart(), thumblen*m_view_range.getEnd(), 1.0f);
  446. }
  447. g.setColour(Colours::white.withAlpha(0.5f));
  448. double sel_len = m_time_sel_end - m_time_sel_start;
  449. //if (sel_len > 0.0 && sel_len < 1.0)
  450. {
  451. int xcorleft = (int)jmap<double>(m_time_sel_start, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  452. int xcorright = (int)jmap<double>(m_time_sel_end, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  453. g.fillRect(xcorleft, m_topmargin, xcorright - xcorleft, getHeight() - m_topmargin);
  454. }
  455. if (m_file_cached.first.getLength() > 0.0 &&
  456. (bool)ShowFileCacheRange.getValue())
  457. {
  458. g.setColour(Colours::red.withAlpha(0.2f));
  459. int xcorleft = (int)jmap<double>(m_file_cached.first.getStart(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  460. int xcorright = (int)jmap<double>(m_file_cached.first.getEnd(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  461. g.fillRect(xcorleft, 0, xcorright - xcorleft, m_topmargin / 2);
  462. xcorleft = (int)jmap<double>(m_file_cached.second.getStart(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  463. xcorright = (int)jmap<double>(m_file_cached.second.getEnd(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  464. if (xcorright - xcorleft>0)
  465. {
  466. g.setColour(Colours::blue.withAlpha(0.2f));
  467. g.fillRect(xcorleft, m_topmargin / 2, xcorright - xcorleft, m_topmargin / 2);
  468. }
  469. }
  470. g.setColour(Colours::white);
  471. if (CursorPosCallback)
  472. {
  473. double pos = jmap<double>(CursorPosCallback(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  474. g.fillRect((int)pos, m_topmargin, 1, getHeight() - m_topmargin);
  475. }
  476. if (m_rec_pos >= 0.0)
  477. {
  478. g.setColour(Colours::lightpink);
  479. double pos = jmap<double>(m_rec_pos, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  480. g.fillRect((int)pos, m_topmargin, 1, getHeight() - m_topmargin);
  481. }
  482. g.setColour(Colours::aqua);
  483. g.drawText(GetFileCallback().getFileName(), 2, m_topmargin + 2, getWidth(), 20, Justification::topLeft);
  484. g.drawText(secondsToString2(thumblen), getWidth() - 200, m_topmargin + 2, 200, 20, Justification::topRight);
  485. }
  486. void WaveformComponent::timerCallback()
  487. {
  488. repaint();
  489. }
  490. void WaveformComponent::setFileCachedRange(std::pair<Range<double>, Range<double>> rng)
  491. {
  492. m_file_cached = rng;
  493. }
  494. void WaveformComponent::setTimerEnabled(bool b)
  495. {
  496. if (b == true)
  497. startTimer(100);
  498. else
  499. stopTimer();
  500. }
  501. void WaveformComponent::setViewRange(Range<double> rng)
  502. {
  503. m_view_range = rng;
  504. m_waveimage = Image();
  505. repaint();
  506. }
  507. void WaveformComponent::mouseDown(const MouseEvent & e)
  508. {
  509. m_mousedown = true;
  510. m_lock_timesel_set = true;
  511. double pos = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  512. if (e.y < m_topmargin || e.mods.isCommandDown())
  513. {
  514. if (SeekCallback)
  515. SeekCallback(pos);
  516. m_didseek = true;
  517. }
  518. else
  519. {
  520. m_time_sel_drag_target = getTimeSelectionEdge(e.x, e.y);
  521. m_drag_time_start = pos;
  522. if (m_time_sel_drag_target == 0)
  523. {
  524. //m_time_sel_start = 0.0;
  525. //m_time_sel_end = 1.0;
  526. }
  527. }
  528. repaint();
  529. }
  530. void WaveformComponent::mouseUp(const MouseEvent & /*e*/)
  531. {
  532. m_lock_timesel_set = false;
  533. m_mousedown = false;
  534. m_didseek = false;
  535. if (m_didchangetimeselection)
  536. {
  537. TimeSelectionChangedCallback(Range<double>(m_time_sel_start, m_time_sel_end), 1);
  538. m_didchangetimeselection = false;
  539. }
  540. }
  541. void WaveformComponent::mouseDrag(const MouseEvent & e)
  542. {
  543. if (m_didseek == true)
  544. return;
  545. if (m_time_sel_drag_target == 0)
  546. {
  547. m_time_sel_start = m_drag_time_start;
  548. m_time_sel_end = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  549. }
  550. if (m_time_sel_drag_target == 1)
  551. {
  552. m_time_sel_start = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  553. }
  554. if (m_time_sel_drag_target == 2)
  555. {
  556. m_time_sel_end = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  557. }
  558. if (m_time_sel_start > m_time_sel_end)
  559. {
  560. std::swap(m_time_sel_start, m_time_sel_end);
  561. if (m_time_sel_drag_target == 1)
  562. m_time_sel_drag_target = 2;
  563. else if (m_time_sel_drag_target == 2)
  564. m_time_sel_drag_target = 1;
  565. }
  566. m_time_sel_start = jlimit(0.0, 1.0, m_time_sel_start);
  567. m_time_sel_end = jlimit(0.0, 1.0, m_time_sel_end);
  568. if (TimeSelectionChangedCallback)
  569. {
  570. if (m_time_sel_end>m_time_sel_start)
  571. TimeSelectionChangedCallback(Range<double>(m_time_sel_start, m_time_sel_end), 0);
  572. else
  573. TimeSelectionChangedCallback(Range<double>(0.0, 1.0), 0);
  574. }
  575. m_didchangetimeselection = true;
  576. repaint();
  577. }
  578. void WaveformComponent::mouseMove(const MouseEvent & e)
  579. {
  580. m_time_sel_drag_target = getTimeSelectionEdge(e.x, e.y);
  581. if (m_time_sel_drag_target == 0)
  582. setMouseCursor(MouseCursor::NormalCursor);
  583. if (m_time_sel_drag_target == 1)
  584. setMouseCursor(MouseCursor::LeftRightResizeCursor);
  585. if (m_time_sel_drag_target == 2)
  586. setMouseCursor(MouseCursor::LeftRightResizeCursor);
  587. }
  588. void WaveformComponent::mouseDoubleClick(const MouseEvent & e)
  589. {
  590. m_time_sel_start = 0.0;
  591. m_time_sel_end = 1.0;
  592. TimeSelectionChangedCallback({ 0.0,1.0 }, 0);
  593. repaint();
  594. }
  595. Range<double> WaveformComponent::getTimeSelection()
  596. {
  597. if (m_time_sel_start >= 0.0 && m_time_sel_end>m_time_sel_start + 0.001)
  598. return { m_time_sel_start, m_time_sel_end };
  599. return { 0.0, 1.0 };
  600. }
  601. void WaveformComponent::setTimeSelection(Range<double> rng)
  602. {
  603. if (m_lock_timesel_set == true)
  604. return;
  605. if (rng.isEmpty())
  606. rng = { -1.0,1.0 };
  607. m_time_sel_start = rng.getStart();
  608. m_time_sel_end = rng.getEnd();
  609. repaint();
  610. }
  611. int WaveformComponent::getTimeSelectionEdge(int x, int y)
  612. {
  613. int xcorleft = (int)jmap<double>(m_time_sel_start, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  614. int xcorright = (int)jmap<double>(m_time_sel_end, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  615. if (juce::Rectangle<int>(xcorleft - 5, m_topmargin, 10, getHeight() - m_topmargin).contains(x, y))
  616. return 1;
  617. if (juce::Rectangle<int>(xcorright - 5, m_topmargin, 10, getHeight() - m_topmargin).contains(x, y))
  618. return 2;
  619. return 0;
  620. }
  621. SpectralVisualizer::SpectralVisualizer()
  622. {
  623. m_img = Image(Image::RGB, 500, 200, true);
  624. }
  625. void SpectralVisualizer::setState(const ProcessParameters & pars, int nfreqs, double samplerate)
  626. {
  627. double t0 = Time::getMillisecondCounterHiRes();
  628. double hz = 440.0;
  629. int numharmonics = 40;
  630. double scaler = 1.0 / numharmonics;
  631. if (m_img.getWidth()!=getWidth() || m_img.getHeight()!=getHeight())
  632. m_img = Image(Image::RGB, getWidth(), getHeight(), true);
  633. if (m_nfreqs == 0 || nfreqs != m_nfreqs)
  634. {
  635. m_nfreqs = nfreqs;
  636. m_insamples = std::vector<REALTYPE>(nfreqs * 2);
  637. m_freqs1 = std::vector<REALTYPE>(nfreqs);
  638. m_freqs2 = std::vector<REALTYPE>(nfreqs);
  639. m_freqs3 = std::vector<REALTYPE>(nfreqs);
  640. m_fft = std::make_unique<FFT>(nfreqs*2);
  641. std::fill(m_insamples.begin(), m_insamples.end(), 0.0f);
  642. for (int i = 0; i < nfreqs; ++i)
  643. {
  644. for (int j = 0; j < numharmonics; ++j)
  645. {
  646. double oscgain = 1.0 - (1.0 / numharmonics)*j;
  647. m_insamples[i] += scaler * oscgain * sin(2 * 3.141592653 / samplerate * i* (hz + hz * j));
  648. }
  649. }
  650. }
  651. //std::fill(m_freqs1.begin(), m_freqs1.end(), 0.0f);
  652. //std::fill(m_freqs2.begin(), m_freqs2.end(), 0.0f);
  653. //std::fill(m_freqs3.begin(), m_freqs3.end(), 0.0f);
  654. //std::fill(m_fft->freq.begin(), m_fft->freq.end(), 0.0f);
  655. for (int i = 0; i < nfreqs; ++i)
  656. {
  657. m_fft->smp[i] = m_insamples[i];
  658. }
  659. m_fft->applywindow(W_HAMMING);
  660. m_fft->smp2freq();
  661. double ratio = pow(2.0f, pars.pitch_shift.cents / 1200.0f);
  662. spectrum_do_pitch_shift(pars, nfreqs, m_fft->freq.data(), m_freqs2.data(), ratio);
  663. spectrum_do_freq_shift(pars, nfreqs, samplerate, m_freqs2.data(), m_freqs1.data());
  664. spectrum_do_compressor(pars, nfreqs, m_freqs1.data(), m_freqs2.data());
  665. spectrum_spread(nfreqs, samplerate, m_freqs3, m_freqs2.data(), m_freqs1.data(), pars.spread.bandwidth);
  666. //if (pars.harmonics.enabled)
  667. // spectrum_do_harmonics(pars, m_freqs3, nfreqs, samplerate, m_freqs1.data(), m_freqs2.data());
  668. //else spectrum_copy(nfreqs, m_freqs1.data(), m_freqs2.data());
  669. Graphics g(m_img);
  670. g.fillAll(Colours::black);
  671. g.setColour(Colours::white);
  672. for (int i = 0; i < nfreqs; ++i)
  673. {
  674. double binfreq = (samplerate / 2 / nfreqs)*i;
  675. double xcor = jmap<double>(binfreq, 0.0, samplerate / 2.0, 0.0, getWidth());
  676. double ycor = getHeight()- jmap<double>(m_freqs2[i], 0.0, nfreqs/128, 0.0, getHeight());
  677. ycor = jlimit<double>(0.0, getHeight(), ycor);
  678. g.drawLine(xcor, getHeight(), xcor, ycor, 1.0);
  679. }
  680. double t1 = Time::getMillisecondCounterHiRes();
  681. m_elapsed = t1 - t0;
  682. repaint();
  683. }
  684. void SpectralVisualizer::paint(Graphics & g)
  685. {
  686. g.drawImage(m_img, 0, 0, getWidth(), getHeight(), 0, 0, m_img.getWidth(), m_img.getHeight());
  687. g.setColour(Colours::yellow);
  688. g.drawText(String(m_elapsed, 1)+" ms", 1, 1, getWidth(), 30, Justification::topLeft);
  689. }
  690. void SpectralChainEditor::paint(Graphics & g)
  691. {
  692. g.fillAll(Colours::black);
  693. if (m_src == nullptr)
  694. return;
  695. int box_w = getWidth() / m_order.size();
  696. int box_h = getHeight();
  697. for (int i = 0; i < m_order.size(); ++i)
  698. {
  699. //if (i!=m_cur_index)
  700. drawBox(g, i, i*box_w, 0, box_w - 30, box_h);
  701. if (i<m_order.size() - 1)
  702. 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);
  703. }
  704. if (m_drag_x>=0 && m_drag_x<getWidth() && m_cur_index>=0)
  705. drawBox(g, m_cur_index, m_drag_x, 0, box_w - 30, box_h);
  706. }
  707. void SpectralChainEditor::setSource(StretchAudioSource * src)
  708. {
  709. m_src = src;
  710. m_order = m_src->getSpectrumProcessOrder();
  711. repaint();
  712. }
  713. void SpectralChainEditor::mouseDown(const MouseEvent & ev)
  714. {
  715. m_did_drag = false;
  716. int box_w = getWidth() / m_order.size();
  717. int box_h = getHeight();
  718. m_cur_index = ev.x / box_w;
  719. if (m_cur_index >= 0)
  720. {
  721. if (ModuleSelectedCallback)
  722. ModuleSelectedCallback(m_order[m_cur_index].m_index);
  723. juce::Rectangle<int> r(box_w*m_cur_index, 1, 12, 12);
  724. if (r.contains(ev.x, ev.y))
  725. {
  726. m_order[m_cur_index].m_enabled = !m_order[m_cur_index].m_enabled;
  727. m_src->setSpectrumProcessOrder(m_order);
  728. if (ModuleOrderOrEnabledChangedCallback)
  729. ModuleOrderOrEnabledChangedCallback();
  730. repaint();
  731. return;
  732. }
  733. }
  734. m_drag_x = -1;
  735. repaint();
  736. }
  737. void SpectralChainEditor::mouseDrag(const MouseEvent & ev)
  738. {
  739. int box_w = getWidth() / m_order.size();
  740. juce::Rectangle<int> r(box_w*m_cur_index, 1, 12, 12);
  741. if (r.contains(ev.x, ev.y))
  742. return;
  743. if (m_cur_index >= 0 && m_cur_index < m_order.size())
  744. {
  745. int box_h = getHeight();
  746. int new_index = ev.x / box_w;
  747. if (new_index >= 0 && new_index < m_order.size() && new_index != m_cur_index)
  748. {
  749. std::swap(m_order[m_cur_index], m_order[new_index]);
  750. m_cur_index = new_index;
  751. m_did_drag = true;
  752. m_src->setSpectrumProcessOrder(m_order);
  753. if (ModuleOrderOrEnabledChangedCallback)
  754. ModuleOrderOrEnabledChangedCallback();
  755. }
  756. m_drag_x = ev.x;
  757. repaint();
  758. }
  759. }
  760. void SpectralChainEditor::mouseUp(const MouseEvent & ev)
  761. {
  762. m_drag_x = -1;
  763. //m_cur_index = -1;
  764. repaint();
  765. }
  766. void SpectralChainEditor::drawBox(Graphics & g, int index, int x, int y, int w, int h)
  767. {
  768. String txt;
  769. if (m_order[index].m_index == 0)
  770. txt = "Harmonics";
  771. if (m_order[index].m_index == 1)
  772. txt = "Tonal vs Noise";
  773. if (m_order[index].m_index == 2)
  774. txt = "Frequency shift";
  775. if (m_order[index].m_index == 3)
  776. txt = "Pitch shift";
  777. if (m_order[index].m_index == 4)
  778. txt = "Octaves";
  779. if (m_order[index].m_index == 5)
  780. txt = "Spread";
  781. if (m_order[index].m_index == 6)
  782. txt = "Filter";
  783. if (m_order[index].m_index == 7)
  784. txt = "Compressor";
  785. if (index == m_cur_index)
  786. {
  787. g.setColour(Colours::darkgrey);
  788. //g.fillRect(i*box_w, 0, box_w - 30, box_h - 1);
  789. g.fillRect(x, y, w, h);
  790. }
  791. g.setColour(Colours::white);
  792. g.drawRect(x, y, w, h);
  793. g.drawFittedText(txt, x,y,w,h, Justification::centred, 3);
  794. g.setColour(Colours::gold);
  795. g.drawRect(x + 2, y + 2, 12, 12);
  796. if (m_order[index].m_enabled == true)
  797. {
  798. g.drawLine(x+2, y+2, x+14, y+14);
  799. g.drawLine(x+2, y+14, x+14, y+2);
  800. }
  801. g.setColour(Colours::white);
  802. }
  803. ParameterComponent::ParameterComponent(AudioProcessorParameter * par, bool notifyOnlyOnRelease) : m_par(par)
  804. {
  805. addAndMakeVisible(&m_label);
  806. m_labeldefcolor = m_label.findColour(Label::textColourId);
  807. m_label.setText(par->getName(50), dontSendNotification);
  808. AudioParameterFloat* floatpar = dynamic_cast<AudioParameterFloat*>(par);
  809. if (floatpar)
  810. {
  811. m_slider = std::make_unique<MySlider>(&floatpar->range);
  812. m_notify_only_on_release = notifyOnlyOnRelease;
  813. m_slider->setRange(floatpar->range.start, floatpar->range.end, floatpar->range.interval);
  814. m_slider->setValue(*floatpar, dontSendNotification);
  815. m_slider->addListener(this);
  816. m_slider->setDoubleClickReturnValue(true, floatpar->range.convertFrom0to1(par->getDefaultValue()));
  817. addAndMakeVisible(m_slider.get());
  818. }
  819. AudioParameterInt* intpar = dynamic_cast<AudioParameterInt*>(par);
  820. if (intpar)
  821. {
  822. m_slider = std::make_unique<MySlider>();
  823. m_notify_only_on_release = notifyOnlyOnRelease;
  824. m_slider->setRange(intpar->getRange().getStart(), intpar->getRange().getEnd(), 1.0);
  825. m_slider->setValue(*intpar, dontSendNotification);
  826. m_slider->addListener(this);
  827. addAndMakeVisible(m_slider.get());
  828. }
  829. AudioParameterChoice* choicepar = dynamic_cast<AudioParameterChoice*>(par);
  830. if (choicepar)
  831. {
  832. }
  833. AudioParameterBool* boolpar = dynamic_cast<AudioParameterBool*>(par);
  834. if (boolpar)
  835. {
  836. m_togglebut = std::make_unique<ToggleButton>();
  837. m_togglebut->setToggleState(*boolpar, dontSendNotification);
  838. m_togglebut->addListener(this);
  839. m_togglebut->setButtonText(par->getName(50));
  840. addAndMakeVisible(m_togglebut.get());
  841. }
  842. }
  843. void ParameterComponent::resized()
  844. {
  845. if (m_slider)
  846. {
  847. int labw = 200;
  848. if (getWidth() < 400)
  849. labw = 100;
  850. m_label.setBounds(0, 0, labw, 24);
  851. m_slider->setBounds(m_label.getRight() + 1, 0, getWidth() - 2 - m_label.getWidth(), 24);
  852. }
  853. if (m_togglebut)
  854. m_togglebut->setBounds(1, 0, getWidth() - 1, 24);
  855. }
  856. void ParameterComponent::sliderValueChanged(Slider * slid)
  857. {
  858. if (m_notify_only_on_release == true)
  859. return;
  860. AudioParameterFloat* floatpar = dynamic_cast<AudioParameterFloat*>(m_par);
  861. if (floatpar != nullptr)
  862. *floatpar = slid->getValue();
  863. AudioParameterInt* intpar = dynamic_cast<AudioParameterInt*>(m_par);
  864. if (intpar != nullptr)
  865. *intpar = slid->getValue();
  866. }
  867. void ParameterComponent::sliderDragStarted(Slider * slid)
  868. {
  869. m_dragging = true;
  870. }
  871. void ParameterComponent::sliderDragEnded(Slider * slid)
  872. {
  873. m_dragging = false;
  874. if (m_notify_only_on_release == false)
  875. return;
  876. AudioParameterFloat* floatpar = dynamic_cast<AudioParameterFloat*>(m_par);
  877. if (floatpar != nullptr)
  878. *floatpar = slid->getValue();
  879. AudioParameterInt* intpar = dynamic_cast<AudioParameterInt*>(m_par);
  880. if (intpar != nullptr)
  881. *intpar = slid->getValue();
  882. }
  883. void ParameterComponent::buttonClicked(Button * but)
  884. {
  885. AudioParameterBool* boolpar = dynamic_cast<AudioParameterBool*>(m_par);
  886. if (m_togglebut != nullptr && m_togglebut->getToggleState() != *boolpar)
  887. {
  888. *boolpar = m_togglebut->getToggleState();
  889. }
  890. }
  891. void ParameterComponent::updateComponent()
  892. {
  893. AudioParameterFloat* floatpar = dynamic_cast<AudioParameterFloat*>(m_par);
  894. if (floatpar != nullptr && m_slider != nullptr && m_dragging == false && (float)m_slider->getValue() != *floatpar)
  895. {
  896. m_slider->setValue(*floatpar, dontSendNotification);
  897. }
  898. AudioParameterInt* intpar = dynamic_cast<AudioParameterInt*>(m_par);
  899. if (intpar != nullptr && m_slider != nullptr && m_dragging == false && (int)m_slider->getValue() != *intpar)
  900. {
  901. m_slider->setValue(*intpar, dontSendNotification);
  902. }
  903. AudioParameterBool* boolpar = dynamic_cast<AudioParameterBool*>(m_par);
  904. if (m_togglebut != nullptr && m_togglebut->getToggleState() != *boolpar)
  905. {
  906. m_togglebut->setToggleState(*boolpar, dontSendNotification);
  907. }
  908. }
  909. void ParameterComponent::setHighLighted(bool b)
  910. {
  911. if (b == false)
  912. {
  913. m_label.setColour(Label::textColourId, m_labeldefcolor);
  914. if (m_togglebut)
  915. m_togglebut->setColour(ToggleButton::textColourId, m_labeldefcolor);
  916. }
  917. else
  918. {
  919. m_label.setColour(Label::textColourId, Colours::yellow);
  920. if (m_togglebut)
  921. m_togglebut->setColour(ToggleButton::textColourId, Colours::yellow);
  922. }
  923. }
  924. MySlider::MySlider(NormalisableRange<float>* range) : m_range(range)
  925. {
  926. }
  927. double MySlider::proportionOfLengthToValue(double x)
  928. {
  929. if (m_range)
  930. return m_range->convertFrom0to1(x);
  931. return Slider::proportionOfLengthToValue(x);
  932. }
  933. double MySlider::valueToProportionOfLength(double x)
  934. {
  935. if (m_range)
  936. return m_range->convertTo0to1(x);
  937. return Slider::valueToProportionOfLength(x);
  938. }
  939. PerfMeterComponent::PerfMeterComponent(PaulstretchpluginAudioProcessor * p)
  940. : m_proc(p)
  941. {}
  942. void PerfMeterComponent::paint(Graphics & g)
  943. {
  944. g.fillAll(Colours::grey);
  945. double amt = m_proc->getPreBufferingPercent();
  946. g.setColour(Colours::green);
  947. int w = amt * getWidth();
  948. g.fillRect(0, 0, w, getHeight());
  949. g.setColour(Colours::white);
  950. g.drawRect(0, 0, getWidth(), getHeight());
  951. g.setFont(10.0f);
  952. if (m_proc->getPreBufferAmount()>0)
  953. g.drawText("PREBUFFER", 0, 0, getWidth(), getHeight(), Justification::centred);
  954. else
  955. g.drawText("NO PREBUFFER", 0, 0, getWidth(), getHeight(), Justification::centred);
  956. }
  957. void PerfMeterComponent::mouseDown(const MouseEvent & ev)
  958. {
  959. PopupMenu bufferingmenu;
  960. int curbufamount = m_proc->getPreBufferAmount();
  961. bufferingmenu.addItem(100, "None", true, curbufamount == -1);
  962. bufferingmenu.addItem(101, "Small", true, curbufamount == 1);
  963. bufferingmenu.addItem(102, "Medium", true, curbufamount == 2);
  964. bufferingmenu.addItem(103, "Large", true, curbufamount == 3);
  965. bufferingmenu.addItem(104, "Very large", true, curbufamount == 4);
  966. bufferingmenu.addItem(105, "Huge", true, curbufamount == 5);
  967. int r = bufferingmenu.show();
  968. if (r >= 100 && r < 200)
  969. {
  970. if (r == 100)
  971. m_proc->m_use_backgroundbuffering = false;
  972. if (r > 100)
  973. m_proc->setPreBufferAmount(r - 100);
  974. }
  975. }