yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
music_editor.cc
Go to the documentation of this file.
1#include "music_editor.h"
2
4
5#include <algorithm>
6#include <cmath>
7#include <ctime>
8#include <iomanip>
9#include <sstream>
10
11#include "absl/strings/str_format.h"
23#include "app/emu/emulator.h"
25#include "app/gui/core/icons.h"
26#include "app/gui/core/input.h"
30#include "core/project.h"
31#include "imgui/imgui.h"
32#include "imgui/misc/cpp/imgui_stdlib.h"
33#include "nlohmann/json.hpp"
34#include "util/log.h"
35#include "util/macro.h"
36
37#ifdef __EMSCRIPTEN__
39#endif
40
41namespace yaze {
42namespace editor {
43
50
52 LOG_INFO("MusicEditor", "Initialize() START: rom_=%p, emulator_=%p",
53 static_cast<void*>(rom_), static_cast<void*>(emulator_));
54
55 // Note: song_window_class_ initialization is deferred to first Update() call
56 // because ImGui::GetID() requires a valid window context which doesn't exist
57 // during Initialize()
58 song_window_class_.DockingAllowUnclassed = true;
59 song_window_class_.DockNodeFlagsOverrideSet = ImGuiDockNodeFlags_None;
60
61 // ==========================================================================
62 // Create SINGLE audio backend - owned here and shared with all emulators
63 // This eliminates the dual-backend bug entirely
64 // ==========================================================================
65 if (!audio_backend_) {
66#ifdef __EMSCRIPTEN__
69#else
72#endif
73
75 config.sample_rate = 48000;
76 config.channels = 2;
77 config.buffer_frames = 1024;
79
80 if (audio_backend_->Initialize(config)) {
81 LOG_INFO("MusicEditor", "Created shared audio backend: %s @ %dHz",
82 audio_backend_->GetBackendName().c_str(), config.sample_rate);
83 } else {
84 LOG_ERROR("MusicEditor", "Failed to initialize audio backend!");
85 audio_backend_.reset();
86 }
87 }
88
89 // Share the audio backend with the main emulator (if available)
92 LOG_INFO("MusicEditor", "Shared audio backend with main emulator");
93 } else {
94 LOG_WARN("MusicEditor",
95 "Cannot share with main emulator: backend=%p, emulator=%p",
96 static_cast<void*>(audio_backend_.get()),
97 static_cast<void*>(emulator_));
98 }
99
100 music_player_ = std::make_unique<editor::music::MusicPlayer>(&music_bank_);
101 if (rom_) {
102 music_player_->SetRom(rom_);
103 LOG_INFO("MusicEditor", "Set ROM on MusicPlayer");
104 } else {
105 LOG_WARN("MusicEditor", "No ROM available for MusicPlayer!");
106 }
107
108 // Inject the main emulator into MusicPlayer
109 if (emulator_) {
110 music_player_->SetEmulator(emulator_);
111 LOG_INFO("MusicEditor", "Injected main emulator into MusicPlayer");
112 } else {
113 LOG_WARN("MusicEditor",
114 "No emulator available to inject into MusicPlayer!");
115 }
116
118 return;
119 auto* panel_manager = dependencies_.panel_manager;
120
121 // Register PanelDescriptors for menu/sidebar visibility
122 panel_manager->RegisterPanel({.card_id = "music.song_browser",
123 .display_name = "Song Browser",
124 .window_title = " Song Browser",
125 .icon = ICON_MD_LIBRARY_MUSIC,
126 .category = "Music",
127 .shortcut_hint = "Ctrl+Shift+B",
128 .priority = 5});
129 panel_manager->RegisterPanel({.card_id = "music.tracker",
130 .display_name = "Playback Control",
131 .window_title = " Playback Control",
132 .icon = ICON_MD_PLAY_CIRCLE,
133 .category = "Music",
134 .shortcut_hint = "Ctrl+Shift+M",
135 .priority = 10});
136 panel_manager->RegisterPanel({.card_id = "music.piano_roll",
137 .display_name = "Piano Roll",
138 .window_title = " Piano Roll",
139 .icon = ICON_MD_PIANO,
140 .category = "Music",
141 .shortcut_hint = "Ctrl+Shift+P",
142 .priority = 15});
143 panel_manager->RegisterPanel({.card_id = "music.instrument_editor",
144 .display_name = "Instrument Editor",
145 .window_title = " Instrument Editor",
146 .icon = ICON_MD_SPEAKER,
147 .category = "Music",
148 .shortcut_hint = "Ctrl+Shift+I",
149 .priority = 20});
150 panel_manager->RegisterPanel({.card_id = "music.sample_editor",
151 .display_name = "Sample Editor",
152 .window_title = " Sample Editor",
153 .icon = ICON_MD_WAVES,
154 .category = "Music",
155 .shortcut_hint = "Ctrl+Shift+S",
156 .priority = 25});
157 panel_manager->RegisterPanel({.card_id = "music.assembly",
158 .display_name = "Assembly View",
159 .window_title = " Music Assembly",
160 .icon = ICON_MD_CODE,
161 .category = "Music",
162 .shortcut_hint = "Ctrl+Shift+A",
163 .priority = 30});
164 // ==========================================================================
165 // Phase 5: Create and register EditorPanel instances
166 // Note: Callbacks are set up on the view classes during Draw() since
167 // PanelManager takes ownership of the panels.
168 // ==========================================================================
169
170 // Song Browser Panel - callbacks are set on song_browser_view_ directly
171 auto song_browser = std::make_unique<MusicSongBrowserPanel>(
173 panel_manager->RegisterEditorPanel(std::move(song_browser));
174
175 // Playback Control Panel
176 auto playback_control = std::make_unique<MusicPlaybackControlPanel>(
178 playback_control->SetOnOpenSong([this](int index) { OpenSong(index); });
179 playback_control->SetOnOpenPianoRoll(
180 [this](int index) { OpenSongPianoRoll(index); });
181 panel_manager->RegisterEditorPanel(std::move(playback_control));
182
183 // Piano Roll Panel
184 auto piano_roll = std::make_unique<MusicPianoRollPanel>(
187 panel_manager->RegisterEditorPanel(std::move(piano_roll));
188
189 // Instrument Editor Panel - callbacks set on instrument_editor_view_
190 auto instrument_editor = std::make_unique<MusicInstrumentEditorPanel>(
192 panel_manager->RegisterEditorPanel(std::move(instrument_editor));
193
194 // Sample Editor Panel - callbacks set on sample_editor_view_
195 auto sample_editor = std::make_unique<MusicSampleEditorPanel>(
197 panel_manager->RegisterEditorPanel(std::move(sample_editor));
198
199 // Assembly Panel
200 auto assembly = std::make_unique<MusicAssemblyPanel>(&assembly_editor_);
201 panel_manager->RegisterEditorPanel(std::move(assembly));
202
203 // Audio debug and help panels removed from the default panel roster.
204}
205
207 LOG_INFO("MusicEditor", "set_emulator(%p): audio_backend_=%p",
208 static_cast<void*>(emulator),
209 static_cast<void*>(audio_backend_.get()));
211 // Share our audio backend with the main emulator (single backend architecture)
212 if (emulator_ && audio_backend_) {
214 LOG_INFO("MusicEditor",
215 "Shared audio backend with main emulator (deferred)");
216 }
217
218 // Inject emulator into MusicPlayer
219 if (music_player_) {
220 music_player_->SetEmulator(emulator_);
221 }
222}
223
234
235absl::Status MusicEditor::Load() {
236 gfx::ScopedTimer timer("MusicEditor::Load");
237 if (project_) {
240 if (music_storage_key_.empty()) {
242 }
243 }
244
245#ifdef __EMSCRIPTEN__
247 auto restore = RestoreMusicState();
248 if (restore.ok() && restore.value()) {
249 LOG_INFO("MusicEditor", "Restored music state from web storage");
250 return absl::OkStatus();
251 } else if (!restore.ok()) {
252 LOG_WARN("MusicEditor", "Failed to restore music state: %s",
253 restore.status().ToString().c_str());
254 }
255 }
256#endif
257
258 if (rom_ && rom_->is_loaded()) {
259 if (music_player_) {
260 music_player_->SetRom(rom_);
261 LOG_INFO("MusicEditor", "Load(): Set ROM on MusicPlayer, IsAudioReady=%d",
262 music_player_->IsAudioReady());
263 }
265 } else {
266 LOG_WARN("MusicEditor", "Load(): No ROM available!");
267 }
268 return absl::OkStatus();
269}
270
272 if (!music_player_)
273 return;
274 auto state = music_player_->GetState();
275 if (state.is_playing && !state.is_paused) {
276 music_player_->Pause();
277 } else if (state.is_paused) {
278 music_player_->Resume();
279 } else {
280 music_player_->PlaySong(state.playing_song_index);
281 }
282}
283
285 if (music_player_) {
286 music_player_->Stop();
287 }
288}
289
290void MusicEditor::SpeedUp(float delta) {
291 if (music_player_) {
292 auto state = music_player_->GetState();
293 music_player_->SetPlaybackSpeed(state.playback_speed + delta);
294 }
295}
296
297void MusicEditor::SlowDown(float delta) {
298 if (music_player_) {
299 auto state = music_player_->GetState();
300 music_player_->SetPlaybackSpeed(state.playback_speed - delta);
301 }
302}
303
304absl::Status MusicEditor::Update() {
305 // Deferred initialization: Initialize song_window_class_.ClassId on first Update()
306 // because ImGui::GetID() requires a valid window context
307 if (song_window_class_.ClassId == 0) {
308 song_window_class_.ClassId = ImGui::GetID("SongTrackerWindowClass");
309 }
310
311 // Update MusicPlayer - this runs the emulator's audio frame
312 // MusicPlayer now controls the main emulator directly for playback.
313 if (music_player_)
314 music_player_->Update();
315
316#ifdef __EMSCRIPTEN__
319 music_dirty_ = true;
320 }
321 auto now = std::chrono::steady_clock::now();
322 const auto elapsed = now - last_music_persist_;
323 if (music_dirty_ && (last_music_persist_.time_since_epoch().count() == 0 ||
324 elapsed > std::chrono::seconds(3))) {
325 auto status = PersistMusicState("autosave");
326 if (!status.ok()) {
327 LOG_WARN("MusicEditor", "Music autosave failed: %s",
328 status.ToString().c_str());
329 }
330 }
331 }
332#endif
333
335 return absl::OkStatus();
336 auto* panel_manager = dependencies_.panel_manager;
337
338 // ==========================================================================
339 // Phase 5 Complete: Static panels now drawn by DrawAllVisiblePanels()
340 // Only auto-show logic and dynamic song windows remain here
341 // ==========================================================================
342
343 // Auto-show Song Browser on first load
344 bool* browser_visible =
345 panel_manager->GetVisibilityFlag("music.song_browser");
346 if (browser_visible && !song_browser_auto_shown_) {
347 *browser_visible = true;
349 }
350
351 // Auto-show Playback Control on first load
352 bool* playback_visible = panel_manager->GetVisibilityFlag("music.tracker");
353 if (playback_visible && !tracker_auto_shown_) {
354 *playback_visible = true;
355 tracker_auto_shown_ = true;
356 }
357
358 // Auto-show Piano Roll on first load
359 bool* piano_roll_visible =
360 panel_manager->GetVisibilityFlag("music.piano_roll");
361 if (piano_roll_visible && !piano_roll_auto_shown_) {
362 *piano_roll_visible = true;
364 }
365
366 // ==========================================================================
367 // Dynamic Per-Song Windows (like dungeon room cards)
368 // TODO(Phase 6): Migrate to ResourcePanel with LRU limits
369 // ==========================================================================
370
371 // Per-Song Tracker Windows - synced with PanelManager for Activity Bar
372 for (int i = 0; i < active_songs_.Size; i++) {
373 int song_index = active_songs_[i];
374 // Use base ID - PanelManager handles session prefixing
375 std::string card_id = absl::StrFormat("music.song_%d", song_index);
376
377 // Check if panel was hidden via Activity Bar
378 bool panel_visible = true;
380 panel_visible = dependencies_.panel_manager->IsPanelVisible(card_id);
381 }
382
383 // If hidden via Activity Bar, close the song
384 if (!panel_visible) {
387 }
388 song_cards_.erase(song_index);
389 song_trackers_.erase(song_index);
390 active_songs_.erase(active_songs_.Data + i);
391 i--;
392 continue;
393 }
394
395 // Category filtering: only draw if Music is active OR panel is pinned
396 bool is_pinned = dependencies_.panel_manager &&
398 std::string active_category =
401 : "";
402
403 if (active_category != "Music" && !is_pinned) {
404 // Not in Music editor and not pinned - skip drawing but keep registered
405 // Panel will reappear when user returns to Music editor
406 continue;
407 }
408
409 bool open = true;
410
411 // Get song name for window title (icon is handled by EditorPanel)
412 auto* song = music_bank_.GetSong(song_index);
413 std::string song_name = song ? song->name : "Unknown";
414 std::string card_title = absl::StrFormat(
415 "[%02X] %s###SongTracker%d", song_index + 1, song_name, song_index);
416
417 // Create card instance if needed
418 if (song_cards_.find(song_index) == song_cards_.end()) {
419 song_cards_[song_index] = std::make_shared<gui::PanelWindow>(
420 card_title.c_str(), ICON_MD_MUSIC_NOTE, &open);
421 song_cards_[song_index]->SetDefaultSize(900, 700);
422
423 // Create dedicated tracker view for this song
424 song_trackers_[song_index] =
425 std::make_unique<editor::music::TrackerView>();
426 song_trackers_[song_index]->SetOnEditCallback(
427 [this, song_index]() { PushUndoState(song_index); });
428 }
429
430 auto& song_card = song_cards_[song_index];
431
432 // Use docking class to group song windows together
433 ImGui::SetNextWindowClass(&song_window_class_);
434
435 if (song_card->Begin(&open)) {
436 DrawSongTrackerWindow(song_index);
437 }
438 song_card->End();
439
440 // Handle close button
441 if (!open) {
442 // Unregister from PanelManager
445 }
446 song_cards_.erase(song_index);
447 song_trackers_.erase(song_index);
448 active_songs_.erase(active_songs_.Data + i);
449 i--;
450 }
451 }
452
453 // Per-song piano roll windows - synced with PanelManager for Activity Bar
454 for (auto it = song_piano_rolls_.begin(); it != song_piano_rolls_.end();) {
455 int song_index = it->first;
456 auto& window = it->second;
457 auto* song = music_bank_.GetSong(song_index);
458 // Use base ID - PanelManager handles session prefixing
459 std::string card_id = absl::StrFormat("music.piano_roll_%d", song_index);
460
461 if (!song || !window.card || !window.view) {
464 }
465 it = song_piano_rolls_.erase(it);
466 continue;
467 }
468
469 // Check if panel was hidden via Activity Bar
470 bool panel_visible = true;
472 panel_visible = dependencies_.panel_manager->IsPanelVisible(card_id);
473 }
474
475 // If hidden via Activity Bar, close the piano roll
476 if (!panel_visible) {
479 }
480 delete window.visible_flag;
481 it = song_piano_rolls_.erase(it);
482 continue;
483 }
484
485 // Category filtering: only draw if Music is active OR panel is pinned
486 bool is_pinned = dependencies_.panel_manager &&
488 std::string active_category =
491 : "";
492
493 if (active_category != "Music" && !is_pinned) {
494 // Not in Music editor and not pinned - skip drawing but keep registered
495 ++it;
496 continue;
497 }
498
499 bool open = true;
500
501 // Use same docking class as tracker windows so they can dock together
502 ImGui::SetNextWindowClass(&song_window_class_);
503
504 if (window.card->Begin(&open)) {
505 window.view->SetOnEditCallback(
506 [this, song_index]() { PushUndoState(song_index); });
507 window.view->SetOnNotePreview(
508 [this, song_index](const zelda3::music::TrackEvent& evt,
509 int segment_idx, int channel_idx) {
510 auto* target = music_bank_.GetSong(song_index);
511 if (!target || !music_player_)
512 return;
513 music_player_->PreviewNote(*target, evt, segment_idx, channel_idx);
514 });
515 window.view->SetOnSegmentPreview(
516 [this, song_index](const zelda3::music::MusicSong& /*unused*/,
517 int segment_idx) {
518 auto* target = music_bank_.GetSong(song_index);
519 if (!target || !music_player_)
520 return;
521 music_player_->PreviewSegment(*target, segment_idx);
522 });
523 // Update playback state for cursor visualization
524 auto state = music_player_ ? music_player_->GetState()
526 window.view->SetPlaybackState(state.is_playing, state.is_paused,
527 state.current_tick);
528 window.view->Draw(song);
529 }
530 window.card->End();
531
532 if (!open) {
533 // Unregister from PanelManager
536 }
537 delete window.visible_flag;
538 it = song_piano_rolls_.erase(it);
539 } else {
540 ++it;
541 }
542 }
543
545
546 return absl::OkStatus();
547}
548
549absl::Status MusicEditor::Save() {
550 if (!rom_)
551 return absl::FailedPreconditionError("No ROM loaded");
553
554#ifdef __EMSCRIPTEN__
555 auto persist_status = PersistMusicState("save");
556 if (!persist_status.ok()) {
557 return persist_status;
558 }
559#endif
560
561 return absl::OkStatus();
562}
563
564absl::StatusOr<bool> MusicEditor::RestoreMusicState() {
565#ifdef __EMSCRIPTEN__
566 if (music_storage_key_.empty()) {
567 return false;
568 }
569
570 auto storage_or = platform::WasmStorage::LoadProject(music_storage_key_);
571 if (!storage_or.ok()) {
572 return false; // Nothing persisted yet
573 }
574
575 try {
576 auto parsed = nlohmann::json::parse(storage_or.value());
578 music_dirty_ = false;
579 last_music_persist_ = std::chrono::steady_clock::now();
580 return true;
581 } catch (const std::exception& e) {
582 return absl::InvalidArgumentError(
583 absl::StrFormat("Failed to parse stored music state: %s", e.what()));
584 }
585#else
586 return false;
587#endif
588}
589
590absl::Status MusicEditor::PersistMusicState(const char* reason) {
591#ifdef __EMSCRIPTEN__
593 return absl::OkStatus();
594 }
595
596 auto serialized = music_bank_.ToJson().dump();
598 platform::WasmStorage::SaveProject(music_storage_key_, serialized));
599
600 if (project_) {
601 auto now = std::chrono::system_clock::now();
602 auto time_t = std::chrono::system_clock::to_time_t(now);
603 std::stringstream ss;
604 ss << std::put_time(std::localtime(&time_t), "%Y-%m-%d %H:%M:%S");
607 }
608
609 music_dirty_ = false;
610 last_music_persist_ = std::chrono::steady_clock::now();
611 if (reason) {
612 LOG_DEBUG("MusicEditor", "Persisted music state (%s)", reason);
613 }
614 return absl::OkStatus();
615#else
616 (void)reason;
617 return absl::OkStatus();
618#endif
619}
620
624
625absl::Status MusicEditor::Cut() {
626 Copy();
627 // In a real implementation, this would delete the selected events
628 // TrackerView::DeleteSelection();
630 return absl::OkStatus();
631}
632
633absl::Status MusicEditor::Copy() {
634 // TODO: Serialize selected events to clipboard
635 // TrackerView should expose a GetSelection() method
636 return absl::UnimplementedError(
637 "Copy not yet implemented - clipboard support coming soon");
638}
639
640absl::Status MusicEditor::Paste() {
641 // TODO: Paste from clipboard
642 // Need to deserialize events and insert at cursor position
643 return absl::UnimplementedError(
644 "Paste not yet implemented - clipboard support coming soon");
645}
646
647absl::Status MusicEditor::Undo() {
649 auto st = undo_manager_.Undo();
650 if (st.ok()) {
652 }
653 return st;
654}
655
656absl::Status MusicEditor::Redo() {
657 auto st = undo_manager_.Redo();
658 if (st.ok()) {
660 }
661 return st;
662}
663
667
668void MusicEditor::PushUndoState(int song_index) {
669 auto* song = music_bank_.GetSong(song_index);
670 if (!song)
671 return;
672
673 // Finalize any pending undo action with current state as "after"
675
676 // Start a new pending undo - capture "before" state
677 pending_undo_before_ = *song;
678 pending_undo_song_index_ = song_index;
680}
681
683 if (!pending_undo_before_.has_value())
684 return;
685
687 if (!song) {
688 pending_undo_before_.reset();
690 return;
691 }
692
693 // Push the action with before/after snapshots
694 undo_manager_.Push(std::make_unique<MusicSongEditAction>(
696 std::move(*pending_undo_before_),
697 *song, // "after" = current state
698 &music_bank_));
699
700 pending_undo_before_.reset();
702}
703
707 // Update current song if selection changed
708 const int selected = song_browser_view_.GetSelectedSongIndex();
709 if (selected != current_song_index_) {
710 // Commit any pending edits before switching the active song selection.
712 current_song_index_ = selected;
713 }
714}
715
716void MusicEditor::OpenSong(int song_index) {
717 // Update current selection
718 current_song_index_ = song_index;
720
721 // Check if already open
722 for (int i = 0; i < active_songs_.Size; i++) {
723 if (active_songs_[i] == song_index) {
724 // Focus the existing window
725 FocusSong(song_index);
726 return;
727 }
728 }
729
730 // Add new song to active list
731 active_songs_.push_back(song_index);
732
733 // Register with PanelManager so it appears in Activity Bar
735 auto* song = music_bank_.GetSong(song_index);
736 std::string song_name =
737 song ? song->name : absl::StrFormat("Song %02X", song_index);
738 // Use base ID - RegisterPanel handles session prefixing
739 std::string card_id = absl::StrFormat("music.song_%d", song_index);
740
742 {.card_id = card_id,
743 .display_name = song_name,
744 .window_title = ICON_MD_MUSIC_NOTE " " + song_name,
745 .icon = ICON_MD_MUSIC_NOTE,
746 .category = "Music",
747 .shortcut_hint = "",
748 .visibility_flag = nullptr,
749 .priority = 200 + song_index});
750
752
753 // NOT auto-pinned - user must explicitly pin to persist across editors
754 }
755
756 LOG_INFO("MusicEditor", "Opened song %d tracker window", song_index);
757}
758
759void MusicEditor::FocusSong(int song_index) {
760 auto it = song_cards_.find(song_index);
761 if (it != song_cards_.end()) {
762 it->second->Focus();
763 }
764}
765
766void MusicEditor::OpenSongPianoRoll(int song_index) {
767 if (song_index < 0 ||
768 song_index >= static_cast<int>(music_bank_.GetSongCount())) {
769 return;
770 }
771
772 auto it = song_piano_rolls_.find(song_index);
773 if (it != song_piano_rolls_.end()) {
774 if (it->second.card && it->second.visible_flag) {
775 *it->second.visible_flag = true;
776 it->second.card->Focus();
777 }
778 return;
779 }
780
781 auto* song = music_bank_.GetSong(song_index);
782 std::string song_name =
783 song ? song->name : absl::StrFormat("Song %02X", song_index);
784 std::string card_title =
785 absl::StrFormat("[%02X] %s - Piano Roll###SongPianoRoll%d",
786 song_index + 1, song_name, song_index);
787
788 SongPianoRollWindow window;
789 window.visible_flag = new bool(true);
790 window.card = std::make_shared<gui::PanelWindow>(
791 card_title.c_str(), ICON_MD_PIANO, window.visible_flag);
792 window.card->SetDefaultSize(900, 450);
793 window.view = std::make_unique<editor::music::PianoRollView>();
794 window.view->SetActiveChannel(0);
795 window.view->SetActiveSegment(0);
796
797 song_piano_rolls_[song_index] = std::move(window);
798
799 // Register with PanelManager so it appears in Activity Bar
801 // Use base ID - RegisterPanel handles session prefixing
802 std::string card_id = absl::StrFormat("music.piano_roll_%d", song_index);
803
805 {.card_id = card_id,
806 .display_name = song_name + " (Piano)",
807 .window_title = ICON_MD_PIANO " " + song_name + " (Piano)",
808 .icon = ICON_MD_PIANO,
809 .category = "Music",
810 .shortcut_hint = "",
811 .visibility_flag = nullptr,
812 .priority = 250 + song_index});
813
815 // NOT auto-pinned - user must explicitly pin to persist across editors
816 }
817}
818
820 auto* song = music_bank_.GetSong(song_index);
821 if (!song) {
822 ImGui::TextDisabled("Song not loaded");
823 return;
824 }
825
826 // Compact toolbar for this song window
827 bool can_play = music_player_ && music_player_->IsAudioReady();
828 auto state = music_player_ ? music_player_->GetState()
830 bool is_playing_this_song =
831 state.is_playing && (state.playing_song_index == song_index);
832 bool is_paused_this_song =
833 state.is_paused && (state.playing_song_index == song_index);
834
835 // === Row 1: Playback Transport ===
836 if (!can_play)
837 ImGui::BeginDisabled();
838
839 // Play/Pause button with status indication
840 if (is_playing_this_song && !is_paused_this_song) {
841 auto sc = gui::GetSuccessButtonColors();
842 gui::StyleColorGuard btn_guard(
843 {{ImGuiCol_Button, sc.button},
844 {ImGuiCol_ButtonHovered, sc.hovered}});
845 if (ImGui::Button(ICON_MD_PAUSE " Pause")) {
846 music_player_->Pause();
847 }
848 } else if (is_paused_this_song) {
849 auto wc = gui::GetWarningButtonColors();
850 gui::StyleColorGuard btn_guard(
851 {{ImGuiCol_Button, wc.button},
852 {ImGuiCol_ButtonHovered, wc.hovered}});
853 if (ImGui::Button(ICON_MD_PLAY_ARROW " Resume")) {
854 music_player_->Resume();
855 }
856 } else {
857 if (ImGui::Button(ICON_MD_PLAY_ARROW " Play")) {
858 music_player_->PlaySong(song_index);
859 }
860 }
861
862 ImGui::SameLine();
863 if (ImGui::Button(ICON_MD_STOP)) {
864 music_player_->Stop();
865 }
866 if (ImGui::IsItemHovered())
867 ImGui::SetTooltip("Stop playback");
868
869 if (!can_play) {
870 ImGui::EndDisabled();
871 if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled)) {
872 ImGui::SetTooltip("Audio not ready - initialize music player first");
873 }
874 }
875
876 // Keyboard shortcuts (when window is focused)
877 if (ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows) &&
878 can_play) {
879 // Focused-window shortcuts remain as fallbacks; also registered with ShortcutManager.
880 if (ImGui::IsKeyPressed(ImGuiKey_Space, false)) {
882 }
883 if (ImGui::IsKeyPressed(ImGuiKey_Escape, false)) {
884 StopPlayback();
885 }
886 if (ImGui::IsKeyPressed(ImGuiKey_Equal, false) ||
887 ImGui::IsKeyPressed(ImGuiKey_KeypadAdd, false)) {
888 SpeedUp();
889 }
890 if (ImGui::IsKeyPressed(ImGuiKey_Minus, false) ||
891 ImGui::IsKeyPressed(ImGuiKey_KeypadSubtract, false)) {
892 SlowDown();
893 }
894 }
895
896 // Status indicator
897 ImGui::SameLine();
898 if (is_playing_this_song && !is_paused_this_song) {
899 ImGui::TextColored(gui::GetSuccessColor(), ICON_MD_GRAPHIC_EQ);
900 if (ImGui::IsItemHovered())
901 ImGui::SetTooltip("Playing");
902 } else if (is_paused_this_song) {
903 ImGui::TextColored(gui::GetWarningColor(), ICON_MD_PAUSE_CIRCLE);
904 if (ImGui::IsItemHovered())
905 ImGui::SetTooltip("Paused");
906 }
907
908 // Right side controls
909 float right_offset = ImGui::GetWindowWidth() - 200;
910 ImGui::SameLine(right_offset);
911
912 // Speed control (with mouse wheel support)
913 ImGui::Text(ICON_MD_SPEED);
914 ImGui::SameLine();
915 ImGui::SetNextItemWidth(55);
916 float speed = state.playback_speed;
917 if (gui::SliderFloatWheel("##Speed", &speed, 0.25f, 2.0f, "%.1fx", 0.1f)) {
918 if (music_player_) {
919 music_player_->SetPlaybackSpeed(speed);
920 }
921 }
922 if (ImGui::IsItemHovered())
923 ImGui::SetTooltip("Playback speed (0.25x - 2.0x) - use mouse wheel");
924
925 ImGui::SameLine();
926 if (ImGui::Button(ICON_MD_PIANO)) {
927 OpenSongPianoRoll(song_index);
928 }
929 if (ImGui::IsItemHovered())
930 ImGui::SetTooltip("Open Piano Roll view");
931
932 // === Row 2: Song Info ===
933 const char* bank_name = nullptr;
934 switch (song->bank) {
935 case 0:
936 bank_name = "Overworld";
937 break;
938 case 1:
939 bank_name = "Dungeon";
940 break;
941 case 2:
942 bank_name = "Credits";
943 break;
944 case 3:
945 bank_name = "Expanded";
946 break;
947 case 4:
948 bank_name = "Auxiliary";
949 break;
950 default:
951 bank_name = "Unknown";
952 break;
953 }
954 ImGui::TextColored(gui::GetDisabledColor(), "[%02X]", song_index + 1);
955 ImGui::SameLine();
956 ImGui::Text("%s", song->name.c_str());
957 ImGui::SameLine();
958 ImGui::TextColored(gui::GetInfoColor(), "(%s)", bank_name);
959
960 if (song->modified) {
961 ImGui::SameLine();
962 ImGui::TextColored(gui::GetWarningColor(),
963 ICON_MD_EDIT " Modified");
964 }
965
966 // Segment count
967 ImGui::SameLine(right_offset);
968 ImGui::TextColored(gui::GetDisabledColor(), "%zu segments",
969 song->segments.size());
970
971 ImGui::Separator();
972
973 // Channel overview shows DSP state when playing
974 if (is_playing_this_song) {
976 ImGui::Separator();
977 }
978
979 // Draw the tracker view for this specific song
980 auto it = song_trackers_.find(song_index);
981 if (it != song_trackers_.end()) {
982 it->second->Draw(song, &music_bank_);
983 } else {
984 // Fallback - shouldn't happen but just in case
986 }
987}
988
989// Playback Control panel - focused on audio playback and current song status
991 DrawToolset();
992
993 ImGui::Separator();
994
995 // Current song info
997 auto state = music_player_ ? music_player_->GetState()
999
1000 if (song) {
1001 ImGui::Text("Selected Song:");
1002 ImGui::SameLine();
1003 ImGui::TextColored(gui::GetInfoColor(), "[%02X] %s",
1004 current_song_index_ + 1, song->name.c_str());
1005
1006 // Song details
1007 ImGui::SameLine();
1008 ImGui::TextDisabled("| %zu segments", song->segments.size());
1009 if (song->modified) {
1010 ImGui::SameLine();
1011 ImGui::TextColored(gui::GetWarningColor(),
1012 ICON_MD_EDIT " Modified");
1013 }
1014 }
1015
1016 // Playback status bar
1017 if (state.is_playing || state.is_paused) {
1018 ImGui::Separator();
1019
1020 // Timeline progress
1021 if (song && !song->segments.empty()) {
1022 uint32_t total_duration = 0;
1023 for (const auto& seg : song->segments) {
1024 total_duration += seg.GetDuration();
1025 }
1026
1027 float progress =
1028 (total_duration > 0)
1029 ? static_cast<float>(state.current_tick) / total_duration
1030 : 0.0f;
1031 progress = std::clamp(progress, 0.0f, 1.0f);
1032
1033 // Time display
1034 float current_seconds = state.ticks_per_second > 0
1035 ? state.current_tick / state.ticks_per_second
1036 : 0.0f;
1037 float total_seconds = state.ticks_per_second > 0
1038 ? total_duration / state.ticks_per_second
1039 : 0.0f;
1040
1041 int cur_min = static_cast<int>(current_seconds) / 60;
1042 int cur_sec = static_cast<int>(current_seconds) % 60;
1043 int tot_min = static_cast<int>(total_seconds) / 60;
1044 int tot_sec = static_cast<int>(total_seconds) % 60;
1045
1046 ImGui::Text("%d:%02d / %d:%02d", cur_min, cur_sec, tot_min, tot_sec);
1047 ImGui::SameLine();
1048
1049 // Progress bar
1050 ImGui::ProgressBar(progress, ImVec2(-1, 0), "");
1051 }
1052
1053 // Segment info
1054 ImGui::Text("Segment: %d | Tick: %u", state.current_segment_index + 1,
1055 state.current_tick);
1056 ImGui::SameLine();
1057 ImGui::TextDisabled("| %.1f ticks/sec | %.2fx speed",
1058 state.ticks_per_second, state.playback_speed);
1059 }
1060
1061 // Channel overview when playing
1062 if (state.is_playing) {
1063 ImGui::Separator();
1065 }
1066
1067 ImGui::Separator();
1068
1069 // Quick action buttons
1070 if (ImGui::Button(ICON_MD_OPEN_IN_NEW " Open Tracker")) {
1072 }
1073 if (ImGui::IsItemHovered())
1074 ImGui::SetTooltip("Open song in dedicated tracker window");
1075
1076 ImGui::SameLine();
1077 if (ImGui::Button(ICON_MD_PIANO " Open Piano Roll")) {
1079 }
1080 if (ImGui::IsItemHovered())
1081 ImGui::SetTooltip("Open piano roll view for this song");
1082
1083 // Help section (collapsed by default)
1084 if (ImGui::CollapsingHeader(ICON_MD_KEYBOARD " Keyboard Shortcuts")) {
1085 ImGui::BulletText("Space: Play/Pause toggle");
1086 ImGui::BulletText("Escape: Stop playback");
1087 ImGui::BulletText("+/-: Increase/decrease speed");
1088 ImGui::BulletText("Arrow keys: Navigate in tracker/piano roll");
1089 ImGui::BulletText("Z,S,X,D,C,V,G,B,H,N,J,M: Piano keyboard (C to B)");
1090 ImGui::BulletText("Ctrl+Wheel: Zoom (Piano Roll)");
1091 }
1092}
1093
1094// Legacy DrawTrackerView for compatibility (calls the tracker view directly)
1099
1102 if (song &&
1103 current_segment_index_ >= static_cast<int>(song->segments.size())) {
1105 }
1106
1111 const zelda3::music::TrackEvent& evt,
1112 int segment_idx, int channel_idx) {
1113 auto* target = music_bank_.GetSong(song_index);
1114 if (!target || !music_player_)
1115 return;
1116 music_player_->PreviewNote(*target, evt, segment_idx, channel_idx);
1117 });
1119 [this, song_index = current_song_index_](
1120 const zelda3::music::MusicSong& /*unused*/, int segment_idx) {
1121 auto* target = music_bank_.GetSong(song_index);
1122 if (!target || !music_player_)
1123 return;
1124 music_player_->PreviewSegment(*target, segment_idx);
1125 });
1126
1127 // Update playback state for cursor visualization
1128 auto state = music_player_ ? music_player_->GetState()
1130 piano_roll_view_.SetPlaybackState(state.is_playing, state.is_paused,
1131 state.current_tick);
1132
1136}
1137
1141
1145
1147 static int current_volume = 100;
1148 auto state = music_player_ ? music_player_->GetState()
1150 bool can_play = music_player_ && music_player_->IsAudioReady();
1151
1152 // Row 1: Transport controls and song info
1154
1155 if (!can_play)
1156 ImGui::BeginDisabled();
1157
1158 // Transport: Play/Pause with visual state indication
1159 const ImVec4 paused_color = gui::GetWarningColor();
1160
1161 if (state.is_playing && !state.is_paused) {
1162 gui::StyleColorGuard btn_guard(ImGuiCol_Button,
1164 if (ImGui::Button(ICON_MD_PAUSE "##Pause"))
1165 music_player_->Pause();
1166 if (ImGui::IsItemHovered())
1167 ImGui::SetTooltip("Pause (Space)");
1168 } else if (state.is_paused) {
1169 gui::StyleColorGuard btn_guard(ImGuiCol_Button,
1171 if (ImGui::Button(ICON_MD_PLAY_ARROW "##Resume"))
1172 music_player_->Resume();
1173 if (ImGui::IsItemHovered())
1174 ImGui::SetTooltip("Resume (Space)");
1175 } else {
1176 if (ImGui::Button(ICON_MD_PLAY_ARROW "##Play"))
1178 if (ImGui::IsItemHovered())
1179 ImGui::SetTooltip("Play (Space)");
1180 }
1181
1182 ImGui::SameLine();
1183 if (ImGui::Button(ICON_MD_STOP "##Stop"))
1184 music_player_->Stop();
1185 if (ImGui::IsItemHovered())
1186 ImGui::SetTooltip("Stop (Escape)");
1187
1188 if (!can_play)
1189 ImGui::EndDisabled();
1190
1191 // Song label with animated playing indicator
1192 ImGui::SameLine();
1193 if (song) {
1194 if (state.is_playing && !state.is_paused) {
1195 // Animated playing indicator
1196 float t = static_cast<float>(ImGui::GetTime() * 3.0);
1197 float alpha = 0.5f + 0.5f * std::sin(t);
1198 auto success_c = gui::GetSuccessColor();
1199 ImGui::TextColored(ImVec4(success_c.x, success_c.y, success_c.z, alpha),
1201 ImGui::SameLine();
1202 } else if (state.is_paused) {
1203 ImGui::TextColored(paused_color, ICON_MD_PAUSE_CIRCLE);
1204 ImGui::SameLine();
1205 }
1206 ImGui::Text("%s", song->name.c_str());
1207 if (song->modified) {
1208 ImGui::SameLine();
1209 ImGui::TextColored(gui::GetWarningColor(), ICON_MD_EDIT);
1210 }
1211 } else {
1212 ImGui::TextDisabled("No song selected");
1213 }
1214
1215 // Time display (when playing)
1216 if (state.is_playing || state.is_paused) {
1217 ImGui::SameLine();
1218 float seconds = state.ticks_per_second > 0
1219 ? state.current_tick / state.ticks_per_second
1220 : 0.0f;
1221 int mins = static_cast<int>(seconds) / 60;
1222 int secs = static_cast<int>(seconds) % 60;
1223 ImGui::TextColored(gui::GetInfoColor(), " %d:%02d", mins, secs);
1224 }
1225
1226 // Right-aligned controls
1227 float right_offset = ImGui::GetWindowWidth() - 380;
1228 ImGui::SameLine(right_offset);
1229
1230 // Speed control with visual feedback
1231 ImGui::Text(ICON_MD_SPEED);
1232 ImGui::SameLine();
1233 ImGui::SetNextItemWidth(70);
1234 float speed = state.playback_speed;
1235 if (gui::SliderFloatWheel("##Speed", &speed, 0.25f, 2.0f, "%.2fx", 0.1f)) {
1236 if (music_player_) {
1237 music_player_->SetPlaybackSpeed(speed);
1238 }
1239 }
1240 if (ImGui::IsItemHovered())
1241 ImGui::SetTooltip("Playback speed (+/- keys)");
1242
1243 ImGui::SameLine();
1244 ImGui::Text(ICON_MD_VOLUME_UP);
1245 ImGui::SameLine();
1246 ImGui::SetNextItemWidth(60);
1247 if (gui::SliderIntWheel("##Vol", &current_volume, 0, 100, "%d%%", 5)) {
1248 if (music_player_)
1249 music_player_->SetVolume(current_volume / 100.0f);
1250 }
1251 if (ImGui::IsItemHovered())
1252 ImGui::SetTooltip("Volume");
1253
1254 ImGui::SameLine();
1255 const bool rom_loaded = rom_ && rom_->is_loaded();
1256 if (!rom_loaded) {
1257 ImGui::BeginDisabled();
1258 }
1259 if (ImGui::Button(ICON_MD_REFRESH)) {
1261 song_names_.clear();
1262 }
1263 if (!rom_loaded) {
1264 ImGui::EndDisabled();
1265 }
1266 if (ImGui::IsItemHovered())
1267 ImGui::SetTooltip("Reload from ROM");
1268
1269 // Interpolation Control
1270 ImGui::SameLine();
1271 ImGui::SetNextItemWidth(100);
1272 {
1273 static int interpolation_type = 2; // Default: Gaussian
1274 const char* items[] = {"Linear", "Hermite", "Gaussian", "Cosine", "Cubic"};
1275 if (ImGui::Combo("##Interp", &interpolation_type, items,
1276 IM_ARRAYSIZE(items))) {
1277 if (music_player_)
1278 music_player_->SetInterpolationType(interpolation_type);
1279 }
1280 if (ImGui::IsItemHovered())
1281 ImGui::SetTooltip(
1282 "Audio interpolation quality\nGaussian = authentic SNES sound");
1283 }
1284
1285 ImGui::Separator();
1286
1287 // Mixer / Visualizer Panel
1288 if (ImGui::BeginTable(
1289 "MixerPanel", 9,
1290 ImGuiTableFlags_Borders | ImGuiTableFlags_SizingStretchProp)) {
1291 // Channel Headers
1292 ImGui::TableSetupColumn("Master", ImGuiTableColumnFlags_WidthFixed, 60.0f);
1293 for (int i = 0; i < 8; i++) {
1294 ImGui::TableSetupColumn(absl::StrFormat("Ch %d", i + 1).c_str());
1295 }
1296 ImGui::TableHeadersRow();
1297
1298 ImGui::TableNextRow();
1299
1300 // Master Oscilloscope (Column 0)
1301 ImGui::TableSetColumnIndex(0);
1302 // Use MusicPlayer's emulator for visualization
1303 emu::Emulator* audio_emu =
1304 music_player_ ? music_player_->emulator() : nullptr;
1305 if (audio_emu && audio_emu->is_snes_initialized()) {
1306 auto& dsp = audio_emu->snes().apu().dsp();
1307
1308 ImGui::Text("Scope");
1309
1310 // Oscilloscope
1311 const int16_t* buffer = dsp.GetSampleBuffer();
1312 uint16_t offset = dsp.GetSampleOffset();
1313
1314 static float scope_values[128];
1315 // Handle ring buffer wrap-around correctly (buffer size is 0x400 samples)
1316 constexpr int kBufferSize = 0x400;
1317 for (int i = 0; i < 128; i++) {
1318 int sample_idx = ((offset - 128 + i + kBufferSize) & (kBufferSize - 1));
1319 scope_values[i] = static_cast<float>(buffer[sample_idx * 2]) /
1320 32768.0f; // Left channel
1321 }
1322
1323 ImGui::PlotLines("##Scope", scope_values, 128, 0, nullptr, -1.0f, 1.0f,
1324 ImVec2(50, 60));
1325 }
1326
1327 // Channel Strips (Columns 1-8)
1328 for (int i = 0; i < 8; i++) {
1329 ImGui::TableSetColumnIndex(i + 1);
1330
1331 if (audio_emu && audio_emu->is_snes_initialized()) {
1332 auto& dsp = audio_emu->snes().apu().dsp();
1333 const auto& ch = dsp.GetChannel(i);
1334
1335 // Mute/Solo Buttons
1336 bool is_muted = dsp.GetChannelMute(i);
1337 bool is_solo = channel_soloed_[i];
1338 const auto& theme = gui::ThemeManager::Get().GetCurrentTheme();
1339
1340 {
1341 std::optional<gui::StyleColorGuard> mute_guard;
1342 if (is_muted) {
1343 mute_guard.emplace(ImGuiCol_Button,
1344 gui::ConvertColorToImVec4(theme.error));
1345 }
1346 if (ImGui::Button(absl::StrFormat("M##%d", i).c_str(),
1347 ImVec2(25, 20))) {
1348 dsp.SetChannelMute(i, !is_muted);
1349 }
1350 }
1351
1352 ImGui::SameLine();
1353
1354 {
1355 std::optional<gui::StyleColorGuard> solo_guard;
1356 if (is_solo) {
1357 solo_guard.emplace(ImGuiCol_Button,
1358 gui::ConvertColorToImVec4(theme.warning));
1359 }
1360 if (ImGui::Button(absl::StrFormat("S##%d", i).c_str(),
1361 ImVec2(25, 20))) {
1363
1364 bool any_solo = false;
1365 for (int j = 0; j < 8; j++)
1366 if (channel_soloed_[j])
1367 any_solo = true;
1368
1369 for (int j = 0; j < 8; j++) {
1370 if (any_solo) {
1371 dsp.SetChannelMute(j, !channel_soloed_[j]);
1372 } else {
1373 dsp.SetChannelMute(j, false);
1374 }
1375 }
1376 }
1377 }
1378
1379 // VU Meter
1380 float level = std::abs(ch.sampleOut) / 32768.0f;
1381 ImGui::ProgressBar(level, ImVec2(-1, 60), "");
1382
1383 // Info
1384 ImGui::Text("Vol: %d %d", ch.volumeL, ch.volumeR);
1385 ImGui::Text("Pitch: %04X", ch.pitch);
1386
1387 // Key On Indicator
1388 if (ch.keyOn) {
1389 ImGui::TextColored(gui::ConvertColorToImVec4(theme.success),
1390 "KEY ON");
1391 } else {
1392 ImGui::TextDisabled("---");
1393 }
1394 } else {
1395 ImGui::TextDisabled("Offline");
1396 }
1397 }
1398
1399 ImGui::EndTable();
1400 }
1401
1402 // Quick audio status (detailed debug in Audio Debug panel)
1403 if (ImGui::CollapsingHeader(ICON_MD_BUG_REPORT " Audio Status")) {
1404 emu::Emulator* debug_emu =
1405 music_player_ ? music_player_->emulator() : nullptr;
1406 if (debug_emu && debug_emu->is_snes_initialized()) {
1407 auto* audio_backend = debug_emu->audio_backend();
1408 if (audio_backend) {
1409 auto status = audio_backend->GetStatus();
1410 auto config = audio_backend->GetConfig();
1411 bool resampling = audio_backend->IsAudioStreamEnabled();
1412
1413 // Compact status line
1414 ImGui::Text("Backend: %s @ %dHz | Queue: %u frames",
1415 audio_backend->GetBackendName().c_str(), config.sample_rate,
1416 status.queued_frames);
1417
1418 // Resampling indicator with warning if disabled
1419 if (resampling) {
1420 ImGui::TextColored(gui::GetSuccessColor(),
1421 "Resampling: 32040 -> %d Hz", config.sample_rate);
1422 } else {
1423 ImGui::TextColored(gui::GetErrorColor(), ICON_MD_WARNING
1424 " Resampling DISABLED - 1.5x speed bug!");
1425 }
1426
1427 if (status.has_underrun) {
1428 ImGui::TextColored(gui::GetWarningColor(),
1429 ICON_MD_WARNING " Buffer underrun");
1430 }
1431
1432 ImGui::TextDisabled("Open Audio Debug panel for full diagnostics");
1433 }
1434 } else {
1435 ImGui::TextDisabled("Play a song to see audio status");
1436 }
1437 }
1438}
1439
1441 if (!music_player_) {
1442 ImGui::TextDisabled("Music player not initialized");
1443 return;
1444 }
1445
1446 // Check if audio emulator is initialized (created on first play)
1447 auto* audio_emu = music_player_->emulator();
1448 if (!audio_emu || !audio_emu->is_snes_initialized()) {
1449 ImGui::TextDisabled("Play a song to see channel activity");
1450 return;
1451 }
1452
1453 // Check available space to avoid ImGui table assertion
1454 ImVec2 avail = ImGui::GetContentRegionAvail();
1455 if (avail.y < 50.0f) {
1456 ImGui::TextDisabled("(Channel view - expand for details)");
1457 return;
1458 }
1459
1460 auto channel_states = music_player_->GetChannelStates();
1461
1462 if (ImGui::BeginTable(
1463 "ChannelOverview", 9,
1464 ImGuiTableFlags_Borders | ImGuiTableFlags_SizingStretchProp)) {
1465 ImGui::TableSetupColumn("Master", ImGuiTableColumnFlags_WidthFixed, 70.0f);
1466 for (int i = 0; i < 8; i++) {
1467 ImGui::TableSetupColumn(absl::StrFormat("Ch %d", i + 1).c_str());
1468 }
1469 ImGui::TableHeadersRow();
1470
1471 ImGui::TableNextRow();
1472
1473 ImGui::TableSetColumnIndex(0);
1474 ImGui::Text("DSP Live");
1475
1476 for (int ch = 0; ch < 8; ++ch) {
1477 ImGui::TableSetColumnIndex(ch + 1);
1478 const auto& state = channel_states[ch];
1479
1480 // Visual indicator for Key On
1481 if (state.key_on) {
1482 ImGui::TextColored(gui::GetSuccessColor(), "ON");
1483 } else {
1484 ImGui::TextDisabled("OFF");
1485 }
1486
1487 // Volume bars
1488 float vol_l = state.volume_l / 128.0f;
1489 float vol_r = state.volume_r / 128.0f;
1490 ImGui::ProgressBar(vol_l, ImVec2(-1, 6.0f), "");
1491 ImGui::ProgressBar(vol_r, ImVec2(-1, 6.0f), "");
1492
1493 // Info
1494 ImGui::Text("S: %02X", state.sample_index);
1495 ImGui::Text("P: %04X", state.pitch);
1496
1497 // ADSR State
1498 const char* adsr_str = "???";
1499 switch (state.adsr_state) {
1500 case 0:
1501 adsr_str = "Att";
1502 break;
1503 case 1:
1504 adsr_str = "Dec";
1505 break;
1506 case 2:
1507 adsr_str = "Sus";
1508 break;
1509 case 3:
1510 adsr_str = "Rel";
1511 break;
1512 }
1513 ImGui::Text("%s", adsr_str);
1514 }
1515
1516 ImGui::EndTable();
1517 }
1518}
1519
1520// ============================================================================
1521// Audio Control Methods (Emulator Integration)
1522// ============================================================================
1523
1524void MusicEditor::SeekToSegment(int segment_index) {
1525 if (music_player_)
1526 music_player_->SeekToSegment(segment_index);
1527}
1528
1529// ============================================================================
1530// ASM Export/Import
1531// ============================================================================
1532
1533void MusicEditor::ExportSongToAsm(int song_index) {
1534 auto* song = music_bank_.GetSong(song_index);
1535 if (!song) {
1536 LOG_WARN("MusicEditor", "ExportSongToAsm: Invalid song index %d",
1537 song_index);
1538 return;
1539 }
1540
1541 // Configure export options
1543 options.label_prefix = song->name;
1544 // Remove spaces and special characters from label
1545 std::replace(options.label_prefix.begin(), options.label_prefix.end(), ' ',
1546 '_');
1547 options.include_comments = true;
1548 options.use_instrument_macros = true;
1549
1550 // Set ARAM address based on bank
1551 if (music_bank_.IsExpandedSong(song_index)) {
1553 } else {
1555 }
1556
1557 // Export to string
1559 auto result = exporter.ExportSong(*song, options);
1560 if (!result.ok()) {
1561 LOG_ERROR("MusicEditor", "ExportSongToAsm failed: %s",
1562 result.status().message().data());
1563 return;
1564 }
1565
1566 // For now, copy to assembly editor buffer
1567 // TODO: Add native file dialog for export path selection
1568 asm_buffer_ = *result;
1570
1571 LOG_INFO("MusicEditor", "Exported song '%s' to ASM (%zu bytes)",
1572 song->name.c_str(), asm_buffer_.size());
1573}
1574
1576 asm_import_target_index_ = song_index;
1577
1578 // If no source is present, open the import dialog for user input
1579 if (asm_buffer_.empty()) {
1580 LOG_INFO("MusicEditor", "No ASM source to import - showing import dialog");
1581 asm_import_error_.clear();
1583 return;
1584 }
1585
1586 // Attempt immediate import using existing buffer
1587 if (!ImportAsmBufferToSong(song_index)) {
1589 return;
1590 }
1591
1592 show_asm_import_popup_ = false;
1594}
1595
1597 auto* song = music_bank_.GetSong(song_index);
1598 if (!song) {
1599 asm_import_error_ = absl::StrFormat("Invalid song index %d", song_index);
1600 LOG_WARN("MusicEditor", "%s", asm_import_error_.c_str());
1601 return false;
1602 }
1603
1604 // Configure import options
1606 options.strict_mode = false;
1607 options.verbose_errors = true;
1608
1609 // Parse the ASM source
1611 auto result = importer.ImportSong(asm_buffer_, options);
1612 if (!result.ok()) {
1613 const auto message = result.status().message();
1614 asm_import_error_.assign(message.data(), message.size());
1615 LOG_ERROR("MusicEditor", "ImportSongFromAsm failed: %s",
1616 asm_import_error_.c_str());
1617 return false;
1618 }
1619
1620 // Log any warnings
1621 for (const auto& warning : result->warnings) {
1622 LOG_WARN("MusicEditor", "ASM import warning: %s", warning.c_str());
1623 }
1624
1625 // Capture undo snapshot before mutating the song.
1626 PushUndoState(song_index);
1627
1628 // Copy parsed song data to target song
1629 // Keep original name if import didn't provide one
1630 std::string original_name = song->name;
1631 *song = result->song;
1632 if (song->name.empty()) {
1633 song->name = original_name;
1634 }
1635 song->modified = true;
1636
1637 LOG_INFO("MusicEditor", "Imported ASM to song '%s' (%d lines, %d bytes)",
1638 song->name.c_str(), result->lines_parsed, result->bytes_generated);
1639
1640 asm_import_error_.clear();
1641 return true;
1642}
1643
1644// ============================================================================
1645// Custom Song Preview (In-Memory Playback)
1646// ============================================================================
1647
1650 ImGui::OpenPopup("Export Song ASM");
1651 show_asm_export_popup_ = false;
1652 }
1654 ImGui::OpenPopup("Import Song ASM");
1655 // Keep flag true until user closes
1656 }
1657
1658 if (ImGui::BeginPopupModal("Export Song ASM", nullptr,
1659 ImGuiWindowFlags_AlwaysAutoResize)) {
1660 ImGui::TextWrapped("Copy the generated ASM below or tweak before saving.");
1661 ImGui::InputTextMultiline("##AsmExportText", &asm_buffer_, ImVec2(520, 260),
1662 ImGuiInputTextFlags_AllowTabInput);
1663
1664 if (ImGui::Button("Copy to Clipboard")) {
1665 ImGui::SetClipboardText(asm_buffer_.c_str());
1666 }
1667 ImGui::SameLine();
1668 if (ImGui::Button("Close")) {
1669 ImGui::CloseCurrentPopup();
1670 }
1671
1672 ImGui::EndPopup();
1673 }
1674
1675 if (ImGui::BeginPopupModal("Import Song ASM", nullptr,
1676 ImGuiWindowFlags_AlwaysAutoResize)) {
1677 int song_slot =
1679 if (song_slot > 0) {
1680 ImGui::Text("Target Song: [%02X]", song_slot);
1681 } else {
1682 ImGui::TextDisabled("Select a song to import into");
1683 }
1684 ImGui::TextWrapped("Paste Oracle of Secrets-compatible ASM here.");
1685
1686 ImGui::InputTextMultiline("##AsmImportText", &asm_buffer_, ImVec2(520, 260),
1687 ImGuiInputTextFlags_AllowTabInput);
1688
1689 if (!asm_import_error_.empty()) {
1690 gui::StyleColorGuard error_text_guard(ImGuiCol_Text,
1692 ImGui::TextWrapped("%s", asm_import_error_.c_str());
1693 }
1694
1695 bool can_import = asm_import_target_index_ >= 0 && !asm_buffer_.empty();
1696 if (!can_import) {
1697 ImGui::BeginDisabled();
1698 }
1699 if (ImGui::Button("Import")) {
1701 show_asm_import_popup_ = false;
1703 ImGui::CloseCurrentPopup();
1704 }
1705 }
1706 if (!can_import) {
1707 ImGui::EndDisabled();
1708 }
1709
1710 ImGui::SameLine();
1711 if (ImGui::Button("Cancel")) {
1712 asm_import_error_.clear();
1713 show_asm_import_popup_ = false;
1715 ImGui::CloseCurrentPopup();
1716 }
1717
1718 ImGui::EndPopup();
1719 } else if (!show_asm_import_popup_) {
1720 // Clear stale error when popup is closed
1721 asm_import_error_.clear();
1722 }
1723}
1724
1725} // namespace editor
1726} // namespace yaze
bool is_loaded() const
Definition rom.h:132
UndoManager undo_manager_
Definition editor.h:307
virtual void SetDependencies(const EditorDependencies &deps)
Definition editor.h:241
EditorDependencies dependencies_
Definition editor.h:306
std::unordered_map< int, std::unique_ptr< editor::music::TrackerView > > song_trackers_
void FocusSong(int song_index)
std::unique_ptr< emu::audio::IAudioBackend > audio_backend_
ImVector< int > active_songs_
std::vector< bool > channel_soloed_
void SlowDown(float delta=0.1f)
void DrawSongTrackerWindow(int song_index)
emu::Emulator * emulator_
std::unordered_map< int, std::shared_ptr< gui::PanelWindow > > song_cards_
std::optional< zelda3::music::MusicSong > pending_undo_before_
absl::Status Paste() override
zelda3::music::MusicBank music_bank_
std::unordered_map< int, SongPianoRollWindow > song_piano_rolls_
void SetProject(project::YazeProject *project)
void Initialize() override
void OpenSong(int song_index)
emu::Emulator * emulator() const
void ExportSongToAsm(int song_index)
editor::music::SampleEditorView sample_editor_view_
absl::Status Save() override
absl::Status Cut() override
void OpenSongPianoRoll(int song_index)
absl::Status Load() override
void SetDependencies(const EditorDependencies &deps) override
void ImportSongFromAsm(int song_index)
absl::StatusOr< bool > RestoreMusicState()
absl::Status Copy() override
void SpeedUp(float delta=0.1f)
absl::Status PersistMusicState(const char *reason=nullptr)
absl::Status Update() override
editor::music::InstrumentEditorView instrument_editor_view_
std::unique_ptr< editor::music::MusicPlayer > music_player_
void set_emulator(emu::Emulator *emulator)
absl::Status Undo() override
absl::Status Redo() override
std::vector< std::string > song_names_
std::chrono::steady_clock::time_point last_music_persist_
AssemblyEditor assembly_editor_
bool ImportAsmBufferToSong(int song_index)
project::YazeProject * project_
void SeekToSegment(int segment_index)
editor::music::TrackerView tracker_view_
editor::music::SongBrowserView song_browser_view_
editor::music::PianoRollView piano_roll_view_
ImGuiWindowClass song_window_class_
bool * GetVisibilityFlag(size_t session_id, const std::string &base_card_id)
bool ShowPanel(size_t session_id, const std::string &base_card_id)
void RegisterPanel(size_t session_id, const PanelDescriptor &base_info)
std::string GetActiveCategory() const
bool IsPanelVisible(size_t session_id, const std::string &base_card_id) const
void UnregisterPanel(size_t session_id, const std::string &base_card_id)
bool IsPanelPinned(size_t session_id, const std::string &base_card_id) const
void Push(std::unique_ptr< UndoAction > action)
absl::Status Redo()
Redo the top action. Returns error if stack is empty.
absl::Status Undo()
Undo the top action. Returns error if stack is empty.
void Draw(MusicBank &bank)
Draw the instrument editor.
void SetPlaybackState(bool is_playing, bool is_paused, uint32_t current_tick)
void Draw(zelda3::music::MusicSong *song, const zelda3::music::MusicBank *bank=nullptr)
Draw the piano roll view for the given song.
void SetOnNotePreview(std::function< void(const zelda3::music::TrackEvent &, int, int)> callback)
Set callback for note preview.
void SetOnEditCallback(std::function< void()> callback)
Set callback for when edits occur.
void SetOnSegmentPreview(std::function< void(const zelda3::music::MusicSong &, int)> callback)
Set callback for segment preview.
void Draw(MusicBank &bank)
Draw the sample editor.
void Draw(MusicBank &bank)
Draw the song browser.
void Draw(MusicSong *song, const MusicBank *bank=nullptr)
Draw the tracker view for the given song.
A class for emulating and debugging SNES games.
Definition emulator.h:40
void SetExternalAudioBackend(audio::IAudioBackend *backend)
Definition emulator.h:82
bool is_snes_initialized() const
Definition emulator.h:127
audio::IAudioBackend * audio_backend()
Definition emulator.h:75
auto snes() -> Snes &
Definition emulator.h:59
static std::unique_ptr< IAudioBackend > Create(BackendType type)
virtual AudioStatus GetStatus() const =0
RAII timer for automatic timing management.
RAII guard for ImGui style colors.
Definition style_guard.h:27
const Theme & GetCurrentTheme() const
static ThemeManager & Get()
Exports MusicSong to Oracle of Secrets music_macros.asm format.
absl::StatusOr< std::string > ExportSong(const MusicSong &song, const AsmExportOptions &options)
Export a song to ASM string.
Imports music_macros.asm format files into MusicSong.
absl::StatusOr< AsmParseResult > ImportSong(const std::string &asm_source, const AsmImportOptions &options)
Import a song from ASM string.
bool HasModifications() const
Check if any music data has been modified.
nlohmann::json ToJson() const
absl::Status LoadFromJson(const nlohmann::json &j)
MusicSong * GetSong(int index)
Get a song by index.
size_t GetSongCount() const
Get the number of songs loaded.
Definition music_bank.h:97
absl::Status SaveToRom(Rom &rom)
Save all modified music data back to ROM.
bool IsExpandedSong(int index) const
Check if a song is from an expanded bank.
absl::Status LoadFromRom(Rom &rom)
Load all music data from a ROM.
#define ICON_MD_PAUSE_CIRCLE
Definition icons.h:1390
#define ICON_MD_PAUSE
Definition icons.h:1389
#define ICON_MD_PIANO
Definition icons.h:1462
#define ICON_MD_LIBRARY_MUSIC
Definition icons.h:1080
#define ICON_MD_WAVES
Definition icons.h:2133
#define ICON_MD_WARNING
Definition icons.h:2123
#define ICON_MD_VOLUME_UP
Definition icons.h:2111
#define ICON_MD_PLAY_ARROW
Definition icons.h:1479
#define ICON_MD_REFRESH
Definition icons.h:1572
#define ICON_MD_CODE
Definition icons.h:434
#define ICON_MD_STOP
Definition icons.h:1862
#define ICON_MD_BUG_REPORT
Definition icons.h:327
#define ICON_MD_GRAPHIC_EQ
Definition icons.h:890
#define ICON_MD_EDIT
Definition icons.h:645
#define ICON_MD_SPEED
Definition icons.h:1817
#define ICON_MD_MUSIC_NOTE
Definition icons.h:1264
#define ICON_MD_KEYBOARD
Definition icons.h:1028
#define ICON_MD_SPEAKER
Definition icons.h:1812
#define ICON_MD_PLAY_CIRCLE
Definition icons.h:1480
#define ICON_MD_OPEN_IN_NEW
Definition icons.h:1354
#define LOG_DEBUG(category, format,...)
Definition log.h:103
#define LOG_ERROR(category, format,...)
Definition log.h:109
#define LOG_WARN(category, format,...)
Definition log.h:107
#define LOG_INFO(category, format,...)
Definition log.h:105
ImVec4 ConvertColorToImVec4(const Color &color)
Definition color.h:134
bool SliderIntWheel(const char *label, int *v, int v_min, int v_max, const char *format, int wheel_step, ImGuiSliderFlags flags)
Definition input.cc:765
ButtonColorSet GetWarningButtonColors()
ImVec4 GetSuccessColor()
Definition ui_helpers.cc:48
bool SliderFloatWheel(const char *label, float *v, float v_min, float v_max, const char *format, float wheel_step, ImGuiSliderFlags flags)
Definition input.cc:749
ButtonColorSet GetSuccessButtonColors()
ImVec4 GetDisabledColor()
Definition ui_helpers.cc:73
ImVec4 GetErrorColor()
Definition ui_helpers.cc:58
ImVec4 GetWarningColor()
Definition ui_helpers.cc:53
ImVec4 GetInfoColor()
Definition ui_helpers.cc:63
constexpr uint16_t kAuxSongTableAram
Definition song_data.h:140
constexpr uint16_t kSongTableAram
Definition song_data.h:82
#define RETURN_IF_ERROR(expr)
Definition snes.cc:22
Unified dependency container for all editor types.
Definition editor.h:163
project::YazeProject * project
Definition editor.h:167
std::unique_ptr< editor::music::PianoRollView > view
std::shared_ptr< gui::PanelWindow > card
Represents the current playback state of the music player.
Modern project structure with comprehensive settings consolidation.
Definition project.h:120
std::string MakeStorageKey(absl::string_view suffix) const
Definition project.cc:452
struct yaze::project::YazeProject::MusicPersistence music_persistence
Options for ASM export in music_macros.asm format.
Options for ASM import from music_macros.asm format.
A complete song composed of segments.
Definition song_data.h:334
A single event in a music track (note, command, or control).
Definition song_data.h:247