From 4211dad692cc101d235799f25e09452d8775c436 Mon Sep 17 00:00:00 2001 From: tildearrow Date: Thu, 11 May 2023 17:46:22 -0500 Subject: [PATCH] sample resample now uses selected rate rather than compat rate --- src/engine/sample.cpp | 50 +++++++++++++++++++++--------------------- src/engine/sample.h | 15 +++++++------ src/gui/sampleEdit.cpp | 17 +++++++------- 3 files changed, 42 insertions(+), 40 deletions(-) diff --git a/src/engine/sample.cpp b/src/engine/sample.cpp index 4f8f2fa3..2f8e7b99 100644 --- a/src/engine/sample.cpp +++ b/src/engine/sample.cpp @@ -701,7 +701,7 @@ bool DivSample::insert(unsigned int pos, unsigned int length) { #define RESAMPLE_BEGIN \ if (samples<1) return true; \ - int finalCount=(double)samples*(r/(double)rate); \ + int finalCount=(double)samples*(tRate/sRate); \ signed char* oldData8=data8; \ short* oldData16=data16; \ if (depth==DIV_SAMPLE_DEPTH_16BIT) { \ @@ -719,10 +719,10 @@ bool DivSample::insert(unsigned int pos, unsigned int length) { } #define RESAMPLE_END \ - if (loopStart>=0) loopStart=(double)loopStart*(r/(double)rate); \ - if (loopEnd>=0) loopEnd=(double)loopEnd*(r/(double)rate); \ - centerRate=(int)((double)centerRate*(r/(double)rate)); \ - rate=r; \ + if (loopStart>=0) loopStart=(double)loopStart*(tRate/sRate); \ + if (loopEnd>=0) loopEnd=(double)loopEnd*(tRate/sRate); \ + centerRate=(int)((double)centerRate*(tRate/sRate)); \ + rate=(int)((double)rate*(tRate/sRate)); \ samples=finalCount; \ if (depth==DIV_SAMPLE_DEPTH_16BIT) { \ delete[] oldData16; \ @@ -730,12 +730,12 @@ bool DivSample::insert(unsigned int pos, unsigned int length) { delete[] oldData8; \ } -bool DivSample::resampleNone(double r) { +bool DivSample::resampleNone(double sRate, double tRate) { RESAMPLE_BEGIN; if (depth==DIV_SAMPLE_DEPTH_16BIT) { for (int i=0; i=samples) { data16[i]=0; } else { @@ -744,7 +744,7 @@ bool DivSample::resampleNone(double r) { } } else if (depth==DIV_SAMPLE_DEPTH_8BIT) { for (int i=0; i=samples) { data8[i]=0; } else { @@ -757,12 +757,12 @@ bool DivSample::resampleNone(double r) { return true; } -bool DivSample::resampleLinear(double r) { +bool DivSample::resampleLinear(double sRate, double tRate) { RESAMPLE_BEGIN; double posFrac=0; unsigned int posInt=0; - double factor=(double)rate/r; + double factor=sRate/tRate; if (depth==DIV_SAMPLE_DEPTH_16BIT) { for (int i=0; irate) { - return resampleSinc(r); + if (tRate>sRate) { + return resampleSinc(sRate,tRate); } else { - return resampleBlep(r); + return resampleBlep(sRate,tRate); } break; } diff --git a/src/engine/sample.h b/src/engine/sample.h index 8006ab0b..ae9172e3 100644 --- a/src/engine/sample.h +++ b/src/engine/sample.h @@ -188,11 +188,11 @@ struct DivSample { /** * @warning DO NOT USE - internal functions */ - bool resampleNone(double rate); - bool resampleLinear(double rate); - bool resampleCubic(double rate); - bool resampleBlep(double rate); - bool resampleSinc(double rate); + bool resampleNone(double sRate, double tRate); + bool resampleLinear(double sRate, double tRate); + bool resampleCubic(double sRate, double tRate); + bool resampleBlep(double sRate, double tRate); + bool resampleSinc(double sRate, double tRate); /** * save this sample to a file. @@ -262,11 +262,12 @@ struct DivSample { /** * change the sample rate. * @warning do not attempt to resample outside of a synchronized block! - * @param rate number of samples. + * @param sRate source rate. + * @param tRate target rate. * @param filter the interpolation filter. * @return whether it was successful. */ - bool resample(double rate, int filter); + bool resample(double sRate, double tRate, int filter); /** * initialize the rest of sample formats for this sample. diff --git a/src/gui/sampleEdit.cpp b/src/gui/sampleEdit.cpp index ce56ae96..ab92f3d8 100644 --- a/src/gui/sampleEdit.cpp +++ b/src/gui/sampleEdit.cpp @@ -188,6 +188,8 @@ void FurnaceGUI::drawSampleEdit() { for (int i=0; irate:sample->centerRate; if (ImGui::BeginTable("SampleProps",(selColumns>1)?4:3,ImGuiTableFlags_SizingStretchSame|ImGuiTableFlags_BordersV|ImGuiTableFlags_BordersOuterH)) { ImGui::TableNextRow(ImGuiTableRowFlags_Headers); @@ -286,7 +288,6 @@ void FurnaceGUI::drawSampleEdit() { } } - int targetRate=sampleCompatRate?sample->rate:sample->centerRate; int sampleNote=round(64.0+(128.0*12.0*log((double)targetRate/8363.0)/log(2.0))); int sampleNoteCoarse=60+(sampleNote>>7); int sampleNoteFine=(sampleNote&127)-64; @@ -620,7 +621,7 @@ void FurnaceGUI::drawSampleEdit() { ImGui::SameLine(); ImGui::Button(ICON_FA_EXPAND "##SResample"); if (ImGui::IsItemClicked()) { - resampleTarget=sample->rate; + resampleTarget=targetRate; } if (ImGui::IsItemHovered()) { ImGui::SetTooltip("Resample"); @@ -641,23 +642,23 @@ void FurnaceGUI::drawSampleEdit() { } ImGui::SameLine(); if (ImGui::Button("==")) { - resampleTarget=sample->rate; + resampleTarget=targetRate; } ImGui::SameLine(); if (ImGui::Button("2.0x")) { resampleTarget*=2.0; } - double factor=resampleTarget/(double)sample->rate; + double factor=resampleTarget/(double)targetRate; if (ImGui::InputDouble("Factor",&factor,0.125,0.5,"%g")) { - resampleTarget=(double)sample->rate*factor; + resampleTarget=(double)targetRate*factor; if (resampleTarget<0) resampleTarget=0; if (resampleTarget>96000) resampleTarget=96000; } ImGui::Combo("Filter",&resampleStrat,resampleStrats,6); if (ImGui::Button("Resample")) { sample->prepareUndo(true); - e->lockEngine([this,sample]() { - if (!sample->resample(resampleTarget,resampleStrat)) { + e->lockEngine([this,sample,targetRate]() { + if (!sample->resample(targetRate,resampleTarget,resampleStrat)) { showError("couldn't resample! make sure your sample is 8 or 16-bit."); } e->renderSamples(); @@ -670,7 +671,7 @@ void FurnaceGUI::drawSampleEdit() { } ImGui::EndPopup(); } else { - resampleTarget=sample->rate; + resampleTarget=targetRate; } ImGui::SameLine(); ImGui::Dummy(ImVec2(4.0*dpiScale,dpiScale));