Skip to content

Commit

Permalink
Add action for creating new SFZ
Browse files Browse the repository at this point in the history
  • Loading branch information
jpcima committed Feb 22, 2021
1 parent 8de978d commit fe2bc7c
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions plugins/editor/src/editor/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ struct Editor::Impl : EditorController::Receiver, IControlListener {
enum {
kTagLoadSfzFile,
kTagEditSfzFile,
kTagCreateNewSfzFile,
kTagOpenSfzFolder,
kTagPreviousSfzFile,
kTagNextSfzFile,
Expand Down Expand Up @@ -135,6 +136,7 @@ struct Editor::Impl : EditorController::Receiver, IControlListener {
}

void chooseSfzFile();
void createNewSfzFile();
void changeSfzFile(const std::string& filePath);
void changeToNextSfzFile(long offset);
void chooseScalaFile();
Expand Down Expand Up @@ -849,6 +851,7 @@ void Editor::Impl::createFrameContents()
if (SActionMenu* menu = fileOperationsMenu_) {
menu->addEntry("Load file", kTagLoadSfzFile);
menu->addEntry("Edit file", kTagEditSfzFile);
menu->addEntry("Create new file", kTagCreateNewSfzFile);
menu->addEntry("Open SFZ folder", kTagOpenSfzFolder);
}

Expand Down Expand Up @@ -914,6 +917,44 @@ void Editor::Impl::chooseSfzFile()
}
}

///
static const char defaultSfzText[] =
"<region>sample=*sine" "\n"
"ampeg_attack=0.02 ampeg_release=0.1" "\n";

static void createDefaultSfzFileIfNotExisting(const fs::path& path)
{
if (!fs::exists(path))
fs::ofstream { path } << defaultSfzText;
}

///
void Editor::Impl::createNewSfzFile()
{
SharedPointer<CNewFileSelector> fs = owned(CNewFileSelector::create(frame_, CNewFileSelector::kSelectSaveFile));

fs->setTitle("Create SFZ file");
fs->setDefaultExtension(CFileExtension("SFZ", "sfz"));

std::string initialDir = getFileChooserInitialDir(currentSfzFile_);
if (!initialDir.empty())
fs->setInitialDirectory(initialDir.c_str());

if (fs->runModal()) {
UTF8StringPtr file = fs->getSelectedFile(0);
std::string fileStr;
if (file && !absl::EndsWithIgnoreCase(file, ".sfz")) {
fileStr = std::string(file) + ".sfz";
file = fileStr.c_str();
}
if (file) {
createDefaultSfzFileIfNotExisting(fs::u8path(file));
changeSfzFile(file);
openFileInExternalEditor(file);
}
}
}

void Editor::Impl::changeSfzFile(const std::string& filePath)
{
ctrl_->uiSendValue(EditId::SfzFile, filePath);
Expand Down Expand Up @@ -1322,6 +1363,13 @@ void Editor::Impl::valueChanged(CControl* ctl)
openFileInExternalEditor(currentSfzFile_.c_str());
break;

case kTagCreateNewSfzFile:
if (value != 1)
break;

createNewSfzFile();
break;

case kTagOpenSfzFolder:
if (value != 1)
break;
Expand Down

0 comments on commit fe2bc7c

Please sign in to comment.