hydrogen 1.2.3
SoundLibraryDatabase.cpp
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
24
25#include <core/Basics/Drumkit.h>
26#include <core/EventQueue.h>
28
29namespace H2Core
30{
31
36
40
42{
43 for ( const auto& pPatternInfo : m_patternInfoVector ) {
44 INFOLOG( QString( "Name: [%1]" ).arg( pPatternInfo->getName() ) );
45 }
46
47 for ( const auto& sCategory : m_patternCategories ) {
48 INFOLOG( QString( "Category: [%1]" ).arg( sCategory ) );
49 }
50}
51
52bool SoundLibraryDatabase::isPatternInstalled( const QString& sPatternName ) const
53{
54 for ( const auto& pPatternInfo : m_patternInfoVector ) {
55 if ( pPatternInfo->getName() == sPatternName ) {
56 return true;
57 }
58 }
59 return false;
60}
61
63{
64 updatePatterns( false );
65 //updateSongs();
66 updateDrumkits( false );
67
69}
70
71void SoundLibraryDatabase::updateDrumkits( bool bTriggerEvent ) {
72
73 m_drumkitDatabase.clear();
74
75 QStringList drumkitPaths;
76 // system drumkits
77 for ( const auto& sDrumkitName : Filesystem::sys_drumkit_list() ) {
78 drumkitPaths <<
80 }
81 // user drumkits
82 for ( const auto& sDrumkitName : Filesystem::usr_drumkit_list() ) {
83 drumkitPaths <<
85 }
86 // custom drumkits added by the user
87 for ( const auto& sDrumkitPath : m_customDrumkitPaths ) {
88 if ( ! drumkitPaths.contains( sDrumkitPath ) ) {
89 drumkitPaths << sDrumkitPath;
90 }
91 }
92
93 for ( const auto& sDrumkitPath : drumkitPaths ) {
94 auto pDrumkit = Drumkit::load( sDrumkitPath );
95 if ( pDrumkit != nullptr ) {
96 if ( m_drumkitDatabase.find( sDrumkitPath ) !=
97 m_drumkitDatabase.end() ) {
98 ERRORLOG( QString( "A drumkit was already loaded from [%1]. Something went wrong." )
99 .arg( sDrumkitPath ) );
100 continue;
101 }
102 else {
103 INFOLOG( QString( "Drumkit [%1] loaded from [%2]" )
104 .arg( pDrumkit->get_name() )
105 .arg( sDrumkitPath ) );
106 }
107
108 m_drumkitDatabase[ sDrumkitPath ] = pDrumkit;
109 }
110 else {
111 ERRORLOG( QString( "Unable to load drumkit at [%1]" ).arg( sDrumkitPath ) );
112 }
113 }
114
115 if ( bTriggerEvent ) {
117 }
118}
119
120void SoundLibraryDatabase::updateDrumkit( const QString& sDrumkitPath, bool bTriggerEvent ) {
121
122 auto pDrumkit = Drumkit::load( sDrumkitPath );
123 if ( pDrumkit != nullptr ) {
124 m_drumkitDatabase[ sDrumkitPath ] = pDrumkit;
125 }
126 else {
127 ERRORLOG( QString( "Unable to load drumkit at [%1]" ).arg( sDrumkitPath ) );
128 }
129
130 if ( bTriggerEvent ) {
132 }
133}
134
135std::shared_ptr<Drumkit> SoundLibraryDatabase::getDrumkit( const QString& sDrumkit, bool bLoad ) {
136
137 // Convert supplied path or drumkit name into absolute path used
138 // either as ID to retrieve the drumkit from cache or for loading
139 // it from disk in case it is not present yet.
140
141 QString sDrumkitPath;
142 if ( sDrumkit.contains( "/" ) || sDrumkit.contains( "\\" ) ) {
143 // Supplied string is a path to a drumkit
144 sDrumkitPath = sDrumkit;
145 }
146 else {
147 // Supplied string it the name of a drumkit
148 sDrumkitPath = Filesystem::drumkit_path_search( sDrumkit,
150 false );
151 }
152 sDrumkitPath = Filesystem::absolute_path( sDrumkitPath );
153
154 if ( sDrumkitPath.isEmpty() ) {
155 ERRORLOG( QString( "Unable determine drumkit path based on supplied string [%1]" )
156 .arg( sDrumkit ) );
157 return nullptr;
158 }
159
160 if ( m_drumkitDatabase.find( sDrumkitPath ) ==
161 m_drumkitDatabase.end() ) {
162 if ( ! bLoad ) {
163 return nullptr;
164 }
165
166 // Drumkit is not present in database yet. We attempt to load
167 // and add it.
168 auto pDrumkit = Drumkit::load( sDrumkitPath,
169 true, // upgrade
170 false // bSilent
171 );
172 if ( pDrumkit == nullptr ) {
173 return nullptr;
174 }
175
176 m_customDrumkitPaths << sDrumkitPath;
177 m_drumkitDatabase[ sDrumkitPath ] = pDrumkit;
178
179 INFOLOG( QString( "Session Drumkit [%1] loaded from [%2]" )
180 .arg( pDrumkit->get_name() )
181 .arg( sDrumkitPath ) );
182
184
185 return pDrumkit;
186 }
187
188 return m_drumkitDatabase.at( sDrumkitPath );
189}
190
191void SoundLibraryDatabase::updatePatterns( bool bTriggerEvent )
192{
193 m_patternInfoVector.clear();
194 m_patternCategories = QStringList();
195
196 // search drumkit subdirectories within patterns user directory
197 foreach ( const QString& sDrumkit, Filesystem::pattern_drumkits() ) {
199 }
200 // search patterns user directory
202
203 if ( bTriggerEvent ) {
205 }
206}
207
208void SoundLibraryDatabase::loadPatternFromDirectory( const QString& sPatternDir )
209{
210 foreach ( const QString& sName, Filesystem::pattern_list( sPatternDir ) ) {
211 QString sFile = sPatternDir + sName;
212 std::shared_ptr<SoundLibraryInfo> pInfo =
213 std::make_shared<SoundLibraryInfo>();
214
215 if ( pInfo->load( sFile ) ) {
216 INFOLOG( QString( "Pattern [%1] of category [%2] loaded from [%3]" )
217 .arg( pInfo->getName() ).arg( pInfo->getCategory() )
218 .arg( sFile ) );
219
220 m_patternInfoVector.push_back( pInfo );
221
222 if ( ! m_patternCategories.contains( pInfo->getCategory() ) ) {
223 m_patternCategories << pInfo->getCategory();
224 }
225 }
226 }
227}
228
229QString SoundLibraryDatabase::toQString( const QString& sPrefix, bool bShort ) const {
230 QString s = Base::sPrintIndention;
231 QString sOutput;
232 if ( ! bShort ) {
233 sOutput = QString( "%1[SoundLibraryDatabase]\n" ).arg( sPrefix )
234 .append( QString( "%1%2m_drumkitDatabase:\n" ).arg( sPrefix ).arg( s ) );
235 for ( const auto& [ ssPath, ddrumkit ] : m_drumkitDatabase ) {
236 sOutput.append( QString( "%1%2%2%3: %4\n" ).arg( sPrefix ).arg( s )
237 .arg( ssPath ).arg( ddrumkit->toQString( "", true ) ) );
238 }
239 sOutput.append( QString( "%1%2m_patternInfoVector:\n" ).arg( sPrefix ).arg( s ) );
240 for ( const auto& ppatternInfo : m_patternInfoVector ) {
241 sOutput.append( QString( "%3\n" )
242 .arg( ppatternInfo->toQString( sPrefix + s + s, bShort ) ) );
243 }
244 sOutput.append( QString( "%1%2m_patternCategories: %3\n" ).arg( sPrefix ).arg( s )
245 .arg( m_patternCategories.join( ", " ) ) );
246 sOutput.append( QString( "%1%2m_customDrumkitPaths:\n" ).arg( sPrefix ).arg( s ) );
247 for ( const auto& ssCustomPath : m_customDrumkitPaths ) {
248 sOutput.append( QString( "%1%2%2%3\n" ).arg( sPrefix ).arg( s )
249 .arg( ssCustomPath ) );
250 }
251 }
252 else {
253
254 sOutput = QString( "%1[SoundLibraryDatabase]\n" ).arg( sPrefix )
255 .append( QString( "%1%2m_drumkitDatabase:\n" ).arg( sPrefix ).arg( s ) );
256 for ( const auto& eentry : m_drumkitDatabase ) {
257 sOutput.append( QString( "%1%2%2%3\n" ).arg( sPrefix ).arg( s )
258 .arg( eentry.first ) );
259 }
260 sOutput.append( QString( "%1%2m_patternInfoVector:\n" ).arg( sPrefix ).arg( s ) );
261 for ( const auto& ppatternInfo : m_patternInfoVector ) {
262 sOutput.append( QString( "%1%2%2%3\n" ).arg( sPrefix ).arg( s )
263 .arg( ppatternInfo->getPath() ) );
264 }
265 sOutput.append( QString( "%1%2m_patternCategories: %3\n" ).arg( sPrefix ).arg( s )
266 .arg( m_patternCategories.join( ", " ) ) );
267 sOutput.append( QString( "%1%2m_customDrumkitPaths:\n" ).arg( sPrefix ).arg( s ) );
268 for ( const auto& ssCustomPath : m_customDrumkitPaths ) {
269 sOutput.append( QString( "%1%2%2%3\n" ).arg( sPrefix ).arg( s )
270 .arg( ssCustomPath ) );
271 }
272 }
273
274 return sOutput;
275}
276};
#define INFOLOG(x)
Definition Object.h:237
#define ERRORLOG(x)
Definition Object.h:239
static QString sPrintIndention
String used to format the debugging string output of some core classes.
Definition Object.h:127
static std::shared_ptr< Drumkit > load(const QString &dk_dir, bool bUpgrade=true, bool bSilent=false)
Load drumkit information from a directory.
Definition Drumkit.cpp:90
static EventQueue * get_instance()
Returns a pointer to the current EventQueue singleton stored in __instance.
Definition EventQueue.h:224
void push_event(const EventType type, const int nValue)
Queues the next event into the EventQueue.
static QString absolute_path(const QString &sFilename, bool bSilent=false)
Convert a direct to an absolute path.
static QString usr_drumkits_dir()
returns user drumkits path
static QStringList pattern_list()
returns a list of existing patterns
static QString sys_drumkits_dir()
returns system drumkits path
static QStringList usr_drumkit_list()
returns list of usable user drumkits ( see Filesystem::drumkit_list )
@ stacked
First, looks in the system drumkits and, afterwards, in the user drumkits.
static QStringList sys_drumkit_list()
returns list of usable system drumkits ( see Filesystem::drumkit_list )
static QString drumkit_path_search(const QString &dk_name, Lookup lookup=Lookup::stacked, bool bSilent=false)
Returns the path to a H2Core::Drumkit folder.
static QStringList pattern_drumkits()
returns a list of existing drumkit sub dir into the patterns directory
static QString patterns_dir()
returns user patterns path
std::map< QString, std::shared_ptr< Drumkit > > m_drumkitDatabase
void updateDrumkits(bool bTriggerEvent=true)
void loadPatternFromDirectory(const QString &path)
bool isPatternInstalled(const QString &sPatternName) const
void updateDrumkit(const QString &sDrumkitPath, bool bTriggerEvent=true)
void updatePatterns(bool bTriggerEvent=true)
QStringList m_customDrumkitPaths
List of drumkits the user supplied via CLI or OSC command but couldn't be found in either the system'...
QString toQString(const QString &sPrefix="", bool bShort=true) const override
Formatted string version for debugging purposes.
std::vector< std::shared_ptr< SoundLibraryInfo > > m_patternInfoVector
std::shared_ptr< Drumkit > getDrumkit(const QString &sDrumkitPath, bool bLoad=true)
Retrieve a drumkit from the database.
@ EVENT_SOUND_LIBRARY_CHANGED
Definition EventQueue.h:176