The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

557 lines
20KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. The code included in this file is provided under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  7. To use, copy, modify, and/or distribute this software for any purpose with or
  8. without fee is hereby granted provided that the above copyright notice and
  9. this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
  11. WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
  12. PURPOSE, ARE DISCLAIMED.
  13. ==============================================================================
  14. */
  15. /*******************************************************************************
  16. The block below describes the properties of this PIP. A PIP is a short snippet
  17. of code that can be read by the Projucer and used to generate a JUCE project.
  18. BEGIN_JUCE_PIP_METADATA
  19. name: AudioPlaybackDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Plays an audio file.
  24. dependencies: juce_audio_basics, juce_audio_devices, juce_audio_formats,
  25. juce_audio_processors, juce_audio_utils, juce_core,
  26. juce_data_structures, juce_events, juce_graphics,
  27. juce_gui_basics, juce_gui_extra
  28. exporters: xcode_mac, vs2022, linux_make, androidstudio, xcode_iphone
  29. type: Component
  30. mainClass: AudioPlaybackDemo
  31. useLocalCopy: 1
  32. END_JUCE_PIP_METADATA
  33. *******************************************************************************/
  34. #pragma once
  35. #include "../Assets/DemoUtilities.h"
  36. inline std::unique_ptr<InputSource> makeInputSource (const URL& url)
  37. {
  38. #if JUCE_ANDROID
  39. if (auto doc = AndroidDocument::fromDocument (url))
  40. return std::make_unique<AndroidDocumentInputSource> (doc);
  41. #endif
  42. #if ! JUCE_IOS
  43. if (url.isLocalFile())
  44. return std::make_unique<FileInputSource> (url.getLocalFile());
  45. #endif
  46. return std::make_unique<URLInputSource> (url);
  47. }
  48. //==============================================================================
  49. class DemoThumbnailComp : public Component,
  50. public ChangeListener,
  51. public FileDragAndDropTarget,
  52. public ChangeBroadcaster,
  53. private ScrollBar::Listener,
  54. private Timer
  55. {
  56. public:
  57. DemoThumbnailComp (AudioFormatManager& formatManager,
  58. AudioTransportSource& source,
  59. Slider& slider)
  60. : transportSource (source),
  61. zoomSlider (slider),
  62. thumbnail (512, formatManager, thumbnailCache)
  63. {
  64. thumbnail.addChangeListener (this);
  65. addAndMakeVisible (scrollbar);
  66. scrollbar.setRangeLimits (visibleRange);
  67. scrollbar.setAutoHide (false);
  68. scrollbar.addListener (this);
  69. currentPositionMarker.setFill (Colours::white.withAlpha (0.85f));
  70. addAndMakeVisible (currentPositionMarker);
  71. }
  72. ~DemoThumbnailComp() override
  73. {
  74. scrollbar.removeListener (this);
  75. thumbnail.removeChangeListener (this);
  76. }
  77. void setURL (const URL& url)
  78. {
  79. if (auto inputSource = makeInputSource (url))
  80. {
  81. thumbnail.setSource (inputSource.release());
  82. Range<double> newRange (0.0, thumbnail.getTotalLength());
  83. scrollbar.setRangeLimits (newRange);
  84. setRange (newRange);
  85. startTimerHz (40);
  86. }
  87. }
  88. URL getLastDroppedFile() const noexcept { return lastFileDropped; }
  89. void setZoomFactor (double amount)
  90. {
  91. if (thumbnail.getTotalLength() > 0)
  92. {
  93. auto newScale = jmax (0.001, thumbnail.getTotalLength() * (1.0 - jlimit (0.0, 0.99, amount)));
  94. auto timeAtCentre = xToTime ((float) getWidth() / 2.0f);
  95. setRange ({ timeAtCentre - newScale * 0.5, timeAtCentre + newScale * 0.5 });
  96. }
  97. }
  98. void setRange (Range<double> newRange)
  99. {
  100. visibleRange = newRange;
  101. scrollbar.setCurrentRange (visibleRange);
  102. updateCursorPosition();
  103. repaint();
  104. }
  105. void setFollowsTransport (bool shouldFollow)
  106. {
  107. isFollowingTransport = shouldFollow;
  108. }
  109. void paint (Graphics& g) override
  110. {
  111. g.fillAll (Colours::darkgrey);
  112. g.setColour (Colours::lightblue);
  113. if (thumbnail.getTotalLength() > 0.0)
  114. {
  115. auto thumbArea = getLocalBounds();
  116. thumbArea.removeFromBottom (scrollbar.getHeight() + 4);
  117. thumbnail.drawChannels (g, thumbArea.reduced (2),
  118. visibleRange.getStart(), visibleRange.getEnd(), 1.0f);
  119. }
  120. else
  121. {
  122. g.setFont (14.0f);
  123. g.drawFittedText ("(No audio file selected)", getLocalBounds(), Justification::centred, 2);
  124. }
  125. }
  126. void resized() override
  127. {
  128. scrollbar.setBounds (getLocalBounds().removeFromBottom (14).reduced (2));
  129. }
  130. void changeListenerCallback (ChangeBroadcaster*) override
  131. {
  132. // this method is called by the thumbnail when it has changed, so we should repaint it..
  133. repaint();
  134. }
  135. bool isInterestedInFileDrag (const StringArray& /*files*/) override
  136. {
  137. return true;
  138. }
  139. void filesDropped (const StringArray& files, int /*x*/, int /*y*/) override
  140. {
  141. lastFileDropped = URL (File (files[0]));
  142. sendChangeMessage();
  143. }
  144. void mouseDown (const MouseEvent& e) override
  145. {
  146. mouseDrag (e);
  147. }
  148. void mouseDrag (const MouseEvent& e) override
  149. {
  150. if (canMoveTransport())
  151. transportSource.setPosition (jmax (0.0, xToTime ((float) e.x)));
  152. }
  153. void mouseUp (const MouseEvent&) override
  154. {
  155. transportSource.start();
  156. }
  157. void mouseWheelMove (const MouseEvent&, const MouseWheelDetails& wheel) override
  158. {
  159. if (thumbnail.getTotalLength() > 0.0)
  160. {
  161. auto newStart = visibleRange.getStart() - wheel.deltaX * (visibleRange.getLength()) / 10.0;
  162. newStart = jlimit (0.0, jmax (0.0, thumbnail.getTotalLength() - (visibleRange.getLength())), newStart);
  163. if (canMoveTransport())
  164. setRange ({ newStart, newStart + visibleRange.getLength() });
  165. if (wheel.deltaY != 0.0f)
  166. zoomSlider.setValue (zoomSlider.getValue() - wheel.deltaY);
  167. repaint();
  168. }
  169. }
  170. private:
  171. AudioTransportSource& transportSource;
  172. Slider& zoomSlider;
  173. ScrollBar scrollbar { false };
  174. AudioThumbnailCache thumbnailCache { 5 };
  175. AudioThumbnail thumbnail;
  176. Range<double> visibleRange;
  177. bool isFollowingTransport = false;
  178. URL lastFileDropped;
  179. DrawableRectangle currentPositionMarker;
  180. float timeToX (const double time) const
  181. {
  182. if (visibleRange.getLength() <= 0)
  183. return 0;
  184. return (float) getWidth() * (float) ((time - visibleRange.getStart()) / visibleRange.getLength());
  185. }
  186. double xToTime (const float x) const
  187. {
  188. return (x / (float) getWidth()) * (visibleRange.getLength()) + visibleRange.getStart();
  189. }
  190. bool canMoveTransport() const noexcept
  191. {
  192. return ! (isFollowingTransport && transportSource.isPlaying());
  193. }
  194. void scrollBarMoved (ScrollBar* scrollBarThatHasMoved, double newRangeStart) override
  195. {
  196. if (scrollBarThatHasMoved == &scrollbar)
  197. if (! (isFollowingTransport && transportSource.isPlaying()))
  198. setRange (visibleRange.movedToStartAt (newRangeStart));
  199. }
  200. void timerCallback() override
  201. {
  202. if (canMoveTransport())
  203. updateCursorPosition();
  204. else
  205. setRange (visibleRange.movedToStartAt (transportSource.getCurrentPosition() - (visibleRange.getLength() / 2.0)));
  206. }
  207. void updateCursorPosition()
  208. {
  209. currentPositionMarker.setVisible (transportSource.isPlaying() || isMouseButtonDown());
  210. currentPositionMarker.setRectangle (Rectangle<float> (timeToX (transportSource.getCurrentPosition()) - 0.75f, 0,
  211. 1.5f, (float) (getHeight() - scrollbar.getHeight())));
  212. }
  213. };
  214. //==============================================================================
  215. class AudioPlaybackDemo : public Component,
  216. #if (JUCE_ANDROID || JUCE_IOS)
  217. private Button::Listener,
  218. #else
  219. private FileBrowserListener,
  220. #endif
  221. private ChangeListener
  222. {
  223. public:
  224. AudioPlaybackDemo()
  225. {
  226. addAndMakeVisible (zoomLabel);
  227. zoomLabel.setFont (Font (15.00f, Font::plain));
  228. zoomLabel.setJustificationType (Justification::centredRight);
  229. zoomLabel.setEditable (false, false, false);
  230. zoomLabel.setColour (TextEditor::textColourId, Colours::black);
  231. zoomLabel.setColour (TextEditor::backgroundColourId, Colour (0x00000000));
  232. addAndMakeVisible (followTransportButton);
  233. followTransportButton.onClick = [this] { updateFollowTransportState(); };
  234. #if (JUCE_ANDROID || JUCE_IOS)
  235. addAndMakeVisible (chooseFileButton);
  236. chooseFileButton.addListener (this);
  237. #else
  238. addAndMakeVisible (fileTreeComp);
  239. directoryList.setDirectory (File::getSpecialLocation (File::userHomeDirectory), true, true);
  240. fileTreeComp.setTitle ("Files");
  241. fileTreeComp.setColour (FileTreeComponent::backgroundColourId, Colours::lightgrey.withAlpha (0.6f));
  242. fileTreeComp.addListener (this);
  243. addAndMakeVisible (explanation);
  244. explanation.setFont (Font (14.00f, Font::plain));
  245. explanation.setJustificationType (Justification::bottomRight);
  246. explanation.setEditable (false, false, false);
  247. explanation.setColour (TextEditor::textColourId, Colours::black);
  248. explanation.setColour (TextEditor::backgroundColourId, Colour (0x00000000));
  249. #endif
  250. addAndMakeVisible (zoomSlider);
  251. zoomSlider.setRange (0, 1, 0);
  252. zoomSlider.onValueChange = [this] { thumbnail->setZoomFactor (zoomSlider.getValue()); };
  253. zoomSlider.setSkewFactor (2);
  254. thumbnail = std::make_unique<DemoThumbnailComp> (formatManager, transportSource, zoomSlider);
  255. addAndMakeVisible (thumbnail.get());
  256. thumbnail->addChangeListener (this);
  257. addAndMakeVisible (startStopButton);
  258. startStopButton.setColour (TextButton::buttonColourId, Colour (0xff79ed7f));
  259. startStopButton.setColour (TextButton::textColourOffId, Colours::black);
  260. startStopButton.onClick = [this] { startOrStop(); };
  261. // audio setup
  262. formatManager.registerBasicFormats();
  263. thread.startThread (3);
  264. #ifndef JUCE_DEMO_RUNNER
  265. RuntimePermissions::request (RuntimePermissions::recordAudio,
  266. [this] (bool granted)
  267. {
  268. int numInputChannels = granted ? 2 : 0;
  269. audioDeviceManager.initialise (numInputChannels, 2, nullptr, true, {}, nullptr);
  270. });
  271. #endif
  272. audioDeviceManager.addAudioCallback (&audioSourcePlayer);
  273. audioSourcePlayer.setSource (&transportSource);
  274. setOpaque (true);
  275. setSize (500, 500);
  276. }
  277. ~AudioPlaybackDemo() override
  278. {
  279. transportSource .setSource (nullptr);
  280. audioSourcePlayer.setSource (nullptr);
  281. audioDeviceManager.removeAudioCallback (&audioSourcePlayer);
  282. #if (JUCE_ANDROID || JUCE_IOS)
  283. chooseFileButton.removeListener (this);
  284. #else
  285. fileTreeComp.removeListener (this);
  286. #endif
  287. thumbnail->removeChangeListener (this);
  288. }
  289. void paint (Graphics& g) override
  290. {
  291. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground));
  292. }
  293. void resized() override
  294. {
  295. auto r = getLocalBounds().reduced (4);
  296. auto controls = r.removeFromBottom (90);
  297. auto controlRightBounds = controls.removeFromRight (controls.getWidth() / 3);
  298. #if (JUCE_ANDROID || JUCE_IOS)
  299. chooseFileButton.setBounds (controlRightBounds.reduced (10));
  300. #else
  301. explanation.setBounds (controlRightBounds);
  302. #endif
  303. auto zoom = controls.removeFromTop (25);
  304. zoomLabel .setBounds (zoom.removeFromLeft (50));
  305. zoomSlider.setBounds (zoom);
  306. followTransportButton.setBounds (controls.removeFromTop (25));
  307. startStopButton .setBounds (controls);
  308. r.removeFromBottom (6);
  309. #if JUCE_ANDROID || JUCE_IOS
  310. thumbnail->setBounds (r);
  311. #else
  312. thumbnail->setBounds (r.removeFromBottom (140));
  313. r.removeFromBottom (6);
  314. fileTreeComp.setBounds (r);
  315. #endif
  316. }
  317. private:
  318. // if this PIP is running inside the demo runner, we'll use the shared device manager instead
  319. #ifndef JUCE_DEMO_RUNNER
  320. AudioDeviceManager audioDeviceManager;
  321. #else
  322. AudioDeviceManager& audioDeviceManager { getSharedAudioDeviceManager (0, 2) };
  323. #endif
  324. AudioFormatManager formatManager;
  325. TimeSliceThread thread { "audio file preview" };
  326. #if (JUCE_ANDROID || JUCE_IOS)
  327. std::unique_ptr<FileChooser> fileChooser;
  328. TextButton chooseFileButton {"Choose Audio File...", "Choose an audio file for playback"};
  329. #else
  330. DirectoryContentsList directoryList {nullptr, thread};
  331. FileTreeComponent fileTreeComp {directoryList};
  332. Label explanation { {}, "Select an audio file in the treeview above, and this page will display its waveform, and let you play it.." };
  333. #endif
  334. URL currentAudioFile;
  335. AudioSourcePlayer audioSourcePlayer;
  336. AudioTransportSource transportSource;
  337. std::unique_ptr<AudioFormatReaderSource> currentAudioFileSource;
  338. std::unique_ptr<DemoThumbnailComp> thumbnail;
  339. Label zoomLabel { {}, "zoom:" };
  340. Slider zoomSlider { Slider::LinearHorizontal, Slider::NoTextBox };
  341. ToggleButton followTransportButton { "Follow Transport" };
  342. TextButton startStopButton { "Play/Stop" };
  343. //==============================================================================
  344. void showAudioResource (URL resource)
  345. {
  346. if (loadURLIntoTransport (resource))
  347. currentAudioFile = std::move (resource);
  348. zoomSlider.setValue (0, dontSendNotification);
  349. thumbnail->setURL (currentAudioFile);
  350. }
  351. bool loadURLIntoTransport (const URL& audioURL)
  352. {
  353. // unload the previous file source and delete it..
  354. transportSource.stop();
  355. transportSource.setSource (nullptr);
  356. currentAudioFileSource.reset();
  357. const auto source = makeInputSource (audioURL);
  358. if (source == nullptr)
  359. return false;
  360. auto stream = rawToUniquePtr (source->createInputStream());
  361. if (stream == nullptr)
  362. return false;
  363. auto reader = rawToUniquePtr (formatManager.createReaderFor (std::move (stream)));
  364. if (reader == nullptr)
  365. return false;
  366. currentAudioFileSource = std::make_unique<AudioFormatReaderSource> (reader.release(), true);
  367. // ..and plug it into our transport source
  368. transportSource.setSource (currentAudioFileSource.get(),
  369. 32768, // tells it to buffer this many samples ahead
  370. &thread, // this is the background thread to use for reading-ahead
  371. currentAudioFileSource->getAudioFormatReader()->sampleRate); // allows for sample rate correction
  372. return true;
  373. }
  374. void startOrStop()
  375. {
  376. if (transportSource.isPlaying())
  377. {
  378. transportSource.stop();
  379. }
  380. else
  381. {
  382. transportSource.setPosition (0);
  383. transportSource.start();
  384. }
  385. }
  386. void updateFollowTransportState()
  387. {
  388. thumbnail->setFollowsTransport (followTransportButton.getToggleState());
  389. }
  390. #if (JUCE_ANDROID || JUCE_IOS)
  391. void buttonClicked (Button* btn) override
  392. {
  393. if (btn == &chooseFileButton && fileChooser.get() == nullptr)
  394. {
  395. if (! RuntimePermissions::isGranted (RuntimePermissions::readExternalStorage))
  396. {
  397. SafePointer<AudioPlaybackDemo> safeThis (this);
  398. RuntimePermissions::request (RuntimePermissions::readExternalStorage,
  399. [safeThis] (bool granted) mutable
  400. {
  401. if (safeThis != nullptr && granted)
  402. safeThis->buttonClicked (&safeThis->chooseFileButton);
  403. });
  404. return;
  405. }
  406. if (FileChooser::isPlatformDialogAvailable())
  407. {
  408. fileChooser = std::make_unique<FileChooser> ("Select an audio file...", File(), "*.wav;*.mp3;*.aif");
  409. fileChooser->launchAsync (FileBrowserComponent::openMode | FileBrowserComponent::canSelectFiles,
  410. [this] (const FileChooser& fc) mutable
  411. {
  412. if (fc.getURLResults().size() > 0)
  413. {
  414. auto u = fc.getURLResult();
  415. showAudioResource (std::move (u));
  416. }
  417. fileChooser = nullptr;
  418. }, nullptr);
  419. }
  420. else
  421. {
  422. NativeMessageBox::showAsync (MessageBoxOptions()
  423. .withIconType (MessageBoxIconType::WarningIcon)
  424. .withTitle ("Enable Code Signing")
  425. .withMessage ("You need to enable code-signing for your iOS project and enable \"iCloud Documents\" "
  426. "permissions to be able to open audio files on your iDevice. See: "
  427. "https://forum.juce.com/t/native-ios-android-file-choosers"),
  428. nullptr);
  429. }
  430. }
  431. }
  432. #else
  433. void selectionChanged() override
  434. {
  435. showAudioResource (URL (fileTreeComp.getSelectedFile()));
  436. }
  437. void fileClicked (const File&, const MouseEvent&) override {}
  438. void fileDoubleClicked (const File&) override {}
  439. void browserRootChanged (const File&) override {}
  440. #endif
  441. void changeListenerCallback (ChangeBroadcaster* source) override
  442. {
  443. if (source == thumbnail.get())
  444. showAudioResource (URL (thumbnail->getLastDroppedFile()));
  445. }
  446. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioPlaybackDemo)
  447. };