hydrogen 1.2.3
Sample.h
Go to the documentation of this file.
1/*
2 * Hydrogen
3 * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net]
4 * Copyright(c) 2008-2024 The hydrogen development team [hydrogen-devel@lists.sourceforge.net]
5 *
6 * http://www.hydrogen-music.org
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (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
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see https://www.gnu.org/licenses
20 *
21 */
22
23#ifndef H2C_SAMPLE_H
24#define H2C_SAMPLE_H
25
26#include <memory>
27#include <vector>
28#include <sndfile.h>
29
30#include <core/License.h>
31#include <core/Object.h>
32
33namespace H2Core
34{
35
42class EnvelopePoint : public H2Core::Object<EnvelopePoint>
43{
45 public:
46 int frame;
47 int value;
49 struct Comparator {
50 bool operator()( const EnvelopePoint& a, const EnvelopePoint& b )
51 {
52 return a.frame < b.frame;
53 }
54 };
62 EnvelopePoint( int f, int v );
64 EnvelopePoint( const EnvelopePoint& other );
65};
66
67class Sample : public H2Core::Object<Sample>
68{
70 public:
71
73 using PanEnvelope = std::vector<EnvelopePoint>;
75 using VelocityEnvelope = std::vector<EnvelopePoint>;
77 class Loops
78 {
79 public:
89 int count;
92 Loops() : start_frame( 0 ), loop_frame( 0 ), end_frame( 0 ), count( 0 ), mode( FORWARD ) { };
94 Loops( const Loops* other ) :
95 start_frame( other->start_frame ),
96 loop_frame( other->loop_frame ),
97 end_frame( other->end_frame ),
98 count( other->count ),
99 mode( other->mode ) { };
101 bool operator ==( const Loops& b ) const
102 {
103 return ( start_frame==b.start_frame && loop_frame==b.loop_frame && end_frame==b.end_frame && count==b.count && mode==b.mode );
104 }
105 QString toQString( const QString& sPrefix = "", bool bShort = true ) const;
106 };
107
110 {
111 public:
112 bool use;
113 float divider;
114 float pitch;
117 Rubberband() : use( false ), divider ( 1.0 ), pitch( 1.0 ), c_settings( 4 ) { };
119 Rubberband( const Rubberband* other ) :
120 use( other->use ),
121 divider ( other->divider ),
122 c_settings( other->c_settings ),
123 pitch( other->pitch ) { };
125 bool operator ==( const Rubberband& b ) const
126 {
127 return ( use==b.use && divider==b.divider && c_settings==b.c_settings && pitch==b.pitch );
128 }
129 QString toQString( const QString& sPrefix = "", bool bShort = true ) const;
130 };
131
141 Sample( const QString& filepath, const License& license = License(), int frames=0, int sample_rate=0, float* data_l=nullptr, float* data_r=nullptr );
143 Sample( std::shared_ptr<Sample> other );
145 ~Sample();
146
147
153 bool write( const QString& path, int format= ( SF_FORMAT_WAV|SF_FORMAT_PCM_16 ) );
154
171 static std::shared_ptr<Sample> load( const QString& filepath, const License& license = License() );
172
200 bool load( float fBpm = 120 );
205 void unload();
206
208 bool is_empty() const;
209 QString get_filepath() const;
211 const QString get_raw_filepath() const;
213 const QString get_filename() const;
215 void set_filename( const QString& filename );
217 void set_filepath( const QString& sFilepath );
222 void set_frames( int value );
224 int get_frames() const;
228 void set_sample_rate( const int sampleRate );
230 int get_sample_rate() const;
232 double get_sample_duration( ) const;
233
237 int get_size() const;
239 float* get_data_l() const;
241 float* get_data_r() const;
246 void set_is_modified( bool value );
248 bool get_is_modified() const;
254 Loops get_loops() const;
256 Rubberband get_rubberband() const;
257 void set_pan_envelope( PanEnvelope envelope );
259 void set_loops( Loops loops );
260 void set_rubberband( Rubberband rubberband );
261
262 License getLicense() const;
263 void setLicense( const License& license );
264
269 static Loops::LoopMode parse_loop_mode( const QString& string );
271 QString get_loop_mode_string() const;
280 QString toQString( const QString& sPrefix = "", bool bShort = true ) const override;
281 private:
285 bool apply_loops();
289 void apply_velocity();
294 void apply_pan();
299 void apply_rubberband( float fBpm );
304 bool exec_rubberband_cli( float fBpm );
305
306 QString __filepath;
309 float* __data_l;
310 float* __data_r;
317 static const std::vector<QString> __loop_modes;
318
332};
333
334// DEFINITIONS
335
336inline void Sample::unload()
337{
338 if ( __data_l != nullptr ) {
339 delete [] __data_l;
340 }
341
342 if ( __data_r != nullptr ) {
343 delete [] __data_r;
344 }
349 __data_l = __data_r = nullptr;
350}
351
352inline bool Sample::is_empty() const
353{
354 return ( __data_l == 0 && __data_r == 0 );
355}
356
357inline const QString Sample::get_raw_filepath() const
358{
359 return __filepath;
360}
361
362inline void Sample::set_filepath( const QString& sFilepath )
363{
364 __filepath = sFilepath;
365}
366
367inline const QString Sample::get_filename() const
368{
369 return __filepath.section( "/", -1 );
370}
371
372inline void Sample::set_frames( int frames )
373{
374 __frames = frames;
375}
376
377inline int Sample::get_frames() const
378{
379 return __frames;
380}
381
382inline int Sample::get_sample_rate() const
383{
384 return __sample_rate;
385}
386
387inline void Sample::set_sample_rate( const int sampleRate )
388{
389 __sample_rate = sampleRate;
390}
391
392inline double Sample::get_sample_duration() const
393{
394 return static_cast<double>(__frames) / static_cast<double>(__sample_rate);
395}
396
397inline int Sample::get_size() const
398{
399 return __frames * sizeof( float ) * 2;
400}
401
402inline float* Sample::get_data_l() const
403{
404 return __data_l;
405}
406
407inline float* Sample::get_data_r() const
408{
409 return __data_r;
410}
411
412inline void Sample::set_is_modified( bool is_modified )
413{
414 __is_modified = is_modified;
415}
416
417inline bool Sample::get_is_modified() const
418{
419 return __is_modified;
420}
421
422inline QString Sample::get_loop_mode_string() const
423{
424 return __loop_modes.at(__loops.mode);
425}
426
431
436
438{
439 return __loops;
440}
441
443{
444 return __rubberband;
445}
446inline void Sample::set_pan_envelope( PanEnvelope envelope ) {
447 __pan_envelope = envelope;
448}
450 __velocity_envelope = envelope;
451}
452inline void Sample::set_loops( Loops loops ) {
453 __loops = loops;
454}
455inline void Sample::set_rubberband( Rubberband rubberband ) {
456 __rubberband = rubberband;
457}
458
460 return m_license;
461}
462inline void Sample::setLicense( const License& license ) {
463 m_license = license;
464}
465
466};
467
468#endif // H2C_SAMPLE_H
469
470/* vim: set softtabstop=4 noexpandtab: */
#define H2_OBJECT(name)
Definition Object.h:224
A container for a sample, being able to apply modifications on it.
Definition Sample.h:43
EnvelopePoint()
default constructor
Definition Sample.cpp:52
int frame
frame index
Definition Sample.h:46
Wrapper class to help Hydrogen deal with the license information specified in a drumkit.
Definition License.h:48
set of loop configuration flags
Definition Sample.h:78
QString toQString(const QString &sPrefix="", bool bShort=true) const
Definition Sample.cpp:731
Loops()
constructor
Definition Sample.h:92
Loops(const Loops *other)
copy constructor
Definition Sample.h:94
LoopMode
possible sample editing loop mode
Definition Sample.h:81
bool operator==(const Loops &b) const
equal to operator
Definition Sample.h:101
int end_frame
the frame index where to end the new sample to
Definition Sample.h:88
int start_frame
the frame index where to start the new sample from
Definition Sample.h:86
LoopMode mode
one of the possible loop modes
Definition Sample.h:90
int count
the counts of loops to apply
Definition Sample.h:89
int loop_frame
the frame index where to start the loop from
Definition Sample.h:87
set of rubberband configuration flags
Definition Sample.h:110
QString toQString(const QString &sPrefix="", bool bShort=true) const
Definition Sample.cpp:753
float pitch
desired pitch
Definition Sample.h:114
int c_settings
TODO should be crispness, see rubberband -h.
Definition Sample.h:115
float divider
TODO should be ratio : desired time ratio.
Definition Sample.h:113
bool use
is rubberband enabled
Definition Sample.h:112
Rubberband()
constructor
Definition Sample.h:117
Rubberband(const Rubberband *other)
copy constructor
Definition Sample.h:119
bool operator==(const Rubberband &b) const
equal to operator
Definition Sample.h:125
int __frames
number of frames in this sample
Definition Sample.h:307
bool __is_modified
true if sample is modified
Definition Sample.h:311
void set_loops(Loops loops)
Definition Sample.h:452
float * __data_l
left channel data
Definition Sample.h:309
bool apply_loops()
apply __loops transformation to the sample
Definition Sample.cpp:241
void set_rubberband(Rubberband rubberband)
Definition Sample.h:455
QString get_filepath() const
Definition Sample.cpp:131
PanEnvelope * get_pan_envelope()
Definition Sample.h:427
QString get_loop_mode_string() const
Definition Sample.h:422
Loops get_loops() const
Definition Sample.h:437
void apply_pan()
apply __pan_envelope transformation to the sample
Definition Sample.cpp:375
void set_velocity_envelope(VelocityEnvelope envelope)
Definition Sample.h:449
int __sample_rate
samplerate for this sample
Definition Sample.h:308
void set_filepath(const QString &sFilepath)
Definition Sample.h:362
void set_pan_envelope(PanEnvelope envelope)
Definition Sample.h:446
bool is_empty() const
Definition Sample.h:352
static Loops::LoopMode parse_loop_mode(const QString &string)
parse the given string and rturn the corresponding loop_mode
Definition Sample.cpp:664
Rubberband __rubberband
set of rubberband parameters
Definition Sample.h:315
int get_sample_rate() const
Definition Sample.h:382
void unload()
Flush the current content of the left and right channel and the current metadata.
Definition Sample.h:336
double get_sample_duration() const
Definition Sample.h:392
const QString get_filename() const
Definition Sample.h:367
bool get_is_modified() const
Definition Sample.h:417
Rubberband get_rubberband() const
Definition Sample.h:442
int get_frames() const
Definition Sample.h:377
void set_is_modified(bool value)
__is_modified setter
Definition Sample.h:412
float * get_data_l() const
Definition Sample.h:402
~Sample()
destructor
Definition Sample.cpp:113
const QString get_raw_filepath() const
Definition Sample.h:357
Loops __loops
set of loop parameters
Definition Sample.h:314
License m_license
Transient property indicating the license associated with the sample.
Definition Sample.h:331
float * __data_r
right channel data
Definition Sample.h:310
static std::shared_ptr< Sample > load(const QString &filepath, const License &license=License())
Definition Sample.cpp:136
std::vector< EnvelopePoint > VelocityEnvelope
define the type used to store velocity envelope points
Definition Sample.h:75
bool exec_rubberband_cli(float fBpm)
call rubberband cli to modify the sample using __rubberband
Definition Sample.cpp:581
float * get_data_r() const
Definition Sample.h:407
void setLicense(const License &license)
Definition Sample.h:462
VelocityEnvelope __velocity_envelope
velocity envelope vector
Definition Sample.h:313
void set_sample_rate(const int sampleRate)
Definition Sample.h:387
Sample(const QString &filepath, const License &license=License(), int frames=0, int sample_rate=0, float *data_l=nullptr, float *data_r=nullptr)
Sample constructor.
Definition Sample.cpp:66
void apply_velocity()
apply __velocity_envelope transformation to the sample
Definition Sample.cpp:344
void set_frames(int value)
__frames setter
Definition Sample.h:372
QString toQString(const QString &sPrefix="", bool bShort=true) const override
Formatted string version for debugging purposes.
Definition Sample.cpp:772
QString __filepath
filepath of the sample
Definition Sample.h:306
bool write(const QString &path, int format=(SF_FORMAT_WAV|SF_FORMAT_PCM_16))
write sample to a file
Definition Sample.cpp:677
PanEnvelope __pan_envelope
pan envelope vector
Definition Sample.h:312
void apply_rubberband(float fBpm)
apply __rubberband transformation to the sample
Definition Sample.cpp:413
VelocityEnvelope * get_velocity_envelope()
Definition Sample.h:432
std::vector< EnvelopePoint > PanEnvelope
define the type used to store pan envelope points
Definition Sample.h:73
int get_size() const
Definition Sample.h:397
static const std::vector< QString > __loop_modes
loop modes string
Definition Sample.h:317
void set_filename(const QString &filename)
Definition Sample.cpp:123
License getLicense() const
Definition Sample.h:459
to be able to sort velocity points vectors
Definition Sample.h:49
bool operator()(const EnvelopePoint &a, const EnvelopePoint &b)
Definition Sample.h:50