diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index 1c5d4dd2e..b8b18cb40 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -1469,6 +1469,97 @@ int DivEngine::addSample() { int DivEngine::addSampleFromFile(const char* path) { BUSY_BEGIN; + warnings=""; + + const char* pathRedux=strrchr(path,DIR_SEPARATOR); + if (pathRedux==NULL) { + pathRedux=path; + } else { + pathRedux++; + } + String stripPath; + const char* pathReduxEnd=strrchr(pathRedux,'.'); + if (pathReduxEnd==NULL) { + stripPath=pathRedux; + } else { + for (const char* i=pathRedux; i!=pathReduxEnd && (*i); i++) { + stripPath+=*i; + } + } + + const char* ext=strrchr(path,'.'); + if (ext!=NULL) { + String extS; + for (; *ext; ext++) { + char i=*ext; + if (i>='A' && i<='Z') { + i+='a'-'A'; + } + extS+=i; + } + if (extS==String(".dmc")) { // read as .dmc + size_t len=0; + DivSample* sample=new DivSample; + int sampleCount=(int)song.sample.size(); + sample->name=stripPath; + + FILE* f=fopen(path,"rb"); + if (f==NULL) { + BUSY_END; + lastError=fmt::sprintf("could not open file! (%s)",strerror(errno)); + delete sample; + return -1; + } + + if (fseek(f,0,SEEK_END)<0) { + fclose(f); + BUSY_END; + lastError=fmt::sprintf("could not get file length! (%s)",strerror(errno)); + delete sample; + return -1; + } + + len=ftell(f); + + if (len==0) { + fclose(f); + BUSY_END; + lastError="file is empty!"; + delete sample; + return -1; + } + + if (fseek(f,0,SEEK_SET)<0) { + fclose(f); + BUSY_END; + lastError=fmt::sprintf("could not seek to beginning of file! (%s)",strerror(errno)); + delete sample; + return -1; + } + + sample->rate=33144; + sample->centerRate=33144; + sample->depth=1; + sample->init(len*8); + + if (fread(sample->dataDPCM,1,len,f)==0) { + fclose(f); + BUSY_END; + lastError=fmt::sprintf("could not read file! (%s)",strerror(errno)); + delete sample; + return -1; + } + + saveLock.lock(); + song.sample.push_back(sample); + song.sampleLen=sampleCount+1; + saveLock.unlock(); + renderSamples(); + BUSY_END; + return sampleCount; + } + } + SF_INFO si; SNDFILE* f=sf_open(path,SFM_READ,&si); if (f==NULL) { @@ -1493,13 +1584,7 @@ int DivEngine::addSampleFromFile(const char* path) { } DivSample* sample=new DivSample; int sampleCount=(int)song.sample.size(); - const char* sName=strrchr(path,DIR_SEPARATOR); - if (sName==NULL) { - sName=path; - } else { - sName++; - } - sample->name=sName; + sample->name=stripPath; int index=0; if ((si.format&SF_FORMAT_SUBMASK)==SF_FORMAT_PCM_U8) { diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index a3cf11ba4..f577691f5 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -1368,9 +1368,9 @@ void FurnaceGUI::openFileDialog(FurnaceGUIFileDialogs type) { if (!dirExists(workingDirSample)) workingDirSample=getHomeDir(); hasOpened=fileDialog->openLoad( "Load Sample", - {"Wave file", "*.wav", + {"compatible files", "*.wav *.dmc", "all files", ".*"}, - "Wave file{.wav},.*", + "compatible files{.wav,.dmc},.*", workingDirSample, dpiScale ); diff --git a/src/gui/sampleEdit.cpp b/src/gui/sampleEdit.cpp index a84c852f6..9919c069f 100644 --- a/src/gui/sampleEdit.cpp +++ b/src/gui/sampleEdit.cpp @@ -28,8 +28,6 @@ #include "sampleUtil.h" #include "util.h" -// TODO: -// - .dmc loading void FurnaceGUI::drawSampleEdit() { if (nextWindow==GUI_WINDOW_SAMPLE_EDIT) { sampleEditOpen=true; diff --git a/src/gui/settings.cpp b/src/gui/settings.cpp index 8bb7addd8..aa6dcb503 100644 --- a/src/gui/settings.cpp +++ b/src/gui/settings.cpp @@ -2257,6 +2257,7 @@ void FurnaceGUI::applyUISettings(bool updateFonts) { ImGuiFileDialog::Instance()->SetFileStyle(IGFD_FileStyleByExtension,".dmp",uiColors[GUI_COLOR_FILE_INSTR],ICON_FA_FILE); ImGuiFileDialog::Instance()->SetFileStyle(IGFD_FileStyleByExtension,".dmw",uiColors[GUI_COLOR_FILE_WAVE],ICON_FA_FILE); ImGuiFileDialog::Instance()->SetFileStyle(IGFD_FileStyleByExtension,".wav",uiColors[GUI_COLOR_FILE_AUDIO],ICON_FA_FILE_AUDIO_O); + ImGuiFileDialog::Instance()->SetFileStyle(IGFD_FileStyleByExtension,".dmc",uiColors[GUI_COLOR_FILE_AUDIO],ICON_FA_FILE_AUDIO_O); ImGuiFileDialog::Instance()->SetFileStyle(IGFD_FileStyleByExtension,".vgm",uiColors[GUI_COLOR_FILE_VGM],ICON_FA_FILE_AUDIO_O); ImGuiFileDialog::Instance()->SetFileStyle(IGFD_FileStyleByExtension,".ttf",uiColors[GUI_COLOR_FILE_FONT],ICON_FA_FONT); ImGuiFileDialog::Instance()->SetFileStyle(IGFD_FileStyleByExtension,".otf",uiColors[GUI_COLOR_FILE_FONT],ICON_FA_FONT);