/[svn]/libgig/trunk/src/tools/wav2gig.cpp
ViewVC logotype

Diff of /libgig/trunk/src/tools/wav2gig.cpp

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

revision 3989 by schoenebeck, Sat Aug 28 13:46:54 2021 UTC revision 3990 by schoenebeck, Mon Aug 30 20:14:15 2021 UTC
# Line 82  static void printVersion() { Line 82  static void printVersion() {
82  static void printUsage() {  static void printUsage() {
83      cout << "wav2gig - Create GigaStudio file from a set of WAV files." << endl;      cout << "wav2gig - Create GigaStudio file from a set of WAV files." << endl;
84      cout << endl;      cout << endl;
85      cout << "Usage: wav2gig [-v] [-f] [-r] GIGFILE WAVFILEORDIR1 [ WAVFILEORDIR2 ... ]" << endl;      cout << "Usage: wav2gig [OPTIONS] GIGFILE WAVFILEORDIR1 [ WAVFILEORDIR2 ... ]" << endl;
86      cout << endl;      cout << endl;
87      cout << "  -v  Print version and exit." << endl;      cout << "  -v  Print version and exit." << endl;
88      cout << endl;      cout << endl;
# Line 90  static void printUsage() { Line 90  static void printUsage() {
90      cout << endl;      cout << endl;
91      cout << "  -r  Recurse through all subdirs of provided input WAV dirs." << endl;      cout << "  -r  Recurse through all subdirs of provided input WAV dirs." << endl;
92      cout << endl;      cout << endl;
93        cout << "  --regex-name1 PATTERN" << endl;
94        cout << endl;
95        cout << "      Regular expression for overriding the NAME1 part of the input sample file name scheme." << endl;
96        cout << endl;
97        cout << "  --regex-name2 PATTERN" << endl;
98        cout << endl;
99        cout << "      Regular expression for overriding the NAME2 part of the input sample file name scheme." << endl;
100        cout << endl;
101        cout << "  --regex-velocity-nr PATTERN" << endl;
102        cout << endl;
103        cout << "      Regular expression for overriding the VELOCITY_NR part of the input sample file name scheme." << endl;
104        cout << endl;
105        cout << "  --regex-note-nr PATTERN" << endl;
106        cout << endl;
107        cout << "      Regular expression for overriding the NOTE_NR part of the input sample file name scheme." << endl;
108        cout << endl;
109        cout << "  --regex-note-name PATTERN" << endl;
110        cout << endl;
111        cout << "      Regular expression for overriding the NOTE_NAME part of the input sample file name scheme." << endl;
112        cout << endl;
113        cout << "Read 'man wav2gig' for detailed help." << endl;
114        cout << endl;
115  }  }
116    
117  static bool beginsWith(const string& haystack, const string& needle) {  static bool beginsWith(const string& haystack, const string& needle) {
# Line 100  static bool endsWith(const string& hayst Line 122  static bool endsWith(const string& hayst
122      return haystack.substr(haystack.size() - needle.size(), needle.size()) == needle;      return haystack.substr(haystack.size() - needle.size(), needle.size()) == needle;
123  }  }
124    
125    static string tokenByRegExGroup(const string& haystack, const string& pattern,
126                                    size_t group = 1)
127    {
128        regex rx(pattern);
129        smatch m;
130        regex_search(haystack, m, rx);
131        return (m.size() <= group) ? (string) "" : (string) m[group];
132    }
133    
134  static bool fileExists(const string& filename) {  static bool fileExists(const string& filename) {
135      FILE* hFile = fopen(filename.c_str(), "r");      FILE* hFile = fopen(filename.c_str(), "r");
136      if (!hFile) return false;      if (!hFile) return false;
# Line 159  static bool isValidWavFile(const string& Line 190  static bool isValidWavFile(const string&
190      return false;      return false;
191  }  }
192    
193    struct FilenameRegExPatterns {
194        string name1;
195        string name2;
196        string velocityNr;
197        string noteNr;
198        string noteName;
199    };
200    
201  static void collectWavFilesOfDir(set<string>& result, string path, bool bRecurse, bool* pbError = NULL) {  static void collectWavFilesOfDir(set<string>& result, string path, bool bRecurse, bool* pbError = NULL) {
202      DIR* d = opendir(path.c_str());      DIR* d = opendir(path.c_str());
203      if (!d) {      if (!d) {
# Line 265  public: Line 304  public:
304    
305  typedef map<int,WavRegion> WavInstrument;  typedef map<int,WavRegion> WavInstrument;
306    
307  static WavInfo getWavInfo(string filename) {  static WavInfo getWavInfo(string filename,
308                              const FilenameRegExPatterns& patterns)
309    {
310      WavInfo wav;      WavInfo wav;
311      wav.fileName = filename;      wav.fileName = filename;
312      wav.sfinfo = {};      wav.sfinfo = {};
# Line 288  static WavInfo getWavInfo(string filenam Line 329  static WavInfo getWavInfo(string filenam
329          }          }
330      }      }
331      {      {
332          regex rx(          wav.name1 = tokenByRegExGroup(filename, patterns.name1);
333              "^([^-]+) - " // name 1 (e.g. "BSTEIN18")          if (wav.name1.empty()) {
334              "([^-]+) - "  // name 2 (e.g. "noname")              cerr << "Unexpected file name format: \"" << filename
335              "([^-]+) - "  // velocity value (e.g. "18")                   << "\" for 'name1' RegEx pattern \"" << patterns.name1
336              "([^-]+) - "  // note number (e.g. "021")                   << "\" !" << endl;
337              "([^.]+)"   // note name (e.g. "a-1")              exit(EXIT_FAILURE);
338          );          }
339          smatch m;          wav.name2 = tokenByRegExGroup(filename, patterns.name2);
340          regex_search(filename, m, rx);          if (wav.name2.empty()) {
341          if (m.size() < 5) {              cerr << "Unexpected file name format: \"" << filename
342              cerr << "Invalid file name format: \"" << filename << "\"!" << endl;                   << "\" for 'name2' RegEx pattern \"" << patterns.name2
343                     << "\" !" << endl;
344                exit(EXIT_FAILURE);
345            }
346            string sVelocity = tokenByRegExGroup(filename, patterns.velocityNr);
347            if (sVelocity.empty()) {
348                cerr << "Unexpected file name format: \"" << filename
349                     << "\" for 'velocity-nr' RegEx pattern \"" << patterns.velocityNr
350                     << "\" !" << endl;
351              exit(EXIT_FAILURE);              exit(EXIT_FAILURE);
352          }          }
         wav.name1    = m[1];  
         string sVelocity = m[3];  
353          wav.velocity = atoi(sVelocity.c_str());          wav.velocity = atoi(sVelocity.c_str());
354          string sNote = m[4];          string sNoteNr = tokenByRegExGroup(filename, patterns.noteNr);
355          wav.note     = atoi(sNote.c_str());          if (sNoteNr.empty()) {
356          wav.name2    = m[2];              cerr << "Unexpected file name format: \"" << filename
357          wav.noteName = m[5];                   << "\" for 'note-nr' RegEx pattern \"" << patterns.noteNr
358                     << "\" !" << endl;
359                exit(EXIT_FAILURE);
360            }
361            wav.note = atoi(sNoteNr.c_str());
362            wav.noteName = tokenByRegExGroup(filename, patterns.noteName);
363            if (wav.noteName.empty()) {
364                cerr << "Unexpected file name format: \"" << filename
365                     << "\" for 'note-name' RegEx pattern \"" << patterns.noteName
366                     << "\" !" << endl;
367                exit(EXIT_FAILURE);
368            }
369      }      }
370      return wav;      return wav;
371  }  }
# Line 377  static gig::Sample* createSample(gig::Fi Line 435  static gig::Sample* createSample(gig::Fi
435  int main(int argc, char *argv[]) {  int main(int argc, char *argv[]) {
436      bool bForce = false;      bool bForce = false;
437      bool bRecursive = false;      bool bRecursive = false;
438        FilenameRegExPatterns patterns = {
439            // name 1 (e.g. "BSTEIN18")
440            .name1 = "^([^-]+) - [^-]+ - [^-]+ - [^-]+ - [^.]+",
441            // name 2 (e.g. "noname")
442            .name2 = "^[^-]+ - ([^-]+) - [^-]+ - [^-]+ - [^.]+",
443            // velocity value (e.g. "18")
444            .velocityNr = "^[^-]+ - [^-]+ - ([^-]+) - [^-]+ - [^.]+",
445            // note number (e.g. "021")
446            .noteNr = "^[^-]+ - [^-]+ - [^-]+ - ([^-]+) - [^.]+",
447            // note name (e.g. "a-1")
448            .noteName = "^[^-]+ - [^-]+ - [^-]+ - [^-]+ - ([^.]+)",
449        };
450    
451      // validate & parse arguments provided to this program      // validate & parse arguments provided to this program
452      int iArg;      int iArg;
453      for (iArg = 1; iArg < argc; ++iArg) {      for (iArg = 1; iArg < argc; ++iArg) {
454          const string opt = argv[iArg];          const string opt = argv[iArg];
455            const string nextOpt = (iArg + 1 < argc) ? argv[iArg + 1] : "";
456          if (opt == "--") { // common for all command line tools: separator between initial option arguments and subsequent file arguments          if (opt == "--") { // common for all command line tools: separator between initial option arguments and subsequent file arguments
457              iArg++;              iArg++;
458              break;              break;
# Line 395  int main(int argc, char *argv[]) { Line 466  int main(int argc, char *argv[]) {
466              bForce = true;              bForce = true;
467          } else if (opt == "-r") {          } else if (opt == "-r") {
468              bRecursive = true;              bRecursive = true;
469            } else if (opt == "--regex-name1") {
470                if (nextOpt.empty() || beginsWith(nextOpt, "-")) {
471                    cerr << "Missing argument for option '" << opt << "'" << endl;
472                    return EXIT_FAILURE;
473                }
474                patterns.name1 = nextOpt;
475            } else if (opt == "--regex-name2") {
476                if (nextOpt.empty() || beginsWith(nextOpt, "-")) {
477                    cerr << "Missing argument for option '" << opt << "'" << endl;
478                    return EXIT_FAILURE;
479                }
480                patterns.name2 = nextOpt;
481            } else if (opt == "--regex-velocity-nr") {
482                if (nextOpt.empty() || beginsWith(nextOpt, "-")) {
483                    cerr << "Missing argument for option '" << opt << "'" << endl;
484                    return EXIT_FAILURE;
485                }
486                patterns.velocityNr = nextOpt;
487            } else if (opt == "--regex-note-nr") {
488                if (nextOpt.empty() || beginsWith(nextOpt, "-")) {
489                    cerr << "Missing argument for option '" << opt << "'" << endl;
490                    return EXIT_FAILURE;
491                }
492                patterns.noteNr = nextOpt;
493            } else if (opt == "--regex-note-name") {
494                if (nextOpt.empty() || beginsWith(nextOpt, "-")) {
495                    cerr << "Missing argument for option '" << opt << "'" << endl;
496                    return EXIT_FAILURE;
497                }
498                patterns.noteName = nextOpt;
499          } else {          } else {
500              cerr << "Unknown option '" << opt << "'" << endl;              cerr << "Unknown option '" << opt << "'" << endl;
501              cerr << endl;              cerr << endl;
# Line 462  int main(int argc, char *argv[]) { Line 563  int main(int argc, char *argv[]) {
563      for (set<string>::const_iterator it = wavFileNames.begin();      for (set<string>::const_iterator it = wavFileNames.begin();
564           it != wavFileNames.end(); ++it)           it != wavFileNames.end(); ++it)
565      {      {
566          WavInfo wavInfo = getWavInfo(*it);          WavInfo wavInfo = getWavInfo(*it, patterns);
567          wavInfo.assertValid(); // make sure collected informations are OK          wavInfo.assertValid(); // make sure collected informations are OK
568          if (wavInstrument[wavInfo.note].count(wavInfo.velocity)) {          if (wavInstrument[wavInfo.note].count(wavInfo.velocity)) {
569              cerr << "Velocity conflict between file '" << wavInfo.fileName              cerr << "Velocity conflict between file '" << wavInfo.fileName

Legend:
Removed from v.3989  
changed lines
  Added in v.3990

  ViewVC Help
Powered by ViewVC