From b8e408a73ea38b2a816cbc2cd54e62a681c33b18 Mon Sep 17 00:00:00 2001 From: xenakios Date: Thu, 31 May 2018 13:55:32 +0300 Subject: [PATCH] Use CriticalSection in audio source. Add bool argument for seek to get immediate seeks. etc --- Source/PS_Source/Input/AInputS.h | 25 +++++++++++++------------ Source/PS_Source/Input/InputS.h | 2 +- Source/PS_Source/StretchSource.cpp | 4 ++-- Source/PluginProcessor.cpp | 3 ++- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/Source/PS_Source/Input/AInputS.h b/Source/PS_Source/Input/AInputS.h index dff5379..6213b50 100644 --- a/Source/PS_Source/Input/AInputS.h +++ b/Source/PS_Source/Input/AInputS.h @@ -36,8 +36,6 @@ inline double ramp(int64_t pos, int64_t totallen, int64_t rampinlen, int64_t ram return 1.0; } -using LockGuard = std::lock_guard; - class AInputS final : public InputS { public: @@ -53,7 +51,7 @@ public: void setAudioBuffer(AudioBuffer* buf, int samplerate, int len) { - LockGuard locker(m_mutex); + ScopedLock locker(m_mutex); m_afreader = nullptr; m_using_memory_buffer = true; m_readbuf = *buf; @@ -64,7 +62,7 @@ public: m_loop_enabled = true; m_crossfadebuf.setSize(info.nchannels, m_crossfadebuf.getNumSamples()); m_cached_file_range = { 0,len }; - seek(m_activerange.getStart()); + seek(m_activerange.getStart(), true); updateXFadeCache(); } virtual AudioBuffer* getAudioBuffer() override @@ -79,7 +77,7 @@ public: AudioFormatReader* reader = m_manager->createReaderFor(file); if (reader) { - LockGuard locker(m_mutex); + ScopedLock locker(m_mutex); m_using_memory_buffer = false; m_afreader = std::unique_ptr(reader); if (m_activerange.isEmpty()) @@ -109,7 +107,7 @@ public: } int readNextBlock(AudioBuffer& abuf, int nsmps, int numchans) override { - LockGuard locker(m_mutex); + ScopedLock locker(m_mutex); if (m_afreader == nullptr && m_using_memory_buffer == false) { jassert(false); @@ -281,11 +279,14 @@ public: updateXFadeCache(); //m_cached_crossfade_range = Range(); } - void seek(double pos) override //0=start,1.0=end + void seek(double pos, bool immediate) override //0=start,1.0=end { - //seekImpl(pos); - - LockGuard locker(m_mutex); + ScopedLock locker(m_mutex); + if (immediate == true) + { + seekImpl(pos); + return; + } if (m_seekfade.state == 0) { m_seekfade.state = 1; @@ -332,7 +333,7 @@ public: } void setActiveRange(Range rng) override { - LockGuard locker(m_mutex); + ScopedLock locker(m_mutex); /* if (rng.contains(getCurrentPositionPercent())) @@ -395,7 +396,7 @@ private: int64_t m_loopcount = 0; bool m_using_memory_buffer = true; AudioFormatManager* m_manager = nullptr; - std::recursive_mutex m_mutex; + CriticalSection m_mutex; struct { int state = 0; // 0 inactive, 1 seek requested, 2 fade out, 3 fade in diff --git a/Source/PS_Source/Input/InputS.h b/Source/PS_Source/Input/InputS.h index 031a9ab..6cf47d5 100644 --- a/Source/PS_Source/Input/InputS.h +++ b/Source/PS_Source/Input/InputS.h @@ -50,7 +50,7 @@ public: nsmps-=readsize; }; }; - virtual void seek(double pos)=0;//0=start,1.0=end + virtual void seek(double pos, bool immediate)=0;//0=start,1.0=end struct { int64_t nsamples=0; diff --git a/Source/PS_Source/StretchSource.cpp b/Source/PS_Source/StretchSource.cpp index 0e79974..cc2b17c 100644 --- a/Source/PS_Source/StretchSource.cpp +++ b/Source/PS_Source/StretchSource.cpp @@ -505,7 +505,7 @@ void StretchAudioSource::initObjects() ScopedLock locker(m_cs); m_inputfile->setActiveRange(m_playrange); if (m_inputfile->getActiveRange().contains(m_inputfile->getCurrentPositionPercent())==false) - m_inputfile->seek(m_playrange.getStart()); + m_inputfile->seek(m_playrange.getStart(), true); m_firstbuffer = true; if (m_stretchoutringbuf.getSize() < m_num_outchans*m_process_fftsize) @@ -695,7 +695,7 @@ void StretchAudioSource::seekPercent(double pos) m_seekpos = pos; //m_firstbuffer = true; //m_resampler->Reset(); - m_inputfile->seek(pos); + m_inputfile->seek(pos, true); ++m_param_change_count; } diff --git a/Source/PluginProcessor.cpp b/Source/PluginProcessor.cpp index 299ac1e..e8b5ce9 100644 --- a/Source/PluginProcessor.cpp +++ b/Source/PluginProcessor.cpp @@ -542,6 +542,7 @@ void PaulstretchpluginAudioProcessor::prepareToPlay(double sampleRate, int sampl m_curmaxblocksize = samplesPerBlock; m_input_buffer.setSize(getMainBusNumInputChannels(), samplesPerBlock); *getBoolParameter(cpi_rewind) = false; + m_lastrewind = false; int numoutchans = *m_outchansparam; if (numoutchans != m_cur_num_out_chans) m_prebuffering_inited = false; @@ -693,7 +694,7 @@ void PaulstretchpluginAudioProcessor::processBlock (AudioSampleBuffer& buffer, M if (m_stretch_source->isLoopEnabled() != *getBoolParameter(cpi_looping_enabled)) m_stretch_source->setLoopingEnabled(*getBoolParameter(cpi_looping_enabled)); bool rew = *getBoolParameter(cpi_rewind); - if (rew !=m_lastrewind) + if (rew != m_lastrewind) { if (rew == true) {