|
| 1 | +/* |
| 2 | + * SampleThumbnail.h |
| 3 | + * |
| 4 | + * Copyright (c) 2024 Khoi Dau <casboi86@gmail.com> |
| 5 | + * Copyright (c) 2024 Sotonye Atemie <sakertooth@gmail.com> |
| 6 | + * |
| 7 | + * This file is part of LMMS - https://lmms.io |
| 8 | + * |
| 9 | + * This program is free software; you can redistribute it and/or |
| 10 | + * modify it under the terms of the GNU General Public |
| 11 | + * License as published by the Free Software Foundation; either |
| 12 | + * version 2 of the License, or (at your option) any later version. |
| 13 | + * |
| 14 | + * This program is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | + * General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU General Public |
| 20 | + * License along with this program (see COPYING); if not, write to the |
| 21 | + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 22 | + * Boston, MA 02110-1301 USA. |
| 23 | + * |
| 24 | + */ |
| 25 | + |
| 26 | +#ifndef LMMS_SAMPLE_THUMBNAIL_H |
| 27 | +#define LMMS_SAMPLE_THUMBNAIL_H |
| 28 | + |
| 29 | +#include <QDateTime> |
| 30 | +#include <QPainter> |
| 31 | +#include <QRect> |
| 32 | +#include <memory> |
| 33 | + |
| 34 | +#include "Sample.h" |
| 35 | +#include "lmms_export.h" |
| 36 | + |
| 37 | +namespace lmms { |
| 38 | + |
| 39 | +/** |
| 40 | + Allows for visualizing sample data. |
| 41 | +
|
| 42 | + On construction, thumbnails will be generated |
| 43 | + at logarathmic intervals of downsampling. Those cached thumbnails will then be further downsampled on the fly and |
| 44 | + transformed in various ways to create the desired waveform. |
| 45 | +
|
| 46 | + Given that we are dealing with far less data to generate |
| 47 | + the visualization however (i.e., we are not reading from original sample data when drawing), this provides a |
| 48 | + significant performance boost that wouldn't be possible otherwise. |
| 49 | + */ |
| 50 | +class LMMS_EXPORT SampleThumbnail |
| 51 | +{ |
| 52 | +public: |
| 53 | + struct VisualizeParameters |
| 54 | + { |
| 55 | + QRect sampleRect; //!< A rectangle that covers the entire range of samples. |
| 56 | + |
| 57 | + QRect drawRect; //!< Specifies the location in `sampleRect` where the waveform will be drawn. Equals |
| 58 | + //!< `sampleRect` when null. |
| 59 | + |
| 60 | + QRect viewportRect; //!< Clips `drawRect`. Equals `drawRect` when null. |
| 61 | + |
| 62 | + float amplification = 1.0f; //!< The amount of amplification to apply to the waveform. |
| 63 | + |
| 64 | + float sampleStart = 0.0f; //!< Where the sample begins for drawing. |
| 65 | + |
| 66 | + float sampleEnd = 1.0f; //!< Where the sample ends for drawing. |
| 67 | + |
| 68 | + bool reversed = false; //!< Determines if the waveform is drawn in reverse or not. |
| 69 | + }; |
| 70 | + |
| 71 | + SampleThumbnail() = default; |
| 72 | + SampleThumbnail(const Sample& sample); |
| 73 | + void visualize(VisualizeParameters parameters, QPainter& painter) const; |
| 74 | + |
| 75 | +private: |
| 76 | + class Thumbnail |
| 77 | + { |
| 78 | + public: |
| 79 | + struct Peak |
| 80 | + { |
| 81 | + Peak() = default; |
| 82 | + |
| 83 | + Peak(float min, float max) |
| 84 | + : min(min) |
| 85 | + , max(max) |
| 86 | + { |
| 87 | + } |
| 88 | + |
| 89 | + Peak(const SampleFrame& frame) |
| 90 | + : min(std::min(frame.left(), frame.right())) |
| 91 | + , max(std::max(frame.left(), frame.right())) |
| 92 | + { |
| 93 | + } |
| 94 | + |
| 95 | + Peak operator+(const Peak& other) const { return Peak(std::min(min, other.min), std::max(max, other.max)); } |
| 96 | + Peak operator+(const SampleFrame& frame) const { return *this + Peak{frame}; } |
| 97 | + |
| 98 | + float min = std::numeric_limits<float>::max(); |
| 99 | + float max = std::numeric_limits<float>::min(); |
| 100 | + }; |
| 101 | + |
| 102 | + Thumbnail() = default; |
| 103 | + Thumbnail(std::vector<Peak> peaks, double samplesPerPeak); |
| 104 | + Thumbnail(const float* buffer, size_t size, size_t width); |
| 105 | + |
| 106 | + Thumbnail zoomOut(float factor) const; |
| 107 | + |
| 108 | + Peak& operator[](size_t index) { return m_peaks[index]; } |
| 109 | + const Peak& operator[](size_t index) const { return m_peaks[index]; } |
| 110 | + |
| 111 | + int width() const { return m_peaks.size(); } |
| 112 | + double samplesPerPeak() const { return m_samplesPerPeak; } |
| 113 | + |
| 114 | + private: |
| 115 | + std::vector<Peak> m_peaks; |
| 116 | + double m_samplesPerPeak = 0.0; |
| 117 | + }; |
| 118 | + |
| 119 | + struct SampleThumbnailEntry |
| 120 | + { |
| 121 | + QString filePath; |
| 122 | + QDateTime lastModified; |
| 123 | + |
| 124 | + friend bool operator==(const SampleThumbnailEntry& first, const SampleThumbnailEntry& second) |
| 125 | + { |
| 126 | + return first.filePath == second.filePath && first.lastModified == second.lastModified; |
| 127 | + } |
| 128 | + }; |
| 129 | + |
| 130 | + struct Hash |
| 131 | + { |
| 132 | + std::size_t operator()(const SampleThumbnailEntry& entry) const noexcept { return qHash(entry.filePath); } |
| 133 | + }; |
| 134 | + |
| 135 | + using ThumbnailCache = std::vector<Thumbnail>; |
| 136 | + std::shared_ptr<ThumbnailCache> m_thumbnailCache = std::make_shared<ThumbnailCache>(); |
| 137 | + |
| 138 | + inline static std::unordered_map<SampleThumbnailEntry, std::shared_ptr<ThumbnailCache>, Hash> s_sampleThumbnailCacheMap; |
| 139 | +}; |
| 140 | + |
| 141 | +} // namespace lmms |
| 142 | + |
| 143 | +#endif // LMMS_SAMPLE_THUMBNAIL_H |
0 commit comments