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

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

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

revision 55 by schoenebeck, Tue Apr 27 09:06:07 2004 UTC revision 608 by schoenebeck, Fri Jun 3 14:35:44 2005 UTC
# Line 2  Line 2 
2   *                                                                         *   *                                                                         *
3   *   libgig - C++ cross-platform Gigasampler format file loader library    *   *   libgig - C++ cross-platform Gigasampler format file loader library    *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003, 2004 by Christian Schoenebeck                     *   *   Copyright (C) 2003-2005 by Christian Schoenebeck                      *
6   *                               <cuse@users.sourceforge.net>              *   *                              <cuse@users.sourceforge.net>               *
7   *                                                                         *   *                                                                         *
8   *   This program is free software; you can redistribute it and/or modify  *   *   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  *   *   it under the terms of the GNU General Public License as published by  *
# Line 39  Line 39 
39    
40  #include <iostream>  #include <iostream>
41  #include <cstdlib>  #include <cstdlib>
42    #include <string.h>
43    #include <string>
44  #include <stdlib.h>  #include <stdlib.h>
45  #include <sys/types.h>  #include <sys/types.h>
46  #include <sys/stat.h>  #include <sys/stat.h>
47  #include <dirent.h>  #include <dirent.h>
48  #include <errno.h>  #include <errno.h>
49  #include <dlfcn.h>  #include <dlfcn.h>
50  #include <audiofile.h>  
51    // only libsndfile is available for Windows, so we use that for writing the sound files
52    #ifdef WIN32
53    # define HAVE_SNDFILE 1
54    #endif // WIN32
55    
56    // abort compilation here if neither libsndfile nor libaudiofile are available
57    #if !HAVE_SNDFILE && !HAVE_AUDIOFILE
58    # error "Neither libsndfile nor libaudiofile seem to be available!"
59    # error "(HAVE_SNDFILE and HAVE_AUDIOFILE are both false)"
60    #endif
61    
62    // we prefer libsndfile before libaudiofile
63    #if HAVE_SNDFILE
64    # include <sndfile.h>
65    #else
66    # include <audiofile.h>
67    #endif // HAVE_SNDFILE
68    
69  #include "gig.h"  #include "gig.h"
70    
71  using namespace std;  using namespace std;
# Line 53  using namespace std; Line 73  using namespace std;
73  typedef map<unsigned int, bool> OrderMap;  typedef map<unsigned int, bool> OrderMap;
74  OrderMap* pOrderedSamples = NULL;  OrderMap* pOrderedSamples = NULL;
75    
76    string Revision();
77    void PrintVersion();
78  void PrintUsage();  void PrintUsage();
79  void ExtractSamples(gig::File* gig, char* destdir, OrderMap* ordered);  void ExtractSamples(gig::File* gig, char* destdir, OrderMap* ordered);
80  int writeWav(const char* filename, void* samples, long samplecount, int channels, int bitdepth, long rate);  int writeWav(const char* filename, void* samples, long samplecount, int channels, int bitdepth, long rate);
81    string ToString(int i);
82    
83    #if !HAVE_SNDFILE // use libaudiofile
84  void* hAFlib; // handle to libaudiofile  void* hAFlib; // handle to libaudiofile
85  void openAFlib(void);  void openAFlib(void);
86  void closeAFlib(void);  void closeAFlib(void);
# Line 69  void(*_afInitRate)(AFfilesetup,int,doubl Line 94  void(*_afInitRate)(AFfilesetup,int,doubl
94  int(*_afWriteFrames)(AFfilehandle,int,const void*,int);  int(*_afWriteFrames)(AFfilehandle,int,const void*,int);
95  AFfilehandle(*_afOpenFile)(const char*,const char*,AFfilesetup);  AFfilehandle(*_afOpenFile)(const char*,const char*,AFfilesetup);
96  int(*_afCloseFile)(AFfilehandle file);  int(*_afCloseFile)(AFfilehandle file);
97  string ToString(int i);  #endif // !HAVE_SNDFILE
98    
99  int main(int argc, char *argv[]) {  int main(int argc, char *argv[]) {
100         if (argc >= 2) {
101            if (argv[1][0] == '-') {
102                switch (argv[1][1]) {
103                    case 'v':
104                        PrintVersion();
105                        return EXIT_SUCCESS;
106                }
107            }
108        }
109      if (argc < 3) {      if (argc < 3) {
110          PrintUsage();          PrintUsage();
111          return EXIT_FAILURE;          return EXIT_FAILURE;
# Line 133  int main(int argc, char *argv[]) { Line 167  int main(int argc, char *argv[]) {
167  }  }
168    
169  void ExtractSamples(gig::File* gig, char* destdir, OrderMap* ordered) {  void ExtractSamples(gig::File* gig, char* destdir, OrderMap* ordered) {
170    #if !HAVE_SNDFILE // use libaudiofile
171      hAFlib = NULL;      hAFlib = NULL;
172      openAFlib();      openAFlib();
173    #endif // !HAVE_SNDFILE
174      uint8_t* pWave  = NULL;      uint8_t* pWave  = NULL;
175      long BufferSize = 0;      long BufferSize = 0;
176      int samples     = 0;      int samples     = 0;
# Line 170  void ExtractSamples(gig::File* gig, char Line 206  void ExtractSamples(gig::File* gig, char
206    
207    
208  #if USE_DISK_STREAMING  #if USE_DISK_STREAMING
         if (pSample->Compressed) { // hack  
             pSample->BitDepth  = 16;  
             pSample->FrameSize = 4;  
         }  
   
209          long neededsize = (pSample->Compressed) ? 10485760 /* 10 MB buffer */          long neededsize = (pSample->Compressed) ? 10485760 /* 10 MB buffer */
210                                                  : pSample->SamplesTotal * pSample->FrameSize;                                                  : pSample->SamplesTotal * pSample->FrameSize;
211          if (BufferSize < neededsize) {          if (BufferSize < neededsize) {
# Line 189  void ExtractSamples(gig::File* gig, char Line 220  void ExtractSamples(gig::File* gig, char
220          uint8_t* pSamplePiece = pWave;          uint8_t* pSamplePiece = pWave;
221          do { // we read the sample in small pieces and increment the size with each run just to test streaming capability          do { // we read the sample in small pieces and increment the size with each run just to test streaming capability
222              readinthisrun = pSample->Read(pSamplePiece, ++samplepiecesize);              readinthisrun = pSample->Read(pSamplePiece, ++samplepiecesize);
223              pSamplePiece += readinthisrun * pSample->FrameSize;              // 24 bit is truncated to 16 by Sample::Read at the moment
224                pSamplePiece += readinthisrun * (2 * pSample->Channels); // readinthisrun * pSample->FrameSize;
225              readsamples  += readinthisrun;              readsamples  += readinthisrun;
226          } while (readinthisrun == samplepiecesize);          } while (readinthisrun == samplepiecesize);
227    
228          if (pSample->Compressed) { // hack          if (pSample->Compressed) { // hack
229              pSample->SamplesTotal = readsamples;              pSample->SamplesTotal = readsamples;
230                pSample->BitDepth = 16;
231          }          }
232  #  else // read in one piece  #  else // read in one piece
233          if (pSample->Compressed) {          if (pSample->Compressed) {
# Line 226  void ExtractSamples(gig::File* gig, char Line 259  void ExtractSamples(gig::File* gig, char
259          pSample = gig->GetNextSample();          pSample = gig->GetNextSample();
260      }      }
261      if (pWave) delete[] (uint8_t*) pWave;      if (pWave) delete[] (uint8_t*) pWave;
262    #if !HAVE_SNDFILE // use libaudiofile
263      closeAFlib();      closeAFlib();
264    #endif // !HAVE_SNDFILE
265  }  }
266    
267  int writeWav(const char* filename, void* samples, long samplecount, int channels, int bitdepth, long rate) {  int writeWav(const char* filename, void* samples, long samplecount, int channels, int bitdepth, long rate) {
268    #if HAVE_SNDFILE
269        SNDFILE* hfile;
270        SF_INFO  sfinfo;
271        int format = SF_FORMAT_WAV;
272        switch (bitdepth) {
273            case 8:
274                format |= SF_FORMAT_PCM_S8;
275                cout << "8 bit" << endl << flush;
276                break;
277            case 16:
278                format |= SF_FORMAT_PCM_16;
279                cout << "16 bit" << endl << flush;
280                break;
281            case 24:
282                format |= SF_FORMAT_PCM_24;
283                cout << "24 bit" << endl << flush;
284                break;
285            case 32:
286                format |= SF_FORMAT_PCM_32;
287                cout << "32 bit" << endl << flush;
288                break;
289            default:
290                cerr << "Error: Bithdepth " << ToString(bitdepth) << " not supported by libsndfile, ignoring sample!\n" << flush;
291                return -1;
292        }
293        memset(&sfinfo, 0, sizeof (sfinfo));
294        sfinfo.samplerate = rate;
295        sfinfo.frames     = samplecount;
296        sfinfo.channels   = channels;
297        sfinfo.format     = format;
298        if (!(hfile = sf_open(filename, SFM_WRITE, &sfinfo))) {
299            cerr << "Error: Unable to open output file \'" << filename << "\'.\n" << flush;
300            return -1;
301        }
302        if (sf_write_short(hfile, (short*)samples, channels * samplecount) != channels * samplecount) {
303            cerr << sf_strerror(hfile) << endl << flush;
304            sf_close(hfile);
305            return -1;
306        }
307        sf_close(hfile);
308    #else // use libaudiofile
309      AFfilesetup setup = _afNewFileSetup();      AFfilesetup setup = _afNewFileSetup();
310      _afInitFileFormat(setup, AF_FILE_WAVE);      _afInitFileFormat(setup, AF_FILE_WAVE);
311      _afInitChannels(setup, AF_DEFAULT_TRACK, channels);      _afInitChannels(setup, AF_DEFAULT_TRACK, channels);
# Line 241  int writeWav(const char* filename, void* Line 317  int writeWav(const char* filename, void*
317      if (_afWriteFrames(hFile, AF_DEFAULT_TRACK, samples, samplecount) < 0) return -1;      if (_afWriteFrames(hFile, AF_DEFAULT_TRACK, samples, samplecount) < 0) return -1;
318      _afCloseFile(hFile);      _afCloseFile(hFile);
319      _afFreeFileSetup(setup);      _afFreeFileSetup(setup);
320    #endif // HAVE_SNDFILE
321    
322      return 0;      return 0; // success
323  }  }
324    
325    #if !HAVE_SNDFILE // use libaudiofile
326  void openAFlib() {  void openAFlib() {
327      hAFlib = dlopen("libaudiofile.so", RTLD_NOW);      hAFlib = dlopen("libaudiofile.so", RTLD_NOW);
328      if (!hAFlib) {      if (!hAFlib) {
# Line 266  void openAFlib() { Line 344  void openAFlib() {
344  void closeAFlib() {  void closeAFlib() {
345      if (hAFlib) dlclose(hAFlib);      if (hAFlib) dlclose(hAFlib);
346  }  }
347    #endif // !HAVE_SNDFILE
348    
349    string Revision() {
350        string s = "$Revision: 1.7 $";
351        return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword
352    }
353    
354    void PrintVersion() {
355        cout << "gigextract revision " << Revision() << endl;
356        cout << "using " << gig::libraryName() << " " << gig::libraryVersion();
357        #if HAVE_SNDFILE
358        char versionBuffer[128];
359        sf_command(NULL, SFC_GET_LIB_VERSION, versionBuffer, 128);
360        cout << ", " << versionBuffer;
361        #else // use libaudiofile
362        cout << "\nbuilt against libaudiofile "
363             << LIBAUDIOFILE_MAJOR_VERSION << "." << LIBAUDIOFILE_MINOR_VERSION;
364        # ifdef LIBAUDIOFILE_MICRO_VERSION
365        cout << "." << LIBAUDIOFILE_MICRO_VERSION;
366        # endif // LIBAUDIOFILE_MICRO_VERSION
367        #endif // HAVE_SNDFILE
368        cout << endl;
369    }
370    
371  void PrintUsage() {  void PrintUsage() {
372      cout << "gigextract - extracts samples from a Gigasampler file." << endl;      cout << "gigextract - extracts samples from a Gigasampler file." << endl;
373      cout << endl;      cout << endl;
374      cout << "Usage: gigextract GIGFILE DESTDIR [SAMPLENR] [ [SAMPLENR] ...]" << endl;      cout << "Usage: gigextract [-v] GIGFILE DESTDIR [SAMPLENR] [ [SAMPLENR] ...]" << endl;
375      cout << endl;      cout << endl;
376      cout << "   GIGFILE  Input Gigasampler (.gig) file." << endl;      cout << "   GIGFILE  Input Gigasampler (.gig) file." << endl;
377      cout << endl;      cout << endl;
# Line 280  void PrintUsage() { Line 381  void PrintUsage() {
381      cout << "            If no sample indices are given, all samples will be extracted" << endl;      cout << "            If no sample indices are given, all samples will be extracted" << endl;
382      cout << "            (use gigdump to look for available samples)." << endl;      cout << "            (use gigdump to look for available samples)." << endl;
383      cout << endl;      cout << endl;
384        cout << "   -v       Print version and exit." << endl;
385        cout << endl;
386  }  }
387    
388  string ToString(int i) {  string ToString(int i) {

Legend:
Removed from v.55  
changed lines
  Added in v.608

  ViewVC Help
Powered by ViewVC