SampleBuffer.h 7.2 KB

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