CFugue
Chords.h
1 /*
2  This is part of CFugue, a C++ Runtime for MIDI Score Programming
3  Copyright (C) 2009 Gopalakrishna Palem
4 
5  For links to further information, or to contact the author,
6  see <http://musicnote.sourceforge.net/>.
7 */
8 
9 #ifndef __INDEXED_VECTORS_H__E2C81E6D_A292_41d7_B830_F7C07D335205__
10 #define __INDEXED_VECTORS_H__E2C81E6D_A292_41d7_B830_F7C07D335205__
11 
12 #include "Common/_TChar.h"
13 #include <vector>
14 #include <map>
15 
16 namespace CFugue
17 {
18  /// Definition of a Chord entry
19  struct ChordDef
20  {
21  typedef signed short HALFSTEP;
22 
23  enum { MAX_INTERVALS = 16, MAX_NAME = 16 };
24 
25  TCHAR szChordName[MAX_NAME]; ///< Name of the Chord
26  HALFSTEP Intervals[MAX_INTERVALS]; ///< The half-step intervals for the Chord. Zero indicates root note
27  unsigned short nIntervalCount; ///< Number of valid entries in the Intervals[] array
28  };
29 
30  /// <Summary>Maniuplates Chord definitions for Western Music</Summary>
31  class Chords
32  {
33  std::map<TCHAR, std::vector<const ChordDef *> > m_Definitions;
34  public:
35  /// Initialize the Chords with default definitions
36  Chords();
37 
38  /// Initialize the Chords with custom Chord definitions.
39  /// If supplied array is empty, default values will be loaded.
40  /// Only references to the ChordDef are stored in the Chords class.
41  /// So ensure that the supplied pChords is not freed till this Chords
42  /// object is released.
43  /// @param pChords Array of Chord definitions.
44  /// @param nSize Size of the pChords Array
45  Chords(const ChordDef* pChords, int nSize);
46 
47  /// Overrides the existing definitions with the supplied values.
48  /// If supplied array is empty, default values will be loaded.
49  /// The supplied array should not be freed before this Chords object is freed.
50  /// @param pChords Array of Chord definitions. Use NULL to load the default values
51  /// @param nSize Size of the pChords Array
52  void LoadDefinitions(const ChordDef* pChords = NULL, int nSize = 0);
53 
54  /// Adds the supplied definitions to the existing ones.
55  /// Caller has to ensure that the new values do not add duplicates to existing ones.
56  /// The input array should not be freed before this Chords object is freed.
57  /// @param pChords Array of Chord definitions.
58  /// @param nSize Size of the pChords Array
59  void AddDefinitions(const ChordDef* pChords, int nSize);
60 
61  /// Retrieves the chord that suits the first part of the given string.
62  /// If you have not added any custom definitions with AddDefinitions(), then
63  /// you might find the static GetDefaultMatchingChord() method more convenient.
64  /// @param szToken the string that has any Chord name at its start
65  /// @param retVal the ChordDef object that has a Chord with its name present in the szToken
66  /// @return the number of characters correctly matched. Zero, if no match found
67  unsigned int ExtractMatchingChord(const TCHAR* szToken, ChordDef* retVal) const;
68 
69  /// Retrieves the chord that suits the first part of the given string.
70  /// Same as ExtractMatchingChord, except that this method is static and uses
71  /// only the in-built default chord definitions for the search. For custom chord
72  /// definitions, you need to create a Chords object and use ExtractMatchingChord.
73  /// @param szToken the string that has any Chord name at its start
74  /// @param retVal the ChordDef object that has a Chord with its name present in the szToken
75  /// @return the number of characters correctly matched. Zero, if no match found
76  static unsigned int GetDefaultMatchingChord(const TCHAR* szToken, ChordDef* retVal);
77  };
78 
79 } // namespace CFugue
80 
81 
82 
83 #endif // __INDEXED_VECTORS_H__E2C81E6D_A292_41d7_B830_F7C07D335205__
Chords()
Initialize the Chords with default definitions.
Definition: Chords.cpp:135
void LoadDefinitions(const ChordDef *pChords=NULL, int nSize=0)
Definition: Chords.cpp:145
unsigned int ExtractMatchingChord(const TCHAR *szToken, ChordDef *retVal) const
Definition: Chords.cpp:163
Definition of a Chord entry.
Definition: Chords.h:19
TCHAR szChordName[MAX_NAME]
Name of the Chord.
Definition: Chords.h:25
HALFSTEP Intervals[MAX_INTERVALS]
The half-step intervals for the Chord. Zero indicates root note.
Definition: Chords.h:26
unsigned short nIntervalCount
Number of valid entries in the Intervals[] array.
Definition: Chords.h:27
Maniuplates Chord definitions for Western Music
Definition: Chords.h:31
static unsigned int GetDefaultMatchingChord(const TCHAR *szToken, ChordDef *retVal)
Definition: Chords.cpp:168
void AddDefinitions(const ChordDef *pChords, int nSize)
Definition: Chords.cpp:156

CFugue, the C++ Music Programming Library © Copyright 2009 Cenacle Research India Private Limited Gopalakrishna Palem