hydrogen 1.2.3
Pattern.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_PATTERN_H
24#define H2C_PATTERN_H
25
26#include <set>
27#include <memory>
28#include <core/License.h>
29#include <core/Object.h>
30#include <core/Basics/Note.h>
31#include <core/Helpers/Xml.h>
32
33namespace H2Core
34{
35
36class XMLNode;
37class Instrument;
38class InstrumentList;
39class PatternList;
40
45class Pattern : public H2Core::Object<Pattern>
46{
48 public:
50 typedef std::multimap <int, Note*> notes_t;
52 typedef notes_t::iterator notes_it_t;
54 typedef notes_t::const_iterator notes_cst_it_t;
56 typedef std::set <Pattern*> virtual_patterns_t;
58 typedef virtual_patterns_t::iterator virtual_patterns_it_t;
60 typedef virtual_patterns_t::const_iterator virtual_patterns_cst_it_t;
61
63 std::set<Pattern*>::iterator begin();
64 std::set<Pattern*>::iterator end();
65
74 Pattern( const QString& name="Pattern", const QString& info="", const QString& category="not_categorized", int length=MAX_NOTES, int denominator=4 );
76 Pattern( Pattern* other );
78 ~Pattern();
79
85 static Pattern* load_file( const QString& pattern_path, std::shared_ptr<InstrumentList> instruments );
95 static Pattern* load_from( XMLNode* node, std::shared_ptr<InstrumentList> instruments, bool bSilent = false );
105 bool save_file( const QString& drumkit_name, const QString& author, const License& license, const QString& pattern_path, bool overwrite=false ) const;
106
108 void set_name( const QString& name );
110 const QString& get_name() const;
112 void set_category( const QString& category );
114 void set_info( const QString& info );
116 const QString& get_info() const;
118 const QString& get_category() const;
120 void set_length( int length );
122 int get_length() const;
124 void set_denominator( int denominator );
126 int get_denominator() const;
128 const notes_t* get_notes() const;
133
138 void insert_note( Note* note );
147 Note* find_note( int idx_a, int idx_b, std::shared_ptr<Instrument> instrument, bool strict=true ) const;
158 Note* find_note( int idx_a, int idx_b, std::shared_ptr<Instrument> instrument, Note::Key key, Note::Octave octave, bool strict=true) const;
163 void remove_note( Note* note );
164
169 bool references( std::shared_ptr<Instrument> instr );
175 void purge_instrument( std::shared_ptr<Instrument> instr, bool bRequiredLock = true );
179 void set_to_old();
180
182 bool virtual_patterns_empty() const;
189 void virtual_patterns_add( Pattern* pattern );
193 void virtual_patterns_del( Pattern* pattern );
208 void addFlattenedVirtualPatterns( PatternList* pPatternList );
215 void removeFlattenedVirtualPatterns( PatternList* pPatternList );
216
217 int longestVirtualPatternLength() const;
221 bool isVirtual() const;
222
228 void save_to( XMLNode* node, const std::shared_ptr<Instrument> instrumentOnly = nullptr ) const;
237 QString toQString( const QString& sPrefix = "", bool bShort = true ) const override;
238
239 private:
252 QString __name;
253 QString __category;
254 QString __info;
264 static bool loadDoc( const QString& sPatternPath, std::shared_ptr<InstrumentList> pInstrumentList, XMLDoc* pDoc, bool bSilent = false );
265};
266
268#define FOREACH_NOTE_CST_IT_BEGIN_END(_notes,_it) \
269 for( Pattern::notes_cst_it_t _it=(_notes)->begin(); (_it)!=(_notes)->end(); (_it)++ )
270
272#define FOREACH_NOTE_CST_IT_BOUND_END(_notes,_it,_bound) \
273 for( Pattern::notes_cst_it_t _it=(_notes)->lower_bound((_bound)); (_it)!=(_notes)->end() && (_it)->first == (_bound); (_it)++ )
274
276#define FOREACH_NOTE_IT_BEGIN_END(_notes,_it) \
277 for( Pattern::notes_it_t _it=(_notes)->begin(); (_it)!=(_notes)->end(); (_it)++ )
278
280#define FOREACH_NOTE_IT_BOUND_END(_notes,_it,_bound) \
281 for( Pattern::notes_it_t _it=(_notes)->lower_bound((_bound)); (_it)!=(_notes)->end() && (_it)->first == (_bound); (_it)++ )
282
285#define FOREACH_NOTE_CST_IT_BEGIN_LENGTH(_notes,_it,_pattern) \
286 for( Pattern::notes_cst_it_t _it=(_notes)->begin(); (_it)!=(_notes)->end() && (_it)->first < (_pattern)->get_length(); (_it)++ )
287
290#define FOREACH_NOTE_CST_IT_BOUND_LENGTH(_notes,_it,_bound,_pattern) \
291 for( Pattern::notes_cst_it_t _it=(_notes)->lower_bound((_bound)); (_it)!=(_notes)->end() && (_it)->first == (_bound) && (_it)->first < (_pattern)->get_length(); (_it)++ )
292
295#define FOREACH_NOTE_IT_BEGIN_LENGTH(_notes,_it,_pattern) \
296 for( Pattern::notes_it_t _it=(_notes)->begin(); (_it)!=(_notes)->end() && (_it)->first < (_pattern)->get_length(); (_it)++ )
297
300#define FOREACH_NOTE_IT_BOUND_LENGTH(_notes,_it,_bound,_pattern) \
301 for( Pattern::notes_it_t _it=(_notes)->lower_bound((_bound)); (_it)!=(_notes)->end() && (_it)->first == (_bound) && (_it)->first < (_pattern)->get_length(); (_it)++ )
302
303// DEFINITIONS
304
305inline void Pattern::set_name( const QString& name )
306{
307 __name = name;
308}
309
310inline const QString& Pattern::get_name() const
311{
312 return __name;
313}
314
315inline void Pattern::set_info( const QString& info )
316{
317 __info = info;
318}
319
320inline const QString& Pattern::get_info() const
321{
322 return __info;
323}
324
325inline void Pattern::set_category( const QString& category )
326{
327 __category = category;
328}
329
330inline const QString& Pattern::get_category() const
331{
332 return __category;
333}
334
335inline void Pattern::set_length( int length )
336{
337 __length = length;
338}
339
340inline int Pattern::get_length() const
341{
342 return __length;
343}
344
345inline void Pattern::set_denominator( int denominator )
346{
347 __denominator = denominator;
348}
349
350inline int Pattern::get_denominator() const
351{
352 return __denominator;
353}
354
356{
357 return &__notes;
358}
359
364
369
370inline void Pattern::insert_note( Note* note )
371{
372 __notes.insert( std::make_pair( note->get_position(), note ) );
373}
374
376{
377 return __virtual_patterns.empty();
378}
379
381{
382 __virtual_patterns.clear();
383}
384
386{
387 __virtual_patterns.insert( pattern );
388}
389
391{
393 if ( it!=__virtual_patterns.end() ) __virtual_patterns.erase( it );
394}
395
400
401};
402
403#endif // H2C_PATTERN_H
404
405/* vim: set softtabstop=4 noexpandtab: */
#define H2_OBJECT(name)
Definition Object.h:224
Wrapper class to help Hydrogen deal with the license information specified in a drumkit.
Definition License.h:48
A note plays an associated instrument with a velocity left and right pan.
Definition Note.h:102
int get_position() const
__position accessor
Definition Note.h:535
Key
possible keys
Definition Note.h:106
Octave
possible octaves
Definition Note.h:110
PatternList is a collection of patterns.
Definition PatternList.h:43
Pattern class is a Note container.
Definition Pattern.h:46
QString __name
the name of thepattern
Definition Pattern.h:252
bool save_file(const QString &drumkit_name, const QString &author, const License &license, const QString &pattern_path, bool overwrite=false) const
save a pattern into an xml file
Definition Pattern.cpp:152
const QString & get_info() const
get the category of the pattern
Definition Pattern.h:320
void set_name(const QString &name)
get the name of the pattern
Definition Pattern.h:305
std::set< Pattern * > virtual_patterns_t
note set iterator type;
Definition Pattern.h:56
const QString & get_name() const
set the category of the pattern
Definition Pattern.h:310
const virtual_patterns_t * get_flattened_virtual_patterns() const
Definition Pattern.h:365
bool virtual_patterns_empty() const
clear __virtual_patterns
Definition Pattern.h:375
void virtual_patterns_add(Pattern *pattern)
add a pattern to __virtual_patterns
Definition Pattern.h:385
const QString & get_category() const
set the length of the pattern
Definition Pattern.h:330
void set_to_old()
mark all notes as old
Definition Pattern.cpp:294
void flattened_virtual_patterns_clear()
Definition Pattern.h:396
void set_denominator(int denominator)
get the denominator of the pattern
Definition Pattern.h:345
QString __info
a description of the pattern
Definition Pattern.h:254
void set_info(const QString &info)
get the info of the pattern
Definition Pattern.h:315
void purge_instrument(std::shared_ptr< Instrument > instr, bool bRequiredLock=true)
delete the notes referencing the given instrument The function is thread safe (it locks the audio dat...
Definition Pattern.cpp:267
bool references(std::shared_ptr< Instrument > instr)
check if this pattern contains a note referencing the given instrument
Definition Pattern.cpp:255
virtual_patterns_t __virtual_patterns
a list of patterns directly referenced by this one
Definition Pattern.h:256
Pattern(const QString &name="Pattern", const QString &info="", const QString &category="not_categorized", int length=MAX_NOTES, int denominator=4)
constructor
Definition Pattern.cpp:38
void virtual_patterns_del(Pattern *pattern)
remove a pattern from virtual_pattern set, flattened virtual patterns have to be rebuilt
Definition Pattern.h:390
notes_t __notes
a multimap (hash with possible multiple values for one key) of note
Definition Pattern.h:255
const virtual_patterns_t * get_virtual_patterns() const
get the flattened virtual pattern set
Definition Pattern.h:360
static Pattern * load_from(XMLNode *node, std::shared_ptr< InstrumentList > instruments, bool bSilent=false)
load a pattern from an XMLNode
Definition Pattern.cpp:121
virtual_patterns_t::iterator virtual_patterns_it_t
note set const iterator type;
Definition Pattern.h:58
void flattened_virtual_patterns_compute()
compute virtual_pattern_transitive_closure_set based on virtual_pattern_transitive_closure_set virtua...
Definition Pattern.cpp:303
void remove_note(Note *note)
removes a given note from __notes, it's not deleted
Definition Pattern.cpp:244
int __length
Determines the accessible range or notes within the pattern.
Definition Pattern.h:250
notes_t::iterator notes_it_t
multimap note const iterator type
Definition Pattern.h:52
int get_length() const
set the denominator of the pattern
Definition Pattern.h:340
void virtual_patterns_clear()
Definition Pattern.h:380
std::set< Pattern * >::iterator begin()
allow iteration of all contained virtual patterns.
Definition Pattern.cpp:349
const notes_t * get_notes() const
get the virtual pattern set
Definition Pattern.h:355
void set_category(const QString &category)
set the info of the pattern
Definition Pattern.h:325
~Pattern()
destructor
Definition Pattern.cpp:59
notes_t::const_iterator notes_cst_it_t
note set type;
Definition Pattern.h:54
std::multimap< int, Note * > notes_t
< multimap note type
Definition Pattern.h:50
static Pattern * load_file(const QString &pattern_path, std::shared_ptr< InstrumentList > instruments)
load a pattern from a file
Definition Pattern.cpp:106
void removeFlattenedVirtualPatterns(PatternList *pPatternList)
Add content of __flattened_virtual_patterns into pPatternList.
Definition Pattern.cpp:326
QString toQString(const QString &sPrefix="", bool bShort=true) const override
Formatted string version for debugging purposes.
Definition Pattern.cpp:357
int get_denominator() const
get the note multimap
Definition Pattern.h:350
Note * find_note(int idx_a, int idx_b, std::shared_ptr< Instrument > instrument, bool strict=true) const
search for a note at a given index within __notes which correspond to the given arguments
Definition Pattern.cpp:217
virtual_patterns_t::const_iterator virtual_patterns_cst_it_t
Definition Pattern.h:60
void save_to(XMLNode *node, const std::shared_ptr< Instrument > instrumentOnly=nullptr) const
save the pattern within the given XMLNode
Definition Pattern.cpp:169
virtual_patterns_t __flattened_virtual_patterns
the complete list of virtual patterns
Definition Pattern.h:257
QString __category
the category of the pattern
Definition Pattern.h:253
bool isVirtual() const
Whether the pattern holds at least one virtual pattern.
Definition Pattern.cpp:345
int longestVirtualPatternLength() const
Definition Pattern.cpp:333
void insert_note(Note *note)
insert a new note within __notes
Definition Pattern.h:370
std::set< Pattern * >::iterator end()
Definition Pattern.cpp:353
int __denominator
the meter denominator of the pattern used in meter (eg 4/4)
Definition Pattern.h:251
void addFlattenedVirtualPatterns(PatternList *pPatternList)
Add content of __flattened_virtual_patterns into pPatternList.
Definition Pattern.cpp:319
void set_length(int length)
get the length of the pattern
Definition Pattern.h:335
static bool loadDoc(const QString &sPatternPath, std::shared_ptr< InstrumentList > pInstrumentList, XMLDoc *pDoc, bool bSilent=false)
Loads the pattern stored in sPatternPath into pDoc and takes care of all the error handling.
Definition Pattern.cpp:66
XMLDoc is a subclass of QDomDocument with read and write methods.
Definition Xml.h:182
XMLNode is a subclass of QDomNode with read and write values methods.
Definition Xml.h:39
#define MAX_NOTES
Maximum number of notes.
Definition config.dox:79