Made 0.4.12 release
[lmms/mlankhorst.git] / include / sample_buffer.h
blob43c36a6bc984e6006f59ad1bc8710aec25d075dc
1 /*
2 * sample_buffer.h - container-class sampleBuffer
4 * Copyright (c) 2005-2009 Tobias Doerffel <tobydox/at/users.sourceforge.net>
6 * This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
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.
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.
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.
26 #ifndef _SAMPLE_BUFFER_H
27 #define _SAMPLE_BUFFER_H
29 #include <QtCore/QMutex>
30 #include <QtCore/QObject>
31 #include <QtCore/QRect>
33 #include <samplerate.h>
35 #include "export.h"
36 #include "interpolation.h"
37 #include "lmms_basics.h"
38 #include "lmms_math.h"
39 #include "shared_object.h"
42 class QPainter;
45 class EXPORT sampleBuffer : public QObject, public sharedObject
47 Q_OBJECT
48 public:
49 class EXPORT handleState
51 public:
52 handleState( bool _varying_pitch = false );
53 virtual ~handleState();
56 private:
57 f_cnt_t m_frameIndex;
58 const bool m_varyingPitch;
59 SRC_STATE * m_resamplingData;
61 friend class sampleBuffer;
63 } ;
66 // constructor which either loads sample _audio_file or decodes
67 // base64-data out of string
68 sampleBuffer( const QString & _audio_file = QString(),
69 bool _is_base64_data = false );
70 sampleBuffer( const sampleFrame * _data, const f_cnt_t _frames );
71 sampleBuffer( const f_cnt_t _frames );
73 virtual ~sampleBuffer();
75 bool play( sampleFrame * _ab, handleState * _state,
76 const fpp_t _frames,
77 const float _freq,
78 const bool _looped = false );
80 void visualize( QPainter & _p, const QRect & _dr, const QRect & _clip );
81 inline void visualize( QPainter & _p, const QRect & _dr )
83 visualize( _p, _dr, _dr );
86 inline const QString & audioFile() const
88 return m_audioFile;
91 inline f_cnt_t startFrame() const
93 return m_startFrame;
96 inline f_cnt_t endFrame() const
98 return m_endFrame;
101 void setLoopStartFrame( f_cnt_t _start )
103 m_varLock.lock();
104 m_loopStartFrame = _start;
105 m_varLock.unlock();
108 void setLoopEndFrame( f_cnt_t _end )
110 m_varLock.lock();
111 m_loopEndFrame = _end;
112 m_varLock.unlock();
115 inline f_cnt_t frames() const
117 return m_frames;
120 inline float amplification() const
122 return m_amplification;
125 inline bool reversed() const
127 return m_reversed;
130 inline float frequency() const
132 return m_frequency;
135 inline void setFrequency( float _freq )
137 m_varLock.lock();
138 m_frequency = _freq;
139 m_varLock.unlock();
142 inline void setSampleRate( sample_rate_t _rate )
144 m_varLock.lock();
145 m_sampleRate = _rate;
146 m_varLock.unlock();
149 inline const sampleFrame * data() const
151 return m_data;
154 QString openAudioFile() const;
156 QString & toBase64( QString & _dst ) const;
159 static sampleBuffer * resample( sampleFrame * _data,
160 const f_cnt_t _frames,
161 const sample_rate_t _src_sr,
162 const sample_rate_t _dst_sr );
164 static inline sampleBuffer * resample( sampleBuffer * _buf,
165 const sample_rate_t _src_sr,
166 const sample_rate_t _dst_sr )
168 return resample( _buf->m_data, _buf->m_frames, _src_sr,
169 _dst_sr );
172 void normalizeSampleRate( const sample_rate_t _src_sr,
173 bool _keep_settings = false );
175 inline sample_t userWaveSample( const float _sample ) const
177 // Precise implementation
178 // const float frame = fraction( _sample ) * m_frames;
179 // const f_cnt_t f1 = static_cast<f_cnt_t>( frame );
180 // const f_cnt_t f2 = ( f1 + 1 ) % m_frames;
181 // sample_t waveSample = linearInterpolate( m_data[f1][0],
182 // m_data[f2][0],
183 // fraction( frame ) );
184 // return waveSample;
186 // Fast implementation
187 const float frame = _sample * m_frames;
188 f_cnt_t f1 = static_cast<f_cnt_t>( frame ) % m_frames;
189 if( f1 < 0 )
191 f1 += m_frames;
193 return m_data[f1][0];
196 static QString tryToMakeRelative( const QString & _file );
197 static QString tryToMakeAbsolute( const QString & _file );
200 public slots:
201 void setAudioFile( const QString & _audio_file );
202 void loadFromBase64( const QString & _data );
203 void setStartFrame( const f_cnt_t _s );
204 void setEndFrame( const f_cnt_t _e );
205 void setAmplification( float _a );
206 void setReversed( bool _on );
209 private:
210 void update( bool _keep_settings = false );
213 f_cnt_t decodeSampleSF( const char * _f, int_sample_t * & _buf,
214 ch_cnt_t & _channels,
215 sample_rate_t & _sample_rate );
216 #ifdef LMMS_HAVE_OGGVORBIS
217 f_cnt_t decodeSampleOGGVorbis( const char * _f, int_sample_t * & _buf,
218 ch_cnt_t & _channels,
219 sample_rate_t & _sample_rate );
220 #endif
221 f_cnt_t decodeSampleDS( const char * _f, int_sample_t * & _buf,
222 ch_cnt_t & _channels,
223 sample_rate_t & _sample_rate );
225 QString m_audioFile;
226 sampleFrame * m_origData;
227 f_cnt_t m_origFrames;
228 sampleFrame * m_data;
229 QMutex m_varLock;
230 f_cnt_t m_frames;
231 f_cnt_t m_startFrame;
232 f_cnt_t m_endFrame;
233 f_cnt_t m_loopStartFrame;
234 f_cnt_t m_loopEndFrame;
235 float m_amplification;
236 bool m_reversed;
237 float m_frequency;
238 sample_rate_t m_sampleRate;
240 sampleFrame * getSampleFragment( f_cnt_t _start, f_cnt_t _frames,
241 bool _looped,
242 sampleFrame * * _tmp ) const;
243 f_cnt_t getLoopedIndex( f_cnt_t _index ) const;
246 signals:
247 void sampleUpdated();
252 #endif