Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions engine/player/PlayerLibraryPolicy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// SPDX-License-Identifier: AGPL-3.0-only
#pragma once
#include <juce_core/juce_core.h>
#include <algorithm>
#include <cstddef>
#include <vector>

namespace armix::player {
// FILE/WORKER only. No audio callback, network, plugin or device access.
// Folder import is a bounded, naturally ordered, user-directed action.
struct PlayerLibraryPolicy final {
static constexpr std::size_t maxSongs=2000;
static constexpr std::size_t maxEntriesInspected=20000;
static bool supported(const juce::File& file) {
return file.hasFileExtension("wav;mp3;flac;aif;aiff;ogg");
}
static bool sameSongName(const juce::File& saved,const juce::File& chosen) {
return supported(chosen) && chosen.existsAsFile() &&
saved.getFileNameWithoutExtension().isNotEmpty() &&
saved.getFileNameWithoutExtension().equalsIgnoreCase(
chosen.getFileNameWithoutExtension());
}
struct Scan {
std::vector<juce::File> files;
bool truncated=false;
};
struct SafeRelink {
std::size_t index=0;
juce::File replacement;
};
// CONTROL thread after scan worker completes. A chosen folder is explicit
// permission to search, NOT permission to guess song identity. Relink only
// an unambiguous full filename (including extension), never a stem match,
// never a path already owned by another track, and never CURRENT live deck.
// Preserve the original row, per-song KEY/TEMPO, and selection.
static std::vector<SafeRelink> planMissingRelinks(
const std::vector<juce::File>& library,
const std::vector<juce::File>& scanned,
int protectedCurrent=-1) {
std::vector<SafeRelink> plan;
for(std::size_t i=0;i<library.size();++i) {
const auto& old=library[i];
if(static_cast<int>(i)==protectedCurrent || old.existsAsFile())
continue;
const auto filename=old.getFileName();
if(filename.isEmpty() || !supported(old))continue;
const juce::File* match=nullptr;
bool ambiguous=false;
for(const auto& candidate:scanned) {
if(!candidate.getFileName().equalsIgnoreCase(filename))
continue;
if(!supported(candidate) || !candidate.existsAsFile())
continue;
if(match) {ambiguous=true;break;}
match=&candidate;
}
if(!match || ambiguous)continue;
// An existing song or second missing row must not be silently
// mapped to one physical file. Require one-to-one ownership.
const auto alreadyOwned=std::any_of(library.begin(),library.end(),
[&](const juce::File& entry) {return entry==*match;});
if(alreadyOwned)continue;
const auto duplicateMissing=std::count_if(library.begin(),library.end(),
[&](const juce::File& entry) {
return !entry.existsAsFile() &&
entry.getFileName().equalsIgnoreCase(filename);
});
if(duplicateMissing!=1)continue;
plan.push_back({i,*match});
}
return plan;
}
static Scan scanFolder(const juce::File& folder) {
Scan result;
if(!folder.isDirectory())return result;
// Iterates lazily and stops at finite bounds, including deep folders.
// Executed by the import worker, never by the WebView message loop.
juce::DirectoryIterator files(folder,true,"*",juce::File::findFiles);
std::size_t inspected=0;
while(files.next()) {
if(++inspected>maxEntriesInspected) {
result.truncated=true;break;
}
const auto file=files.getFile();
if(!supported(file))continue;
if(result.files.size()>=maxSongs) {
result.truncated=true;break;
}
result.files.push_back(file);
}
std::sort(result.files.begin(),result.files.end(),
[](const juce::File& a,const juce::File& b) {
const int c=a.getFileName().compareNatural(b.getFileName(),false);
if(c!=0)return c<0;
return a.getFullPathName().compareIgnoreCase(
b.getFullPathName())<0;
});
result.files.erase(std::unique(result.files.begin(),result.files.end()),
result.files.end());
return result;
}
};
} // namespace armix::player
12 changes: 10 additions & 2 deletions site/preview-v2/assets/arson-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
toggle.addEventListener("click",()=>open(root.hidden));
$("playerClose").addEventListener("click",()=>open(false));
$("playerAdd").addEventListener("click",()=>send("playerAdd"));
$("playerAddFolder").addEventListener("click",()=>send("playerAddFolder"));
$("playerRelink").addEventListener("click",()=>send("playerRelink"));
$("playerPlay").addEventListener("click",()=>{
send(model.transport?.state==="PLAYING"?"playerPause":"playerPlay");
});
Expand Down Expand Up @@ -144,11 +146,17 @@
"Music playing · output muted",
ERROR:"Unable to decode this file"
}[state]||"");
const indicator=(t.processingChangePending && !model.error && !t.error &&
const indicator=model.libraryBusy && !model.error?
"Adding songs from folder…":
(t.processingChangePending && !model.error && !t.error &&
state!=="EMPTY" && state!=="ERROR")
?"Preparing music KEY/TEMPO in decoder…":detail;
?"Preparing music KEY/TEMPO in decoder…":detail;
showFeedback(indicator,Boolean(model.error||t.error));
$("playerChooseOutput").hidden=!native || !Boolean(model.chooseOutput);
const missing=model.tracks.find(x=>x.id===model.selected)?.missing;
$("playerRelink").hidden=!native || !missing;
$("playerAddFolder").disabled=Boolean(model.libraryBusy);
$("playerAdd").disabled=Boolean(model.libraryBusy);
$("playerCount").textContent=model.tracks.length+" songs";
renderTracks();
}
Expand Down
17 changes: 16 additions & 1 deletion site/preview-v2/assets/mixer-contextual-buttons.css
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ body :is(.mx-peq-global,.mx-comp-power,.mx-harm-power):focus-visible{outline:2px
#arsonPlayer .ap-listhead{display:flex;align-items:center;gap:8px;
padding:10px 13px;border-top:1px solid #273a43;border-bottom:1px solid #273a43}
#arsonPlayer .ap-listhead strong{font-size:12px;font-weight:550}
#arsonPlayer #playerAdd{margin-left:auto;border:1px solid #41616a;
#arsonPlayer #playerAdd{margin-left:0;white-space:nowrap;border:1px solid #41616a;
border-radius:6px;padding:6px 10px;background:#223f47;color:#bfe9e6}
#arsonPlayer #playerSearch{width:calc(100% - 26px);margin:9px 13px;
padding:8px 10px;border-radius:7px;background:#0f1b22;
Expand Down Expand Up @@ -322,3 +322,18 @@ body :is(.mx-peq-global,.mx-comp-power,.mx-harm-power):focus-visible{outline:2px
#arsonPlayer #playerTempoValue{width:36px}
#arsonPlayer .ap-reset{padding:5px}
}

/* M3 smart library controls: lightweight, keyboard-accessible icon and
one contextual Locate file action. No additional cards or UI modes. */
#arsonPlayer #playerAddFolder{margin-left:auto;display:inline-flex;
align-items:center;justify-content:center;width:29px;height:29px;
flex:0 0 29px;border:1px solid #45616a;border-radius:6px;
background:#223941;color:#a7d4d2}
#arsonPlayer #playerAddFolder:hover{background:#2c5058}
#arsonPlayer #playerRelink{border:1px solid #41616a;
border-radius:6px;padding:5px 8px;background:#223f47;
color:#bfe9e6;white-space:nowrap;font-size:11px}
#arsonPlayer #playerRelink:hover{background:#2b5761}
#arsonPlayer #playerRelink[hidden]{display:none!important}
#arsonPlayer #playerAdd:disabled,
#arsonPlayer #playerAddFolder:disabled{opacity:.5;cursor:wait}
4 changes: 2 additions & 2 deletions site/preview-v2/assets/mixer-contextual-buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
<div class="ap-pitchctrl"><span>TEMPO</span><button id="playerTempoMinus" type="button" aria-label="Slow music by five percent">−</button><output id="playerTempoValue" aria-live="polite">100%</output><button id="playerTempoPlus" type="button" aria-label="Speed music by five percent">+</button></div>
<button class="ap-reset" id="playerPitchReset" type="button" title="Reset key and tempo for current song">Reset</button>
</div>
<div class="ap-statusbar"><div id="playerFeedback" class="ap-feedback" role="status" aria-live="polite">Add music to begin</div><button id="playerChooseOutput" type="button" hidden>Choose output</button></div>
<div class="ap-listhead"><strong>Setlist</strong><span id="playerCount" class="ap-sub">0 songs</span><button id="playerAdd" type="button">+ Add Music</button></div>
<div class="ap-statusbar"><div id="playerFeedback" class="ap-feedback" role="status" aria-live="polite">Add music to begin</div><button id="playerChooseOutput" type="button" hidden>Choose output</button><button id="playerRelink" type="button" hidden>Locate file</button></div>
<div class="ap-listhead"><strong>Setlist</strong><span id="playerCount" class="ap-sub">0 songs</span><button id="playerAddFolder" class="ap-folder" type="button" aria-label="Add all music from a folder" title="Add music folder"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linejoin="round" stroke-linecap="round" aria-hidden="true"><path d="M3 7a2 2 0 0 1 2-2h5l2 2h7a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V7z"/></svg></button><button id="playerAdd" type="button">+ Add Music</button></div>
<input id="playerSearch" type="search" placeholder="Search songs" aria-label="Search music in setlist" autocomplete="off">
<div id="playerList" role="listbox" aria-label="Setlist songs"></div>
<div class="ap-foot">Click to prepare · Double-click to play · Native audio through your mixer</div>`;
Expand Down
137 changes: 137 additions & 0 deletions src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
#include <functional>
#include <vector>
#include "../engine/player/PlayerSetlistPolicy.h"
#include "../engine/player/PlayerLibraryPolicy.h"
#include <juce_core/juce_core.h>
#include <thread>

namespace
{
Expand Down Expand Up @@ -382,6 +384,7 @@ class ConsoleWindow final : public juce::DocumentWindow, private juce::Timer, pr
int playerSelected_=-1,playerCurrent_=-1;
int playerOutputPendingIndex_=-1; // only after explicit Choose output gesture
bool playerAutoNext_=false;
bool playerFolderBusy_=false; // GUI only; background scanner never touches this
std::uint64_t lastAutoAdvanceGeneration_=0;
juce::String playerError_;
juce::File lastPluginFolder_ = juce::File::getSpecialLocation (juce::File::userHomeDirectory);
Expand Down Expand Up @@ -2448,6 +2451,7 @@ class ConsoleWindow final : public juce::DocumentWindow, private juce::Timer, pr
obj->setProperty("selected",playerSelected_);
obj->setProperty("current",playerCurrent_);
obj->setProperty("autoNext",playerAutoNext_);
obj->setProperty("libraryBusy",playerFolderBusy_);
obj->setProperty("error",playerError_);
obj->setProperty("chooseOutput",
playerError_.containsIgnoreCase("Choose a verified MAIN output") ||
Expand Down Expand Up @@ -2513,6 +2517,137 @@ class ConsoleWindow final : public juce::DocumentWindow, private juce::Timer, pr
});
});
}
void choosePlayerFolder() {
if(playerFileChooser_ || playerFolderBusy_)return;
playerFileChooser_=std::make_unique<juce::FileChooser>(
"Add Music Folder",
juce::File::getSpecialLocation(juce::File::userMusicDirectory),
"*",true);
playerFileChooser_->launchAsync(
juce::FileBrowserComponent::openMode |
juce::FileBrowserComponent::canSelectDirectories,
[safe=juce::Component::SafePointer<ConsoleWindow>(this)]
(const juce::FileChooser& picker) {
if(!safe)return;
const auto folder=picker.getResult();
// FileChooser must not be destroyed inside its callback.
juce::MessageManager::callAsync([safe] {
if(safe)safe->playerFileChooser_.reset();
});
if(!folder.isDirectory())return;
safe->playerFolderBusy_=true;
safe->playerError_.clear();
safe->emitPlayerState();
// Only filesystem enumeration, bounded in scope and output,
// is performed on the worker. Never block the JUCE message
// loop or ASIO callback while importing a large USB folder.
// Copy immutable IDs/paths on GUI thread; expensive USB
// existence checks and relink planning belong on the worker.
const auto initialLibrary=safe->playerLibrary_;
const int protectedCurrent=safe->playerCurrent_;
std::thread([safe,folder,initialLibrary,protectedCurrent] {
auto scan=armix::player::PlayerLibraryPolicy::scanFolder(folder);
auto relinks=armix::player::PlayerLibraryPolicy::planMissingRelinks(
initialLibrary,scan.files,protectedCurrent);
juce::MessageManager::callAsync(
[safe,scan=std::move(scan),
relinks=std::move(relinks),initialLibrary]() mutable {
if(!safe)return;
safe->playerFolderBusy_=false;
int added=0,relinked=0;
// Update only the exact rows the worker saw.
// Edits made during the scan must remain intact.
for(const auto& change:relinks) {
if(change.index>=initialLibrary.size() ||
change.index>=safe->playerLibrary_.size() ||
static_cast<int>(change.index)==safe->playerCurrent_ ||
safe->playerLibrary_[change.index]!=
initialLibrary[change.index] ||
std::find(safe->playerLibrary_.begin(),
safe->playerLibrary_.end(),
change.replacement)!=safe->playerLibrary_.end())
continue;
safe->playerLibrary_[change.index]=change.replacement;
++relinked; // keep stable IDs + KEY/TEMPO
}
int first=-1;
for(const auto& file:scan.files) {
const auto found=std::find(
safe->playerLibrary_.begin(),
safe->playerLibrary_.end(),file);
if(found!=safe->playerLibrary_.end())continue;
if(safe->playerLibrary_.size()>=
armix::player::PlayerLibraryPolicy::maxSongs)break;
if(first<0)
first=static_cast<int>(safe->playerLibrary_.size());
safe->playerLibrary_.push_back(file);
safe->playerTrackSettings_.push_back({});
++added;
}
if(safe->playerSelected_<0 && first>=0)
safe->playerSelected_=first;
safe->playerError_=scan.truncated?
"Folder scan reached its safe size limit; only some songs were found":
added==0 && relinked==0?
"No new supported songs or safe missing-file matches found":
juce::String{};
safe->persistPlayerLibrary();
safe->emitPlayerState(true);
});
}).detach();
});
}

void choosePlayerRelink() {
if(playerFileChooser_ || playerFolderBusy_)return;
const int index=playerSelected_;
if(index<0 || index>=static_cast<int>(playerLibrary_.size()))return;
const auto old=playerLibrary_[static_cast<std::size_t>(index)];
if(old.existsAsFile())return; // only resolve genuinely missing music
playerFileChooser_=std::make_unique<juce::FileChooser>(
"Locate Missing Music — "+old.getFileName(),
old.getParentDirectory().isDirectory()?
old.getParentDirectory():
juce::File::getSpecialLocation(juce::File::userMusicDirectory),
"*.wav;*.mp3;*.flac;*.aif;*.aiff;*.ogg",true);
playerFileChooser_->launchAsync(
juce::FileBrowserComponent::openMode |
juce::FileBrowserComponent::canSelectFiles,
[safe=juce::Component::SafePointer<ConsoleWindow>(this),index,old]
(const juce::FileChooser& picker) {
if(!safe)return;
const auto chosen=picker.getResult();
juce::MessageManager::callAsync([safe] {
if(safe)safe->playerFileChooser_.reset();
});
if(chosen==juce::File{})return;
if(index>=static_cast<int>(safe->playerLibrary_.size()) ||
safe->playerLibrary_[static_cast<std::size_t>(index)]!=old) {
safe->playerError_="Setlist changed; locate the song again";
} else if(!armix::player::PlayerLibraryPolicy::sameSongName(
old,chosen)) {
safe->playerError_="Choose the same song filename to avoid relinking the wrong music";
} else if(std::find(safe->playerLibrary_.begin(),
safe->playerLibrary_.end(),chosen)!=
safe->playerLibrary_.end()) {
safe->playerError_="This music file is already in your setlist";
} else {
// Preserve same logical song ID, order and per-song
// KEY/TEMPO. Never start a song or switch the output
// as a side effect of locating a file.
if(safe->playerCurrent_==index) {
safe->engine_.playerStop();
safe->playerCurrent_=-1;
}
safe->playerLibrary_[static_cast<std::size_t>(index)]=chosen;
safe->playerOutputPendingIndex_=-1;
safe->playerError_.clear();
safe->persistPlayerLibrary();
}
safe->emitPlayerState(true);
});
}

void playTransportMusic() {
const auto phase=engine_.playerStatus().getProperty(
"state","EMPTY").toString().toStdString();
Expand Down Expand Up @@ -2588,6 +2723,8 @@ class ConsoleWindow final : public juce::DocumentWindow, private juce::Timer, pr
}
if(action=="playerSubscribe")return emitPlayerState(true);
if(action=="playerAdd")return choosePlayerFiles();
if(action=="playerAddFolder")return choosePlayerFolder();
if(action=="playerRelink")return choosePlayerRelink();
if(action=="playerKeyStep" || action=="playerTempoStep" ||
action=="playerPitchTempoReset") {
const auto target=armix::player::SetlistPolicy::pitchTempoTarget(
Expand Down
Loading
Loading