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