Debian Codegraph @

src/gui/mixer.cpp

raw
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 "intConst.h"
22 #include <fmt/printf.h>
23 #include <imgui.h>
24 #include "../ta-log.h"
25 #include "imgui_internal.h"
26
27 const char* portNamesStereo[2]={
28 _N("left"),
29 _N("right")
30 };
31
32 ImVec2 FurnaceGUI::calcPortSetSize(String label, int ins, int outs) {
33 ImGuiStyle& style=ImGui::GetStyle();
34
35 ImVec2 labelSize=ImGui::CalcTextSize(label.c_str(),NULL,false,ImGui::GetWindowSize().x*0.6f);
36
37 ImVec2 size=labelSize;
38
39 // pad
40 size.x+=style.FramePadding.x*2.0f;
41 size.y+=style.FramePadding.y*2.0f;
42
43 // space for ports
44 size.y+=MAX(ins,outs)*(labelSize.y+style.FramePadding.y+style.ItemSpacing.y);
45
46 return size;
47 }
48
49 bool FurnaceGUI::portSet(String label, unsigned int portSetID, int ins, int outs, int activeIns, int activeOuts, int& clickedPort, std::map<unsigned int,ImVec2>& portPos) {
50 String portID=fmt::sprintf("portSet%.4x",portSetID);
51
52 ImDrawList* dl=ImGui::GetWindowDrawList();
53 ImGuiWindow* window=ImGui::GetCurrentWindow();
54 ImGuiStyle& style=ImGui::GetStyle();
55
56 ImVec2 labelSize=ImGui::CalcTextSize(label.c_str(),NULL,false,ImGui::GetWindowSize().x*0.6f);
57
58 ImVec2 size=labelSize;
59
60 // pad
61 size.x+=style.FramePadding.x*2.0f;
62 size.y+=style.FramePadding.y*2.0f;
63
64 // space for ports
65 size.y+=MAX(ins,outs)*(ImGui::GetFontSize()+style.FramePadding.y+style.ItemSpacing.y);
66
67 ImVec4 portSetBorderColor=uiColors[GUI_COLOR_PATCHBAY_PORTSET];
68 ImVec4 portSetColor=ImVec4(
69 portSetBorderColor.x*0.75f,
70 portSetBorderColor.y*0.75f,
71 portSetBorderColor.z*0.75f,
72 portSetBorderColor.w
73 );
74
75 ImVec4 portBorderColor=uiColors[GUI_COLOR_PATCHBAY_PORT];
76 ImVec4 portColor=ImVec4(
77 portBorderColor.x*0.75f,
78 portBorderColor.y*0.75f,
79 portBorderColor.z*0.75f,
80 portBorderColor.w
81 );
82
83 ImVec4 portBorderColorH=uiColors[GUI_COLOR_PATCHBAY_PORT_HIDDEN];
84 ImVec4 portColorH=ImVec4(
85 portBorderColorH.x*0.75f,
86 portBorderColorH.y*0.75f,
87 portBorderColorH.z*0.75f,
88 portBorderColorH.w
89 );
90
91 ImVec2 minArea=window->DC.CursorPos;
92 ImVec2 maxArea=ImVec2(
93 minArea.x+size.x,
94 minArea.y+size.y
95 );
96 ImRect rect=ImRect(minArea,maxArea);
97
98 ImVec2 textPos=ImVec2(
99 minArea.x+style.FramePadding.x,
100 minArea.y+style.FramePadding.y
101 );
102
103 ImGui::ItemSize(size,style.FramePadding.y);
104 bool visible=ImGui::ItemAdd(rect,ImGui::GetID(portID.c_str()));
105 bool hovered=false;
106 bool active=false;
107 if (visible) {
108 hovered=ImGui::ItemHoverable(rect,ImGui::GetID(portID.c_str()),0);
109 active=(hovered && ImGui::IsMouseClicked(ImGuiMouseButton_Left));
110
111 if (hovered && ImGui::IsMouseClicked(ImGuiMouseButton_Right)) {
112 ImGui::OpenPopup("SubPortOptions");
113 selectedPortSet=portSetID;
114 clickedPort=-1;
115 }
116
117 if (hovered) hoveredPortSet=portSetID;
118 if (active) clickedPort=-1;
119
120 // label
121 dl->AddRectFilled(minArea,maxArea,ImGui::GetColorU32(portSetColor),0.0f);
122 dl->AddRect(minArea,maxArea,ImGui::GetColorU32((selectedPortSet==portSetID)?uiColors[GUI_COLOR_TEXT]:portSetBorderColor),0.0f,0,dpiScale);
123 dl->AddText(ImGui::GetFont(),ImGui::GetFontSize(),textPos,ImGui::GetColorU32(uiColors[GUI_COLOR_TEXT]),label.c_str(),NULL,ImGui::GetWindowSize().x*0.6f);
124 }
125
126 // input ports
127 for (int i=0; i<ins; i++) {
128 String portLabel=_("input");
129 String subPortID=fmt::sprintf("subPort%.5x",(portSetID<<4)|i);
130 if (ins==2) {
131 portLabel=_(portNamesStereo[i&1]);
132 } else if (ins>2) {
133 portLabel=fmt::sprintf("%d",i+1);
134 }
135 ImVec2 portLabelSize=ImGui::CalcTextSize(portLabel.c_str());
136
137 ImVec2 portMin=ImVec2(
138 minArea.x,
139 minArea.y+style.FramePadding.y+labelSize.y+style.ItemSpacing.y+(style.ItemSpacing.y+portLabelSize.y+style.FramePadding.y)*i
140 );
141 ImVec2 portMax=ImVec2(
142 minArea.x+portLabelSize.x+style.FramePadding.x,
143 portMin.y+style.FramePadding.y+portLabelSize.y
144 );
145 ImRect portRect=ImRect(portMin,portMax);
146 ImVec2 portLabelPos=portMin;
147 portLabelPos.x+=style.FramePadding.x*0.5;
148 portLabelPos.y+=style.FramePadding.y*0.5;
149
150 portPos[(portSetID<<4)|i]=ImLerp(portMin,portMax,ImVec2(0.0f,0.5f));
151
152 if (visible) {
153 if (ImGui::ItemAdd(portRect,ImGui::GetID(subPortID.c_str()))) {
154 dl->AddRectFilled(portMin,portMax,ImGui::GetColorU32((i<activeIns)?portColor:portColorH),0.0f);
155 dl->AddRect(portMin,portMax,ImGui::GetColorU32((i<activeIns)?portBorderColor:portBorderColorH),0.0f,dpiScale);
156 dl->AddText(portLabelPos,ImGui::GetColorU32(uiColors[GUI_COLOR_TEXT]),portLabel.c_str());
157 }
158
159 if (ImGui::IsMouseHoveringRect(portMin,portMax)) {
160 hoveredSubPort=i;
161 if (active) clickedPort=i;
162 }
163 }
164 }
165
166 // output ports
167 for (int i=0; i<outs; i++) {
168 String portLabel=_("output");
169 String subPortID=fmt::sprintf("subPort%.5x",(portSetID<<4)|i);
170 if (outs==2) {
171 portLabel=_(portNamesStereo[i&1]);
172 } else if (outs>2) {
173 portLabel=fmt::sprintf("%d",i+1);
174 }
175 ImVec2 portLabelSize=ImGui::CalcTextSize(portLabel.c_str());
176
177 ImVec2 portMin=ImVec2(
178 maxArea.x-portLabelSize.x-style.FramePadding.x,
179 minArea.y+style.FramePadding.y+labelSize.y+style.ItemSpacing.y+(style.ItemSpacing.y+portLabelSize.y+style.FramePadding.y)*i
180 );
181 ImVec2 portMax=ImVec2(
182 maxArea.x,
183 portMin.y+style.FramePadding.y+portLabelSize.y
184 );
185 ImRect portRect=ImRect(portMin,portMax);
186 ImVec2 portLabelPos=portMin;
187 portLabelPos.x+=style.FramePadding.x*0.5;
188 portLabelPos.y+=style.FramePadding.y*0.5;
189
190 portPos[(portSetID<<4)|i]=ImLerp(portMin,portMax,ImVec2(1.0f,0.5f));
191
192 if (visible) {
193 if (ImGui::ItemAdd(portRect,ImGui::GetID(subPortID.c_str()))) {
194 dl->AddRectFilled(portMin,portMax,ImGui::GetColorU32((i<activeOuts)?portColor:portColorH),0.0f);
195 dl->AddRect(portMin,portMax,ImGui::GetColorU32((i<activeOuts)?portBorderColor:portBorderColorH),0.0f,dpiScale);
196 dl->AddText(portLabelPos,ImGui::GetColorU32(uiColors[GUI_COLOR_TEXT]),portLabel.c_str());
197 }
198
199 if (ImGui::IsMouseHoveringRect(portMin,portMax)) {
200 if (active) clickedPort=i;
201 hoveredSubPort=i;
202 }
203 }
204 }
205
206 if (visible && hovered && ImGui::IsMouseClicked(ImGuiMouseButton_Left)) return true;
207
208 return false;
209 }
210
211 void FurnaceGUI::drawMixer() {
212 if (nextWindow==GUI_WINDOW_MIXER) {
213 mixerOpen=true;
214 ImGui::SetNextWindowFocus();
215 nextWindow=GUI_WINDOW_NOTHING;
216 }
217 if (!mixerOpen) return;
218 if (mobileUI) {
219 patWindowPos=(portrait?ImVec2(0.0f,(mobileMenuPos*-0.65*canvasH)):ImVec2((0.16*canvasH)+0.5*canvasW*mobileMenuPos,0.0f));
220 patWindowSize=(portrait?ImVec2(canvasW,canvasH-(0.16*canvasW)):ImVec2(canvasW-(0.16*canvasH),canvasH));
221 ImGui::SetNextWindowPos(patWindowPos);
222 ImGui::SetNextWindowSize(patWindowSize);
223 } else {
224 ImGui::SetNextWindowSizeConstraints(ImVec2(400.0f*dpiScale,200.0f*dpiScale),ImVec2(canvasW,canvasH));
225 }
226 if (ImGui::Begin("Mixer",&mixerOpen,globalWinFlags|(settings.allowEditDocking?0:ImGuiWindowFlags_NoDocking),_("Mixer"))) {
227 if (ImGui::BeginTabBar("MixerView")) {
228 if (ImGui::BeginTabItem(_("Mixer"))) {
229 if (ImGui::SliderFloat(_("Master Volume"),&e->song.masterVol,0,3,"%.2fx")) {
230 if (e->song.masterVol<0) e->song.masterVol=0;
231 if (e->song.masterVol>3) e->song.masterVol=3;
232 MARK_MODIFIED;
233 } rightClickable
234
235 if (ImGui::BeginTable("CurPortSet",2)) {
236 ImGui::TableSetupColumn("c0",ImGuiTableColumnFlags_WidthStretch);
237 ImGui::TableSetupColumn("c1",ImGuiTableColumnFlags_WidthFixed);
238
239 for (int i=0; i<e->song.systemLen; i++) {
240 bool doInvert=e->song.systemVol[i]<0;
241 float vol=fabs(e->song.systemVol[i]);
242
243 ImGui::PushID(i);
244
245 ImGui::TableNextRow();
246 ImGui::TableNextColumn();
247 ImGui::AlignTextToFramePadding();
248 ImGui::Text("%d. %s",i+1,getSystemName(e->song.system[i]));
249 ImGui::TableNextColumn();
250 if (ImGui::Checkbox(_("Invert"),&doInvert)) {
251 e->song.systemVol[i]=-e->song.systemVol[i];
252 MARK_MODIFIED;
253 }
254
255 ImGui::TableNextRow();
256 ImGui::TableNextColumn();
257 ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
258 if (CWSliderFloat("##Volume",&vol,0,2)) {
259 if (doInvert) {
260 if (vol<0.0001) vol=0.0001;
261 }
262 if (vol<0) vol=0;
263 if (vol>10) vol=10;
264 e->song.systemVol[i]=(doInvert)?-vol:vol;
265 MARK_MODIFIED;
266 } rightClickable
267 ImGui::TableNextColumn();
268 ImGui::Text(_("Volume"));
269
270 ImGui::TableNextRow();
271 ImGui::TableNextColumn();
272 ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
273 if (CWSliderFloat("##Panning",&e->song.systemPan[i],-1.0f,1.0f)) {
274 if (e->song.systemPan[i]<-1.0f) e->song.systemPan[i]=-1.0f;
275 if (e->song.systemPan[i]>1.0f) e->song.systemPan[i]=1.0f;
276 MARK_MODIFIED;
277 } rightClickable
278 ImGui::TableNextColumn();
279 ImGui::Text(_("Panning"));
280
281 ImGui::TableNextRow();
282 ImGui::TableNextColumn();
283 ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
284 if (CWSliderFloat("##FrontRear",&e->song.systemPanFR[i],-1.0f,1.0f)) {
285 if (e->song.systemPanFR[i]<-1.0f) e->song.systemPanFR[i]=-1.0f;
286 if (e->song.systemPanFR[i]>1.0f) e->song.systemPanFR[i]=1.0f;
287 MARK_MODIFIED;
288 } rightClickable
289 ImGui::TableNextColumn();
290 ImGui::Text(_("Front/Rear"));
291
292 ImGui::PopID();
293 }
294
295 ImGui::EndTable();
296 }
297 ImGui::EndTabItem();
298 }
299 if (ImGui::BeginTabItem(_("Patchbay"))) {
300 std::map<unsigned int,ImVec2> portPos;
301
302 if (ImGui::BeginTable("PatchbayOptions",3)) {
303 ImGui::TableNextRow();
304 ImGui::TableNextColumn();
305 if (ImGui::Checkbox(_("Automatic patchbay"),&e->song.patchbayAuto)) {
306 if (e->song.patchbayAuto) e->autoPatchbayP();
307 MARK_MODIFIED;
308 }
309 ImGui::TableNextColumn();
310 ImGui::Checkbox(_("Display hidden ports"),&displayHiddenPorts);
311 ImGui::TableNextColumn();
312 ImGui::Checkbox(_("Display internal"),&displayInternalPorts);
313 ImGui::EndTable();
314 }
315
316 hoveredPortSet=0x1fff;
317 hoveredSubPort=-1;
318
319 if (ImGui::BeginChild("Patchbay",ImVec2(0,0),ImGuiChildFlags_Border)) {
320 ImDrawList* dl=ImGui::GetWindowDrawList();
321 ImVec2 topPos=ImGui::GetCursorPos();
322 ImVec2 sysSize=calcPortSetSize(_("System"),displayHiddenPorts?DIV_MAX_OUTPUTS:e->getAudioDescGot().outChans,0);
323 topPos.x+=ImGui::GetContentRegionAvail().x-sysSize.x;
324 if (ImGui::GetContentRegionAvail().y>sysSize.y) topPos.y+=(ImGui::GetContentRegionAvail().y-sysSize.y)*0.5+ImGui::GetScrollY();
325
326 if (ImGui::IsWindowHovered() && ImGui::IsMouseClicked(ImGuiMouseButton_Left)) selectedPortSet=0x1fff;
327
328 if (portDragActive) {
329 dl->AddLine(subPortPos,ImGui::GetMousePos(),ImGui::GetColorU32(uiColors[GUI_COLOR_PATCHBAY_CONNECTION]),2.0f*dpiScale);
330 }
331
332 for (int i=0; i<e->song.systemLen; i++) {
333 DivDispatch* dispatch=e->getDispatch(i);
334 if (dispatch==NULL) continue;
335 int outputs=dispatch->getOutputCount();
336 if (portSet(fmt::sprintf("%d. %s",i+1,getSystemName(e->song.system[i])),i,0,outputs,0,outputs,selectedSubPort,portPos)) {
337 selectedPortSet=i;
338 if (selectedSubPort>=0) {
339 portDragActive=true;
340 ImGui::InhibitInertialScroll();
341
342 auto subPortI=portPos.find((selectedPortSet<<4)|selectedSubPort);
343 if (subPortI!=portPos.cend()) {
344 subPortPos=subPortI->second;
345 } else {
346 portDragActive=false;
347 }
348 }
349 }
350 }
351
352 // metronome/sample preview
353 if (displayInternalPorts) {
354 if (portSet(_("Sample Preview"),0xffd,0,1,0,1,selectedSubPort,portPos)) {
355 selectedPortSet=0xffd;
356 if (selectedSubPort>=0) {
357 portDragActive=true;
358 ImGui::InhibitInertialScroll();
359 auto subPortI=portPos.find((selectedPortSet<<4)|selectedSubPort);
360 if (subPortI!=portPos.cend()) {
361 subPortPos=subPortI->second;
362 } else {
363 portDragActive=false;
364 }
365 }
366 }
367 if (portSet(_("Metronome"),0xffe,0,1,0,1,selectedSubPort,portPos)) {
368 selectedPortSet=0xffe;
369 if (selectedSubPort>=0) {
370 portDragActive=true;
371 ImGui::InhibitInertialScroll();
372 auto subPortI=portPos.find((selectedPortSet<<4)|selectedSubPort);
373 if (subPortI!=portPos.cend()) {
374 subPortPos=subPortI->second;
375 } else {
376 portDragActive=false;
377 }
378 }
379 }
380 }
381
382 ImGui::SetCursorPos(topPos);
383 if (portSet(_("System"),0x1000,displayHiddenPorts?DIV_MAX_OUTPUTS:e->getAudioDescGot().outChans,0,e->getAudioDescGot().outChans,0,selectedSubPort,portPos)) {
384 selectedPortSet=0x1000;
385 if (selectedSubPort>=0) {
386 portDragActive=true;
387 ImGui::InhibitInertialScroll();
388 auto subPortI=portPos.find((selectedPortSet<<4)|selectedSubPort);
389 if (subPortI!=portPos.cend()) {
390 subPortPos=subPortI->second;
391 } else {
392 portDragActive=false;
393 }
394 }
395 }
396
397 if (portDragActive) {
398 if (ImGui::IsMouseReleased(ImGuiMouseButton_Left)) {
399 portDragActive=false;
400 if (hoveredPortSet!=0x1fff && hoveredSubPort>=0 && selectedPortSet!=hoveredPortSet) {
401 unsigned int src=(selectedPortSet<<4)|selectedSubPort;
402 unsigned int dest=(hoveredPortSet<<4)|hoveredSubPort;
403
404 if (src&0x10000) {
405 src^=dest;
406 dest^=src;
407 src^=dest;
408 }
409
410 src&=0xffff;
411 dest&=0xffff;
412
413 if (!e->patchConnect(src,dest)) {
414 e->patchDisconnect(src,dest);
415 }
416 MARK_MODIFIED;
417 }
418 }
419 }
420
421 // draw connections
422 for (unsigned int i: e->song.patchbay) {
423 if ((i>>20)==selectedPortSet) continue;
424 auto portSrcI=portPos.find(i>>16);
425 auto portDestI=portPos.find(0x10000|(i&0xffff));
426 if (portSrcI!=portPos.cend() && portDestI!=portPos.cend()) {
427 ImVec2 portSrc=portSrcI->second;
428 ImVec2 portDest=portDestI->second;
429 dl->AddLine(portSrc,portDest,ImGui::GetColorU32(uiColors[GUI_COLOR_PATCHBAY_CONNECTION_BG]),2.0f*dpiScale);
430 }
431 }
432
433 // foreground
434 for (unsigned int i: e->song.patchbay) {
435 if ((i>>20)!=selectedPortSet) continue;
436 auto portSrcI=portPos.find(i>>16);
437 auto portDestI=portPos.find(0x10000|(i&0xffff));
438 if (portSrcI!=portPos.cend() && portDestI!=portPos.cend()) {
439 ImVec2 portSrc=portSrcI->second;
440 ImVec2 portDest=portDestI->second;
441 dl->AddLine(portSrc,portDest,ImGui::GetColorU32(uiColors[GUI_COLOR_PATCHBAY_CONNECTION]),2.0f*dpiScale);
442 }
443 }
444 }
445 if (ImGui::BeginPopup("SubPortOptions",ImGuiWindowFlags_NoMove|ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoSavedSettings)) {
446 if (ImGui::MenuItem(_("disconnect all"))) {
447 e->patchDisconnectAll(selectedPortSet);
448 MARK_MODIFIED;
449 }
450 ImGui::EndPopup();
451 }
452 ImGui::EndChild();
453 ImGui::EndTabItem();
454 }
455 ImGui::EndTabBar();
456 }
457
458 }
459 if (ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows)) curWindow=GUI_WINDOW_MIXER;
460 ImGui::End();
461 }