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.
This commit is contained in:
Michael Gregorius
2024-04-29 19:22:37 +02:00
committed by GitHub
parent c0a4df49a2
commit bb6a77aa0f
3 changed files with 28 additions and 10 deletions

View File

@@ -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;

View File

@@ -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);
}
@@ -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)
{
{
@@ -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)
@@ -210,6 +222,8 @@ void SampleClip::setIsPlaying(bool isPlaying)
void SampleClip::updateLength()
{
emit sampleChanged();
Engine::getSong()->setModified();
}

View File

@@ -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<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);
}
}
}