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.

365 lines
13KB

  1. /*
  2. ==============================================================================
  3. This is an automatically generated file created by the Jucer!
  4. Creation date: 1 May 2011 12:08:14pm
  5. Be careful when adding custom code to these files, as only the code within
  6. the "//[xyz]" and "//[/xyz]" sections will be retained when the file is loaded
  7. and re-saved.
  8. Jucer version: 1.12
  9. ------------------------------------------------------------------------------
  10. The Jucer is part of the JUCE library - "Jules' Utility Class Extensions"
  11. Copyright 2004-6 by Raw Material Software ltd.
  12. ==============================================================================
  13. */
  14. //[Headers] You can add your own extra header files here...
  15. //[/Headers]
  16. #include "AudioDemoPlaybackPage.h"
  17. //[MiscUserDefs] You can add your own user definitions and misc code here...
  18. class DemoThumbnailComp : public Component,
  19. public ChangeListener,
  20. public FileDragAndDropTarget
  21. {
  22. public:
  23. DemoThumbnailComp()
  24. : thumbnailCache (5),
  25. thumbnail (512, formatManager, thumbnailCache)
  26. {
  27. startTime = endTime = 0;
  28. formatManager.registerBasicFormats();
  29. thumbnail.addChangeListener (this);
  30. }
  31. ~DemoThumbnailComp()
  32. {
  33. thumbnail.removeChangeListener (this);
  34. }
  35. void setFile (const File& file)
  36. {
  37. thumbnail.setSource (new FileInputSource (file));
  38. startTime = 0;
  39. endTime = thumbnail.getTotalLength();
  40. }
  41. void setZoomFactor (double amount)
  42. {
  43. if (thumbnail.getTotalLength() > 0)
  44. {
  45. double timeDisplayed = jmax (0.001, (thumbnail.getTotalLength() - startTime) * (1.0 - jlimit (0.0, 1.0, amount)));
  46. endTime = startTime + timeDisplayed;
  47. repaint();
  48. }
  49. }
  50. void mouseWheelMove (const MouseEvent&, float wheelIncrementX, float wheelIncrementY)
  51. {
  52. if (thumbnail.getTotalLength() > 0)
  53. {
  54. double newStart = startTime + (wheelIncrementX + wheelIncrementY) * (endTime - startTime) / 10.0;
  55. newStart = jlimit (0.0, thumbnail.getTotalLength() - (endTime - startTime), newStart);
  56. endTime = newStart + (endTime - startTime);
  57. startTime = newStart;
  58. repaint();
  59. }
  60. }
  61. void paint (Graphics& g)
  62. {
  63. g.fillAll (Colours::white);
  64. g.setColour (Colours::mediumblue);
  65. if (thumbnail.getTotalLength() > 0)
  66. {
  67. thumbnail.drawChannels (g, getLocalBounds().reduced (2, 2),
  68. startTime, endTime, 1.0f);
  69. }
  70. else
  71. {
  72. g.setFont (14.0f);
  73. g.drawFittedText ("(No audio file selected)", 0, 0, getWidth(), getHeight(),
  74. Justification::centred, 2);
  75. }
  76. }
  77. void changeListenerCallback (ChangeBroadcaster*)
  78. {
  79. // this method is called by the thumbnail when it has changed, so we should repaint it..
  80. repaint();
  81. }
  82. bool isInterestedInFileDrag (const StringArray& /*files*/)
  83. {
  84. return true;
  85. }
  86. void filesDropped (const StringArray& files, int /*x*/, int /*y*/)
  87. {
  88. AudioDemoPlaybackPage* demoPage = findParentComponentOfClass ((AudioDemoPlaybackPage*) 0);
  89. if (demoPage != 0)
  90. demoPage->showFile (File (files[0]));
  91. }
  92. AudioFormatManager formatManager;
  93. AudioThumbnailCache thumbnailCache;
  94. AudioThumbnail thumbnail;
  95. double startTime, endTime;
  96. };
  97. //[/MiscUserDefs]
  98. //==============================================================================
  99. AudioDemoPlaybackPage::AudioDemoPlaybackPage (AudioDeviceManager& deviceManager_)
  100. : deviceManager (deviceManager_),
  101. thread ("audio file preview"),
  102. directoryList (0, thread),
  103. zoomLabel (0),
  104. thumbnail (0),
  105. startStopButton (0),
  106. fileTreeComp (0),
  107. explanation (0),
  108. zoomSlider (0)
  109. {
  110. addAndMakeVisible (zoomLabel = new Label (String::empty,
  111. L"zoom:"));
  112. zoomLabel->setFont (Font (15.0000f, Font::plain));
  113. zoomLabel->setJustificationType (Justification::centredRight);
  114. zoomLabel->setEditable (false, false, false);
  115. zoomLabel->setColour (TextEditor::textColourId, Colours::black);
  116. zoomLabel->setColour (TextEditor::backgroundColourId, Colour (0x0));
  117. addAndMakeVisible (thumbnail = new DemoThumbnailComp());
  118. addAndMakeVisible (startStopButton = new TextButton (String::empty));
  119. startStopButton->setButtonText (L"Play/Stop");
  120. startStopButton->addListener (this);
  121. startStopButton->setColour (TextButton::buttonColourId, Colour (0xff79ed7f));
  122. addAndMakeVisible (fileTreeComp = new FileTreeComponent (directoryList));
  123. addAndMakeVisible (explanation = new Label (String::empty,
  124. L"Select an audio file in the treeview above, and this page will display its waveform, and let you play it.."));
  125. explanation->setFont (Font (14.0000f, Font::plain));
  126. explanation->setJustificationType (Justification::bottomRight);
  127. explanation->setEditable (false, false, false);
  128. explanation->setColour (TextEditor::textColourId, Colours::black);
  129. explanation->setColour (TextEditor::backgroundColourId, Colour (0x0));
  130. addAndMakeVisible (zoomSlider = new Slider (String::empty));
  131. zoomSlider->setRange (0, 1, 0);
  132. zoomSlider->setSliderStyle (Slider::LinearHorizontal);
  133. zoomSlider->setTextBoxStyle (Slider::NoTextBox, false, 80, 20);
  134. zoomSlider->addListener (this);
  135. zoomSlider->setSkewFactor (2);
  136. //[UserPreSize]
  137. //[/UserPreSize]
  138. setSize (600, 400);
  139. //[Constructor] You can add your own custom stuff here..
  140. directoryList.setDirectory (File::getSpecialLocation (File::userHomeDirectory), true, true);
  141. thread.startThread (3);
  142. fileTreeComp->setColour (FileTreeComponent::backgroundColourId, Colours::white);
  143. fileTreeComp->addListener (this);
  144. deviceManager.addAudioCallback (&audioSourcePlayer);
  145. audioSourcePlayer.setSource (&transportSource);
  146. //[/Constructor]
  147. }
  148. AudioDemoPlaybackPage::~AudioDemoPlaybackPage()
  149. {
  150. //[Destructor_pre]. You can add your own custom destruction code here..
  151. transportSource.setSource (0);
  152. audioSourcePlayer.setSource (0);
  153. deviceManager.removeAudioCallback (&audioSourcePlayer);
  154. fileTreeComp->removeListener (this);
  155. //[/Destructor_pre]
  156. deleteAndZero (zoomLabel);
  157. deleteAndZero (thumbnail);
  158. deleteAndZero (startStopButton);
  159. deleteAndZero (fileTreeComp);
  160. deleteAndZero (explanation);
  161. deleteAndZero (zoomSlider);
  162. //[Destructor]. You can add your own custom destruction code here..
  163. //[/Destructor]
  164. }
  165. //==============================================================================
  166. void AudioDemoPlaybackPage::paint (Graphics& g)
  167. {
  168. //[UserPrePaint] Add your own custom painting code here..
  169. //[/UserPrePaint]
  170. g.fillAll (Colours::lightgrey);
  171. //[UserPaint] Add your own custom painting code here..
  172. //[/UserPaint]
  173. }
  174. void AudioDemoPlaybackPage::resized()
  175. {
  176. zoomLabel->setBounds (16, getHeight() - 90, 55, 24);
  177. thumbnail->setBounds (16, getHeight() - 221, getWidth() - 32, 123);
  178. startStopButton->setBounds (16, getHeight() - 46, 150, 32);
  179. fileTreeComp->setBounds (16, 8, getWidth() - 32, getHeight() - 245);
  180. explanation->setBounds (256, getHeight() - 82, getWidth() - 275, 64);
  181. zoomSlider->setBounds (72, getHeight() - 90, 200, 24);
  182. //[UserResized] Add your own custom resize handling here..
  183. //[/UserResized]
  184. }
  185. void AudioDemoPlaybackPage::buttonClicked (Button* buttonThatWasClicked)
  186. {
  187. //[UserbuttonClicked_Pre]
  188. //[/UserbuttonClicked_Pre]
  189. if (buttonThatWasClicked == startStopButton)
  190. {
  191. //[UserButtonCode_startStopButton] -- add your button handler code here..
  192. if (transportSource.isPlaying())
  193. {
  194. transportSource.stop();
  195. }
  196. else
  197. {
  198. transportSource.setPosition (0);
  199. transportSource.start();
  200. }
  201. //[/UserButtonCode_startStopButton]
  202. }
  203. //[UserbuttonClicked_Post]
  204. //[/UserbuttonClicked_Post]
  205. }
  206. void AudioDemoPlaybackPage::sliderValueChanged (Slider* sliderThatWasMoved)
  207. {
  208. //[UsersliderValueChanged_Pre]
  209. //[/UsersliderValueChanged_Pre]
  210. if (sliderThatWasMoved == zoomSlider)
  211. {
  212. //[UserSliderCode_zoomSlider] -- add your slider handling code here..
  213. thumbnail->setZoomFactor (zoomSlider->getValue());
  214. //[/UserSliderCode_zoomSlider]
  215. }
  216. //[UsersliderValueChanged_Post]
  217. //[/UsersliderValueChanged_Post]
  218. }
  219. //[MiscUserCode] You can add your own definitions of your custom methods or any other code here...
  220. void AudioDemoPlaybackPage::showFile (const File& file)
  221. {
  222. loadFileIntoTransport (file);
  223. zoomSlider->setValue (0, false, false);
  224. thumbnail->setFile (file);
  225. }
  226. void AudioDemoPlaybackPage::loadFileIntoTransport (const File& audioFile)
  227. {
  228. // unload the previous file source and delete it..
  229. transportSource.stop();
  230. transportSource.setSource (0);
  231. currentAudioFileSource = 0;
  232. // get a format manager and set it up with the basic types (wav and aiff).
  233. AudioFormatManager formatManager;
  234. formatManager.registerBasicFormats();
  235. AudioFormatReader* reader = formatManager.createReaderFor (audioFile);
  236. if (reader != 0)
  237. {
  238. currentAudioFileSource = new AudioFormatReaderSource (reader, true);
  239. // ..and plug it into our transport source
  240. transportSource.setSource (currentAudioFileSource,
  241. 32768, // tells it to buffer this many samples ahead
  242. reader->sampleRate);
  243. }
  244. }
  245. void AudioDemoPlaybackPage::selectionChanged()
  246. {
  247. showFile (fileTreeComp->getSelectedFile());
  248. }
  249. void AudioDemoPlaybackPage::fileClicked (const File&, const MouseEvent&)
  250. {
  251. }
  252. void AudioDemoPlaybackPage::fileDoubleClicked (const File&)
  253. {
  254. }
  255. //[/MiscUserCode]
  256. //==============================================================================
  257. #if 0
  258. /* -- Jucer information section --
  259. This is where the Jucer puts all of its metadata, so don't change anything in here!
  260. BEGIN_JUCER_METADATA
  261. <JUCER_COMPONENT documentType="Component" className="AudioDemoPlaybackPage" componentName=""
  262. parentClasses="public Component, public FileBrowserListener"
  263. constructorParams="AudioDeviceManager&amp; deviceManager_" variableInitialisers="deviceManager (deviceManager_),&#10;thread (&quot;audio file preview&quot;),&#10;directoryList (0, thread)"
  264. snapPixels="8" snapActive="1" snapShown="1" overlayOpacity="0.330000013"
  265. fixedSize="0" initialWidth="600" initialHeight="400">
  266. <BACKGROUND backgroundColour="ffd3d3d3"/>
  267. <LABEL name="" id="d4f78f975d81c8d3" memberName="zoomLabel" virtualName=""
  268. explicitFocusOrder="0" pos="16 90R 55 24" edTextCol="ff000000"
  269. edBkgCol="0" labelText="zoom:" editableSingleClick="0" editableDoubleClick="0"
  270. focusDiscardsChanges="0" fontname="Default font" fontsize="15"
  271. bold="0" italic="0" justification="34"/>
  272. <GENERICCOMPONENT name="" id="beef657b0e007936" memberName="thumbnail" virtualName=""
  273. explicitFocusOrder="0" pos="16 221R 32M 123" class="DemoThumbnailComp"
  274. params=""/>
  275. <TEXTBUTTON name="" id="abe446e2f3f09420" memberName="startStopButton" virtualName=""
  276. explicitFocusOrder="0" pos="16 46R 150 32" bgColOff="ff79ed7f"
  277. buttonText="Play/Stop" connectedEdges="0" needsCallback="1" radioGroupId="0"/>
  278. <GENERICCOMPONENT name="" id="1de1dc6a18a9032b" memberName="fileTreeComp" virtualName=""
  279. explicitFocusOrder="0" pos="16 8 32M 245M" class="FileTreeComponent"
  280. params="directoryList"/>
  281. <LABEL name="" id="7db7d0a64ef21311" memberName="explanation" virtualName=""
  282. explicitFocusOrder="0" pos="256 82R 275M 64" edTextCol="ff000000"
  283. edBkgCol="0" labelText="Select an audio file in the treeview above, and this page will display its waveform, and let you play it.."
  284. editableSingleClick="0" editableDoubleClick="0" focusDiscardsChanges="0"
  285. fontname="Default font" fontsize="14" bold="0" italic="0" justification="18"/>
  286. <SLIDER name="" id="38bbc108f4c96092" memberName="zoomSlider" virtualName=""
  287. explicitFocusOrder="0" pos="72 90R 200 24" min="0" max="1" int="0"
  288. style="LinearHorizontal" textBoxPos="NoTextBox" textBoxEditable="1"
  289. textBoxWidth="80" textBoxHeight="20" skewFactor="2"/>
  290. </JUCER_COMPONENT>
  291. END_JUCER_METADATA
  292. */
  293. #endif