Skip to content

Commit 3d1a43e

Browse files
committed
Refactoring
1 parent fc7bdd2 commit 3d1a43e

File tree

6 files changed

+49
-24
lines changed

6 files changed

+49
-24
lines changed

src/Robocode/Arctangent.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <cmath>
44
#include <complex>
55
#include <concepts>
6+
#include <numbers>
67

78
/**
89
* Arctangent approximation according to [1].
@@ -43,7 +44,7 @@ namespace Arctangent
4344
const T phi = q + std::copysign(e, s);
4445

4546
// translate the result from [0, 4) to [0, 2pi)
46-
return phi * T(1.57079632679489661923);
47+
return phi * T(std::numbers::pi / 2);
4748
}
4849

4950
template<std::floating_point T>

src/Robocode/Assert.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
while(0) //
3131

3232
template<typename... Args>
33-
static void assert_throw_if(
33+
inline void assert_throw_if(
3434
const bool condition,
3535
const char* expression,
3636
const char* file,
@@ -51,7 +51,7 @@ static void assert_throw_if(
5151
va_start(args, format);
5252

5353
va_copy(temp, args);
54-
auto size = std::vsnprintf(nullptr, 0, format, temp);
54+
int size = std::vsnprintf(nullptr, 0, format, temp);
5555
va_end(temp);
5656

5757
std::string buffer(size + 1, '\0');

src/Robocode/Channel.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ Channel::Channel(const size_t index, const double samplerate, const std::span<co
44
{
55
const size_t dftsize = frequencies.size();
66

7-
// https://newt.phys.unsw.edu.au/jw/notes.html
8-
const double hz = std::pow(2, (double(index) - 69) / 12) * concertpitch;
9-
107
config.dftsize = dftsize;
118

12-
config.freq = hz;
9+
config.freq = midi_to_hertz(index, concertpitch);
1310
config.gain = 0;
1411

1512
config.samplerate = samplerate;
@@ -21,7 +18,7 @@ Channel::Channel(const size_t index, const double samplerate, const std::span<co
2118

2219
for (size_t i = 0; i < dftsize; ++i)
2320
{
24-
config.chnfreqs[i] = hz * frequencies[i];
21+
config.chnfreqs[i] = config.freq * frequencies[i];
2522
}
2623
}
2724

@@ -57,7 +54,7 @@ double Channel::synthesize(const std::span<std::complex<double>> dft,
5754
{
5855
assert_true(dft.size() == config.dftsize, "Invalid DFT size!");
5956

60-
const double freq2phase = (2 * std::numbers::pi) / config.samplerate;
57+
const double hz2rad = hertz_to_radian(config.samplerate);
6158

6259
const double gain = config.gain;
6360
const auto& chnfreqs = config.chnfreqs;
@@ -70,7 +67,7 @@ double Channel::synthesize(const std::span<std::complex<double>> dft,
7067
{
7168
const double newfreq = chnfreqs[i];
7269

73-
phase[i] += newfreq * freq2phase;
70+
phase[i] += newfreq * hz2rad;
7471

7572
if (newfreq <= dftfreqs.front())
7673
{
@@ -94,7 +91,7 @@ double Channel::synthesize(const std::span<std::complex<double>> dft,
9491
{
9592
const double newfreq = chnfreqs[i] * pvcfreqs[i];
9693

97-
phase[i] += newfreq * freq2phase;
94+
phase[i] += newfreq * hz2rad;
9895

9996
if (newfreq <= dftfreqs.front())
10097
{

src/Robocode/Header.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
#include <vector>
1818

1919
#include <Robocode/Assert.h>
20+
#include <Robocode/Math.h>

src/Robocode/Math.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#pragma once
2+
3+
#include <Robocode/Arctangent.h>
4+
5+
inline double angle(const std::complex<double>& z)
6+
{
7+
#if defined(ENABLE_ARCTANGENT_APPROXIMATION)
8+
return Arctangent::atan2(z);
9+
#else
10+
return std::arg(z);
11+
#endif
12+
}
13+
14+
inline double wrap(const double angle)
15+
{
16+
const double pi = 2 * std::numbers::pi;
17+
18+
return angle - pi * std::floor(angle / pi + 0.5);
19+
}
20+
21+
// (2 * pi) / sr
22+
inline double hertz_to_radian(const double samplerate)
23+
{
24+
return (2 * std::numbers::pi) / samplerate;
25+
}
26+
27+
// sr / (2 * pi)
28+
inline double radian_to_hertz(const double samplerate)
29+
{
30+
return samplerate / (2 * std::numbers::pi);
31+
}
32+
33+
// https://newt.phys.unsw.edu.au/jw/notes.html
34+
inline double midi_to_hertz(const double midi, const double concertpitch)
35+
{
36+
return std::pow(2, (midi - 69) / 12) * concertpitch;
37+
}

src/Robocode/Vocoder.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,21 @@
11
#include <Robocode/Vocoder.h>
22

3-
#include <Robocode/Arctangent.h>
4-
5-
inline double angle(const std::complex<double>& z)
6-
{
7-
#if defined(ENABLE_ARCTANGENT_APPROXIMATION)
8-
return Arctangent::atan2(z);
9-
#else
10-
return std::arg(z);
11-
#endif
12-
}
13-
143
Vocoder::Vocoder(const double samplerate, const std::span<const double> frequencies) :
154
dftsize(frequencies.size())
165
{
176
cache.resize(dftsize);
187
slope.resize(dftsize);
198
freqs.resize(dftsize);
209

21-
const double phase2freq = samplerate / (2 * std::numbers::pi);
10+
const double rad2hz = radian_to_hertz(samplerate);
2211

2312
std::transform(
2413
frequencies.begin(),
2514
frequencies.end(),
2615
slope.begin(),
2716
[&](const double freq)
2817
{
29-
return (freq > 0) ? phase2freq / freq : 0;
18+
return (freq > 0) ? rad2hz / freq : 0;
3019
});
3120
}
3221

0 commit comments

Comments
 (0)