Skip to content

Commit

Permalink
Serialize all data within SampleBuffer when using Base64
Browse files Browse the repository at this point in the history
  • Loading branch information
sakertooth committed Jan 21, 2023
1 parent edf404c commit f3c35b6
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 17 deletions.
2 changes: 1 addition & 1 deletion include/Sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ namespace lmms
Sample(std::shared_ptr<SampleBuffer> buffer);

static auto createFromAudioFile(const QString& audioFile) -> std::shared_ptr<Sample>;
static auto createFromBase64(const QString& base64, sample_rate_t sampleRate = Engine::audioEngine()->processingSampleRate()) -> std::shared_ptr<Sample>;
static auto createFromBase64(const QString& base64) -> std::shared_ptr<Sample>;

auto play(sampleFrame* dst, PlaybackState* state, fpp_t frames, float freq, LoopMode loopMode = LoopMode::LoopOff) -> bool;

Expand Down
4 changes: 2 additions & 2 deletions include/SampleBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class LMMS_EXPORT SampleBuffer
SampleBuffer& operator=(SampleBuffer&& other);

static std::shared_ptr<SampleBuffer> createFromAudioFile(const QString& audioFile);
static std::shared_ptr<SampleBuffer> createFromBase64(const QString& base64, sample_rate_t sampleRate = audioEngineSampleRate());
static std::shared_ptr<SampleBuffer> createFromBase64(const QString& base64);

void resample(sample_rate_t newSampleRate);
void normalizeSampleRate(sample_rate_t srcSR, bool keepSettings = false);
Expand All @@ -91,7 +91,7 @@ public slots:
static sample_rate_t audioEngineSampleRate();

void loadFromAudioFile(const QString& audioFile);
void loadFromBase64(const QString& data, sample_rate_t sampleRate = audioEngineSampleRate());
void loadFromBase64(const QString& data);

void decodeSampleSF(const QString& fileName);
void decodeSampleDS(const QString& fileName);
Expand Down
4 changes: 2 additions & 2 deletions src/core/Sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ namespace lmms
return std::make_shared<Sample>(buffer);
}

auto Sample::createFromBase64(const QString& base64, sample_rate_t sampleRate) -> std::shared_ptr<Sample>
auto Sample::createFromBase64(const QString& base64) -> std::shared_ptr<Sample>
{
auto buffer = SampleBuffer::createFromBase64(base64, sampleRate);
auto buffer = SampleBuffer::createFromBase64(base64);
return std::make_shared<Sample>(buffer);
}

Expand Down
20 changes: 9 additions & 11 deletions src/core/SampleBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ std::shared_ptr<SampleBuffer> SampleBuffer::createFromAudioFile(const QString& a
return buffer;
}

std::shared_ptr<SampleBuffer> SampleBuffer::createFromBase64(const QString& base64, sample_rate_t sampleRate)
std::shared_ptr<SampleBuffer> SampleBuffer::createFromBase64(const QString& base64)
{
auto buffer = std::make_shared<SampleBuffer>();
buffer->loadFromBase64(base64, sampleRate);
buffer->loadFromBase64(base64);
return buffer;
}

Expand Down Expand Up @@ -268,8 +268,8 @@ void SampleBuffer::decodeSampleDS(const QString& fileName)
QString SampleBuffer::toBase64() const
{
// TODO: Replace with non-Qt equivalent
auto data = reinterpret_cast<const char*>(m_data.data());
auto byteArray = QByteArray{data, static_cast<int>(m_data.size() * sizeof(sampleFrame))};
auto data = reinterpret_cast<const char*>(this);
auto byteArray = QByteArray{data, sizeof(*this)};
return QString::fromUtf8(byteArray.toBase64());
}

Expand Down Expand Up @@ -340,22 +340,20 @@ void SampleBuffer::loadFromAudioFile(const QString& audioFile)
Engine::audioEngine()->doneChangeInModel();
}

void SampleBuffer::loadFromBase64(const QString& data, sample_rate_t sampleRate)
void SampleBuffer::loadFromBase64(const QString& data)
{
if (data.isEmpty()) { return; }

Engine::audioEngine()->requestChangeInModel();
const auto lockGuard = std::unique_lock{m_mutex};

// TODO: Replace with non-Qt equivalent
const auto base64Data = data.toUtf8().toBase64();
const auto sampleFrameData = reinterpret_cast<const sampleFrame*>(base64Data.constData());
const auto numFrames = base64Data.size() / sizeof(sampleFrame);
auto base64Data = data.toUtf8().toBase64();
auto sampleBufferData = reinterpret_cast<SampleBuffer*>(base64Data.data());

m_data = std::vector<sampleFrame>(sampleFrameData, sampleFrameData + numFrames);
m_sampleRate = sampleRate;
*this = std::move(*sampleBufferData);

if (sampleRate != audioEngineSampleRate())
if (m_sampleRate != audioEngineSampleRate())
{
resample(audioEngineSampleRate());
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/SampleClip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ void SampleClip::loadSettings( const QDomElement & _this )
setSampleFile( _this.attribute( "src" ) );
if( sampleFile().isEmpty() && _this.hasAttribute( "data" ) )
{
m_sample = Sample::createFromBase64(_this.attribute( "data" ), _this.attribute( "sample_rate" ).toInt());
m_sample = Sample::createFromBase64(_this.attribute("data"));
}
changeLength( _this.attribute( "len" ).toInt() );
setMuted( _this.attribute( "muted" ).toInt() );
Expand Down

0 comments on commit f3c35b6

Please sign in to comment.