Skip to content

Commit

Permalink
Only set sample clips for valid files (#7224)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
michaelgregorius committed Apr 29, 2024
1 parent c0a4df4 commit bb6a77a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
2 changes: 2 additions & 0 deletions include/SampleClip.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ class SampleClip : public Clip
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;
Expand Down
14 changes: 14 additions & 0 deletions src/core/SampleClip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ void SampleClip::changeLength( const TimePos & _length )
Clip::changeLength(std::max(static_cast<int>(_length), 1));
}

void SampleClip::changeLengthToSampleLength()
{
int length = m_sample.sampleSize() / Engine::framesPerTick();
changeLength(length);
}



Expand All @@ -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<const SampleBuffer> sb)
{
{
Expand All @@ -129,6 +139,8 @@ void SampleClip::setSampleBuffer(std::shared_ptr<const SampleBuffer> sb)
updateLength();

emit sampleChanged();

Engine::getSong()->setModified();
}

void SampleClip::setSampleFile(const QString& sf)
Expand Down Expand Up @@ -210,6 +222,8 @@ void SampleClip::setIsPlaying(bool isPlaying)
void SampleClip::updateLength()
{
emit sampleChanged();

Engine::getSong()->setModified();
}


Expand Down
22 changes: 12 additions & 10 deletions src/gui/clips/SampleClipView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ void SampleClipView::dropEvent( QDropEvent * _de )
m_clip->updateLength();
update();
_de->accept();
Engine::getSong()->setModified();
}
else
{
Expand Down Expand Up @@ -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<int>(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);
}
}
}

Expand Down

0 comments on commit bb6a77a

Please sign in to comment.