| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2025 tildearrow and contributors |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "gui.h" |
| 21 |
#include "imgui_internal.h" |
| 22 |
#include <imgui.h> |
| 23 |
#include "../ta-log.h" |
| 24 |
#include "../engine/filter.h" |
| 25 |
|
| 26 |
void FurnaceGUI::readOsc() { |
| 27 |
int writePos=e->oscWritePos; |
| 28 |
int readPos=e->oscReadPos; |
| 29 |
int avail=0; |
| 30 |
int total=0; |
| 31 |
if (firstFrame) { |
| 32 |
readPos=writePos; |
| 33 |
} |
| 34 |
if (writePos>=readPos) { |
| 35 |
avail=writePos-readPos; |
| 36 |
} else { |
| 37 |
avail=writePos-readPos+32768; |
| 38 |
} |
| 39 |
if (oscTotal==0) { |
| 40 |
oscTotal=ImGui::GetIO().DeltaTime*e->getAudioDescGot().rate; |
| 41 |
} else { |
| 42 |
oscTotal=(oscTotal+(int)round(ImGui::GetIO().DeltaTime*e->getAudioDescGot().rate))>>1; |
| 43 |
} |
| 44 |
int bias=avail-oscTotal-e->getAudioDescGot().bufsize; |
| 45 |
if (bias<0) bias=0; |
| 46 |
total=oscTotal+(bias>>6); |
| 47 |
if (total>avail) total=avail; |
| 48 |
//printf("total: %d. avail: %d bias: %d\n",total,avail,bias); |
| 49 |
|
| 50 |
int winSize=e->getAudioDescGot().rate*(oscWindowSize/1000.0); |
| 51 |
int oscReadPos=(writePos-winSize)&0x7fff; |
| 52 |
|
| 53 |
for (int ch=0; ch<e->getAudioDescGot().outChans; ch++) { |
| 54 |
if (oscValues[ch]==NULL) { |
| 55 |
oscValues[ch]=new float[2048]; |
| 56 |
} |
| 57 |
memset(oscValues[ch],0,2048*sizeof(float)); |
| 58 |
float* sincITable=DivFilterTables::getSincIntegralSmallTable(); |
| 59 |
|
| 60 |
float posFrac=0.0; |
| 61 |
float factor=(float)oscWidth/(float)winSize; |
| 62 |
int posInt=oscReadPos-(8.0f/factor); |
| 63 |
for (int i=7; i<oscWidth-9; i++) { |
| 64 |
oscValues[ch][i]+=e->oscBuf[ch][posInt&0x7fff]; |
| 65 |
|
| 66 |
posFrac+=1.0; |
| 67 |
while (posFrac>=1.0) { |
| 68 |
unsigned int n=((unsigned int)(posFrac*64.0))&63; |
| 69 |
posFrac-=factor; |
| 70 |
posInt++; |
| 71 |
|
| 72 |
float* t1=&sincITable[(63-n)<<3]; |
| 73 |
float* t2=&sincITable[n<<3]; |
| 74 |
float delta=e->oscBuf[ch][posInt&0x7fff]-e->oscBuf[ch][(posInt-1)&0x7fff]; |
| 75 |
|
| 76 |
oscValues[ch][i-7]+=t1[7]*-delta; |
| 77 |
oscValues[ch][i-6]+=t1[6]*-delta; |
| 78 |
oscValues[ch][i-5]+=t1[5]*-delta; |
| 79 |
oscValues[ch][i-4]+=t1[4]*-delta; |
| 80 |
oscValues[ch][i-3]+=t1[3]*-delta; |
| 81 |
oscValues[ch][i-2]+=t1[2]*-delta; |
| 82 |
oscValues[ch][i-1]+=t1[1]*-delta; |
| 83 |
oscValues[ch][i] +=t1[0]*-delta; |
| 84 |
|
| 85 |
oscValues[ch][i+1]+=t2[0]*delta; |
| 86 |
oscValues[ch][i+2]+=t2[1]*delta; |
| 87 |
oscValues[ch][i+3]+=t2[2]*delta; |
| 88 |
oscValues[ch][i+4]+=t2[3]*delta; |
| 89 |
oscValues[ch][i+5]+=t2[4]*delta; |
| 90 |
oscValues[ch][i+6]+=t2[5]*delta; |
| 91 |
oscValues[ch][i+7]+=t2[6]*delta; |
| 92 |
oscValues[ch][i+8]+=t2[7]*delta; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
for (int i=0; i<oscWidth; i++) { |
| 97 |
if (oscValues[ch][i]>0.001f || oscValues[ch][i]<-0.001f) { |
| 98 |
WAKE_UP; |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
if (oscValuesAverage==NULL) { |
| 104 |
oscValuesAverage=new float[2048]; |
| 105 |
} |
| 106 |
memset(oscValuesAverage,0,2048*sizeof(float)); |
| 107 |
for (int i=0; i<oscWidth; i++) { |
| 108 |
float avg=0; |
| 109 |
for (int j=0; j<e->getAudioDescGot().outChans; j++) { |
| 110 |
avg+=oscValues[j][i]; |
| 111 |
} |
| 112 |
avg/=e->getAudioDescGot().outChans; |
| 113 |
oscValuesAverage[i]=avg*oscZoom*2.0f; |
| 114 |
} |
| 115 |
|
| 116 |
/*for (int i=0; i<oscWidth; i++) { |
| 117 |
oscValues[i]=(i&1)?0.3:0; |
| 118 |
}*/ |
| 119 |
|
| 120 |
float peakDecay=0.05f*60.0f*ImGui::GetIO().DeltaTime; |
| 121 |
for (int i=0; i<e->getAudioDescGot().outChans; i++) { |
| 122 |
peak[i]*=1.0-peakDecay; |
| 123 |
if (peak[i]<0.0001) { |
| 124 |
peak[i]=0.0; |
| 125 |
} else { |
| 126 |
WAKE_UP; |
| 127 |
} |
| 128 |
float newPeak=peak[i]; |
| 129 |
for (int j=0; j<total; j++) { |
| 130 |
int pos=(readPos+j)&0x7fff; |
| 131 |
if (fabs(e->oscBuf[i][pos])>newPeak) { |
| 132 |
newPeak=fabs(e->oscBuf[i][pos]); |
| 133 |
} |
| 134 |
} |
| 135 |
peak[i]+=(newPeak-peak[i])*0.9; |
| 136 |
} |
| 137 |
|
| 138 |
readPos=(readPos+total)&0x7fff; |
| 139 |
e->oscReadPos=readPos; |
| 140 |
} |
| 141 |
|
| 142 |
PendingDrawOsc _do; |
| 143 |
|
| 144 |
static void _drawOsc(const ImDrawList* drawList, const ImDrawCmd* cmd) { |
| 145 |
if (cmd!=NULL) { |
| 146 |
if (cmd->UserCallbackData!=NULL) { |
| 147 |
((FurnaceGUI*)(((PendingDrawOsc*)cmd->UserCallbackData)->gui))->runPendingDrawOsc((PendingDrawOsc*)cmd->UserCallbackData); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
void FurnaceGUI::runPendingDrawOsc(PendingDrawOsc* which) { |
| 153 |
rend->drawOsc(which->data,which->len,which->pos0,which->pos1,which->color,ImVec2(canvasW,canvasH),which->lineSize); |
| 154 |
} |
| 155 |
|
| 156 |
void FurnaceGUI::drawOsc() { |
| 157 |
if (nextWindow==GUI_WINDOW_OSCILLOSCOPE) { |
| 158 |
oscOpen=true; |
| 159 |
ImGui::SetNextWindowFocus(); |
| 160 |
nextWindow=GUI_WINDOW_NOTHING; |
| 161 |
} |
| 162 |
if (!oscOpen) return; |
| 163 |
ImGui::SetNextWindowSizeConstraints(ImVec2(64.0f*dpiScale,32.0f*dpiScale),ImVec2(canvasW,canvasH)); |
| 164 |
if (settings.oscTakesEntireWindow) { |
| 165 |
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding,ImVec2(0,0)); |
| 166 |
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing,ImVec2(0,0)); |
| 167 |
ImGui::PushStyleVar(ImGuiStyleVar_ItemInnerSpacing,ImVec2(0,0)); |
| 168 |
} |
| 169 |
if (ImGui::Begin("Oscilloscope",&oscOpen,globalWinFlags,_("Oscilloscope"))) { |
| 170 |
if (oscZoomSlider) { |
| 171 |
if (ImGui::VSliderFloat("##OscZoom",ImVec2(20.0f*dpiScale,ImGui::GetContentRegionAvail().y),&oscZoom,0.5,2.0)) { |
| 172 |
if (oscZoom<0.5) oscZoom=0.5; |
| 173 |
if (oscZoom>2.0) oscZoom=2.0; |
| 174 |
} rightClickable |
| 175 |
if (ImGui::IsItemHovered()) { |
| 176 |
ImGui::SetTooltip(_("zoom: %.2fx (%.1fdB)"),oscZoom,20.0*log10(oscZoom*2.0)); |
| 177 |
} |
| 178 |
if (ImGui::IsItemClicked(ImGuiMouseButton_Middle)) { |
| 179 |
oscZoom=0.5; |
| 180 |
} |
| 181 |
ImGui::SameLine(); |
| 182 |
if (ImGui::VSliderFloat("##OscWinSize",ImVec2(20.0f*dpiScale,ImGui::GetContentRegionAvail().y),&oscWindowSize,5.0,100.0)) { |
| 183 |
if (oscWindowSize<5.0) oscWindowSize=5.0; |
| 184 |
if (oscWindowSize>100.0) oscWindowSize=100.0; |
| 185 |
} rightClickable |
| 186 |
if (ImGui::IsItemHovered()) { |
| 187 |
ImGui::SetTooltip(_("window size: %.1fms"),oscWindowSize); |
| 188 |
} |
| 189 |
if (ImGui::IsItemClicked(ImGuiMouseButton_Middle)) { |
| 190 |
oscWindowSize=20.0; |
| 191 |
} |
| 192 |
ImGui::SameLine(); |
| 193 |
} |
| 194 |
|
| 195 |
ImDrawList* dl=ImGui::GetWindowDrawList(); |
| 196 |
ImGuiWindow* window=ImGui::GetCurrentWindow(); |
| 197 |
|
| 198 |
static ImVec2 waveform[2048]; |
| 199 |
ImVec2 size=ImGui::GetContentRegionAvail(); |
| 200 |
|
| 201 |
ImVec2 minArea=window->DC.CursorPos; |
| 202 |
ImVec2 maxArea=ImVec2( |
| 203 |
minArea.x+size.x, |
| 204 |
minArea.y+size.y |
| 205 |
); |
| 206 |
ImRect rect=ImRect(minArea,maxArea); |
| 207 |
ImRect inRect=rect; |
| 208 |
inRect.Min.x+=dpiScale; |
| 209 |
inRect.Min.y+=dpiScale; |
| 210 |
inRect.Max.x-=dpiScale; |
| 211 |
inRect.Max.y-=dpiScale; |
| 212 |
ImGuiStyle& style=ImGui::GetStyle(); |
| 213 |
ImU32 color=ImGui::GetColorU32(isClipping?uiColors[GUI_COLOR_OSC_WAVE_PEAK]:uiColors[GUI_COLOR_OSC_WAVE]); |
| 214 |
ImU32 borderColor=ImGui::GetColorU32(uiColors[GUI_COLOR_OSC_BORDER]); |
| 215 |
ImU32 refColor=ImGui::GetColorU32(uiColors[GUI_COLOR_OSC_REF]); |
| 216 |
ImU32 guideColor=ImGui::GetColorU32(uiColors[GUI_COLOR_OSC_GUIDE]); |
| 217 |
ImGui::ItemSize(size,style.FramePadding.y); |
| 218 |
if (ImGui::ItemAdd(rect,ImGui::GetID("wsDisplay"))) { |
| 219 |
if (safeMode || renderBackend==GUI_BACKEND_SOFTWARE) { |
| 220 |
dl->AddRectFilled( |
| 221 |
inRect.Min, |
| 222 |
inRect.Max, |
| 223 |
ImGui::GetColorU32(uiColors[GUI_COLOR_OSC_BG4]) |
| 224 |
); |
| 225 |
} else { |
| 226 |
dl->AddRectFilledMultiColor( |
| 227 |
inRect.Min, |
| 228 |
inRect.Max, |
| 229 |
ImGui::GetColorU32(uiColors[GUI_COLOR_OSC_BG1]), |
| 230 |
ImGui::GetColorU32(uiColors[GUI_COLOR_OSC_BG2]), |
| 231 |
ImGui::GetColorU32(uiColors[GUI_COLOR_OSC_BG4]), |
| 232 |
ImGui::GetColorU32(uiColors[GUI_COLOR_OSC_BG3]), |
| 233 |
settings.oscRoundedCorners?(8.0f*dpiScale):0.0f |
| 234 |
); |
| 235 |
} |
| 236 |
|
| 237 |
dl->AddLine( |
| 238 |
ImLerp(rect.Min,rect.Max,ImVec2(0.0f,0.5f)), |
| 239 |
ImLerp(rect.Min,rect.Max,ImVec2(1.0f,0.5f)), |
| 240 |
refColor, |
| 241 |
dpiScale |
| 242 |
); |
| 243 |
|
| 244 |
dl->AddLine( |
| 245 |
ImLerp(rect.Min,rect.Max,ImVec2(0.48f,0.125f)), |
| 246 |
ImLerp(rect.Min,rect.Max,ImVec2(0.52f,0.125f)), |
| 247 |
guideColor, |
| 248 |
dpiScale |
| 249 |
); |
| 250 |
|
| 251 |
dl->AddLine( |
| 252 |
ImLerp(rect.Min,rect.Max,ImVec2(0.47f,0.25f)), |
| 253 |
ImLerp(rect.Min,rect.Max,ImVec2(0.53f,0.25f)), |
| 254 |
guideColor, |
| 255 |
dpiScale |
| 256 |
); |
| 257 |
|
| 258 |
dl->AddLine( |
| 259 |
ImLerp(rect.Min,rect.Max,ImVec2(0.45f,0.375f)), |
| 260 |
ImLerp(rect.Min,rect.Max,ImVec2(0.55f,0.375f)), |
| 261 |
guideColor, |
| 262 |
dpiScale |
| 263 |
); |
| 264 |
|
| 265 |
dl->AddLine( |
| 266 |
ImLerp(rect.Min,rect.Max,ImVec2(0.45f,0.625f)), |
| 267 |
ImLerp(rect.Min,rect.Max,ImVec2(0.55f,0.625f)), |
| 268 |
guideColor, |
| 269 |
dpiScale |
| 270 |
); |
| 271 |
|
| 272 |
dl->AddLine( |
| 273 |
ImLerp(rect.Min,rect.Max,ImVec2(0.47f,0.75f)), |
| 274 |
ImLerp(rect.Min,rect.Max,ImVec2(0.53f,0.75f)), |
| 275 |
guideColor, |
| 276 |
dpiScale |
| 277 |
); |
| 278 |
|
| 279 |
dl->AddLine( |
| 280 |
ImLerp(rect.Min,rect.Max,ImVec2(0.48f,0.875f)), |
| 281 |
ImLerp(rect.Min,rect.Max,ImVec2(0.52f,0.875f)), |
| 282 |
guideColor, |
| 283 |
dpiScale |
| 284 |
); |
| 285 |
|
| 286 |
dl->AddLine( |
| 287 |
ImLerp(rect.Min,rect.Max,ImVec2(0.5f,0.08f)), |
| 288 |
ImLerp(rect.Min,rect.Max,ImVec2(0.5f,0.92f)), |
| 289 |
guideColor, |
| 290 |
dpiScale |
| 291 |
); |
| 292 |
|
| 293 |
oscWidth=round(inRect.Max.x-inRect.Min.x)+24; |
| 294 |
if (oscWidth<17) oscWidth=17; |
| 295 |
if (oscWidth>2048) oscWidth=2048; |
| 296 |
|
| 297 |
ImDrawListFlags prevFlags=dl->Flags; |
| 298 |
if (!settings.oscAntiAlias || safeMode) { |
| 299 |
dl->Flags&=~(ImDrawListFlags_AntiAliasedLines|ImDrawListFlags_AntiAliasedLinesUseTex); |
| 300 |
} |
| 301 |
|
| 302 |
if ((oscWidth-24)>0) { |
| 303 |
if (settings.oscMono) { |
| 304 |
if (rend->supportsDrawOsc() && settings.shaderOsc) { |
| 305 |
_do.gui=this; |
| 306 |
_do.data=&oscValuesAverage[12]; |
| 307 |
_do.len=oscWidth-24; |
| 308 |
_do.pos0=inRect.Min; |
| 309 |
_do.pos1=inRect.Max; |
| 310 |
_do.color=isClipping?uiColors[GUI_COLOR_OSC_WAVE_PEAK]:uiColors[GUI_COLOR_OSC_WAVE]; |
| 311 |
_do.lineSize=dpiScale*settings.oscLineSize; |
| 312 |
|
| 313 |
dl->AddCallback(_drawOsc,&_do); |
| 314 |
dl->AddCallback(ImDrawCallback_ResetRenderState,NULL); |
| 315 |
} else { |
| 316 |
for (int i=0; i<oscWidth-24; i++) { |
| 317 |
float x=(float)i/(float)(oscWidth-24); |
| 318 |
float y=oscValuesAverage[i+12]*0.5f; |
| 319 |
if (!settings.oscEscapesBoundary) { |
| 320 |
if (y<-0.5f) y=-0.5f; |
| 321 |
if (y>0.5f) y=0.5f; |
| 322 |
} |
| 323 |
waveform[i]=ImLerp(inRect.Min,inRect.Max,ImVec2(x,0.5f-y)); |
| 324 |
} |
| 325 |
|
| 326 |
if (settings.oscEscapesBoundary) { |
| 327 |
dl->PushClipRectFullScreen(); |
| 328 |
dl->AddPolyline(waveform,oscWidth-24,color,ImDrawFlags_None,dpiScale*settings.oscLineSize); |
| 329 |
dl->PopClipRect(); |
| 330 |
} else { |
| 331 |
dl->AddPolyline(waveform,oscWidth-24,color,ImDrawFlags_None,dpiScale*settings.oscLineSize); |
| 332 |
} |
| 333 |
} |
| 334 |
} else { |
| 335 |
for (int ch=0; ch<e->getAudioDescGot().outChans; ch++) { |
| 336 |
if (!isClipping) { |
| 337 |
color=ImGui::GetColorU32(uiColors[GUI_COLOR_OSC_WAVE_CH0+ch]); |
| 338 |
} |
| 339 |
|
| 340 |
if (rend->supportsDrawOsc() && settings.shaderOsc) { |
| 341 |
_do.gui=this; |
| 342 |
_do.data=&oscValues[ch][12]; |
| 343 |
_do.len=oscWidth-24; |
| 344 |
_do.pos0=inRect.Min; |
| 345 |
_do.pos1=inRect.Max; |
| 346 |
_do.color=isClipping?uiColors[GUI_COLOR_OSC_WAVE_PEAK]:uiColors[GUI_COLOR_OSC_WAVE_CH0+ch]; |
| 347 |
_do.lineSize=dpiScale*settings.oscLineSize; |
| 348 |
|
| 349 |
dl->AddCallback(_drawOsc,&_do); |
| 350 |
dl->AddCallback(ImDrawCallback_ResetRenderState,NULL); |
| 351 |
} else { |
| 352 |
for (int i=0; i<oscWidth-24; i++) { |
| 353 |
float x=(float)i/(float)(oscWidth-24); |
| 354 |
float y=oscValues[ch][i+12]*oscZoom; |
| 355 |
if (!settings.oscEscapesBoundary) { |
| 356 |
if (y<-0.5f) y=-0.5f; |
| 357 |
if (y>0.5f) y=0.5f; |
| 358 |
} |
| 359 |
waveform[i]=ImLerp(inRect.Min,inRect.Max,ImVec2(x,0.5f-y)); |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
if (settings.oscEscapesBoundary) { |
| 364 |
dl->PushClipRectFullScreen(); |
| 365 |
dl->AddPolyline(waveform,oscWidth-24,color,ImDrawFlags_None,dpiScale*settings.oscLineSize); |
| 366 |
dl->PopClipRect(); |
| 367 |
} else { |
| 368 |
dl->AddPolyline(waveform,oscWidth-24,color,ImDrawFlags_None,dpiScale*settings.oscLineSize); |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
dl->Flags=prevFlags; |
| 376 |
|
| 377 |
if (settings.oscBorder) { |
| 378 |
dl->AddRect(inRect.Min,inRect.Max,borderColor,(settings.oscRoundedCorners && !(safeMode || renderBackend==GUI_BACKEND_SOFTWARE))?(8.0f*dpiScale):0.0f,0,1.5f*dpiScale); |
| 379 |
} |
| 380 |
} |
| 381 |
if (oscZoomSlider && ImGui::IsItemHovered()) { |
| 382 |
float val=20.0*log10(2.0*fabs(0.5-((ImGui::GetMousePos().y-inRect.Min.y)/(inRect.Max.y-inRect.Min.y)))); |
| 383 |
if (val>0.0f) val=0.0f; |
| 384 |
if (val<=-INFINITY) { |
| 385 |
ImGui::SetTooltip(_("(-Infinity)dB")); |
| 386 |
} else { |
| 387 |
ImGui::SetTooltip("%.1fdB",val); |
| 388 |
} |
| 389 |
} |
| 390 |
if (ImGui::IsItemClicked(ImGuiMouseButton_Right)) { |
| 391 |
oscZoomSlider=!oscZoomSlider; |
| 392 |
} |
| 393 |
if (mobileUI && ImGui::IsItemHovered() && CHECK_LONG_HOLD) { |
| 394 |
oscZoomSlider=!oscZoomSlider; |
| 395 |
NOTIFY_LONG_HOLD; |
| 396 |
} |
| 397 |
} |
| 398 |
if (settings.oscTakesEntireWindow) { |
| 399 |
ImGui::PopStyleVar(3); |
| 400 |
} |
| 401 |
if (ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows)) curWindow=GUI_WINDOW_OSCILLOSCOPE; |
| 402 |
ImGui::End(); |
| 403 |
} |