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>
36 #include "interpolation.h"
37 #include "lmms_basics.h"
38 #include "lmms_math.h"
39 #include "shared_object.h"
45 class EXPORT sampleBuffer
: public QObject
, public sharedObject
49 class EXPORT handleState
52 handleState( bool _varying_pitch
= false );
53 virtual ~handleState();
58 const bool m_varyingPitch
;
59 SRC_STATE
* m_resamplingData
;
61 friend class sampleBuffer
;
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
,
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
91 inline f_cnt_t
startFrame() const
96 inline f_cnt_t
endFrame() const
101 void setLoopStartFrame( f_cnt_t _start
)
104 m_loopStartFrame
= _start
;
108 void setLoopEndFrame( f_cnt_t _end
)
111 m_loopEndFrame
= _end
;
115 inline f_cnt_t
frames() const
120 inline float amplification() const
122 return m_amplification
;
125 inline bool reversed() const
130 inline float frequency() const
135 inline void setFrequency( float _freq
)
142 inline void setSampleRate( sample_rate_t _rate
)
145 m_sampleRate
= _rate
;
149 inline const sampleFrame
* data() const
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
,
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],
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
;
193 return m_data
[f1
][0];
196 static QString
tryToMakeRelative( const QString
& _file
);
197 static QString
tryToMakeAbsolute( const QString
& _file
);
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
);
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
);
221 f_cnt_t
decodeSampleDS( const char * _f
, int_sample_t
* & _buf
,
222 ch_cnt_t
& _channels
,
223 sample_rate_t
& _sample_rate
);
226 sampleFrame
* m_origData
;
227 f_cnt_t m_origFrames
;
228 sampleFrame
* m_data
;
231 f_cnt_t m_startFrame
;
233 f_cnt_t m_loopStartFrame
;
234 f_cnt_t m_loopEndFrame
;
235 float m_amplification
;
238 sample_rate_t m_sampleRate
;
240 sampleFrame
* getSampleFragment( f_cnt_t _start
, f_cnt_t _frames
,
242 sampleFrame
* * _tmp
) const;
243 f_cnt_t
getLoopedIndex( f_cnt_t _index
) const;
247 void sampleUpdated();