diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index 6a43f4ab5..ecab835fd 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -1485,6 +1485,26 @@ void DivEngine::syncReset() { isBusy.unlock(); } +const int sampleRates[6]={ + 4000, 8000, 11025, 16000, 22050, 32000 +}; + +void DivEngine::previewSample(int sample) { + isBusy.lock(); + if (sample<0 || sample>(int)song.sample.size()) { + sPreview.sample=-1; + sPreview.pos=0; + isBusy.unlock(); + return; + } + blip_clear(bb[2]); + blip_set_rates(bb[2],sampleRates[song.sample[sample]->rate],got.rate); + prevSample[2]=0; + sPreview.pos=0; + sPreview.sample=sample; + isBusy.unlock(); +} + String DivEngine::getConfigPath() { return configPath; } @@ -1941,13 +1961,23 @@ bool DivEngine::init(String outName) { logE("not enough memory!\n"); return false; } + + bb[2]=blip_new(32768); + if (bb[2]==NULL) { + logE("not enough memory!\n"); + return false; + } bbOut[0]=new short[got.bufsize]; bbOut[1]=new short[got.bufsize]; + bbOut[2]=new short[got.bufsize]; bbIn[0]=new short[32768]; bbIn[1]=new short[32768]; + bbIn[2]=new short[32768]; bbInLen=32768; + + blip_set_rates(bb[2],44100,got.rate); for (int i=0; i<64; i++) { vibTable[i]=127*sin(((double)i/64.0)*(2*M_PI)); diff --git a/src/engine/engine.h b/src/engine/engine.h index cadbd81d0..3b125b0a8 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -92,13 +92,21 @@ class DivEngine { String configFile; String lastError; + struct SamplePreview { + int sample; + unsigned int pos; + SamplePreview(): + sample(-1), + pos(0) {} + } sPreview; + short vibTable[64]; - blip_buffer_t* bb[2]; + blip_buffer_t* bb[3]; size_t bbInLen; - int temp[2], prevSample[2]; - short* bbIn[2]; - short* bbOut[2]; + int temp[3], prevSample[3]; + short* bbIn[3]; + short* bbOut[3]; size_t totalProcessed; @@ -155,6 +163,9 @@ class DivEngine { // reset playback state void syncReset(); + // trigger sample preview + void previewSample(int sample); + // get config path String getConfigPath(); diff --git a/src/engine/playback.cpp b/src/engine/playback.cpp index 9f642fea9..cfe615a34 100644 --- a/src/engine/playback.cpp +++ b/src/engine/playback.cpp @@ -802,9 +802,36 @@ bool DivEngine::nextTick(bool noAccum) { } void DivEngine::nextBuf(float** in, float** out, int inChans, int outChans, unsigned int size) { + memset(out[0],0,size*sizeof(float)); + memset(out[1],0,size*sizeof(float)); + + isBusy.lock(); + if (sPreview.sample>=0 && sPreview.sample<(int)song.sample.size()) { + DivSample* s=song.sample[sPreview.sample]; + size_t prevtotal=blip_clocks_needed(bb[2],size); + + for (size_t i=0; i=s->rendLength) { + temp[2]=0; + } else { + temp[2]=s->rendData[sPreview.pos++]; + } + blip_add_delta(bb[2],i,temp[2]-prevSample[2]); + prevSample[2]=temp[2]; + } + + if (sPreview.pos>=s->rendLength) sPreview.sample=-1; + + blip_end_frame(bb[2],prevtotal); + blip_read_samples(bb[2],bbOut[2],size,0); + for (size_t i=0; iisStereo()) { for (size_t i=0; irenderSamplesP(); } + ImGui::SameLine(); + if (ImGui::Button(ICON_FA_PLAY "##PreviewSample")) { + e->previewSample(curSample); + } } } if (ImGui::IsWindowFocused()) curWindow=GUI_WINDOW_SAMPLE_EDIT;