SampleBuffer.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 "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 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 EXPORT handleState
  53. {
  54. MM_OPERATORS
  55. public:
  56. handleState( bool _varying_pitch = false, int interpolation_mode = 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. // constructor which either loads sample _audio_file or decodes
  87. // base64-data out of string
  88. SampleBuffer( const QString & _audio_file = QString(),
  89. bool _is_base64_data = false );
  90. SampleBuffer( const sampleFrame * _data, const f_cnt_t _frames );
  91. SampleBuffer( const f_cnt_t _frames );
  92. virtual ~SampleBuffer();
  93. bool play( sampleFrame * _ab, handleState * _state,
  94. const fpp_t _frames,
  95. const float _freq,
  96. const LoopMode _loopmode = LoopOff );
  97. void visualize( QPainter & _p, const QRect & _dr, const QRect & _clip, f_cnt_t _from_frame = 0, f_cnt_t _to_frame = 0 );
  98. inline void visualize( QPainter & _p, const QRect & _dr, f_cnt_t _from_frame = 0, f_cnt_t _to_frame = 0 )
  99. {
  100. visualize( _p, _dr, _dr, _from_frame, _to_frame );
  101. }
  102. inline const QString & audioFile() const
  103. {
  104. return m_audioFile;
  105. }
  106. inline f_cnt_t startFrame() const
  107. {
  108. return m_startFrame;
  109. }
  110. inline f_cnt_t endFrame() const
  111. {
  112. return m_endFrame;
  113. }
  114. inline f_cnt_t loopStartFrame() const
  115. {
  116. return m_loopStartFrame;
  117. }
  118. inline f_cnt_t loopEndFrame() const
  119. {
  120. return m_loopEndFrame;
  121. }
  122. void setLoopStartFrame( f_cnt_t _start )
  123. {
  124. m_loopStartFrame = _start;
  125. }
  126. void setLoopEndFrame( f_cnt_t _end )
  127. {
  128. m_loopEndFrame = _end;
  129. }
  130. void setAllPointFrames( f_cnt_t _start, f_cnt_t _end, f_cnt_t _loopstart, f_cnt_t _loopend )
  131. {
  132. m_startFrame = _start;
  133. m_endFrame = _end;
  134. m_loopStartFrame = _loopstart;
  135. m_loopEndFrame = _loopend;
  136. }
  137. inline f_cnt_t frames() const
  138. {
  139. return m_frames;
  140. }
  141. inline float amplification() const
  142. {
  143. return m_amplification;
  144. }
  145. inline bool reversed() const
  146. {
  147. return m_reversed;
  148. }
  149. inline float frequency() const
  150. {
  151. return m_frequency;
  152. }
  153. sample_rate_t sampleRate() const
  154. {
  155. return m_sampleRate;
  156. }
  157. int sampleLength() const
  158. {
  159. return double( m_endFrame - m_startFrame ) / m_sampleRate * 1000;
  160. }
  161. inline void setFrequency( float _freq )
  162. {
  163. m_frequency = _freq;
  164. }
  165. inline void setSampleRate( sample_rate_t _rate )
  166. {
  167. m_sampleRate = _rate;
  168. }
  169. inline const sampleFrame * data() const
  170. {
  171. return m_data;
  172. }
  173. QString openAudioFile() const;
  174. QString openAndSetAudioFile();
  175. QString openAndSetWaveformFile();
  176. QString & toBase64( QString & _dst ) const;
  177. // protect calls from the GUI to this function with dataReadLock() and
  178. // dataUnlock()
  179. SampleBuffer * resample( const sample_rate_t _src_sr,
  180. const sample_rate_t _dst_sr );
  181. void normalizeSampleRate( const sample_rate_t _src_sr,
  182. bool _keep_settings = false );
  183. // protect calls from the GUI to this function with dataReadLock() and
  184. // dataUnlock(), out of loops for efficiency
  185. inline sample_t userWaveSample( const float _sample ) const
  186. {
  187. f_cnt_t frames = m_frames;
  188. sampleFrame * data = m_data;
  189. const float frame = _sample * frames;
  190. f_cnt_t f1 = static_cast<f_cnt_t>( frame ) % frames;
  191. if( f1 < 0 )
  192. {
  193. f1 += frames;
  194. }
  195. return linearInterpolate( data[f1][0], data[ (f1 + 1) % frames ][0], fraction( frame ) );
  196. }
  197. void dataReadLock()
  198. {
  199. m_varLock.lockForRead();
  200. }
  201. void dataUnlock()
  202. {
  203. m_varLock.unlock();
  204. }
  205. static QString tryToMakeRelative( const QString & _file );
  206. static QString tryToMakeAbsolute(const QString & file);
  207. public slots:
  208. void setAudioFile( const QString & _audio_file );
  209. void loadFromBase64( const QString & _data );
  210. void setStartFrame( const f_cnt_t _s );
  211. void setEndFrame( const f_cnt_t _e );
  212. void setAmplification( float _a );
  213. void setReversed( bool _on );
  214. void sampleRateChanged();
  215. private:
  216. static sample_rate_t mixerSampleRate();
  217. void update( bool _keep_settings = false );
  218. void convertIntToFloat ( int_sample_t * & _ibuf, f_cnt_t _frames, int _channels);
  219. void directFloatWrite ( sample_t * & _fbuf, f_cnt_t _frames, int _channels);
  220. f_cnt_t decodeSampleSF( QString _f, sample_t * & _buf,
  221. ch_cnt_t & _channels,
  222. sample_rate_t & _sample_rate );
  223. #ifdef LMMS_HAVE_OGGVORBIS
  224. f_cnt_t decodeSampleOGGVorbis( QString _f, int_sample_t * & _buf,
  225. ch_cnt_t & _channels,
  226. sample_rate_t & _sample_rate );
  227. #endif
  228. f_cnt_t decodeSampleDS( QString _f, int_sample_t * & _buf,
  229. ch_cnt_t & _channels,
  230. sample_rate_t & _sample_rate );
  231. QString m_audioFile;
  232. sampleFrame * m_origData;
  233. f_cnt_t m_origFrames;
  234. sampleFrame * m_data;
  235. QReadWriteLock m_varLock;
  236. f_cnt_t m_frames;
  237. f_cnt_t m_startFrame;
  238. f_cnt_t m_endFrame;
  239. f_cnt_t m_loopStartFrame;
  240. f_cnt_t m_loopEndFrame;
  241. float m_amplification;
  242. bool m_reversed;
  243. float m_frequency;
  244. sample_rate_t m_sampleRate;
  245. sampleFrame * getSampleFragment( f_cnt_t _index, f_cnt_t _frames,
  246. LoopMode _loopmode,
  247. sampleFrame * * _tmp,
  248. bool * _backwards, f_cnt_t _loopstart, f_cnt_t _loopend,
  249. f_cnt_t _end ) const;
  250. f_cnt_t getLoopedIndex( f_cnt_t _index, f_cnt_t _startf, f_cnt_t _endf ) const;
  251. f_cnt_t getPingPongIndex( f_cnt_t _index, f_cnt_t _startf, f_cnt_t _endf ) const;
  252. signals:
  253. void sampleUpdated();
  254. } ;
  255. #endif