ScreenshotHelper.cpp (4062B)
1 #include "ScreenshotHelper.h" 2 #include "../PluginProcessor.h" 3 4 #if ! JUCE_LINUX 5 6 ScreenshotHelper::ScreenshotHelper() 7 { 8 this->commandOption = "--screenshots"; 9 this->argumentDescription = "--screenshots --out=[DIR]"; 10 this->shortDescription = "Generates screenshots for ChowTapeModel documentation"; 11 this->longDescription = ""; 12 this->command = std::bind (&ScreenshotHelper::takeScreenshots, this, std::placeholders::_1); 13 } 14 15 /** 16 * Process audio through the plugin so the screenshots have 17 * some signal show up in the meters and scopes. 18 */ 19 void processAudio (AudioProcessor* plugin) 20 { 21 constexpr double fs = 48000.0; 22 constexpr int blockSize = 1024; 23 constexpr float freq = 200.0f; 24 25 AudioBuffer<float> buffer (2, blockSize); 26 for (int ch = 0; ch < 2; ++ch) 27 { 28 auto* data = buffer.getWritePointer (ch); 29 for (int n = 0; n < blockSize; ++n) 30 data[n] = -0.7f * std::cos (MathConstants<float>::twoPi * freq * (float) n / (float) fs); 31 } 32 33 plugin->prepareToPlay (fs, blockSize); 34 35 MidiBuffer midi; 36 plugin->processBlock (buffer, midi); 37 } 38 39 void findTabbedComponents (Component* root, Array<foleys::Container*>& tabbedComps) 40 { 41 if (root == nullptr) 42 return; 43 44 for (auto child : root->getChildren()) 45 { 46 if (auto tabbedComponent = dynamic_cast<foleys::Container*> (child)) 47 { 48 if (tabbedComponent->layout == foleys::Container::Layout::Tabbed) 49 tabbedComps.add (tabbedComponent); 50 } 51 52 findTabbedComponents (child, tabbedComps); 53 } 54 } 55 56 void ScreenshotHelper::screenshotTab (foleys::Container* container, int tabIdx, const File& outDir) 57 { 58 container->tabbedButtons->setCurrentTabIndex (tabIdx); 59 auto name = container->tabbedButtons->getTabButton (tabIdx)->getName(); 60 61 int index = 0; 62 for (auto& child : container->children) 63 child->setVisible (tabIdx == index++); 64 65 screenshotForBounds (container, container->getLocalBounds(), outDir, name + ".png"); 66 } 67 68 void ScreenshotHelper::takeScreenshots (const ArgumentList& args) 69 { 70 File outputDir = File::getCurrentWorkingDirectory(); 71 if (args.containsOption ("--out")) 72 outputDir = args.getExistingFolderForOption ("--out"); 73 74 std::cout << "Generating screenshots... Saving to " << outputDir.getFullPathName() << std::endl; 75 76 std::unique_ptr<AudioProcessor> plugin (createPluginFilterOfType (AudioProcessor::WrapperType::wrapperType_Standalone)); 77 processAudio (plugin.get()); 78 std::unique_ptr<AudioProcessorEditor> editor (plugin->createEditorIfNeeded()); 79 MessageManager::getInstance()->runDispatchLoopUntil (100); 80 81 // make sure all plugin sections are enabled 82 StringArray onOffIDs { "ifilt_onoff", "hyst_onoff", "tone_onoff", "loss_onoff", "chew_onoff", "deg_onoff", "flutter_onoff", "comp_onoff" }; 83 for (auto param : plugin->getParameters()) 84 { 85 if (auto* paramCast = dynamic_cast<RangedAudioParameter*> (param)) 86 { 87 if (onOffIDs.contains (paramCast->paramID)) 88 paramCast->setValueNotifyingHost (1.0f); 89 } 90 } 91 92 // full screenshot 93 screenshotForBounds (editor.get(), editor->getLocalBounds(), outputDir, "full_gui.png"); 94 95 // get tabbed components 96 Array<foleys::Container*> tabbedComps; 97 findTabbedComponents (editor.get(), tabbedComps); 98 99 for (auto c : tabbedComps) 100 for (int i = 0; i < c->tabbedButtons->getNumTabs(); ++i) 101 screenshotTab (c, i, outputDir); 102 103 plugin->editorBeingDeleted (editor.get()); 104 } 105 106 void ScreenshotHelper::screenshotForBounds (Component* editor, Rectangle<int> bounds, const File& dir, const String& filename) 107 { 108 auto screenshot = editor->createComponentSnapshot (bounds); 109 110 File pngFile = dir.getChildFile (filename); 111 pngFile.deleteFile(); 112 pngFile.create(); 113 auto pngStream = pngFile.createOutputStream(); 114 115 if (pngStream->openedOk()) 116 { 117 PNGImageFormat pngImage; 118 pngImage.writeImageToStream (screenshot, *pngStream.get()); 119 } 120 } 121 122 #endif // ! JUCE_LINUX