/[svn]/linuxsampler/trunk/src/db/InstrumentsDbUtilities.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/db/InstrumentsDbUtilities.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1781 by iliev, Mon Sep 29 18:21:21 2008 UTC revision 3101 by schoenebeck, Sun Feb 5 18:03:58 2017 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2   *                                                                         *   *                                                                         *
3   *   Copyright (C) 2007, 2008 Grigor Iliev                                 *   *   Copyright (C) 2007 - 2009 Grigor Iliev                                *
4   *                                                                         *   *                                                                         *
5   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
6   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
# Line 23  Line 23 
23  #include "../common/File.h"  #include "../common/File.h"
24  #include "../common/global_private.h"  #include "../common/global_private.h"
25    
26    #include <algorithm>
27  #include <errno.h>  #include <errno.h>
28    
29  #include "../common/Exception.h"  #include "../common/Exception.h"
30  #include "InstrumentsDb.h"  #include "InstrumentsDb.h"
31    #include "../engines/sfz/sfz.h"
32    #if HAVE_SF2
33    # if AC_APPLE_UNIVERSAL_BUILD
34    #  include <libgig/SF.h>
35    # else
36    #  include <SF.h>
37    # endif
38    #endif // HAVE_SF2
39    
40  namespace LinuxSampler {  namespace LinuxSampler {
41    
42        class GigFileInfo : public InstrumentFileInfo {
43        public:
44            GigFileInfo(String fileName) : InstrumentFileInfo(fileName) {
45                m_gig  = NULL;
46                m_riff = NULL;
47                try {
48                    m_riff = new RIFF::File(fileName);
49                    m_gig  = new gig::File(m_riff);
50                    m_gig->SetAutoLoad(false); // avoid time consuming samples scanning
51                } catch (RIFF::Exception e) {
52                    throw Exception(e.Message);
53                } catch (...) {
54                    throw Exception("Unknown exception while accessing gig file");
55                }
56            }
57    
58            virtual ~GigFileInfo() {
59                if (m_gig)  delete m_gig;
60                if (m_riff) delete m_riff;
61            }
62    
63            String formatName() OVERRIDE {
64                return "GIG";
65            }
66    
67            String formatVersion() OVERRIDE {
68                return (m_gig->pVersion) ? ToString(m_gig->pVersion->major) : "";
69            }
70    
71            optional<InstrumentInfo> getInstrumentInfo(int index, ScanProgress* pProgress) OVERRIDE {
72                InstrumentInfo info;
73                try {
74                    ::gig::progress_t* progress = (pProgress) ? &pProgress->GigFileProgress : NULL;
75                    ::gig::Instrument* pInstrument = m_gig->GetInstrument(index, progress);
76                    if (!pInstrument)
77                        return optional<InstrumentInfo>::nothing;
78    
79                    info.instrumentName = pInstrument->pInfo->Name;
80                    info.product = (!pInstrument->pInfo->Product.empty()) ? pInstrument->pInfo->Product : m_gig->pInfo->Product;
81                    info.artists = (!pInstrument->pInfo->Artists.empty()) ? pInstrument->pInfo->Artists : m_gig->pInfo->Artists;
82                    info.keywords = (!pInstrument->pInfo->Keywords.empty()) ? pInstrument->pInfo->Keywords : m_gig->pInfo->Keywords;
83                    info.comments = (!pInstrument->pInfo->Comments.empty()) ? pInstrument->pInfo->Comments : m_gig->pInfo->Comments;
84                    info.isDrum = pInstrument->IsDrum;
85                } catch (RIFF::Exception e) {
86                    throw Exception(e.Message);
87                } catch (...) {
88                    throw Exception("Unknown exception while accessing gig file");
89                }
90                return info;
91            }
92        private:
93            ::RIFF::File* m_riff;
94            ::gig::File*  m_gig;
95        };
96    
97        class SFZFileInfo : public InstrumentFileInfo {
98        public:
99            SFZFileInfo(String fileName) : InstrumentFileInfo(fileName) {
100                m_sfz = NULL;
101                try {
102                    m_sfz = new ::sfz::File(fileName);
103                } catch (sfz::Exception e) {
104                    throw Exception(e.Message());
105                } catch (...) {
106                    throw Exception("Unknown exception while accessing sfz file");
107                }
108            }
109    
110            virtual ~SFZFileInfo() {
111                if (m_sfz) delete m_sfz;
112            }
113    
114            String formatName() OVERRIDE {
115                return "SFZ";
116            }
117    
118            String formatVersion() OVERRIDE {
119                return "";
120            }
121    
122            optional<InstrumentInfo> getInstrumentInfo(int index, ScanProgress* pProgress) OVERRIDE {
123                if (index != 0)
124                    return optional<InstrumentInfo>::nothing;
125    
126                InstrumentInfo info;
127                // yeah, lousy info, but SFZ does not provide any meta info unfortunately yet
128                return info;
129            }
130        private:
131            ::sfz::File* m_sfz;
132        };
133    
134    #if HAVE_SF2
135    
136        class Sf2FileInfo : public InstrumentFileInfo {
137        public:
138            Sf2FileInfo(String fileName) : InstrumentFileInfo(fileName) {
139                m_sf2  = NULL;
140                m_riff = NULL;
141                try {
142                    m_riff = new RIFF::File(fileName);
143                    m_sf2  = new sf2::File(m_riff);
144                } catch (RIFF::Exception e) {
145                    throw Exception(e.Message);
146                } catch (...) {
147                    throw Exception("Unknown exception while accessing sf2 file");
148                }
149            }
150    
151            virtual ~Sf2FileInfo() {
152                if (m_sf2)  delete m_sf2;
153                if (m_riff) delete m_riff;
154            }
155    
156            String formatName() OVERRIDE {
157                return "SF2";
158            }
159    
160            String formatVersion() OVERRIDE {
161                if (!m_sf2->pInfo || !m_sf2->pInfo->pVer) return "";
162                String major = ToString(m_sf2->pInfo->pVer->Major);
163                //String minor = ToString(m_sf2->pInfo->pVer->Minor);
164                //return major + "." + minor;
165                return major;
166            }
167    
168            optional<InstrumentInfo> getInstrumentInfo(int index, ScanProgress* pProgress) OVERRIDE {
169                if (index >= m_sf2->GetPresetCount())
170                    return optional<InstrumentInfo>::nothing;
171    
172                InstrumentInfo info;
173                try {
174                    ::sf2::Preset* preset = m_sf2->GetPreset(index);
175                    if (!preset)
176                        return optional<InstrumentInfo>::nothing;
177    
178                    info.instrumentName = preset->Name;
179                    if (m_sf2->pInfo) {
180                        info.product = m_sf2->pInfo->Product;
181                        info.comments = m_sf2->pInfo->Comments;
182                        info.artists = m_sf2->pInfo->Engineers;
183                    }
184                } catch (RIFF::Exception e) {
185                    throw Exception(e.Message);
186                } catch (...) {
187                    throw Exception("Unknown exception while accessing gig file");
188                }
189                return info;
190            }
191        private:
192            ::RIFF::File* m_riff;
193            ::sf2::File*  m_sf2;
194        };
195    
196    #endif // #if HAVE_SF2
197    
198      void DbInstrument::Copy(const DbInstrument& Instr) {      void DbInstrument::Copy(const DbInstrument& Instr) {
199          if (this == &Instr) return;          if (this == &Instr) return;
200    
# Line 65  namespace LinuxSampler { Line 230  namespace LinuxSampler {
230      void SearchQuery::SetFormatFamilies(String s) {      void SearchQuery::SetFormatFamilies(String s) {
231          if (s.length() == 0) return;          if (s.length() == 0) return;
232          int i = 0;          int i = 0;
233          int j = s.find(',', 0);          int j = (int) s.find(',', 0);
234                    
235          while (j != std::string::npos) {          while (j != std::string::npos) {
236              FormatFamilies.push_back(s.substr(i, j - i));              FormatFamilies.push_back(s.substr(i, j - i));
237              i = j + 1;              i = j + 1;
238              j = s.find(',', i);              j = (int) s.find(',', i);
239          }          }
240                    
241          if (i < s.length()) FormatFamilies.push_back(s.substr(i));          if (i < s.length()) FormatFamilies.push_back(s.substr(i));
# Line 99  namespace LinuxSampler { Line 264  namespace LinuxSampler {
264      String SearchQuery::GetMin(String s) {      String SearchQuery::GetMin(String s) {
265          if (s.length() < 3) return "";          if (s.length() < 3) return "";
266          if (s.at(0) == '.' && s.at(1) == '.') return "";          if (s.at(0) == '.' && s.at(1) == '.') return "";
267          int i = s.find("..");          int i = (int) s.find("..");
268          if (i == std::string::npos) return "";          if (i == std::string::npos) return "";
269          return s.substr(0, i);          return s.substr(0, i);
270      }      }
# Line 107  namespace LinuxSampler { Line 272  namespace LinuxSampler {
272      String SearchQuery::GetMax(String s) {      String SearchQuery::GetMax(String s) {
273          if (s.length() < 3) return "";          if (s.length() < 3) return "";
274          if (s.find("..", s.length() - 2) != std::string::npos) return "";          if (s.find("..", s.length() - 2) != std::string::npos) return "";
275          int i = s.find("..");          int i = (int) s.find("..");
276          if (i == std::string::npos) return "";          if (i == std::string::npos) return "";
277          return s.substr(i + 2);          return s.substr(i + 2);
278      }      }
# Line 152  namespace LinuxSampler { Line 317  namespace LinuxSampler {
317          if (Pattern.length() == 0) return;          if (Pattern.length() == 0) return;
318    
319          if (IsRegex(Pattern)) {          if (IsRegex(Pattern)) {
320    #ifndef WIN32
321              Sql << " AND " << Col << " regexp ?";              Sql << " AND " << Col << " regexp ?";
322    #else
323                for (int i = 0; i < Pattern.length(); i++) {
324                    if (Pattern.at(i) == '?') Pattern.at(i) = '_';
325                    else if (Pattern.at(i) == '*') Pattern.at(i) = '%';
326                }
327                Sql << " AND " << Col << " LIKE ?";
328    #endif
329              Params.push_back(Pattern);              Params.push_back(Pattern);
330              return;              return;
331          }          }
# Line 242  namespace LinuxSampler { Line 415  namespace LinuxSampler {
415      }      }
416    
417      StringListPtr DirectoryFinder::GetDirectories() {      StringListPtr DirectoryFinder::GetDirectories() {
418    #if __cplusplus >= 201103L && !CONFIG_NO_CPP11STL
419            return std::move(pDirectories);
420    #else
421          return pDirectories;          return pDirectories;
422    #endif
423      }      }
424            
425      void DirectoryFinder::ProcessDirectory(String Path, int DirId) {      void DirectoryFinder::ProcessDirectory(String Path, int DirId) {
# Line 355  namespace LinuxSampler { Line 532  namespace LinuxSampler {
532      }      }
533    
534      StringListPtr InstrumentFinder::GetInstruments() {      StringListPtr InstrumentFinder::GetInstruments() {
535    #if __cplusplus >= 201103L && !CONFIG_NO_CPP11STL
536            return std::move(pInstruments);
537    #else
538          return pInstruments;          return pInstruments;
539    #endif
540      }      }
541    
542      void DirectoryCounter::ProcessDirectory(String Path, int DirId) {      void DirectoryCounter::ProcessDirectory(String Path, int DirId) {
# Line 510  namespace LinuxSampler { Line 691  namespace LinuxSampler {
691    
692              for (int i = 0; i < fileList->size(); i++) {              for (int i = 0; i < fileList->size(); i++) {
693                  String s = fileList->at(i);                  String s = fileList->at(i);
694                  if (s.length() < 4) continue;                  if (InstrumentFileInfo::isSupportedFile(s)) count++;
                 if(!strcasecmp(".gig", s.substr(s.length() - 4).c_str())) count++;  
695              }              }
696          } catch(Exception e) {          } catch(Exception e) {
697              e.PrintMessage();              e.PrintMessage();
# Line 575  namespace LinuxSampler { Line 755  namespace LinuxSampler {
755              String subdir = Path;              String subdir = Path;
756              if(subdir.length() > FsDir.length()) {              if(subdir.length() > FsDir.length()) {
757                  subdir = subdir.substr(FsDir.length());                  subdir = subdir.substr(FsDir.length());
758    #ifdef WIN32
759                    replace(subdir.begin(), subdir.end(), '\\', '/');
760    #endif
761                  dir += subdir;                  dir += subdir;
762              }              }
763          }          }
# Line 603  namespace LinuxSampler { Line 786  namespace LinuxSampler {
786    
787      void InstrumentFileCounter::FileEntry(std::string Path) {      void InstrumentFileCounter::FileEntry(std::string Path) {
788          dmsg(2,("InstrumentFileCounter: FileEntry(Path=%s)\n", Path.c_str()));          dmsg(2,("InstrumentFileCounter: FileEntry(Path=%s)\n", Path.c_str()));
789          if(Path.length() < 4) return;          if (InstrumentFileInfo::isSupportedFile(Path)) FileCount++;
         if(!strcasecmp(".gig", Path.substr(Path.length() - 4).c_str())) FileCount++;  
790      };      };
791    
792    
793        InstrumentFileInfo* InstrumentFileInfo::getFileInfoFor(String filename) {
794            if (filename.length() < 4) return NULL;
795            String fileExtension = filename.substr(filename.length() - 4);
796            if (!strcasecmp(".gig", fileExtension.c_str()))
797                return new GigFileInfo(filename);
798            if (!strcasecmp(".sfz", fileExtension.c_str()))
799                return new SFZFileInfo(filename);
800            #if HAVE_SF2
801            if (!strcasecmp(".sf2", fileExtension.c_str()))
802                return new Sf2FileInfo(filename);
803            #endif
804            return NULL;
805        }
806    
807        bool InstrumentFileInfo::isSupportedFile(String filename) {
808            if (filename.length() < 4) return false;
809            String fileExtension = filename.substr(filename.length() - 4);
810            if (!strcasecmp(".gig", fileExtension.c_str())) return true;
811            if (!strcasecmp(".sfz", fileExtension.c_str())) return true;
812            #if HAVE_SF2
813            if (!strcasecmp(".sf2", fileExtension.c_str())) return true;
814            #endif
815            return false;
816        }
817    
818  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.1781  
changed lines
  Added in v.3101

  ViewVC Help
Powered by ViewVC