commit e5b308b099154810f01a1b80f0caa231dc2c9076
parent 76925a7bea9e2b21feab5fb85c365a877ca5e426
Author: Matt Demanett <matt@demanett.net>
Date: Wed, 21 Oct 2020 23:09:07 -0400
Scrub snprintfs in favor of format().
Diffstat:
4 files changed, 23 insertions(+), 45 deletions(-)
diff --git a/src/Ranalyzer.cpp b/src/Ranalyzer.cpp
@@ -298,13 +298,11 @@ struct RanalyzerDisplay : AnalyzerDisplay, ChannelDisplayListener {
const int textY = -4;
const int charPx = 5;
- const int sLen = 100;
- char s[sLen];
int x = _insetAround + 2;
- int n = snprintf(s, sLen, "Bin width %0.1f HZ", APP->engine->getSampleRate() / (float)(_module->_core._size / _module->_core._binAverageN));
- drawText(args, s, x, _insetTop + textY);
- x += n * charPx + 20;
+ std::string s = format("Bin width %0.1f HZ", APP->engine->getSampleRate() / (float)(_module->_core._size / _module->_core._binAverageN));
+ drawText(args, s.c_str(), x, _insetTop + textY);
+ x += s.size() * charPx + 20;
const char* labels[3] = { "TEST", "RESPONSE", "ANALYSIS" };
for (int i = 0; i < 3; ++i) {
diff --git a/src/Reftone.cpp b/src/Reftone.cpp
@@ -70,17 +70,9 @@ void ReftoneDisplay::draw(const DrawArgs& args) {
mFrequency = _module->_frequency;
}
- const int n = 20;
- char octave[n];
- snprintf(octave, n, "%d", mOctave);
-
- char fine[n];
- fine[0] = mFine < 0.0 ? '-' : '+';
- snprintf(fine + 1, n - 1, "%02d", abs((int)(mFine * 100)));
-
- char frequency[20];
- snprintf(frequency, n, mFrequency >= 1000.0 ? "%0.0f" : "%0.1f", mFrequency);
-
+ std::string octave = std::to_string(mOctave);
+ std::string fine = format("%s%02d", mFine < 0.0 ? "-" : "+", abs((int)(mFine * 100)));
+ std::string frequency = format(mFrequency >= 1000.0 ? "%0.0f" : "%0.1f", mFrequency);
const char* pitch = NULL;
const char* sharpFlat = NULL;
switch (mPitch) {
@@ -143,15 +135,13 @@ void ReftoneDisplay::draw(const DrawArgs& args) {
if (sharpFlat) {
drawText(args, pitch, 3, 20, 28);
drawText(args, sharpFlat, 16, 12, 16);
- drawText(args, octave, 22, 20, 28);
+ drawText(args, octave.c_str(), 22, 20, 28);
}
else {
- char s[n];
- snprintf(s, n, "%s%s", pitch, octave);
- drawCenteredText(args, s, 20, 28);
+ drawCenteredText(args, (pitch + octave).c_str(), 20, 28);
}
- drawCenteredText(args, fine, 32.5, 14);
- drawCenteredText(args, frequency, 45, 14);
+ drawCenteredText(args, fine.c_str(), 32.5, 14);
+ drawCenteredText(args, frequency.c_str(), 45, 14);
}
void ReftoneDisplay::drawBackground(const DrawArgs& args) {
diff --git a/src/analyzer_base.cpp b/src/analyzer_base.cpp
@@ -534,13 +534,11 @@ void AnalyzerDisplay::drawHeader(const DrawArgs& args, float rangeMinHz, float r
const int textY = -4;
const int charPx = 5;
- const int sLen = 100;
- char s[sLen];
int x = _insetAround + 2;
- int n = snprintf(s, sLen, "Peaks (+/-%0.1f):", (APP->engine->getSampleRate() / 2.0f) / (float)(_module->_core._size / _module->_core._binAverageN));
- drawText(args, s, x, _insetTop + textY);
- x += n * charPx - 0;
+ std::string s = format("Peaks (+/-%0.1f):", (APP->engine->getSampleRate() / 2.0f) / (float)(_module->_core._size / _module->_core._binAverageN));
+ drawText(args, s.c_str(), x, _insetTop + textY);
+ x += s.size() * charPx - 0;
int spacing = 3;
if (_size.x > 300) {
@@ -549,8 +547,8 @@ void AnalyzerDisplay::drawHeader(const DrawArgs& args, float rangeMinHz, float r
}
for (int i = 0; i < _module->_core._nChannels; ++i) {
if (_module->_core._channels[i]) {
- snprintf(s, sLen, "%c:%7.1f", 'A' + i, _module->_core.getPeak(i, rangeMinHz, rangeMaxHz));
- drawText(args, s, x, _insetTop + textY, 0.0, &_channelColors[i % channelColorsN]);
+ s = format("%c:%7.1f", 'A' + i, _module->_core.getPeak(i, rangeMinHz, rangeMaxHz));
+ drawText(args, s.c_str(), x, _insetTop + textY, 0.0, &_channelColors[i % channelColorsN]);
}
x += 9 * charPx + spacing;
}
@@ -715,10 +713,8 @@ void AnalyzerDisplay::drawXAxis(const DrawArgs& args, float strokeWidth, Frequen
if (x > lastX + 0.1f && x < 1.0f) {
lastX = x;
x *= _graphSize.x;
- const int sLen = 32;
- char s[sLen];
- snprintf(s, sLen, "%dk", (int)(hz / 1000.0f));
- drawText(args, s, _insetLeft + x - 7, _size.y - 2);
+ std::string s = format("%dk", (int)(hz / 1000.0f));
+ drawText(args, s.c_str(), _insetLeft + x - 7, _size.y - 2);
}
hz += increment;
}
@@ -769,17 +765,15 @@ void AnalyzerDisplay::drawXAxis(const DrawArgs& args, float strokeWidth, Frequen
if (x > lastX + xMin && x < 0.99f) {
lastX = x;
x *= _graphSize.x;
- const int sLen = 32;
- char s[sLen];
float dhz = hz / divisor;
- int n = 0;
+ std::string s;
if (dhz - (int)dhz < 0.0001f) {
- n = snprintf(s, sLen, "%d%s", (int)dhz, suffix);
+ s = format("%d%s", (int)dhz, suffix);
}
else {
- n = snprintf(s, sLen, "%0.1f%s", dhz, suffix);
+ s = format("%0.1f%s", dhz, suffix);
}
- drawText(args, s, _insetLeft + x - (n <= 2 ? 5 : 8), _size.y - 2);
+ drawText(args, s.c_str(), _insetLeft + x - (s.size() <= 2 ? 5 : 8), _size.y - 2);
}
}
hz += spacing;
diff --git a/src/poly_channels.cpp b/src/poly_channels.cpp
@@ -26,16 +26,12 @@ Menu* PolyChannelsMenuItem::createChildMenu() {
PolyChannelsModule* m = _module;
menu->addChild(new OptionMenuItem("Monophonic", [m]() { return m->_polyChannels == 1; }, [m]() { m->_polyChannels = 1; }));
for (int i = 2; i <= _maxChannels; i++) {
- char s[16];
- snprintf(s, 16, "%d", i);
- menu->addChild(new OptionMenuItem(s, [m, i]() { return m->_polyChannels == i; }, [m, i]() { m->_polyChannels = i; }));
+ menu->addChild(new OptionMenuItem(std::to_string(i).c_str(), [m, i]() { return m->_polyChannels == i; }, [m, i]() { m->_polyChannels = i; }));
}
return menu;
}
void PolyChannelsMenuItem::step() {
MenuItem::step();
- char s[16];
- snprintf(s, 16, "%d ▸", _module->_polyChannels);
- this->rightText = s;
+ this->rightText = format("%d ▸", _module->_polyChannels);
}