Browse Source

Move random to its own namespace, shuffle files around

tags/v1.0.0
Andrew Belt 6 years ago
parent
commit
587f936a99
42 changed files with 181 additions and 198 deletions
  1. +1
    -2
      include/asset.hpp
  2. +2
    -0
      include/audio.hpp
  3. +1
    -1
      include/color.hpp
  4. +61
    -3
      include/common.hpp
  5. +1
    -1
      include/dsp/ringbuffer.hpp
  6. +1
    -1
      include/engine.hpp
  7. +1
    -2
      include/gamepad.hpp
  8. +2
    -3
      include/keyboard.hpp
  9. +1
    -0
      include/logger.hpp
  10. +1
    -3
      include/math.hpp
  11. +1
    -2
      include/midi.hpp
  12. +1
    -0
      include/network.hpp
  13. +1
    -0
      include/plugin.hpp
  14. +8
    -2
      include/rack.hpp
  15. +31
    -0
      include/random.hpp
  16. +1
    -2
      include/settings.hpp
  17. +2
    -2
      include/string.hpp
  18. +1
    -1
      include/system.hpp
  19. +0
    -86
      include/util/common.hpp
  20. +1
    -1
      include/widgets.hpp
  21. +3
    -1
      include/window.hpp
  22. +1
    -5
      src/app/ModuleWidget.cpp
  23. +2
    -1
      src/app/ParamWidget.cpp
  24. +1
    -0
      src/app/PluginManagerWidget.cpp
  25. +2
    -4
      src/app/RackScene.cpp
  26. +2
    -6
      src/app/RackWidget.cpp
  27. +3
    -4
      src/asset.cpp
  28. +1
    -1
      src/audio.cpp
  29. +4
    -5
      src/bridge.cpp
  30. +4
    -4
      src/engine.cpp
  31. +1
    -1
      src/gamepad.cpp
  32. +1
    -1
      src/keyboard.cpp
  33. +0
    -1
      src/logger.cpp
  34. +6
    -14
      src/main.cpp
  35. +1
    -0
      src/midi.cpp
  36. +0
    -1
      src/network.cpp
  37. +3
    -9
      src/plugin.cpp
  38. +12
    -9
      src/random.cpp
  39. +2
    -1
      src/rtmidi.cpp
  40. +2
    -5
      src/settings.cpp
  41. +0
    -1
      src/system.cpp
  42. +11
    -12
      src/window.cpp

+ 1
- 2
include/asset.hpp View File

@@ -1,6 +1,5 @@
#pragma once

#include <string>
#include "common.hpp"
#include "plugin.hpp"




+ 2
- 0
include/audio.hpp View File

@@ -9,6 +9,8 @@
#include <RtAudio.h>
#pragma GCC diagnostic pop

#include "common.hpp"


namespace rack {



+ 1
- 1
include/color.hpp View File

@@ -1,6 +1,6 @@
#pragma once

#include "util/common.hpp"
#include "common.hpp"
#include "string.hpp"
#include "nanovg.h"



include/macros.hpp → include/common.hpp View File

@@ -1,5 +1,20 @@
#pragma once

// Include some of the C++ standard library for convenience
#include <cstdlib>
#include <cstdio>
#include <cstdint>
#include <cstdarg>
#include <climits>
#include <cmath>
#include <cstring>
#include <cassert>
#include <string>


/** Deprecation notice for GCC */
#define DEPRECATED __attribute__ ((deprecated))


/** Concatenates two literals or two macros
Example:
@@ -11,6 +26,7 @@ expands to
#define CONCAT_LITERAL(x, y) x ## y
#define CONCAT(x, y) CONCAT_LITERAL(x, y)


/** Surrounds raw text with quotes
Example:
#define NAME "world"
@@ -22,9 +38,11 @@ and of course the C++ lexer/parser then concatenates the string literals.
#define TOSTRING_LITERAL(x) #x
#define TOSTRING(x) TOSTRING_LITERAL(x)


/** Produces the length of a static array in number of elements */
#define LENGTHOF(arr) (sizeof(arr) / sizeof((arr)[0]))


/** Reserve space for `count` enums starting with `name`.
Example:
enum Foo {
@@ -36,9 +54,6 @@ Example:
*/
#define ENUMS(name, count) name, name ## _LAST = name + (count) - 1

/** Deprecation notice for GCC */
#define DEPRECATED __attribute__ ((deprecated))


/** References binary files compiled into the program.
For example, to include a file "Test.dat" directly into your program binary, add
@@ -65,3 +80,46 @@ to get its size in bytes.
// The symbol "_binary_##sym##_size" doesn't seem to be valid after a plugin is dynamically loaded, so simply take the difference between the two addresses.
#define BINARY_SIZE(sym) ((size_t) (&_binary_##sym##_end - &_binary_##sym##_start))
#endif


/** C#-style property constructor
Example:
Foo *foo = construct<Foo>(&Foo::greeting, "Hello world");
*/
template<typename T>
T *construct() {
return new T();
}

template<typename T, typename F, typename V, typename... Args>
T *construct(F f, V v, Args... args) {
T *o = construct<T>(args...);
o->*f = v;
return o;
}

/** Defers code until the scope is destructed
From http://www.gingerbill.org/article/defer-in-cpp.html

Example:
file = fopen(...);
DEFER({
fclose(file);
});
*/
template<typename F>
struct DeferWrapper {
F f;
DeferWrapper(F f) : f(f) {}
~DeferWrapper() { f(); }
};

template<typename F>
DeferWrapper<F> deferWrapper(F f) {
return DeferWrapper<F>(f);
}

#define DEFER(code) auto CONCAT(_defer_, __COUNTER__) = deferWrapper([&]() code)

/** Deprecated lowercase macro */
#define defer(...) DEFER(__VA_ARGS__)

+ 1
- 1
include/dsp/ringbuffer.hpp View File

@@ -1,7 +1,7 @@
#pragma once

#include <string.h>
#include "util/common.hpp"
#include "common.hpp"


namespace rack {


+ 1
- 1
include/engine.hpp View File

@@ -1,6 +1,6 @@
#pragma once
#include <vector>
#include "util/common.hpp"
#include "common.hpp"
#include <jansson.h>




+ 1
- 2
include/gamepad.hpp View File

@@ -1,6 +1,5 @@
#pragma once

#include "util/common.hpp"
#include "common.hpp"
#include "midi.hpp"




+ 2
- 3
include/keyboard.hpp View File

@@ -1,8 +1,7 @@
#pragma once

#include "util/common.hpp"
#include "midi.hpp"
#include <map>
#include "common.hpp"
#include "midi.hpp"


namespace rack {


+ 1
- 0
include/logger.hpp View File

@@ -1,4 +1,5 @@
#pragma once
#include "common.hpp"


/** Example usage:


+ 1
- 3
include/math.hpp View File

@@ -1,8 +1,6 @@
#pragma once
#include <cmath>
#include <cstdlib>
#include <algorithm> // for std::min, max
#include "macros.hpp"
#include "common.hpp"


namespace rack {


+ 1
- 2
include/midi.hpp View File

@@ -1,10 +1,9 @@
#pragma once

#include "util/common.hpp"
#include <vector>
#include <queue>
#include <set>
#include <jansson.h>
#include "common.hpp"


namespace rack {


+ 1
- 0
include/network.hpp View File

@@ -1,6 +1,7 @@
#pragma once
#include <string>
#include <jansson.h>
#include "common.hpp"


namespace rack {


+ 1
- 0
include/plugin.hpp View File

@@ -1,6 +1,7 @@
#pragma once
#include <string>
#include <list>
#include "common.hpp"
#include "tags.hpp"




+ 8
- 2
include/rack.hpp View File

@@ -1,10 +1,17 @@
#pragma once

// Include headers that plugins will likely use, for convenience
#include "util/common.hpp"
#include "common.hpp"
#include "math.hpp"
#include "string.hpp"
#include "logger.hpp"
#include "system.hpp"
#include "random.hpp"
#include "network.hpp"
#include "asset.hpp"
#include "plugin.hpp"
#include "engine.hpp"
#include "window.hpp"
#include "widgets.hpp"
#include "app.hpp"
#include "ui.hpp"
@@ -16,7 +23,6 @@ namespace rack {

using namespace math;
using namespace string;
using namespace logger;


} // namespace rack

+ 31
- 0
include/random.hpp View File

@@ -0,0 +1,31 @@
#pragma once
#include <cstdint>
#include "common.hpp"


namespace rack {
namespace random {


/** Seeds the RNG with the current time */
void init();
/** Returns a uniform random uint32_t from 0 to UINT32_MAX */
uint32_t u32();
uint64_t u64();
/** Returns a uniform random float in the interval [0.0, 1.0) */
float uniform();
/** Returns a normal random number with mean 0 and standard deviation 1 */
float normal();


} // namespace random


DEPRECATED inline float randomu32() {return random::u32();}
DEPRECATED inline float randomu64() {return random::u64();}
DEPRECATED inline float randomUniform() {return random::uniform();}
DEPRECATED inline float randomNormal() {return random::normal();}
DEPRECATED inline float randomf() {return random::uniform();}


} // namespace rack

+ 1
- 2
include/settings.hpp View File

@@ -1,6 +1,5 @@
#pragma once

#include <string>
#include "common.hpp"


namespace rack {


+ 2
- 2
include/string.hpp View File

@@ -1,7 +1,7 @@
#pragma once

#include "util/common.hpp"
#include <cstdarg>
#include "common.hpp"
#include <algorithm> // for transform
#include <libgen.h> // for dirname and basename




+ 1
- 1
include/system.hpp View File

@@ -1,6 +1,6 @@
#pragma once
#include <string>
#include <vector>
#include "common.hpp"


namespace rack {


+ 0
- 86
include/util/common.hpp View File

@@ -1,86 +0,0 @@
#pragma once

// Include most of the C++ standard library for convenience
#include <cstdlib>
#include <cstdio>
#include <cstdint>
#include <cstring>
#include <cassert>
#include <climits>

#include <string>
#include <vector>
#include <condition_variable>
#include <mutex>

#include "macros.hpp"
#include "math.hpp"
#include "string.hpp"
#include "logger.hpp"
#include "system.hpp"


namespace rack {

////////////////////
// Template hacks
////////////////////

/** C#-style property constructor
Example:
Foo *foo = construct<Foo>(&Foo::greeting, "Hello world");
*/
template<typename T>
T *construct() {
return new T();
}

template<typename T, typename F, typename V, typename... Args>
T *construct(F f, V v, Args... args) {
T *o = construct<T>(args...);
o->*f = v;
return o;
}

/** Defers code until the scope is destructed
From http://www.gingerbill.org/article/defer-in-cpp.html

Example:
file = fopen(...);
defer({
fclose(file);
});
*/
template<typename F>
struct DeferWrapper {
F f;
DeferWrapper(F f) : f(f) {}
~DeferWrapper() { f(); }
};

template<typename F>
DeferWrapper<F> deferWrapper(F f) {
return DeferWrapper<F>(f);
}

#define defer(code) auto CONCAT(_defer_, __COUNTER__) = deferWrapper([&]() code)

////////////////////
// Random number generator
// random.cpp
////////////////////

/** Seeds the RNG with the current time */
void randomInit();
/** Returns a uniform random uint32_t from 0 to UINT32_MAX */
uint32_t randomu32();
uint64_t randomu64();
/** Returns a uniform random float in the interval [0.0, 1.0) */
float randomUniform();
/** Returns a normal random number with mean 0 and standard deviation 1 */
float randomNormal();

DEPRECATED inline float randomf() {return randomUniform();}


} // namespace rack

+ 1
- 1
include/widgets.hpp View File

@@ -5,7 +5,7 @@
#include "nanovg.h"
#include "nanosvg.h"

#include "util/common.hpp"
#include "common.hpp"
#include "events.hpp"
#include "color.hpp"



+ 3
- 1
include/window.hpp View File

@@ -1,9 +1,11 @@
#pragma once
#include "widgets.hpp"
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include "common.hpp"
#include "widgets.hpp"


#ifdef ARCH_MAC
#define WINDOW_MOD_KEY_NAME "Cmd"


+ 1
- 5
src/app/ModuleWidget.cpp View File

@@ -1,9 +1,5 @@
#include "app.hpp"
#include "engine.hpp"
#include "plugin.hpp"
#include "window.hpp"
#include "asset.hpp"
#include "osdialog.h"
#include "rack.hpp"


namespace rack {


+ 2
- 1
src/app/ParamWidget.cpp View File

@@ -1,5 +1,6 @@
#include "app.hpp"
#include "engine.hpp"
#include "random.hpp"


namespace rack {
@@ -31,7 +32,7 @@ void ParamWidget::reset() {
void ParamWidget::randomize() {
// Infinite params should not be randomized
if (randomizable && std::isfinite(minValue) && std::isfinite(maxValue)) {
setValue(math::rescale(randomUniform(), 0.f, 1.f, minValue, maxValue));
setValue(math::rescale(random::uniform(), 0.f, 1.f, minValue, maxValue));
}
}



+ 1
- 0
src/app/PluginManagerWidget.cpp View File

@@ -1,4 +1,5 @@
#include <thread>
#include "system.hpp"
#include "app.hpp"
#include "plugin.hpp"
#include "window.hpp"


+ 2
- 4
src/app/RackScene.cpp View File

@@ -1,8 +1,6 @@
#include "app.hpp"
#include "window.hpp"
#include "osdialog.h"
#include <string.h>
#include <thread>
#include "osdialog.h"
#include "rack.hpp"


namespace rack {


+ 2
- 6
src/app/RackWidget.cpp View File

@@ -1,12 +1,8 @@
#include "app.hpp"
#include "engine.hpp"
#include "plugin.hpp"
#include "window.hpp"
#include "settings.hpp"
#include "asset.hpp"
#include <map>
#include <algorithm>
#include "osdialog.h"
#include "rack.hpp"
#include "settings.hpp"


namespace rack {


+ 3
- 4
src/asset.cpp View File

@@ -1,7 +1,3 @@
#include "asset.hpp"
#include "util/common.hpp"
#include "osdialog.h"

#if ARCH_MAC
#include <CoreFoundation/CoreFoundation.h>
#include <pwd.h>
@@ -19,6 +15,9 @@
#include <pwd.h>
#endif

#include "rack.hpp"
#include "osdialog.h"


namespace rack {
namespace asset {


+ 1
- 1
src/audio.cpp View File

@@ -1,5 +1,5 @@
#include "rack.hpp"
#include "audio.hpp"
#include "util/common.hpp"
#include "bridge.hpp"




+ 4
- 5
src/bridge.cpp View File

@@ -1,7 +1,3 @@
#include "bridge.hpp"
#include "util/common.hpp"
#include "dsp/ringbuffer.hpp"

#include <unistd.h>
#if ARCH_WIN
#include <winsock2.h>
@@ -13,9 +9,12 @@
#include <fcntl.h>
#endif


#include <thread>

#include "rack.hpp"
#include "bridge.hpp"
#include "dsp/ringbuffer.hpp"


namespace rack {



+ 4
- 4
src/engine.cpp View File

@@ -1,14 +1,14 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <vector>
#include <algorithm>
#include <chrono>
#include <thread>
#include <condition_variable>
#include <mutex>

#include <xmmintrin.h>
#include <pmmintrin.h>

#include "rack.hpp"
#include "engine.hpp"




+ 1
- 1
src/gamepad.cpp View File

@@ -1,4 +1,4 @@
#include <GLFW/glfw3.h>
#include "rack.hpp"
#include "gamepad.hpp"




+ 1
- 1
src/keyboard.cpp View File

@@ -1,5 +1,5 @@
#include "rack.hpp"
#include "keyboard.hpp"
#include <GLFW/glfw3.h>


namespace rack {


+ 0
- 1
src/logger.cpp View File

@@ -1,4 +1,3 @@
#include <cstdarg>
#include <chrono>
#include "logger.hpp"
#include "asset.hpp"


+ 6
- 14
src/main.cpp View File

@@ -1,19 +1,11 @@
#include "util/common.hpp"
#include "engine.hpp"
#include "window.hpp"
#include "app.hpp"
#include "plugin.hpp"
#include "settings.hpp"
#include "asset.hpp"
#include "bridge.hpp"
#include "midi.hpp"
#include <unistd.h>
#include "osdialog.h"
#include "rack.hpp"
#include "rtmidi.hpp"
#include "keyboard.hpp"
#include "gamepad.hpp"
#include "color.hpp"

#include "osdialog.h"
#include <unistd.h>
#include "bridge.hpp"
#include "settings.hpp"

#ifdef ARCH_WIN
#include <Windows.h>
@@ -59,7 +51,7 @@ int main(int argc, char* argv[]) {
#endif

// Initialize environment
randomInit();
random::init();
asset::init(devMode);
logger::init(devMode);



+ 1
- 0
src/midi.cpp View File

@@ -1,3 +1,4 @@
#include "rack.hpp"
#include "midi.hpp"
#include "rtmidi.hpp"
#include "bridge.hpp"


+ 0
- 1
src/network.cpp View File

@@ -2,7 +2,6 @@
#include <curl/curl.h>
#include <openssl/sha.h>

#include "util/common.hpp"
#include "network.hpp"




+ 3
- 9
src/plugin.cpp View File

@@ -1,12 +1,3 @@
#include "plugin.hpp"
#include "app.hpp"
#include "asset.hpp"
#include "network.hpp"
#include "osdialog.h"

#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
@@ -28,6 +19,9 @@
#endif
#include <dirent.h>

#include "rack.hpp"
#include "osdialog.h"


namespace rack {



src/util/random.cpp → src/random.cpp View File

@@ -1,9 +1,11 @@
#include "util/common.hpp"
#include <time.h>
#include <sys/time.h>
#include "random.hpp"
#include "math.hpp"


namespace rack {
namespace random {


// xoroshiro128+
@@ -27,7 +29,7 @@ static uint64_t xoroshiro128plus_next(void) {
return result;
}

void randomInit() {
void init() {
// Only allow the seed to be initialized once during the lifetime of the program.
assert(xoroshiro128plus_state[0] == 0 && xoroshiro128plus_state[1] == 0);
struct timeval tv;
@@ -40,33 +42,34 @@ void randomInit() {
}
}

uint32_t randomu32() {
uint32_t u32() {
return xoroshiro128plus_next() >> 32;
}

uint64_t randomu64() {
uint64_t u64() {
return xoroshiro128plus_next();
}

float randomUniform() {
float uniform() {
// 24 bits of granularity is the best that can be done with floats while ensuring that the return value lies in [0.0, 1.0).
return (xoroshiro128plus_next() >> (64 - 24)) / powf(2, 24);
}

float randomNormal() {
float normal() {
// Box-Muller transform
float radius = sqrtf(-2.f * logf(1.f - randomUniform()));
float theta = 2.f * M_PI * randomUniform();
float radius = sqrtf(-2.f * logf(1.f - uniform()));
float theta = 2.f * M_PI * uniform();
return radius * sinf(theta);

// // Central Limit Theorem
// const int n = 8;
// float sum = 0.0;
// for (int i = 0; i < n; i++) {
// sum += randomUniform();
// sum += uniform();
// }
// return (sum - n / 2.f) / sqrtf(n / 12.f);
}


} // namespace random
} // namespace rack

+ 2
- 1
src/rtmidi.cpp View File

@@ -1,5 +1,6 @@
#include "rtmidi.hpp"
#include <map>
#include "rack.hpp"
#include "rtmidi.hpp"


namespace rack {


+ 2
- 5
src/settings.cpp View File

@@ -1,9 +1,6 @@
#include "settings.hpp"
#include "app.hpp"
#include "window.hpp"
#include "engine.hpp"
#include "plugin.hpp"
#include <jansson.h>
#include "rack.hpp"
#include "settings.hpp"


namespace rack {


+ 0
- 1
src/system.cpp View File

@@ -7,7 +7,6 @@
#endif

#include "system.hpp"
#include "util/common.hpp"


namespace rack {


+ 11
- 12
src/window.cpp View File

@@ -1,16 +1,20 @@
#include "window.hpp"
#include "app.hpp"
#include "asset.hpp"
#include "gamepad.hpp"
#include "keyboard.hpp"
#include "color.hpp"

#include <map>
#include <queue>
#include <thread>

#ifdef ARCH_MAC
// For CGAssociateMouseAndMouseCursorPosition
#include <ApplicationServices/ApplicationServices.h>
#endif


#include "osdialog.h"

#include "rack.hpp"
#include "window.hpp"
#include "keyboard.hpp"
#include "gamepad.hpp"

#define NANOVG_GL2_IMPLEMENTATION 1
// #define NANOVG_GL3_IMPLEMENTATION 1
// #define NANOVG_GLES2_IMPLEMENTATION 1
@@ -25,11 +29,6 @@
#define NANOSVG_ALL_COLOR_KEYWORDS
#include "nanosvg.h"

#ifdef ARCH_MAC
// For CGAssociateMouseAndMouseCursorPosition
#include <ApplicationServices/ApplicationServices.h>
#endif


namespace rack {



Loading…
Cancel
Save