GUi: add save sample as raw data

This commit is contained in:
tildearrow 2023-05-02 03:57:25 -05:00
parent 27935cec11
commit ffdff26792
8 changed files with 73 additions and 1 deletions

View File

@ -19,6 +19,7 @@
#include "sample.h"
#include "../ta-log.h"
#include "../fileutils.h"
#include <math.h>
#include <string.h>
#ifdef HAVE_SNDFILE
@ -444,6 +445,28 @@ bool DivSample::save(const char* path) {
#endif
}
bool DivSample::saveRaw(const char* path) {
if (samples<1) {
logE("sample is empty though!");
return false;
}
FILE* f=ps_fopen(path,"wb");
if (f==NULL) {
logE("could not save sample: %s!",strerror(errno));
return false;
}
if (depth==DIV_SAMPLE_DEPTH_BRR && getLoopStartPosition(DIV_SAMPLE_DEPTH_BRR)) {\
// TODO: BRR loop pos?
}
if (fwrite(getCurBuf(),1,getCurBufLen(),f)!=getCurBufLen()) {
logW("did not write entire instrument!");
}
fclose(f);
return true;
}
// 16-bit memory is padded to 512, to make things easier for ADPCM-A/B.
bool DivSample::initInternal(DivSampleDepth d, int count) {
switch (d) {

View File

@ -201,6 +201,13 @@ struct DivSample {
*/
bool save(const char* path);
/**
* save this sample to a file (raw).
* @param path a path.
* @return whether saving succeeded or not.
*/
bool saveRaw(const char* path);
/**
* @warning DO NOT USE - internal function
* initialize sample data.

View File

@ -700,6 +700,11 @@ void FurnaceGUI::drawSampleList(bool asChild) {
if (ImGui::IsItemHovered()) {
ImGui::SetTooltip("Save");
}
if (ImGui::BeginPopupContextItem("SampleSaveOpt")) {
if (ImGui::MenuItem("save raw...")) {
doAction(GUI_ACTION_SAMPLE_LIST_SAVE_RAW);
}
}
ImGui::SameLine();
if (ImGui::ArrowButton("SampleUp",ImGuiDir_Up)) {
doAction(GUI_ACTION_SAMPLE_LIST_MOVE_UP);

View File

@ -815,6 +815,9 @@ void FurnaceGUI::doAction(int what) {
case GUI_ACTION_SAMPLE_LIST_SAVE:
if (curSample>=0 && curSample<(int)e->song.sample.size()) openFileDialog(GUI_FILE_SAMPLE_SAVE);
break;
case GUI_ACTION_SAMPLE_LIST_SAVE_RAW:
if (curSample>=0 && curSample<(int)e->song.sample.size()) openFileDialog(GUI_FILE_SAMPLE_SAVE_RAW);
break;
case GUI_ACTION_SAMPLE_LIST_MOVE_UP:
if (e->moveSampleUp(curSample)) {
curSample--;

View File

@ -1726,6 +1726,16 @@ void FurnaceGUI::openFileDialog(FurnaceGUIFileDialogs type) {
dpiScale
);
break;
case GUI_FILE_SAMPLE_SAVE_RAW:
if (!dirExists(workingDirSample)) workingDirSample=getHomeDir();
hasOpened=fileDialog->openSave(
"Load Raw Sample",
{"all files", "*"},
".*",
workingDirSample,
dpiScale
);
break;
case GUI_FILE_EXPORT_AUDIO_ONE:
if (!dirExists(workingDirAudioExport)) workingDirAudioExport=getHomeDir();
hasOpened=fileDialog->openSave(
@ -4447,6 +4457,7 @@ bool FurnaceGUI::loop() {
case GUI_FILE_SAMPLE_OPEN_REPLACE:
case GUI_FILE_SAMPLE_OPEN_REPLACE_RAW:
case GUI_FILE_SAMPLE_SAVE:
case GUI_FILE_SAMPLE_SAVE_RAW:
workingDirSample=fileDialog->getPath()+DIR_SEPARATOR_STR;
break;
case GUI_FILE_EXPORT_AUDIO_ONE:
@ -4714,7 +4725,16 @@ bool FurnaceGUI::loop() {
break;
case GUI_FILE_SAMPLE_SAVE:
if (curSample>=0 && curSample<(int)e->song.sample.size()) {
e->song.sample[curSample]->save(copyOfName.c_str());
if (!e->song.sample[curSample]->save(copyOfName.c_str())) {
showError("could not save sample! open Log Viewer for more information.");
}
}
break;
case GUI_FILE_SAMPLE_SAVE_RAW:
if (curSample>=0 && curSample<(int)e->song.sample.size()) {
if (!e->song.sample[curSample]->saveRaw(copyOfName.c_str())) {
showError("could not save sample! open Log Viewer for more information.");
}
}
break;
case GUI_FILE_EXPORT_AUDIO_ONE:

View File

@ -365,6 +365,7 @@ enum FurnaceGUIFileDialogs {
GUI_FILE_SAMPLE_OPEN_REPLACE,
GUI_FILE_SAMPLE_OPEN_REPLACE_RAW,
GUI_FILE_SAMPLE_SAVE,
GUI_FILE_SAMPLE_SAVE_RAW,
GUI_FILE_EXPORT_AUDIO_ONE,
GUI_FILE_EXPORT_AUDIO_PER_SYS,
GUI_FILE_EXPORT_AUDIO_PER_CHANNEL,
@ -591,6 +592,7 @@ enum FurnaceGUIActions {
GUI_ACTION_SAMPLE_LIST_OPEN_RAW,
GUI_ACTION_SAMPLE_LIST_OPEN_REPLACE_RAW,
GUI_ACTION_SAMPLE_LIST_SAVE,
GUI_ACTION_SAMPLE_LIST_SAVE_RAW,
GUI_ACTION_SAMPLE_LIST_MOVE_UP,
GUI_ACTION_SAMPLE_LIST_MOVE_DOWN,
GUI_ACTION_SAMPLE_LIST_DELETE,

View File

@ -636,6 +636,7 @@ const FurnaceGUIActionDef guiActions[GUI_ACTION_MAX]={
D("SAMPLE_LIST_OPEN_RAW", "Import raw data", 0),
D("SAMPLE_LIST_OPEN_REPLACE_RAW", "Import raw data (replace current)", 0),
D("SAMPLE_LIST_SAVE", "Save", 0),
D("SAMPLE_LIST_SAVE_RAW", "Save (raw)", 0),
D("SAMPLE_LIST_MOVE_UP", "Move up", FURKMOD_SHIFT|SDLK_UP),
D("SAMPLE_LIST_MOVE_DOWN", "Move down", FURKMOD_SHIFT|SDLK_DOWN),
D("SAMPLE_LIST_DELETE", "Delete", 0),

View File

@ -133,6 +133,12 @@ void FurnaceGUI::drawSampleEdit() {
if (ImGui::IsItemHovered()) {
ImGui::SetTooltip("Open");
}
if (ImGui::BeginPopupContextItem("SampleEOpenOpt")) {
if (ImGui::MenuItem("import raw...")) {
doAction((curSample>=0 && curSample<(int)e->song.sample.size())?GUI_ACTION_SAMPLE_LIST_OPEN_REPLACE_RAW:GUI_ACTION_SAMPLE_LIST_OPEN_RAW);
}
ImGui::EndPopup();
}
ImGui::SameLine();
if (ImGui::Button(ICON_FA_FLOPPY_O "##SESave")) {
doAction(GUI_ACTION_SAMPLE_LIST_SAVE);
@ -140,6 +146,11 @@ void FurnaceGUI::drawSampleEdit() {
if (ImGui::IsItemHovered()) {
ImGui::SetTooltip("Save");
}
if (ImGui::BeginPopupContextItem("SampleESaveOpt")) {
if (ImGui::MenuItem("save raw...")) {
doAction(GUI_ACTION_SAMPLE_LIST_SAVE_RAW);
}
}
ImGui::SameLine();