Revert "Remove use of shared ownership for Sample"

This reverts commit 1d452331d1.
In some cases, you can infact do away with shared ownership
on Sample if there are no writes being made to either of them,
but to make sure changes are reflected to the object in cases
where writes do happen, they should work with the same one.
This commit is contained in:
sakertooth
2023-09-01 14:25:46 -04:00
parent b1516ea40e
commit 5ac915971b
10 changed files with 78 additions and 69 deletions

View File

@@ -90,7 +90,8 @@ SampleClip::SampleClip( Track * _track ) :
SampleClip::SampleClip(const SampleClip& orig) :
SampleClip(orig.getTrack())
{
m_sample = orig.m_sample;
// TODO C++20: Deprecated, use std::atomic<std::shared_ptr> instead
std::atomic_store(&m_sample, orig.m_sample);
m_isPlaying = orig.m_isPlaying;
}
@@ -119,7 +120,7 @@ void SampleClip::changeLength( const TimePos & _length )
QString SampleClip::sampleFile() const
{
return m_sample.sampleFile();
return m_sample->sampleFile();
}
@@ -128,7 +129,7 @@ void SampleClip::setSampleBuffer( SampleBuffer* sb )
{
// TODO C++20: Deprecated, use std::atomic<std::shared_ptr> instead
auto buffer = std::shared_ptr<const SampleBuffer>(sb);
m_sample = Sample(buffer);
std::atomic_store(&m_sample, std::make_shared<Sample>(buffer));
updateLength();
emit sampleChanged();
@@ -148,7 +149,8 @@ void SampleClip::setSampleFile( const QString & _sf )
else
{ //Otherwise set it to the sample's length
auto buffer = gui::SampleLoader::createBufferFromFile(_sf);
m_sample = Sample(std::move(buffer));
// TODO C++20: Deprecated, use std::atomic<std::shared_ptr> instead
std::atomic_store(&m_sample, std::make_shared<Sample>(std::move(buffer)));
length = sampleLength();
}
changeLength(length);
@@ -219,7 +221,7 @@ void SampleClip::updateLength()
TimePos SampleClip::sampleLength() const
{
return static_cast<int>(m_sample.playbackSize() / Engine::framesPerTick());
return static_cast<int>(m_sample->playbackSize() / Engine::framesPerTick());
}
@@ -227,7 +229,7 @@ TimePos SampleClip::sampleLength() const
void SampleClip::setSampleStartFrame(f_cnt_t startFrame)
{
m_sample.setStartFrame(startFrame);
m_sample->setStartFrame(startFrame);
}
@@ -235,7 +237,7 @@ void SampleClip::setSampleStartFrame(f_cnt_t startFrame)
void SampleClip::setSamplePlayLength(f_cnt_t length)
{
m_sample.setEndFrame(length);
m_sample->setEndFrame(length);
}
@@ -258,15 +260,15 @@ void SampleClip::saveSettings( QDomDocument & _doc, QDomElement & _this )
if( sampleFile() == "" )
{
QString s;
_this.setAttribute("data", m_sample.toBase64());
_this.setAttribute("data", m_sample->toBase64());
}
_this.setAttribute("sample_rate", m_sample.sampleRate());
_this.setAttribute("sample_rate", m_sample->sampleRate());
if( usesCustomClipColor() )
{
_this.setAttribute( "color", color().name() );
}
if (m_sample.reversed())
if (m_sample->reversed())
{
_this.setAttribute("reversed", "true");
}
@@ -289,7 +291,8 @@ void SampleClip::loadSettings( const QDomElement & _this )
Engine::audioEngine()->processingSampleRate();
auto buffer = gui::SampleLoader::createBufferFromBase64(_this.attribute("data"), sampleRate);
m_sample = Sample(std::move(buffer));
// TODO C++20: Deprecated, use std::atomic<std::shared_ptr> instead
std::atomic_store(&m_sample, std::make_shared<Sample>(std::move(buffer)));
}
changeLength( _this.attribute( "len" ).toInt() );
setMuted( _this.attribute( "muted" ).toInt() );
@@ -307,7 +310,7 @@ void SampleClip::loadSettings( const QDomElement & _this )
if(_this.hasAttribute("reversed"))
{
m_sample.setReversed(true);
m_sample->setReversed(true);
emit wasReversed(); // tell SampleClipView to update the view
}
}

View File

@@ -35,7 +35,7 @@ namespace lmms
{
SamplePlayHandle::SamplePlayHandle(const Sample& sample, bool ownAudioPort) :
SamplePlayHandle::SamplePlayHandle(std::shared_ptr<const Sample> sample, bool ownAudioPort) :
PlayHandle( Type::SamplePlayHandle ),
m_sample(sample),
m_doneMayReturnTrue( true ),
@@ -56,7 +56,7 @@ SamplePlayHandle::SamplePlayHandle(const Sample& sample, bool ownAudioPort) :
SamplePlayHandle::SamplePlayHandle( const QString& sampleFile ) :
SamplePlayHandle(Sample(sampleFile), true)
SamplePlayHandle(std::make_shared<const Sample>(sampleFile), true)
{
}
@@ -113,7 +113,7 @@ void SamplePlayHandle::play( sampleFrame * buffer )
m_volumeModel->value() / DefaultVolume } };*/
// SamplePlayHandle always plays the sample at its original pitch;
// it is used only for previews, SampleTracks and the metronome.
if (!m_sample.play(workingBuffer, &m_state, frames, DefaultBaseFreq))
if (!m_sample->play(workingBuffer, &m_state, frames, DefaultBaseFreq))
{
memset(workingBuffer, 0, frames * sizeof(sampleFrame));
}
@@ -143,8 +143,8 @@ bool SamplePlayHandle::isFromTrack( const Track * _track ) const
f_cnt_t SamplePlayHandle::totalFrames() const
{
return (m_sample.endFrame() - m_sample.startFrame()) *
(static_cast<float>(Engine::audioEngine()->processingSampleRate()) / m_sample.sampleRate());
return (m_sample->endFrame() - m_sample->startFrame()) *
(static_cast<float>(Engine::audioEngine()->processingSampleRate()) / m_sample->sampleRate());
}

View File

@@ -60,8 +60,8 @@ void SampleClipView::updateSample()
update();
// set tooltip to filename so that user can see what sample this
// sample-clip contains
setToolTip(m_clip->m_sample.sampleFile() != "" ?
PathUtil::toAbsolute(m_clip->m_sample.sampleFile()) :
setToolTip(m_clip->m_sample->sampleFile() != "" ?
PathUtil::toAbsolute(m_clip->m_sample->sampleFile()) :
tr( "Double-click to open sample" ) );
}
@@ -173,9 +173,9 @@ void SampleClipView::mouseDoubleClickEvent( QMouseEvent * )
QString af = gui::SampleLoader::openAudioFile();
if ( af.isEmpty() ) {} //Don't do anything if no file is loaded
else if (af == m_clip->m_sample.sampleFile())
else if (af == m_clip->m_sample->sampleFile())
{ //Instead of reloading the existing file, just reset the size
int length = static_cast<int>(m_clip->m_sample.sampleSize() / Engine::framesPerTick());
int length = static_cast<int>(m_clip->m_sample->sampleSize() / Engine::framesPerTick());
m_clip->changeLength(length);
}
else
@@ -262,9 +262,9 @@ void SampleClipView::paintEvent( QPaintEvent * pe )
float offset = m_clip->startTimeOffset() / ticksPerBar * pixelsPerBar();
QRect r = QRect( offset, spacing,
qMax( static_cast<int>( m_clip->sampleLength() * ppb / ticksPerBar ), 1 ), rect().bottom() - 2 * spacing );
m_clip->m_sample.visualize(p, r);
m_clip->m_sample->visualize(p, r);
QString name = PathUtil::cleanName(m_clip->m_sample.sampleFile());
QString name = PathUtil::cleanName(m_clip->m_sample->sampleFile());
paintTextLabel(name, p);
// disable antialiasing for borders, since its not needed
@@ -317,7 +317,7 @@ void SampleClipView::paintEvent( QPaintEvent * pe )
void SampleClipView::reverseSample()
{
m_clip->m_sample.setReversed(!m_clip->m_sample.reversed());
m_clip->sample()->setReversed(!m_clip->sample()->reversed());
Engine::getSong()->setModified();
update();
}

View File

@@ -108,10 +108,10 @@ bool SampleTrack::play( const TimePos & _start, const fpp_t _frames,
{
if( sClip->isPlaying() == false && _start >= (sClip->startPosition() + sClip->startTimeOffset()) )
{
auto bufferFramesPerTick = Engine::framesPerTick(sClip->sample().sampleRate());
auto bufferFramesPerTick = Engine::framesPerTick(sClip->sample()->sampleRate());
f_cnt_t sampleStart = bufferFramesPerTick * ( _start - sClip->startPosition() - sClip->startTimeOffset() );
f_cnt_t clipFrameLength = bufferFramesPerTick * ( sClip->endPosition() - sClip->startPosition() - sClip->startTimeOffset() );
f_cnt_t sampleBufferLength = sClip->sample().sampleSize();
f_cnt_t sampleBufferLength = sClip->sample()->sampleSize();
//if the Clip smaller than the sample length we play only until Clip end
//else we play the sample to the end but nothing more
f_cnt_t samplePlayLength = clipFrameLength > sampleBufferLength ? sampleBufferLength : clipFrameLength;