From ceb27728d38cf065eb7bce90c7372d7b420d1189 Mon Sep 17 00:00:00 2001 From: tildearrow Date: Sat, 17 Dec 2022 22:45:08 -0500 Subject: [PATCH] add option to swap nibbles on raw sample import issue #791 --- src/engine/engine.cpp | 10 +++++++++- src/engine/engine.h | 2 +- src/gui/gui.cpp | 7 ++++++- src/gui/gui.h | 2 +- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index 40f9f8cf6..281e7c5d5 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -3293,7 +3293,7 @@ DivSample* DivEngine::sampleFromFile(const char* path) { #endif } -DivSample* DivEngine::sampleFromFileRaw(const char* path, DivSampleDepth depth, int channels, bool bigEndian, bool unsign) { +DivSample* DivEngine::sampleFromFileRaw(const char* path, DivSampleDepth depth, int channels, bool bigEndian, bool unsign, bool swapNibbles) { if (song.sample.size()>=256) { lastError="too many samples!"; return NULL; @@ -3459,6 +3459,14 @@ DivSample* DivEngine::sampleFromFileRaw(const char* path, DivSampleDepth depth, } delete[] buf; + // swap nibbles if needed + if (swapNibbles) { + unsigned char* b=(unsigned char*)sample->getCurBuf(); + for (unsigned int i=0; igetCurBufLen(); i++) { + b[i]=(b[i]<<4)|(b[i]>>4); + } + } + BUSY_END; return sample; } diff --git a/src/engine/engine.h b/src/engine/engine.h index 612bd4be7..0e10ff66a 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -796,7 +796,7 @@ class DivEngine { DivSample* sampleFromFile(const char* path); // get raw sample - DivSample* sampleFromFileRaw(const char* path, DivSampleDepth depth, int channels, bool bigEndian, bool unsign); + DivSample* sampleFromFileRaw(const char* path, DivSampleDepth depth, int channels, bool bigEndian, bool unsign, bool swapNibbles); // delete sample void delSample(int index); diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index 6753fdfd7..76af88fcb 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -5019,8 +5019,12 @@ bool FurnaceGUI::loop() { ImGui::Checkbox("Big endian",&pendingRawSampleBigEndian); ImGui::EndDisabled(); + ImGui::BeginDisabled(pendingRawSampleDepth==DIV_SAMPLE_DEPTH_16BIT); + ImGui::Checkbox("Swap nibbles",&pendingRawSampleSwapNibbles); + ImGui::EndDisabled(); + if (ImGui::Button("OK")) { - DivSample* s=e->sampleFromFileRaw(pendingRawSample.c_str(),(DivSampleDepth)pendingRawSampleDepth,pendingRawSampleChannels,pendingRawSampleBigEndian,pendingRawSampleUnsigned); + DivSample* s=e->sampleFromFileRaw(pendingRawSample.c_str(),(DivSampleDepth)pendingRawSampleDepth,pendingRawSampleChannels,pendingRawSampleBigEndian,pendingRawSampleUnsigned,pendingRawSampleSwapNibbles); if (s==NULL) { showError(e->getLastError()); } else { @@ -5684,6 +5688,7 @@ FurnaceGUI::FurnaceGUI(): pendingRawSampleChannels(1), pendingRawSampleUnsigned(false), pendingRawSampleBigEndian(false), + pendingRawSampleSwapNibbles(false), globalWinFlags(0), curFileDialog(GUI_FILE_OPEN), warnAction(GUI_WARN_OPEN), diff --git a/src/gui/gui.h b/src/gui/gui.h index aa0ff29a9..805ce67ca 100644 --- a/src/gui/gui.h +++ b/src/gui/gui.h @@ -1116,7 +1116,7 @@ class FurnaceGUI { String pendingRawSample; int pendingRawSampleDepth, pendingRawSampleChannels; - bool pendingRawSampleUnsigned, pendingRawSampleBigEndian; + bool pendingRawSampleUnsigned, pendingRawSampleBigEndian, pendingRawSampleSwapNibbles; ImGuiWindowFlags globalWinFlags;