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.

187 lines
5.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #include "Main.h"
  20. DSPSamplesApplication::DSPSamplesApplication()
  21. : TimeSliceThread ("Audio File Reader Thread"), demoIndex (-1)
  22. {
  23. loopState.addListener (this);
  24. }
  25. void DSPSamplesApplication::initialise (const String&)
  26. {
  27. formatManager.registerBasicFormats();
  28. audioDeviceManager.addAudioCallback (&audioSourcePlayer);
  29. audioDeviceManager.initialiseWithDefaultDevices (0, 2);
  30. setCurrentDemo (0);
  31. startThread();
  32. mainWindow = new MainWindow (getApplicationName());
  33. }
  34. void DSPSamplesApplication::shutdown()
  35. {
  36. signalThreadShouldExit();
  37. stop();
  38. audioDeviceManager.removeAudioCallback (&audioSourcePlayer);
  39. waitForThreadToExit (10000);
  40. mainWindow = nullptr;
  41. }
  42. //==============================================================================
  43. DSPSamplesApplication& DSPSamplesApplication::getApp()
  44. {
  45. auto* app = dynamic_cast<DSPSamplesApplication*> (JUCEApplication::getInstance());
  46. jassert (app != nullptr);
  47. return *app;
  48. }
  49. //==============================================================================
  50. bool DSPSamplesApplication::loadFile (const File& fileToPlay)
  51. {
  52. stop();
  53. audioSourcePlayer.setSource (nullptr);
  54. mainWindow->setTransportSource (nullptr);
  55. transportSource = nullptr;
  56. readerSource = nullptr;
  57. reader = formatManager.createReaderFor (fileToPlay);
  58. if (reader != nullptr)
  59. {
  60. readerSource = new AudioFormatReaderSource (reader, false);
  61. readerSource->setLooping (loopState.getValue());
  62. init();
  63. return true;
  64. }
  65. return false;
  66. }
  67. void DSPSamplesApplication::togglePlay()
  68. {
  69. if (playState.getValue())
  70. stop();
  71. else
  72. play();
  73. }
  74. void DSPSamplesApplication::stop()
  75. {
  76. playState = false;
  77. if (transportSource != nullptr)
  78. {
  79. transportSource->stop();
  80. transportSource->setPosition (0);
  81. }
  82. }
  83. void DSPSamplesApplication::init()
  84. {
  85. if (transportSource == nullptr)
  86. {
  87. transportSource = new AudioTransportSource();
  88. transportSource->addChangeListener (this);
  89. if (readerSource != nullptr)
  90. {
  91. if (auto* device = audioDeviceManager.getCurrentAudioDevice())
  92. {
  93. transportSource->setSource (readerSource, roundToInt (device->getCurrentSampleRate()), this, reader->sampleRate);
  94. // tell the main window about this so that it can do the seeking behaviour...
  95. mainWindow->setTransportSource (transportSource);
  96. }
  97. }
  98. }
  99. audioSourcePlayer.setSource (nullptr);
  100. currentDemo = nullptr;
  101. if (currentDemo == nullptr)
  102. if (auto demo = Demo::getList()[demoIndex])
  103. if (demo->name.isNotEmpty())
  104. currentDemo = demo->createDemo (*transportSource);
  105. audioSourcePlayer.setSource (currentDemo);
  106. if (mainWindow != nullptr)
  107. mainWindow->initParameters();
  108. }
  109. void DSPSamplesApplication::play()
  110. {
  111. if (readerSource == nullptr)
  112. return;
  113. if (transportSource->getCurrentPosition() >= transportSource->getLengthInSeconds()
  114. || transportSource->getCurrentPosition() < 0)
  115. transportSource->setPosition (0);
  116. transportSource->start();
  117. playState = true;
  118. }
  119. void DSPSamplesApplication::setLooping (bool shouldLoop)
  120. {
  121. if (readerSource != nullptr)
  122. readerSource->setLooping (shouldLoop);
  123. }
  124. void DSPSamplesApplication::changeListenerCallback (ChangeBroadcaster*)
  125. {
  126. if (playState.getValue() && ! transportSource->isPlaying())
  127. stop();
  128. }
  129. void DSPSamplesApplication::setCurrentDemo (int index, bool force)
  130. {
  131. if ((index != demoIndex || force) && isPositiveAndBelow (index, Demo::getList().size()))
  132. {
  133. demoIndex = index;
  134. init();
  135. if (playState.getValue())
  136. play();
  137. }
  138. }
  139. void DSPSamplesApplication::valueChanged (Value& v)
  140. {
  141. if (readerSource != nullptr)
  142. readerSource->setLooping (v.getValue());
  143. }
  144. //==============================================================================
  145. // This macro generates the main() routine that launches the app.
  146. START_JUCE_APPLICATION (DSPSamplesApplication)