Benchmarks.cpp (4112B)
1 #include "Benchmarks.h" 2 #include "../PluginProcessor.h" 3 4 namespace 5 { 6 constexpr double pluginSampleRate = 44100.0; 7 constexpr int samplesPerBlock = 256; 8 constexpr int nChs = 2; 9 } // namespace 10 11 Benchmarks::Benchmarks() 12 { 13 this->commandOption = "--bench"; 14 this->argumentDescription = "--bench --file=FILE --mode=MODE"; 15 this->shortDescription = "Runs benchmarks for ChowTapeModel"; 16 this->longDescription = ""; 17 this->command = std::bind (&Benchmarks::runBenchmarks, this, std::placeholders::_1); 18 } 19 20 void getAudioFile (AudioBuffer<float>& buffer, const File& file) 21 { 22 AudioFormatManager formatManager; 23 formatManager.registerBasicFormats(); 24 25 std::unique_ptr<InputStream> inputStream = file.createInputStream(); 26 if (inputStream == nullptr) 27 return; 28 29 std::unique_ptr<AudioFormatReader> reader (formatManager.createReaderFor (std::move (inputStream))); 30 if (reader == nullptr) 31 return; 32 33 buffer.setSize ((int) reader->numChannels, (int) reader->lengthInSamples); 34 reader->read (buffer.getArrayOfWritePointers(), buffer.getNumChannels(), 0, buffer.getNumSamples()); 35 } 36 37 void setParameters (AudioProcessor* plugin, int mode) 38 { 39 auto params = plugin->getParameters(); 40 for (auto param : params) 41 { 42 if (param->getName (1024) == "Oversampling") 43 { 44 param->setValueNotifyingHost (3.0f / 4.0f); // 8x 45 std::cout << "Setting parameter " << param->getName (1024) 46 << ": " << param->getText (param->getValue(), 1024) << std::endl; 47 } 48 49 if (param->getName (1024) == "Tape Mode") 50 { 51 param->setValueNotifyingHost ((float) mode / 5.0f); 52 std::cout << "Setting parameter " << param->getName (1024) 53 << ": " << param->getText (param->getValue(), 1024) << std::endl; 54 } 55 } 56 } 57 58 double timeAudioProcess (AudioProcessor* plugin, AudioBuffer<float>& audio, const int blockSize) 59 { 60 Time time; 61 62 auto totalNumSamples = audio.getNumSamples(); 63 int samplePtr = 0; 64 MidiBuffer midi; 65 66 auto start = time.getMillisecondCounterHiRes(); 67 while (totalNumSamples > 0) 68 { 69 auto curBlockSize = jmin (totalNumSamples, blockSize); 70 totalNumSamples -= curBlockSize; 71 72 AudioBuffer<float> curBuff (audio.getArrayOfWritePointers(), nChs, samplePtr, curBlockSize); 73 plugin->processBlock (curBuff, midi); 74 75 samplePtr += curBlockSize; 76 } 77 78 return (time.getMillisecondCounterHiRes() - start) / 1000.0; 79 } 80 81 void Benchmarks::runBenchmarks (const ArgumentList& args) 82 { 83 ignoreUnused (args); 84 85 std::cout << "Loading plugin..." << std::endl; 86 auto plugin = std::make_unique<ChowtapeModelAudioProcessor>(); 87 88 File audioFile; 89 if (args.containsOption ("--file")) 90 { 91 audioFile = args.getExistingFileForOption ("--file"); 92 } 93 else 94 { 95 auto rootFolder = File::getSpecialLocation (File::currentExecutableFile); 96 while (rootFolder.getFileName() != "AnalogTapeModel") 97 rootFolder = rootFolder.getParentDirectory(); 98 99 audioFile = rootFolder.getChildFile ("Testing/Canada_Dry.wav"); 100 } 101 102 std::cout << "Loading audio file: " << audioFile.getFullPathName() << std::endl; 103 AudioBuffer<float> audio; 104 getAudioFile (audio, audioFile); 105 const double audioLength = audio.getNumSamples() / pluginSampleRate; // seconds 106 107 if (audioLength == 0.0) 108 { 109 std::cout << "No audio found in file!" << std::endl; 110 return; 111 } 112 113 std::cout << "Setting parameters..." << std::endl; 114 int mode = 4; // STN 115 if (args.containsOption ("--mode")) 116 mode = args.getValueForOption ("--mode").getIntValue(); 117 setParameters (plugin.get(), mode); 118 119 std::cout << "Processing audio..." << std::endl; 120 plugin->prepareToPlay (pluginSampleRate, samplesPerBlock); 121 auto time = timeAudioProcess (plugin.get(), audio, samplesPerBlock); 122 plugin->releaseResources(); 123 124 std::cout << "Results:" << std::endl; 125 std::cout << audioLength / time << "x real-time" << std::endl; 126 std::cout << time << " seconds" << std::endl; 127 }