From c7b24f706fc7fa90abb3a4fe295624818255510f Mon Sep 17 00:00:00 2001 From: tildearrow Date: Wed, 24 May 2023 02:54:36 -0500 Subject: [PATCH] GUI: improve sample type conversion --- src/engine/sample.cpp | 34 ++++++++++++++++++++++++++++++++++ src/engine/sample.h | 7 +++++++ src/gui/sampleEdit.cpp | 3 +-- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/engine/sample.cpp b/src/engine/sample.cpp index ecd9752e..346f9a12 100644 --- a/src/engine/sample.cpp +++ b/src/engine/sample.cpp @@ -699,6 +699,40 @@ bool DivSample::insert(unsigned int pos, unsigned int length) { return false; } +void DivSample::convert(DivSampleDepth newDepth) { + render(); + depth=newDepth; + switch (depth) { + case DIV_SAMPLE_DEPTH_1BIT: + setSampleCount((samples+7)&(~7)); + break; + case DIV_SAMPLE_DEPTH_1BIT_DPCM: + setSampleCount((1+((((samples+7)/8)+15)&(~15)))<<3); + break; + case DIV_SAMPLE_DEPTH_YMZ_ADPCM: + setSampleCount(((lengthZ+3)&(~0x03))*2); + break; + case DIV_SAMPLE_DEPTH_QSOUND_ADPCM: // QSound ADPCM + setSampleCount((samples+1)&(~1)); + break; + case DIV_SAMPLE_DEPTH_ADPCM_A: // ADPCM-A + setSampleCount((samples+1)&(~1)); + break; + case DIV_SAMPLE_DEPTH_ADPCM_B: // ADPCM-B + setSampleCount((samples+1)&(~1)); + break; + case DIV_SAMPLE_DEPTH_BRR: // BRR + setSampleCount(16*(lengthBRR/9)); + break; + case DIV_SAMPLE_DEPTH_VOX: // VOX + setSampleCount((samples+1)&(~1)); + break; + default: + break; + } + render(); +} + #define RESAMPLE_BEGIN \ if (samples<1) return true; \ int finalCount=(double)samples*(tRate/sRate); \ diff --git a/src/engine/sample.h b/src/engine/sample.h index ae9172e3..c2f08ced 100644 --- a/src/engine/sample.h +++ b/src/engine/sample.h @@ -269,6 +269,13 @@ struct DivSample { */ bool resample(double sRate, double tRate, int filter); + /** + * convert sample depth. + * @warning do not attempt to do this outside of a synchronized block! + * @param newDepth the new depth. + */ + void convert(DivSampleDepth newDepth); + /** * initialize the rest of sample formats for this sample. */ diff --git a/src/gui/sampleEdit.cpp b/src/gui/sampleEdit.cpp index 1888aff1..e0f079ba 100644 --- a/src/gui/sampleEdit.cpp +++ b/src/gui/sampleEdit.cpp @@ -257,8 +257,7 @@ void FurnaceGUI::drawSampleEdit() { if (ImGui::Selectable(sampleDepths[i])) { sample->prepareUndo(true); e->lockEngine([this,sample,i]() { - sample->render(); - sample->depth=(DivSampleDepth)i; + sample->convert((DivSampleDepth)i); e->renderSamples(); }); updateSampleTex=true;