synth stuffs

This commit is contained in:
Squishy Bloob 2024-04-03 10:27:07 +00:00
parent 51ebae5c04
commit 0e775a235f
13 changed files with 89172 additions and 0 deletions

View File

@ -19,7 +19,9 @@
</ProjectConfiguration> </ProjectConfiguration>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="src\audio.cpp" />
<ClCompile Include="src\draw\upscale.cpp" /> <ClCompile Include="src\draw\upscale.cpp" />
<ClCompile Include="src\synth.cpp" />
<ClCompile Include="src\util.cpp" /> <ClCompile Include="src\util.cpp" />
<ClCompile Include="src\ini.cpp" /> <ClCompile Include="src\ini.cpp" />
<ClCompile Include="src\draw\draw_text.cpp" /> <ClCompile Include="src\draw\draw_text.cpp" />
@ -31,19 +33,23 @@
<ClCompile Include="src\game.cpp" /> <ClCompile Include="src\game.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="include\audio.h" />
<ClInclude Include="include\bloob.h" /> <ClInclude Include="include\bloob.h" />
<ClInclude Include="include\font.h" /> <ClInclude Include="include\font.h" />
<ClInclude Include="include\game.h" /> <ClInclude Include="include\game.h" />
<ClInclude Include="include\image.h" /> <ClInclude Include="include\image.h" />
<ClInclude Include="include\ini.h" /> <ClInclude Include="include\ini.h" />
<ClInclude Include="include\keys.h" />
<ClInclude Include="include\rect.h" /> <ClInclude Include="include\rect.h" />
<ClInclude Include="include\resource.h" /> <ClInclude Include="include\resource.h" />
<ClInclude Include="include\settings.h" /> <ClInclude Include="include\settings.h" />
<ClInclude Include="include\synth.h" />
<ClInclude Include="include\tileset.h" /> <ClInclude Include="include\tileset.h" />
<ClInclude Include="include\util.h" /> <ClInclude Include="include\util.h" />
<ClInclude Include="include\vec2.h" /> <ClInclude Include="include\vec2.h" />
<ClInclude Include="include\resource_manager.h" /> <ClInclude Include="include\resource_manager.h" />
<ClInclude Include="include\math_util.h" /> <ClInclude Include="include\math_util.h" />
<ClInclude Include="src\miniaudio.h" />
<ClInclude Include="src\stb_image.h" /> <ClInclude Include="src\stb_image.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -45,6 +45,12 @@
<ClCompile Include="src\draw\upscale.cpp"> <ClCompile Include="src\draw\upscale.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\audio.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\synth.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="include\game.h"> <ClInclude Include="include\game.h">
@ -89,6 +95,18 @@
<ClInclude Include="include\math_util.h"> <ClInclude Include="include\math_util.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\miniaudio.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\audio.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\synth.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\keys.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="fontconvert.bat"> <None Include="fontconvert.bat">

21
blooblib/gen_wave.py Normal file
View File

@ -0,0 +1,21 @@
import math
def sin(i):
return math.sin(i / 256.0 * math.pi * 2)
def sqr(i):
if i < 128:
return -1.0
else:
return 1.0
def to_str(i):
return hex(int(((i + 1.0)/2) * 65535) - 32768)
def note_to_freq(i): #16ths of a tone starting at c0
return hex(round(2**(i/16.0/12)*16.3516))
#print(", ".join(list(map(to_str, map(sqr, range(256))))))
print(", ".join(list(map(note_to_freq, range(120*16)))))

19
blooblib/include/audio.h Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include <cstdint>
using sample = int16_t;
struct ma_device;
extern sample lookup_sin[256], lookup_square[256];
struct audio {
audio(int samplerate);
~audio();
void callback(sample* output, int frames);
private:
int _samplerate;
ma_device* _device;
};

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "audio.h"
#include "font.h" #include "font.h"
#include "game.h" #include "game.h"
#include "image.h" #include "image.h"
@ -9,6 +10,7 @@
#include "resource.h" #include "resource.h"
#include "resource_manager.h" #include "resource_manager.h"
#include "settings.h" #include "settings.h"
#include "synth.h"
#include "tileset.h" #include "tileset.h"
#include "util.h" #include "util.h"
#include "vec2.h" #include "vec2.h"

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "audio.h"
#include "image.h" #include "image.h"
#include "settings.h" #include "settings.h"
#include "resource_manager.h" #include "resource_manager.h"
@ -24,6 +25,7 @@ private:
int _fps; int _fps;
int _argc; int _argc;
char** _argv; char** _argv;
audio* _audio;
}; };
template<typename T> template<typename T>

12
blooblib/include/keys.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
enum class key {
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z,
n0, n1, n2, n3, n4, n5, n6, n7, n8, n9,
np0, np1, np2, np3, np4, np5, np6, np7, np8, np9,
np_mul, np_div, np_add, np_seperator, np_enter,
f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12,
backspace, tab, enter, lshift, ltrl, caps, esc, space,
end, home, insert, del, page_up, page_down, up, down, left, right,
};

13
blooblib/include/synth.h Normal file
View File

@ -0,0 +1,13 @@
#pragma once
#include <cstdint>
//notes are 16th steps, starting at C-0
extern uint16_t lookup_note_to_freq[16*120];
struct phase_accumulator {
phase_accumulator();
uint16_t acc;
uint16_t inc;
void tick();
};

68
blooblib/src/audio.cpp Normal file
View File

@ -0,0 +1,68 @@
#include "audio.h"
#include <format>
#include <iostream>
#include <stdexcept>
#include "math_util.h"
#include "synth.h"
#define MA_ENABLE_ONLY_SPECIFIC_BACKENDS
#define MA_ENABLE_WASAPI
#define MA_NO_DECODING
#define MA_NO_ENCODING
#define MA_NO_WAV
#define MA_NO_FLAC
#define MA_NO_MP3
#define MA_NO_RESOURCE_MANAGER
#define MA_NO_NODE_GRAPH
#define MA_NO_ENGINE
#define MA_NO_GENERATION
#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"
void static_callback(ma_device* device, void* output, void const* input, ma_uint32 frame_count) {
(reinterpret_cast<audio*>(device->pUserData))->callback(reinterpret_cast<sample*>(output), frame_count);
}
audio::audio(int samplerate) : _samplerate(samplerate) {
ma_device_config config = ma_device_config_init(ma_device_type_playback);
config.playback.format = ma_format_s16;
config.playback.channels = 2;
config.sampleRate = samplerate;
config.dataCallback = static_callback;
config.pUserData = this;
_device = new ma_device();
if(ma_device_init(NULL, &config, _device) != MA_SUCCESS)
throw new std::runtime_error("couldn't initialise audio device");
std::cout << "opened audio device: " << _device->playback.name << std::endl;
if(ma_device_start(_device) != MA_SUCCESS)
throw new std::runtime_error("couldn't start audio device");
}
audio::~audio() {
ma_device_uninit(_device);
}
phase_accumulator pa;
void audio::callback(sample* output, int frames) {
for(int i = 0; i < frames; ++i) {
pa.tick();
uint16_t s = lookup_sin[pa.acc >> 8];
int16_t val = s;
val = 0;
*output = val;
++output;
*output = val;
++output;
}
}
sample lookup_sin[256] = { -0x1, 0x323, 0x647, 0x96a, 0xc8b, 0xfaa, 0x12c7, 0x15e1, 0x18f8, 0x1c0a, 0x1f19, 0x2223, 0x2527, 0x2826, 0x2b1e, 0x2e10, 0x30fb, 0x33de, 0x36b9, 0x398c, 0x3c55, 0x3f16, 0x41cd, 0x447a, 0x471c, 0x49b3, 0x4c3f, 0x4ebf, 0x5132, 0x539a, 0x55f4, 0x5842, 0x5a81, 0x5cb3, 0x5ed6, 0x60eb, 0x62f1, 0x64e7, 0x66ce, 0x68a5, 0x6a6c, 0x6c23, 0x6dc9, 0x6f5e, 0x70e1, 0x7254, 0x73b4, 0x7503, 0x7640, 0x776b, 0x7883, 0x7989, 0x7a7c, 0x7b5c, 0x7c28, 0x7ce2, 0x7d89, 0x7e1c, 0x7e9c, 0x7f08, 0x7f61, 0x7fa6, 0x7fd7, 0x7ff5, 0x7fff, 0x7ff5, 0x7fd7, 0x7fa6, 0x7f61, 0x7f08, 0x7e9c, 0x7e1c, 0x7d89, 0x7ce2, 0x7c28, 0x7b5c, 0x7a7c, 0x7989, 0x7883, 0x776b, 0x7640, 0x7503, 0x73b4, 0x7254, 0x70e1, 0x6f5e, 0x6dc9, 0x6c23, 0x6a6c, 0x68a5, 0x66ce, 0x64e7, 0x62f1, 0x60eb, 0x5ed6, 0x5cb3, 0x5a81, 0x5842, 0x55f4, 0x539a, 0x5132, 0x4ebf, 0x4c3f, 0x49b3, 0x471c, 0x447a, 0x41cd, 0x3f16, 0x3c55, 0x398c, 0x36b9, 0x33de, 0x30fb, 0x2e10, 0x2b1e, 0x2826, 0x2527, 0x2223, 0x1f19, 0x1c0a, 0x18f8, 0x15e1, 0x12c7, 0xfaa, 0xc8b, 0x96a, 0x647, 0x323, -0x1, -0x325, -0x649, -0x96c, -0xc8d, -0xfac, -0x12c9, -0x15e3, -0x18fa, -0x1c0c, -0x1f1b, -0x2225, -0x2529, -0x2828, -0x2b20, -0x2e12, -0x30fd, -0x33e0, -0x36bb, -0x398e, -0x3c57, -0x3f18, -0x41cf, -0x447c, -0x471e, -0x49b5, -0x4c41, -0x4ec1, -0x5134, -0x539c, -0x55f6, -0x5844, -0x5a83, -0x5cb5, -0x5ed8, -0x60ed, -0x62f3, -0x64e9, -0x66d0, -0x68a7, -0x6a6e, -0x6c25, -0x6dcb, -0x6f60, -0x70e3, -0x7256, -0x73b6, -0x7505, -0x7642, -0x776d, -0x7885, -0x798b, -0x7a7e, -0x7b5e, -0x7c2a, -0x7ce4, -0x7d8b, -0x7e1e, -0x7e9e, -0x7f0a, -0x7f63, -0x7fa8, -0x7fd9, -0x7ff7, -0x8000, -0x7ff7, -0x7fd9, -0x7fa8, -0x7f63, -0x7f0a, -0x7e9e, -0x7e1e, -0x7d8b, -0x7ce4, -0x7c2a, -0x7b5e, -0x7a7e, -0x798b, -0x7885, -0x776d, -0x7642, -0x7505, -0x73b6, -0x7256, -0x70e3, -0x6f60, -0x6dcb, -0x6c25, -0x6a6e, -0x68a7, -0x66d0, -0x64e9, -0x62f3, -0x60ed, -0x5ed8, -0x5cb5, -0x5a83, -0x5844, -0x55f6, -0x539c, -0x5134, -0x4ec1, -0x4c41, -0x49b5, -0x471e, -0x447c, -0x41cf, -0x3f18, -0x3c57, -0x398e, -0x36bb, -0x33e0, -0x30fd, -0x2e12, -0x2b20, -0x2828, -0x2529, -0x2225, -0x1f1b, -0x1c0c, -0x18fa, -0x15e3, -0x12c9, -0xfac, -0xc8d, -0x96c, -0x649, -0x325 };
sample lookup_square[256] = { -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, -0x8000, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff };

View File

@ -27,6 +27,9 @@ void game::run() {
std::cout << "a\n"; std::cout << "a\n";
settings settings; settings settings;
init(settings); init(settings);
_audio = new audio(1<<16);
auto title = std::wstring(settings.title.begin(), settings.title.end()); auto title = std::wstring(settings.title.begin(), settings.title.end());
auto instance = GetModuleHandle(0); auto instance = GetModuleHandle(0);

88995
blooblib/src/miniaudio.h Normal file

File diff suppressed because it is too large Load Diff

10
blooblib/src/synth.cpp Normal file

File diff suppressed because one or more lines are too long

View File

@ -37,7 +37,10 @@ void my_game::init(settings& settings) {
//cursor(&font['\\']); //cursor(&font['\\']);
} }
extern phase_accumulator pa;
void my_game::update() { void my_game::update() {
pa.inc = lookup_note_to_freq[(24 << 4) + mouse_pos().y];
time = std::fmod(time + 1.0 / 60.0, 1.0); time = std::fmod(time + 1.0 / 60.0, 1.0);
} }