Skip to content

Commit

Permalink
DPI awareness, UI scale option & small changes to AudioAnalyzer (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfranx committed Jul 3, 2019
1 parent 230960d commit f4e9f13
Show file tree
Hide file tree
Showing 22 changed files with 191 additions and 108 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[v1.0.2]
+ add ability to load audio files
+ add DPI awareness
+ fix for "VSync not being applied on startup"

[v1.0.1]
Expand Down
47 changes: 33 additions & 14 deletions GUIManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace ed
m_cachedFontSize = 15;
m_performanceMode = false;
m_perfModeFake = false;
m_fontNeedsUpdate = false;

Settings::Instance().Load();
m_loadTemplateList();
Expand All @@ -47,7 +48,7 @@ namespace ed

// Initialize imgui
ImGui::CreateContext();

ImGuiIO& io = ImGui::GetIO();
io.Fonts->AddFontDefault();
io.IniFilename = IMGUI_INI_FILE;
Expand Down Expand Up @@ -80,6 +81,15 @@ namespace ed
((CodeEditorUI*)Get(ViewID::Code))->SetTrackFileChanges(Settings::Instance().General.RecompileOnFileChange);

((OptionsUI*)m_options)->SetGroup(OptionsUI::Page::General);

// enable dpi awareness
ImGui_ImplWin32_EnableDpiAwareness();

if (Settings::Instance().General.AutoScale)
Settings::Instance().DPIScale = ImGui_ImplWin32_GetDpiScaleForHwnd((void*)m_wnd->GetWindowHandle());
m_cacheUIScale = Settings::Instance().TempScale = Settings::Instance().DPIScale;

ImGui::GetStyle().ScaleAllSizes(Settings::Instance().DPIScale);
}
GUIManager::~GUIManager()
{
Expand Down Expand Up @@ -127,23 +137,25 @@ namespace ed
((m_cachedCustomFont != settings.General.CustomFont ||
m_cachedFont != settings.General.Font ||
m_cachedFontSize != settings.General.FontSize) &&
strcmp(settings.General.Font, "null") != 0))
strcmp(settings.General.Font, "null") != 0) ||
m_fontNeedsUpdate)
{
std::pair<std::string, int> edFont = ((CodeEditorUI*)Get(ViewID::Code))->GetFont();

m_cachedCustomFont = settings.General.CustomFont;
m_cachedFont = settings.General.Font;
m_cachedFontSize = settings.General.FontSize;
m_fontNeedsUpdate = false;

ImFontAtlas* fonts = ImGui::GetIO().Fonts;
fonts->Clear();

ImFont* font = nullptr;
if (!m_cachedCustomFont)
font = fonts->AddFontDefault();
else font = fonts->AddFontFromFileTTF(m_cachedFont.c_str(), m_cachedFontSize);
else font = fonts->AddFontFromFileTTF(m_cachedFont.c_str(), m_cachedFontSize * Settings::Instance().DPIScale);

ImFont* edFontPtr = fonts->AddFontFromFileTTF(edFont.first.c_str(), edFont.second);
ImFont* edFontPtr = fonts->AddFontFromFileTTF(edFont.first.c_str(), edFont.second * Settings::Instance().DPIScale);

if (font == nullptr || edFontPtr == nullptr) {
fonts->Clear();
Expand Down Expand Up @@ -388,7 +400,7 @@ namespace ed
}

// Create Item popup
ImGui::SetNextWindowSize(ImVec2(430, 175), ImGuiCond_Once);
ImGui::SetNextWindowSize(ImVec2(430 * Settings::Instance().DPIScale, 175 * Settings::Instance().DPIScale), ImGuiCond_Once);
if (ImGui::BeginPopupModal("Create Item##main_create_item")) {
m_createUI->Update(delta);

Expand All @@ -403,7 +415,7 @@ namespace ed
}

// Create RT popup
ImGui::SetNextWindowSize(ImVec2(430, 175), ImGuiCond_Once);
ImGui::SetNextWindowSize(ImVec2(430 * Settings::Instance().DPIScale, 175 * Settings::Instance().DPIScale), ImGuiCond_Once);
if (ImGui::BeginPopupModal("Create RT##main_create_rt")) {
static char buf[65] = { 0 };
ImGui::InputText("Name", buf, 64);
Expand All @@ -418,7 +430,7 @@ namespace ed
}

// Create about popup
ImGui::SetNextWindowSize(ImVec2(250, 145), ImGuiCond_Once);
ImGui::SetNextWindowSize(ImVec2(250 * Settings::Instance().DPIScale, 145 * Settings::Instance().DPIScale), ImGuiCond_Once);
if (ImGui::BeginPopupModal("About##main_about")) {
ImGui::Text("(C) 2019 dfranx");
ImGui::Text("Version 1.0");
Expand All @@ -435,7 +447,7 @@ namespace ed
}

// Create new project
ImGui::SetNextWindowSize(ImVec2(300, 100), ImGuiCond_Once);
ImGui::SetNextWindowSize(ImVec2(300 * Settings::Instance().DPIScale, 100 * Settings::Instance().DPIScale), ImGuiCond_Once);
if (ImGui::BeginPopupModal("Are you sure?##main_new_proj")) {
ImGui::TextWrapped("You will lose your unsaved progress if you create new project");
if (ImGui::Button("Yes")) {
Expand Down Expand Up @@ -565,11 +577,11 @@ namespace ed
};

float height = abs(ImGui::GetWindowContentRegionMax().y - ImGui::GetWindowContentRegionMin().y - ImGui::GetStyle().WindowPadding.y*2) / ImGui::GetTextLineHeightWithSpacing() - 1;

ImGui::Columns(2);
ImGui::SetColumnWidth(0, 100 + ImGui::GetStyle().WindowPadding.x * 2);
ImGui::SetColumnWidth(0, 100 * Settings::Instance().DPIScale + ImGui::GetStyle().WindowPadding.x * 2);
ImGui::PushStyleColor(ImGuiCol_FrameBg, ImVec4(0, 0, 0, 0));
ImGui::PushItemWidth(100);
ImGui::PushItemWidth(100 * Settings::Instance().DPIScale);
if (ImGui::ListBox("##optiongroups", &m_optGroup, optGroups, _ARRAYSIZE(optGroups), height))
options->SetGroup((OptionsUI::Page)m_optGroup);
ImGui::PopStyleColor();
Expand All @@ -580,8 +592,8 @@ namespace ed

ImGui::Columns();

ImGui::SetCursorPosX(ImGui::GetWindowWidth() - 160);
if (ImGui::Button("OK", ImVec2(70, 0))) {
ImGui::SetCursorPosX(ImGui::GetWindowWidth() - 160 * Settings::Instance().DPIScale);
if (ImGui::Button("OK", ImVec2(70 * Settings::Instance().DPIScale, 0))) {
Settings::Instance().Save();
KeyboardShortcuts::Instance().Save();

Expand All @@ -599,12 +611,19 @@ namespace ed
code->SetTrackFileChanges(Settings::Instance().General.RecompileOnFileChange);
code->UpdateShortcuts();

if (Settings::Instance().TempScale != m_cacheUIScale) {
Settings::Instance().DPIScale = Settings::Instance().TempScale;
ImGui::GetStyle().ScaleAllSizes(Settings::Instance().DPIScale / m_cacheUIScale);
m_cacheUIScale = Settings::Instance().DPIScale;
m_fontNeedsUpdate = true;
}

m_optionsOpened = false;
}

ImGui::SameLine();

ImGui::SetCursorPosX(ImGui::GetWindowWidth()-80);
ImGui::SetCursorPosX(ImGui::GetWindowWidth()-80 * Settings::Instance().DPIScale);
if (ImGui::Button("Cancel", ImVec2(-1, 0))) {
Settings::Instance() = *m_settingsBkp;
KeyboardShortcuts::Instance().SetMap(m_shortcutsBkp);
Expand Down
2 changes: 2 additions & 0 deletions GUIManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ namespace ed
std::string m_cachedFont;
int m_cachedFontSize;
bool m_cachedCustomFont;
bool m_fontNeedsUpdate;
float m_cacheUIScale;

Settings* m_settingsBkp;
std::map<std::string, KeyboardShortcuts::Shortcut> m_shortcutsBkp;
Expand Down
71 changes: 33 additions & 38 deletions Objects/AudioAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,54 +16,52 @@ namespace ed
AudioAnalyzer::AudioAnalyzer()
{
m_sensitivity = 1.0;
m_isSetup = 0;
}

AudioAnalyzer::~AudioAnalyzer()
{

}
double* AudioAnalyzer::FFT(ml::AudioFile& file, int curSample)
void AudioAnalyzer::m_setup(int rate)
{
int rate = file.GetInfo()->nSamplesPerSec;
int channels = file.GetInfo()->nChannels;
curSample *= channels;

float smoothing[BufferOutSize];
// set up
double freqconst = log(HighFrequency - LowFrequency) / log(pow(BufferOutSize, LogScale));

int fall[BufferOutSize];
float fpeak[BufferOutSize], flast[BufferOutSize], fmem[BufferOutSize];

float fc[BufferOutSize], x;
int lcf[BufferOutSize], hcf[BufferOutSize];
float x;

// Calc freq range for each bar
for (int i = 0; i < BufferOutSize; i++) {
fc[i] = pow(powf(i, (LogScale - 1.0) * ((double)i + 1.0) / ((double)BufferOutSize) + 1.0), freqconst) + LowFrequency;
x = fc[i] / (rate / 2);
lcf[i] = x * (SampleCount / 2);
m_fc[i] = pow(powf(i, (LogScale - 1.0) * ((double)i + 1.0) / ((double)BufferOutSize) + 1.0), freqconst) + LowFrequency;
x = m_fc[i] / (rate / 2);
m_lcf[i] = x * (SampleCount / 2);
if (i != 0)
hcf[i - 1] = lcf[i] - 1 > lcf[i - 1] ? lcf[i] - 1 : lcf[i - 1];
m_hcf[i - 1] = m_lcf[i] - 1 > m_lcf[i - 1] ? m_lcf[i] - 1 : m_lcf[i - 1];
}
hcf[BufferOutSize - 1] = HighFrequency * SampleCount / rate;
m_hcf[BufferOutSize - 1] = HighFrequency * SampleCount / rate;

// Calc smoothing
for (int i = 0; i < BufferOutSize; i++) {
smoothing[i] = pow(fc[i], 0.64); // TODO: Add smoothing factor to config
smoothing[i] *= Smooth[(int)(i / BufferOutSize * sizeof(Smooth) / sizeof(*Smooth))];
m_smoothing[i] = pow(m_fc[i], 0.64); // TODO: Add smoothing factor to config
m_smoothing[i] *= Smooth[(int)(i / BufferOutSize * sizeof(Smooth) / sizeof(*Smooth))];
}

int n = 0;
int16_t buf[SampleCount];

// Clear arrays
for (int i = 0; i < BufferOutSize; i++)
buf[i] = fall[i] = fpeak[i] = flast[i] = fmem[i] = 0;


m_fall[i] = m_fpeak[i] = m_flast[i] = m_fmem[i] = 0;
}
double* AudioAnalyzer::FFT(ml::AudioFile& file, int curSample)
{
int rate = file.GetInfo()->nSamplesPerSec;
int channels = file.GetInfo()->nChannels;
curSample *= channels;

if (m_isSetup != rate) {
m_setup(rate);
m_isSetup = rate;
}

// Spliting channels
int n = 0;
std::valarray<std::complex<double>> fftIn(SampleCount);
for (int i = 0; i < SampleCount / 2; i += 2) {
if (curSample + i > file.GetTotalSamplesPerChannel()*channels || curSample + i + 1 > file.GetTotalSamplesPerChannel() * channels)
Expand All @@ -74,14 +72,11 @@ namespace ed
if (n == SampleCount - 1) n = 0;
}




// Run fftw
m_fftAlgorithm(fftIn);

// Separate fftw output
m_seperateFreqBands(&fftIn[0], BufferOutSize, lcf, hcf, smoothing, m_sensitivity, BufferOutSize);
m_seperateFreqBands(&fftIn[0], BufferOutSize, m_lcf, m_hcf, m_smoothing, m_sensitivity, BufferOutSize);

/* Processing */
// Waves
Expand All @@ -98,28 +93,28 @@ namespace ed

// Gravity
for (int i = 0; i < BufferOutSize; i++) {
if (m_fftOut[i] < flast[i]) {
m_fftOut[i] = fpeak[i] - (Gravity * fall[i] * fall[i]);
fall[i]++;
if (m_fftOut[i] < m_flast[i]) {
m_fftOut[i] = m_fpeak[i] - (Gravity * m_fall[i] * m_fall[i]);
m_fall[i]++;
}
else {
fpeak[i] = m_fftOut[i];
fall[i] = 0;
m_fpeak[i] = m_fftOut[i];
m_fall[i] = 0;
}

flast[i] = m_fftOut[i];
m_flast[i] = m_fftOut[i];
}

// Integral
for (int i = 0; i < BufferOutSize; i++) {
m_fftOut[i] = (int)(m_fftOut[i] * 100);
m_fftOut[i] += fmem[i] * 0.9; // TODO: Add integral to config
fmem[i] = m_fftOut[i];
m_fftOut[i] += m_fmem[i] * 0.9; // TODO: Add integral to config
m_fmem[i] = m_fftOut[i];

int diff = 100 - m_fftOut[i];
if (diff < 0) diff = 0;
double div = 1 / (diff + 1);
fmem[i] *= 1 - div / 20;
m_fmem[i] *= 1 - div / 20;
m_fftOut[i] /= 100.0;
}

Expand Down
11 changes: 11 additions & 0 deletions Objects/AudioAnalyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ namespace ed
void m_fftAlgorithm(std::valarray<std::complex<double>>& x);
void m_seperateFreqBands(std::complex<double>* in, int n, int* lcf, int* hcf, float* k, double sensitivity, int in_samples);

int m_isSetup;
void m_setup(int rate);

float m_smoothing[BufferOutSize];

int m_fall[BufferOutSize];
float m_fpeak[BufferOutSize], m_flast[BufferOutSize], m_fmem[BufferOutSize];

float m_fc[BufferOutSize];
int m_lcf[BufferOutSize], m_hcf[BufferOutSize];

double m_fftOut[SampleCount];
double m_sensitivity;
};
Expand Down
4 changes: 4 additions & 0 deletions Objects/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ namespace ed
General.RecompileOnFileChange = ini.GetBoolean("general", "trackfilechange", true);
General.StartUpTemplate = ini.Get("general", "template", "HLSL");
General.CustomFont = ini.GetBoolean("general", "customfont", false);
General.AutoScale = ini.GetBoolean("general", "autoscale", true);
DPIScale = ini.GetReal("general", "uiscale", 1.0f);
strcpy(General.Font, ini.Get("general", "font", "null").c_str());
General.FontSize = ini.GetInteger("general", "fontsize", 15);

Expand Down Expand Up @@ -72,6 +74,8 @@ namespace ed
ini << "customfont=" << General.CustomFont << std::endl;
ini << "font=" << General.Font << std::endl;
ini << "fontsize=" << General.FontSize << std::endl;
ini << "autoscale=" << General.AutoScale << std::endl;
ini << "uiscale=" << DPIScale << std::endl;

ini << "[preview]" << std::endl;
ini << "fxaa=" << Preview.FXAA << std::endl;
Expand Down
3 changes: 3 additions & 0 deletions Objects/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace ed
void Load();
void Save();

float DPIScale; // shouldn't be changed by users
float TempScale; // copy this value to DPIScale on "Ok" button press
std::string Theme;

struct strGeneral {
Expand All @@ -28,6 +30,7 @@ namespace ed
bool CustomFont;
char Font[256];
int FontSize;
bool AutoScale;
} General;

struct strEditor {
Expand Down
Loading

0 comments on commit f4e9f13

Please sign in to comment.