Skip to content

Commit

Permalink
Minimize Qt Usage and adjust parameter types
Browse files Browse the repository at this point in the history
* Sample::openSample() should be implemented in the Qt frontend
* Sample::visualize should be implemented in the Qt frontend
* SampleBufferV2::loadFromBase64 still needs an interface for decoding Base64 without Qt
  • Loading branch information
sakertooth committed Jun 8, 2022
1 parent 917a6f6 commit d21f960
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 289 deletions.
80 changes: 38 additions & 42 deletions include/Sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@
#ifndef SAMPLE_H
#define SAMPLE_H

#include <QObject>
#include <QPainter>
#include <QRect>
#include <QString>
#include <memory>
#include <samplerate.h>
#include <string>

#include "Note.h"
#include "SampleBufferCache.h"
Expand All @@ -48,72 +47,69 @@ class Sample
};

Sample() = default;
Sample(const QString& strData, SampleBufferV2::StrDataType dataType);
Sample(const sampleFrame* data, const f_cnt_t numFrames);
Sample(const std::string& strData, const SampleBufferV2::StrDataType dataType);
Sample(const sampleFrame* data, const int numFrames);
explicit Sample(const SampleBufferV2* buffer);
explicit Sample(const f_cnt_t numFrames);

explicit Sample(const int numFrames);
Sample(const Sample& other);
Sample& operator=(const Sample& other);

Sample(Sample&& other);
Sample& operator=(Sample&& other);

bool play(sampleFrame* dst, const fpp_t numFrames, const float freq);
void visualize(QPainter& painter, const QRect& drawingRect, f_cnt_t fromFrame = 0, f_cnt_t toFrame = 0);
Sample& operator=(Sample other);
friend void swap(Sample& first, Sample& second);

QString sampleFile() const;
bool play(sampleFrame* dst, const int numFrames, const float freq);
void visualize(QPainter& painter, const QRect& drawingRect, const int fromFrame = 0, const int toFrame = 0);

std::string sampleFile() const;
std::shared_ptr<const SampleBufferV2> sampleBuffer() const;
sample_rate_t sampleRate() const;
int sampleRate() const;
float amplification() const;
float frequency() const;
bool reversed() const;
bool varyingPitch() const;
int interpolationMode() const;
f_cnt_t startFrame() const;
f_cnt_t endFrame() const;
f_cnt_t loopStartFrame() const;
f_cnt_t loopEndFrame() const;
f_cnt_t frameIndex() const;
f_cnt_t numFrames() const;
int startFrame() const;
int endFrame() const;
int loopStartFrame() const;
int loopEndFrame() const;
int frameIndex() const;
int numFrames() const;
PlaybackType playback() const;

void setSampleData(const QString& str, SampleBufferV2::StrDataType dataType);
void setSampleData(const std::string& str, const SampleBufferV2::StrDataType dataType);
void setSampleBuffer(const SampleBufferV2* buffer);
void setAmplification(float amplification);
void setFrequency(float frequency);
void setReversed(bool reversed);
void setVaryingPitch(bool varyingPitch);
void setInterpolationMode(int interpolationMode);
void setStartFrame(f_cnt_t start);
void setEndFrame(f_cnt_t end);
void setLoopStartFrame(f_cnt_t loopStart);
void setLoopEndFrame(f_cnt_t loopEnd);
void setFrameIndex(f_cnt_t frameIndex);
void setPlayback(PlaybackType playback);

void loadAudioFile(const QString& audioFile);
void loadBase64(const QString& base64);
void setAmplification(const float amplification);
void setFrequency(const float frequency);
void setReversed(const bool reversed);
void setVaryingPitch(const bool varyingPitch);
void setInterpolationMode(const int interpolationMode);
void setStartFrame(const int start);
void setEndFrame(const int end);
void setLoopStartFrame(const int loopStart);
void setLoopEndFrame(const int loopEnd);
void setFrameIndex(const int frameIndex);
void setPlayback(const PlaybackType playback);

void loadAudioFile(const std::string& audioFile);
void loadBase64(const std::string& base64);
void resetMarkers();
int calculateTickLength() const;
QString openSample();

private:
std::shared_ptr<const SampleBufferV2> m_sampleBuffer = nullptr;
std::shared_ptr<const SampleBufferV2> m_sampleBuffer;
float m_amplification = 1.0f;
float m_frequency = DefaultBaseFreq;
bool m_reversed = false;
bool m_varyingPitch = false;
bool m_pingPongBackwards = false;
int m_interpolationMode = SRC_LINEAR;
f_cnt_t m_startFrame = 0;
f_cnt_t m_endFrame = 0;
f_cnt_t m_loopStartFrame = 0;
f_cnt_t m_loopEndFrame = 0;
f_cnt_t m_frameIndex = 0;
int m_startFrame = 0;
int m_endFrame = 0;
int m_loopStartFrame = 0;
int m_loopEndFrame = 0;
int m_frameIndex = 0;
PlaybackType m_playback = PlaybackType::Regular;
SRC_STATE* resampleState = nullptr;
SRC_STATE* m_resampleState = nullptr;
};

#endif
11 changes: 5 additions & 6 deletions include/SampleBufferCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,19 @@
#ifndef SAMPLE_BUFFER_CACHE_H
#define SAMPLE_BUFFER_CACHE_H

#include <QHash>
#include <QString>
#include <memory>
#include <string>

#include "SampleBufferV2.h"

class SampleBufferCache
{
public:
std::shared_ptr<const SampleBufferV2> get(const QString& id);
std::shared_ptr<const SampleBufferV2> add(const QString& id, const SampleBufferV2* buffer);

std::shared_ptr<const SampleBufferV2> get(const std::string& id);
std::shared_ptr<const SampleBufferV2> add(const std::string& id, const SampleBufferV2* buffer);
bool contains(const std::string& id);
private:
QHash<QString, std::weak_ptr<const SampleBufferV2>> m_hash;
std::unordered_map<std::string, std::weak_ptr<const SampleBufferV2>> m_map;
};

#endif
44 changes: 20 additions & 24 deletions include/SampleBufferV2.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@
#ifndef SAMPLE_BUFFER_V2_H
#define SAMPLE_BUFFER_V2_H

#include "Engine.h"
#include "AudioEngine.h"
#include "lmms_basics.h"

#include <QObject>
#include <QString>
#include <filesystem>
#include <memory>
#include <optional>
#include <vector>

#include "AudioEngine.h"
#include "Engine.h"
#include "lmms_basics.h"

class SampleBufferV2
{
Expand All @@ -44,35 +43,32 @@ class SampleBufferV2
Base64
};

SampleBufferV2(const QString& strData, StrDataType dataType);
SampleBufferV2(const sampleFrame* data, const f_cnt_t numFrames);
explicit SampleBufferV2(const f_cnt_t numFrames);

SampleBufferV2(const std::string& strData, const StrDataType dataType);
SampleBufferV2(const sampleFrame* data, const int numFrames);
SampleBufferV2(const SampleBufferV2& other) = delete;
SampleBufferV2& operator=(const SampleBufferV2& other) = delete;

SampleBufferV2(SampleBufferV2&& other);
explicit SampleBufferV2(const int numFrames);

SampleBufferV2& operator=(SampleBufferV2& other) = delete;
SampleBufferV2& operator=(SampleBufferV2&& other);

const std::vector<sampleFrame>& sampleData() const;
sample_rate_t originalSampleRate() const;
f_cnt_t numFrames() const;
const std::optional<std::filesystem::path>& filePath() const;
int originalSampleRate() const;

const QString& filePath() const;
bool hasFilePath() const;
std::string toBase64() const;
int numFrames() const;

QString toBase64() const;

private:
void loadFromAudioFile(const QString& audioFilePath);
void loadFromDrumSynthFile(const QString& drumSynthFilePath);
void loadFromBase64(const QString& str);
void resample(const sample_rate_t oldSampleRate, const sample_rate_t newSampleRate);
void loadFromAudioFile(const std::filesystem::path& audioFilePath);
void loadFromDrumSynthFile(const std::filesystem::path& drumSynthFilePath);
void loadFromBase64(const std::string& base64);
void resample(const int oldSampleRate, const int newSampleRate);

private:
std::vector<sampleFrame> m_sampleData;
sample_rate_t m_originalSampleRate;
QString m_filePath;
std::optional<std::filesystem::path> m_filePath;
int m_originalSampleRate = 0;
};

#endif
Loading

0 comments on commit d21f960

Please sign in to comment.