SampleBuffer.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * SampleBuffer.h - container-class SampleBuffer
  3. *
  4. * Copyright (c) 2005-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program (see COPYING); if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301 USA.
  22. *
  23. */
  24. #ifndef SAMPLE_BUFFER_H
  25. #define SAMPLE_BUFFER_H
  26. #include <memory>
  27. #include <QtCore/QReadWriteLock>
  28. #include <QtCore/QObject>
  29. #include <samplerate.h>
  30. #include "lmms_export.h"
  31. #include "interpolation.h"
  32. #include "lmms_basics.h"
  33. #include "lmms_math.h"
  34. #include "shared_object.h"
  35. #include "OscillatorConstants.h"
  36. #include "MemoryManager.h"
  37. class QPainter;
  38. class QRect;
  39. // values for buffer margins, used for various libsamplerate interpolation modes
  40. // the array positions correspond to the converter_type parameter values in libsamplerate
  41. // if there appears problems with playback on some interpolation mode, then the value for that mode
  42. // may need to be higher - conversely, to optimize, some may work with lower values
  43. const f_cnt_t MARGIN[] = { 64, 64, 64, 4, 4 };
  44. class LMMS_EXPORT SampleBuffer : public QObject, public sharedObject
  45. {
  46. Q_OBJECT
  47. MM_OPERATORS
  48. public:
  49. enum LoopMode {
  50. LoopOff = 0,
  51. LoopOn,
  52. LoopPingPong
  53. };
  54. class LMMS_EXPORT handleState
  55. {
  56. MM_OPERATORS
  57. public:
  58. handleState(bool varyingPitch = false, int interpolationMode = SRC_LINEAR);
  59. virtual ~handleState();
  60. const f_cnt_t frameIndex() const
  61. {
  62. return m_frameIndex;
  63. }
  64. void setFrameIndex(f_cnt_t index)
  65. {
  66. m_frameIndex = index;
  67. }
  68. bool isBackwards() const
  69. {
  70. return m_isBackwards;
  71. }
  72. void setBackwards(bool backwards)
  73. {
  74. m_isBackwards = backwards;
  75. }
  76. int interpolationMode() const
  77. {
  78. return m_interpolationMode;
  79. }
  80. private:
  81. f_cnt_t m_frameIndex;
  82. const bool m_varyingPitch;
  83. bool m_isBackwards;
  84. SRC_STATE * m_resamplingData;
  85. int m_interpolationMode;
  86. friend class SampleBuffer;
  87. } ;
  88. SampleBuffer();
  89. // constructor which either loads sample _audio_file or decodes
  90. // base64-data out of string
  91. SampleBuffer(const QString & audioFile, bool isBase64Data = false);
  92. SampleBuffer(const sampleFrame * data, const f_cnt_t frames);
  93. explicit SampleBuffer(const f_cnt_t frames);
  94. SampleBuffer(const SampleBuffer & orig);
  95. friend void swap(SampleBuffer & first, SampleBuffer & second) noexcept;
  96. SampleBuffer& operator= (const SampleBuffer that);
  97. virtual ~SampleBuffer();
  98. bool play(
  99. sampleFrame * ab,
  100. handleState * state,
  101. const fpp_t frames,
  102. const float freq,
  103. const LoopMode loopMode = LoopOff
  104. );
  105. void visualize(
  106. QPainter & p,
  107. const QRect & dr,
  108. const QRect & clip,
  109. f_cnt_t fromFrame = 0,
  110. f_cnt_t toFrame = 0
  111. );
  112. inline void visualize(
  113. QPainter & p,
  114. const QRect & dr,
  115. f_cnt_t fromFrame = 0,
  116. f_cnt_t toFrame = 0
  117. )
  118. {
  119. visualize(p, dr, dr, fromFrame, toFrame);
  120. }
  121. inline const QString & audioFile() const
  122. {
  123. return m_audioFile;
  124. }
  125. inline f_cnt_t startFrame() const
  126. {
  127. return m_startFrame;
  128. }
  129. inline f_cnt_t endFrame() const
  130. {
  131. return m_endFrame;
  132. }
  133. inline f_cnt_t loopStartFrame() const
  134. {
  135. return m_loopStartFrame;
  136. }
  137. inline f_cnt_t loopEndFrame() const
  138. {
  139. return m_loopEndFrame;
  140. }
  141. void setLoopStartFrame(f_cnt_t start)
  142. {
  143. m_loopStartFrame = start;
  144. }
  145. void setLoopEndFrame(f_cnt_t end)
  146. {
  147. m_loopEndFrame = end;
  148. }
  149. void setAllPointFrames(
  150. f_cnt_t start,
  151. f_cnt_t end,
  152. f_cnt_t loopStart,
  153. f_cnt_t loopEnd
  154. )
  155. {
  156. m_startFrame = start;
  157. m_endFrame = end;
  158. m_loopStartFrame = loopStart;
  159. m_loopEndFrame = loopEnd;
  160. }
  161. inline f_cnt_t frames() const
  162. {
  163. return m_frames;
  164. }
  165. inline float amplification() const
  166. {
  167. return m_amplification;
  168. }
  169. inline bool reversed() const
  170. {
  171. return m_reversed;
  172. }
  173. inline float frequency() const
  174. {
  175. return m_frequency;
  176. }
  177. sample_rate_t sampleRate() const
  178. {
  179. return m_sampleRate;
  180. }
  181. int sampleLength() const
  182. {
  183. return double(m_endFrame - m_startFrame) / m_sampleRate * 1000;
  184. }
  185. inline void setFrequency(float freq)
  186. {
  187. m_frequency = freq;
  188. }
  189. inline void setSampleRate(sample_rate_t rate)
  190. {
  191. m_sampleRate = rate;
  192. }
  193. inline const sampleFrame * data() const
  194. {
  195. return m_data;
  196. }
  197. QString openAudioFile() const;
  198. QString openAndSetAudioFile();
  199. QString openAndSetWaveformFile();
  200. QString & toBase64(QString & dst) const;
  201. // protect calls from the GUI to this function with dataReadLock() and
  202. // dataUnlock()
  203. SampleBuffer * resample(const sample_rate_t srcSR, const sample_rate_t dstSR);
  204. void normalizeSampleRate(const sample_rate_t srcSR, bool keepSettings = false);
  205. // protect calls from the GUI to this function with dataReadLock() and
  206. // dataUnlock(), out of loops for efficiency
  207. inline sample_t userWaveSample(const float sample) const
  208. {
  209. f_cnt_t frames = m_frames;
  210. sampleFrame * data = m_data;
  211. const float frame = sample * frames;
  212. f_cnt_t f1 = static_cast<f_cnt_t>(frame) % frames;
  213. if (f1 < 0)
  214. {
  215. f1 += frames;
  216. }
  217. return linearInterpolate(data[f1][0], data[(f1 + 1) % frames][0], fraction(frame));
  218. }
  219. void dataReadLock()
  220. {
  221. m_varLock.lockForRead();
  222. }
  223. void dataUnlock()
  224. {
  225. m_varLock.unlock();
  226. }
  227. std::unique_ptr<OscillatorConstants::waveform_t> m_userAntiAliasWaveTable;
  228. public slots:
  229. void setAudioFile(const QString & audioFile);
  230. void loadFromBase64(const QString & data);
  231. void setStartFrame(const f_cnt_t s);
  232. void setEndFrame(const f_cnt_t e);
  233. void setAmplification(float a);
  234. void setReversed(bool on);
  235. void sampleRateChanged();
  236. private:
  237. static sample_rate_t audioEngineSampleRate();
  238. void update(bool keepSettings = false);
  239. void convertIntToFloat(int_sample_t * & ibuf, f_cnt_t frames, int channels);
  240. void directFloatWrite(sample_t * & fbuf, f_cnt_t frames, int channels);
  241. f_cnt_t decodeSampleSF(
  242. QString fileName,
  243. sample_t * & buf,
  244. ch_cnt_t & channels,
  245. sample_rate_t & samplerate
  246. );
  247. #ifdef LMMS_HAVE_OGGVORBIS
  248. f_cnt_t decodeSampleOGGVorbis(
  249. QString fileName,
  250. int_sample_t * & buf,
  251. ch_cnt_t & channels,
  252. sample_rate_t & samplerate
  253. );
  254. #endif
  255. f_cnt_t decodeSampleDS(
  256. QString fileName,
  257. int_sample_t * & buf,
  258. ch_cnt_t & channels,
  259. sample_rate_t & samplerate
  260. );
  261. QString m_audioFile;
  262. sampleFrame * m_origData;
  263. f_cnt_t m_origFrames;
  264. sampleFrame * m_data;
  265. mutable QReadWriteLock m_varLock;
  266. f_cnt_t m_frames;
  267. f_cnt_t m_startFrame;
  268. f_cnt_t m_endFrame;
  269. f_cnt_t m_loopStartFrame;
  270. f_cnt_t m_loopEndFrame;
  271. float m_amplification;
  272. bool m_reversed;
  273. float m_frequency;
  274. sample_rate_t m_sampleRate;
  275. sampleFrame * getSampleFragment(
  276. f_cnt_t index,
  277. f_cnt_t frames,
  278. LoopMode loopMode,
  279. sampleFrame * * tmp,
  280. bool * backwards,
  281. f_cnt_t loopStart,
  282. f_cnt_t loopEnd,
  283. f_cnt_t end
  284. ) const;
  285. f_cnt_t getLoopedIndex(f_cnt_t index, f_cnt_t startf, f_cnt_t endf) const;
  286. f_cnt_t getPingPongIndex(f_cnt_t index, f_cnt_t startf, f_cnt_t endf) const;
  287. signals:
  288. void sampleUpdated();
  289. } ;
  290. #endif