hydrogen 1.2.6
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-2025 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
39
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 };
55
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 ) { };
100
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 ) { };
124
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
132 static QString sndfileFormatToQString( int nFormat );
133
143 Sample( const QString& filepath, const License& license = License(), int frames=0, int sample_rate=0, float* data_l=nullptr, float* data_r=nullptr );
145 Sample( std::shared_ptr<Sample> other );
147 ~Sample();
148
149
155 bool write( const QString& path, int format= ( SF_FORMAT_WAV|SF_FORMAT_PCM_16 ) );
156
173 static std::shared_ptr<Sample> load( const QString& filepath, const License& license = License() );
174
202 bool load( float fBpm = 120 );
207 void unload();
208
210 bool is_empty() const;
211 QString get_filepath() const;
213 const QString get_raw_filepath() const;
215 const QString get_filename() const;
217 void set_filename( const QString& filename );
219 void set_filepath( const QString& sFilepath );
224 void set_frames( int value );
226 int get_frames() const;
230 void set_sample_rate( const int sampleRate );
232 int get_sample_rate() const;
234 double get_sample_duration( ) const;
235
239 int get_size() const;
241 float* get_data_l() const;
243 float* get_data_r() const;
248 void set_is_modified( bool value );
250 bool get_is_modified() const;
256 Loops get_loops() const;
258 Rubberband get_rubberband() const;
259 void set_pan_envelope( PanEnvelope envelope );
261 void set_loops( Loops loops );
262 void set_rubberband( Rubberband rubberband );
263
264 License getLicense() const;
265 void setLicense( const License& license );
266
271 static Loops::LoopMode parse_loop_mode( const QString& string );
273 QString get_loop_mode_string() const;
282 QString toQString( const QString& sPrefix = "", bool bShort = true ) const override;
283 private:
287 bool apply_loops();
291 void apply_velocity();
296 void apply_pan();
301 void apply_rubberband( float fBpm );
306 bool exec_rubberband_cli( float fBpm );
307
308 QString __filepath;
311 float* __data_l;
312 float* __data_r;
319 static const std::vector<QString> __loop_modes;
320
334};
335
336// DEFINITIONS
337
338inline void Sample::unload()
339{
340 if ( __data_l != nullptr ) {
341 delete [] __data_l;
342 }
343
344 if ( __data_r != nullptr ) {
345 delete [] __data_r;
346 }
350
351 __data_l = __data_r = nullptr;
352}
353
354inline bool Sample::is_empty() const
355{
356 return ( __data_l == 0 && __data_r == 0 );
357}
358
359inline const QString Sample::get_raw_filepath() const
360{
361 return __filepath;
362}
363
364inline void Sample::set_filepath( const QString& sFilepath )
365{
366 __filepath = sFilepath;
367}
368
369inline const QString Sample::get_filename() const
370{
371 return __filepath.section( "/", -1 );
372}
373
374inline void Sample::set_frames( int frames )
375{
376 __frames = frames;
377}
378
379inline int Sample::get_frames() const
380{
381 return __frames;
382}
383
384inline int Sample::get_sample_rate() const
385{
386 return __sample_rate;
387}
388
389inline void Sample::set_sample_rate( const int sampleRate )
390{
391 __sample_rate = sampleRate;
392}
393
394inline double Sample::get_sample_duration() const
395{
396 return static_cast<double>(__frames) / static_cast<double>(__sample_rate);
397}
398
399inline int Sample::get_size() const
400{
401 return __frames * sizeof( float ) * 2;
402}
403
404inline float* Sample::get_data_l() const
405{
406 return __data_l;
407}
408
409inline float* Sample::get_data_r() const
410{
411 return __data_r;
412}
413
414inline void Sample::set_is_modified( bool is_modified )
415{
416 __is_modified = is_modified;
417}
418
419inline bool Sample::get_is_modified() const
420{
421 return __is_modified;
422}
423
424inline QString Sample::get_loop_mode_string() const
425{
426 return __loop_modes.at(__loops.mode);
427}
428
433
438
440{
441 return __loops;
442}
443
445{
446 return __rubberband;
447}
448inline void Sample::set_pan_envelope( PanEnvelope envelope ) {
449 __pan_envelope = envelope;
450}
452 __velocity_envelope = envelope;
453}
454inline void Sample::set_loops( Loops loops ) {
455 __loops = loops;
456}
457inline void Sample::set_rubberband( Rubberband rubberband ) {
458 __rubberband = rubberband;
459}
460
462 return m_license;
463}
464inline void Sample::setLicense( const License& license ) {
465 m_license = license;
466}
467
468};
469
470#endif // H2C_SAMPLE_H
471
472/* vim: set softtabstop=4 noexpandtab: */
#define H2_OBJECT(name)
Definition Object.h:227
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:769
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:791
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:309
bool __is_modified
true if sample is modified
Definition Sample.h:313
void set_loops(Loops loops)
Definition Sample.h:454
float * __data_l
left channel data
Definition Sample.h:311
bool apply_loops()
apply __loops transformation to the sample
Definition Sample.cpp:260
void set_rubberband(Rubberband rubberband)
Definition Sample.h:457
QString get_filepath() const
Definition Sample.cpp:131
PanEnvelope * get_pan_envelope()
Definition Sample.h:429
QString get_loop_mode_string() const
Definition Sample.h:424
Loops get_loops() const
Definition Sample.h:439
void apply_pan()
apply __pan_envelope transformation to the sample
Definition Sample.cpp:394
void set_velocity_envelope(VelocityEnvelope envelope)
Definition Sample.h:451
int __sample_rate
samplerate for this sample
Definition Sample.h:310
void set_filepath(const QString &sFilepath)
Definition Sample.h:364
void set_pan_envelope(PanEnvelope envelope)
Definition Sample.h:448
bool is_empty() const
Definition Sample.h:354
static Loops::LoopMode parse_loop_mode(const QString &string)
parse the given string and rturn the corresponding loop_mode
Definition Sample.cpp:683
Rubberband __rubberband
set of rubberband parameters
Definition Sample.h:317
int get_sample_rate() const
Definition Sample.h:384
void unload()
Flush the current content of the left and right channel and the current metadata.
Definition Sample.h:338
double get_sample_duration() const
Definition Sample.h:394
const QString get_filename() const
Definition Sample.h:369
bool get_is_modified() const
Definition Sample.h:419
static QString sndfileFormatToQString(int nFormat)
Definition Sample.cpp:836
Rubberband get_rubberband() const
Definition Sample.h:444
int get_frames() const
Definition Sample.h:379
void set_is_modified(bool value)
__is_modified setter
Definition Sample.h:414
float * get_data_l() const
Definition Sample.h:404
~Sample()
destructor
Definition Sample.cpp:113
const QString get_raw_filepath() const
Definition Sample.h:359
Loops __loops
set of loop parameters
Definition Sample.h:316
License m_license
Transient property indicating the license associated with the sample.
Definition Sample.h:333
float * __data_r
right channel data
Definition Sample.h:312
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:600
float * get_data_r() const
Definition Sample.h:409
void setLicense(const License &license)
Definition Sample.h:464
VelocityEnvelope __velocity_envelope
velocity envelope vector
Definition Sample.h:315
void set_sample_rate(const int sampleRate)
Definition Sample.h:389
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:363
void set_frames(int value)
__frames setter
Definition Sample.h:374
QString toQString(const QString &sPrefix="", bool bShort=true) const override
Formatted string version for debugging purposes.
Definition Sample.cpp:810
QString __filepath
filepath of the sample
Definition Sample.h:308
bool write(const QString &path, int format=(SF_FORMAT_WAV|SF_FORMAT_PCM_16))
write sample to a file
Definition Sample.cpp:696
PanEnvelope __pan_envelope
pan envelope vector
Definition Sample.h:314
void apply_rubberband(float fBpm)
apply __rubberband transformation to the sample
Definition Sample.cpp:432
VelocityEnvelope * get_velocity_envelope()
Definition Sample.h:434
std::vector< EnvelopePoint > PanEnvelope
define the type used to store pan envelope points
Definition Sample.h:73
int get_size() const
Definition Sample.h:399
static const std::vector< QString > __loop_modes
loop modes string
Definition Sample.h:319
void set_filename(const QString &filename)
Definition Sample.cpp:123
License getLicense() const
Definition Sample.h:461
to be able to sort velocity points vectors
Definition Sample.h:49
bool operator()(const EnvelopePoint &a, const EnvelopePoint &b)
Definition Sample.h:50