From bb6a77aa0fccb270dfe98c014c3142a2578e0aeb Mon Sep 17 00:00:00 2001 From: Michael Gregorius Date: Mon, 29 Apr 2024 19:22:37 +0200 Subject: [PATCH] Only set sample clips for valid files (#7224) Check if a non-empty buffer was loaded and only set the sample clip if that's the case. ## Other changes Move setting the song to modified towards the core in the context of `SampleClip`. Previously the `SampleClipView` did this but it's none of it's business. Introduce `SampleClip::changeLengthToSampleLength` which changes the length of the clip to the length of the sample. This was also previously done by the view which is again the wrong place to do the necessary calculations. An unnecessary `static_cast` was removed while carrying over the code. Add the method `SampleClip::hasSampleFileLoaded` which checks if the loaded sample corresponds to a given file name. Fix code formatting. --- include/SampleClip.h | 2 ++ src/core/SampleClip.cpp | 14 ++++++++++++++ src/gui/clips/SampleClipView.cpp | 22 ++++++++++++---------- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/include/SampleClip.h b/include/SampleClip.h index da11996b1..3beca338b 100644 --- a/include/SampleClip.h +++ b/include/SampleClip.h @@ -55,7 +55,9 @@ public: SampleClip& operator=( const SampleClip& that ) = delete; void changeLength( const TimePos & _length ) override; + void changeLengthToSampleLength(); const QString& sampleFile() const; + bool hasSampleFileLoaded(const QString & filename) const; void saveSettings( QDomDocument & _doc, QDomElement & _parent ) override; void loadSettings( const QDomElement & _this ) override; diff --git a/src/core/SampleClip.cpp b/src/core/SampleClip.cpp index 42d4f6441..9a1c0731a 100644 --- a/src/core/SampleClip.cpp +++ b/src/core/SampleClip.cpp @@ -112,6 +112,11 @@ void SampleClip::changeLength( const TimePos & _length ) Clip::changeLength(std::max(static_cast(_length), 1)); } +void SampleClip::changeLengthToSampleLength() +{ + int length = m_sample.sampleSize() / Engine::framesPerTick(); + changeLength(length); +} @@ -120,6 +125,11 @@ const QString& SampleClip::sampleFile() const return m_sample.sampleFile(); } +bool SampleClip::hasSampleFileLoaded(const QString & filename) const +{ + return m_sample.sampleFile() == filename; +} + void SampleClip::setSampleBuffer(std::shared_ptr sb) { { @@ -129,6 +139,8 @@ void SampleClip::setSampleBuffer(std::shared_ptr sb) updateLength(); emit sampleChanged(); + + Engine::getSong()->setModified(); } void SampleClip::setSampleFile(const QString& sf) @@ -210,6 +222,8 @@ void SampleClip::setIsPlaying(bool isPlaying) void SampleClip::updateLength() { emit sampleChanged(); + + Engine::getSong()->setModified(); } diff --git a/src/gui/clips/SampleClipView.cpp b/src/gui/clips/SampleClipView.cpp index ef46325e4..30f09caa8 100644 --- a/src/gui/clips/SampleClipView.cpp +++ b/src/gui/clips/SampleClipView.cpp @@ -127,7 +127,6 @@ void SampleClipView::dropEvent( QDropEvent * _de ) m_clip->updateLength(); update(); _de->accept(); - Engine::getSong()->setModified(); } else { @@ -181,18 +180,21 @@ void SampleClipView::mouseReleaseEvent(QMouseEvent *_me) void SampleClipView::mouseDoubleClickEvent( QMouseEvent * ) { - QString af = SampleLoader::openAudioFile(); + const QString selectedAudioFile = SampleLoader::openAudioFile(); - if ( af.isEmpty() ) {} //Don't do anything if no file is loaded - else if (af == m_clip->m_sample.sampleFile()) - { //Instead of reloading the existing file, just reset the size - int length = static_cast(m_clip->m_sample.sampleSize() / Engine::framesPerTick()); - m_clip->changeLength(length); + if (selectedAudioFile.isEmpty()) { return; } + + if (m_clip->hasSampleFileLoaded(selectedAudioFile)) + { + m_clip->changeLengthToSampleLength(); } else - { //Otherwise load the new file as ususal - m_clip->setSampleFile( af ); - Engine::getSong()->setModified(); + { + auto sampleBuffer = SampleLoader::createBufferFromFile(selectedAudioFile); + if (sampleBuffer != SampleBuffer::emptyBuffer()) + { + m_clip->setSampleBuffer(sampleBuffer); + } } }