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

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

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

revision 365 by persson, Thu Feb 10 19:16:31 2005 UTC revision 933 by schoenebeck, Fri Nov 24 12:50:05 2006 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2   *                                                                         *   *                                                                         *
3   *   libgig - C++ cross-platform Gigasampler format file loader library    *   *   libgig - C++ cross-platform Gigasampler format file access library    *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003, 2004 by Christian Schoenebeck                     *   *   Copyright (C) 2003-2006 by Christian Schoenebeck                      *
6   *                               <cuse@users.sourceforge.net>              *   *                              <cuse@users.sourceforge.net>               *
7     *                                                                         *
8     *   This program is part of libgig.                                       *
9   *                                                                         *   *                                                                         *
10   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
11   *   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 40  Line 42 
42  #include <iostream>  #include <iostream>
43  #include <cstdlib>  #include <cstdlib>
44  #include <string.h>  #include <string.h>
45    #include <string>
46  #include <stdlib.h>  #include <stdlib.h>
47  #include <sys/types.h>  #include <sys/types.h>
48  #include <sys/stat.h>  #include <sys/stat.h>
# Line 52  Line 55 
55  # define HAVE_SNDFILE 1  # define HAVE_SNDFILE 1
56  #endif // WIN32  #endif // WIN32
57    
58    // abort compilation here if neither libsndfile nor libaudiofile are available
59    #if !HAVE_SNDFILE && !HAVE_AUDIOFILE
60    # error "Neither libsndfile nor libaudiofile seem to be available!"
61    # error "(HAVE_SNDFILE and HAVE_AUDIOFILE are both false)"
62    #endif
63    
64  // we prefer libsndfile before libaudiofile  // we prefer libsndfile before libaudiofile
65  #if HAVE_SNDFILE  #if HAVE_SNDFILE
66  # include <sndfile.h>  # include <sndfile.h>
# Line 66  using namespace std; Line 75  using namespace std;
75  typedef map<unsigned int, bool> OrderMap;  typedef map<unsigned int, bool> OrderMap;
76  OrderMap* pOrderedSamples = NULL;  OrderMap* pOrderedSamples = NULL;
77    
78    string Revision();
79    void PrintVersion();
80  void PrintUsage();  void PrintUsage();
81  void ExtractSamples(gig::File* gig, char* destdir, OrderMap* ordered);  void ExtractSamples(gig::File* gig, char* destdir, OrderMap* ordered);
82  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);
83  string ToString(int i);  string ToString(int i);
84    
85  #ifndef HAVE_SNDFILE  #if !HAVE_SNDFILE // use libaudiofile
86  void* hAFlib; // handle to libaudiofile  void* hAFlib; // handle to libaudiofile
87  void openAFlib(void);  void openAFlib(void);
88  void closeAFlib(void);  void closeAFlib(void);
# Line 88  int(*_afCloseFile)(AFfilehandle file); Line 99  int(*_afCloseFile)(AFfilehandle file);
99  #endif // !HAVE_SNDFILE  #endif // !HAVE_SNDFILE
100    
101  int main(int argc, char *argv[]) {  int main(int argc, char *argv[]) {
102         if (argc >= 2) {
103            if (argv[1][0] == '-') {
104                switch (argv[1][1]) {
105                    case 'v':
106                        PrintVersion();
107                        return EXIT_SUCCESS;
108                }
109            }
110        }
111      if (argc < 3) {      if (argc < 3) {
112          PrintUsage();          PrintUsage();
113          return EXIT_FAILURE;          return EXIT_FAILURE;
# Line 149  int main(int argc, char *argv[]) { Line 169  int main(int argc, char *argv[]) {
169  }  }
170    
171  void ExtractSamples(gig::File* gig, char* destdir, OrderMap* ordered) {  void ExtractSamples(gig::File* gig, char* destdir, OrderMap* ordered) {
172  #ifndef HAVE_SNDFILE  #if !HAVE_SNDFILE // use libaudiofile
173      hAFlib = NULL;      hAFlib = NULL;
174      openAFlib();      openAFlib();
175  #endif // !HAVE_SNDFILE  #endif // !HAVE_SNDFILE
176      uint8_t* pWave  = NULL;      uint8_t* pWave  = NULL;
177        int* pIntWave = NULL;
178      long BufferSize = 0;      long BufferSize = 0;
179      int samples     = 0;      int samples     = 0;
180        gig::buffer_t decompressionBuffer;
181        decompressionBuffer.Size = 0;
182        unsigned long decompressionBufferSize = 0;
183      cout << "Seeking for available samples..." << flush;      cout << "Seeking for available samples..." << flush;
184      gig::Sample* pSample = gig->GetFirstSample();      gig::Sample* pSample = gig->GetFirstSample();
185      cout << "OK" << endl << flush;      cout << "OK" << endl << flush;
# Line 188  void ExtractSamples(gig::File* gig, char Line 212  void ExtractSamples(gig::File* gig, char
212    
213    
214  #if USE_DISK_STREAMING  #if USE_DISK_STREAMING
215          long neededsize = (pSample->Compressed) ? 10485760 /* 10 MB buffer */          long neededsize = pSample->BitDepth == 24 ?
216                                                  : pSample->SamplesTotal * pSample->FrameSize;              pSample->SamplesTotal * pSample->Channels * sizeof(int) :
217                pSample->SamplesTotal * pSample->FrameSize;
218          if (BufferSize < neededsize) {          if (BufferSize < neededsize) {
219              if (pWave) delete[] pWave;              if (pWave) delete[] pWave;
220              pWave = new uint8_t[neededsize];              pWave = new uint8_t[neededsize];
221              BufferSize = neededsize;              BufferSize = neededsize;
222          }          }
223            pIntWave = (int*)pWave;
224  #  if HASHED_READS_TEST  #  if HASHED_READS_TEST
225          unsigned long readsamples     = 0,          unsigned long readinthisrun   = 0,
                       readinthisrun   = 0,  
226                        samplepiecesize = 2000;                        samplepiecesize = 2000;
227          uint8_t* pSamplePiece = pWave;          uint8_t* pSamplePiece = pWave;
228          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
229              readinthisrun = pSample->Read(pSamplePiece, ++samplepiecesize);              readinthisrun = pSample->Read(pSamplePiece, ++samplepiecesize);
230              // 24 bit is truncated to 16 by Sample::Read at the moment              pSamplePiece += readinthisrun * pSample->FrameSize;
             pSamplePiece += readinthisrun * (2 * pSample->Channels); // readinthisrun * pSample->FrameSize;  
             readsamples  += readinthisrun;  
231          } while (readinthisrun == samplepiecesize);          } while (readinthisrun == samplepiecesize);
232    
         if (pSample->Compressed) { // hack  
             pSample->SamplesTotal = readsamples;  
             pSample->BitDepth = 16;  
         }  
233  #  else // read in one piece  #  else // read in one piece
234          if (pSample->Compressed) {          if (pSample->Compressed) {
235              pSample->SamplesTotal = pSample->Read(pWave, 10485760 >> 2); // assumes 16 bit stereo              if (decompressionBufferSize < pSample->SamplesTotal) {
236          }                  gig::Sample::DestroyDecompressionBuffer(decompressionBuffer);
237          else {                  decompressionBuffer = gig::Sample::CreateDecompressionBuffer(pSample->SamplesTotal);
238                    decompressionBufferSize = pSample->SamplesTotal;
239                }
240                pSample->Read(pWave, pSample->SamplesTotal, &decompressionBuffer);
241            } else {
242              pSample->Read(pWave, pSample->SamplesTotal);              pSample->Read(pWave, pSample->SamplesTotal);
243          }          }
244  #  endif // HASHED_READS_TEST  #  endif // HASHED_READS_TEST
245  #else // no disk streaming  #else // no disk streaming
246          if (pSample->Compressed) {          if (pSample->Compressed) {
247              cout << "Sorry, sample is compressed and Sample::LoadSampleData() has no decompression routine yet! - Solution: set USE_DISK_STREAMING in gigextract.cpp (line 29) to 1 and recompile!" << endl;              cout << "Sorry, sample is compressed and Sample::LoadSampleData() only decompresses the beginning of the sample - Solution: set USE_DISK_STREAMING in gigextract.cpp (line 32) to 1 and recompile!" << endl;
248            } else {
249                gig::buffer_t buffer = pSample->LoadSampleData(); // load wave into RAM
250                pWave = static_cast<uint8_t*>(buffer.pStart);
251                if (pSample->BitDepth == 24) {
252                    long neededsize = pSample->SamplesTotal * pSample->Channels;
253                    if (BufferSize < neededsize) {
254                        if (pIntWave) delete[] pIntWave;
255                        pIntWave = new int[neededsize];
256                        BufferSize = neededsize;
257                    }
258                }
259          }          }
         else pWave = (uint8_t*) pSample->LoadSampleData(); // load wave into RAM  
260  #endif // USE_DISK_STREAMING  #endif // USE_DISK_STREAMING
261          if (pWave) {          if (pWave) {
262    
263                // Both libsndfile and libaudiofile uses int for 24 bit
264                // samples. libgig however returns 3 bytes per sample, so
265                // we have to convert the wave data before writing.
266                if (pSample->BitDepth == 24) {
267                    int n = pSample->SamplesTotal * pSample->Channels;
268                    for (int i = n - 1 ; i >= 0 ; i--) {
269    #if HAVE_SNDFILE
270                        pIntWave[i] = pWave[i * 3] << 8 | pWave[i * 3 + 1] << 16 | pWave[i * 3 + 2] << 24;
271    #else
272                        pIntWave[i] = pWave[i * 3] | pWave[i * 3 + 1] << 8 | pWave[i * 3 + 2] << 16;
273    #endif
274                    }
275                }
276    
277              int res = writeWav(filename.c_str(),              int res = writeWav(filename.c_str(),
278                                 pWave,                                 pSample->BitDepth == 24 ? static_cast<void*>(pIntWave) : pWave,
279                                 pSample->SamplesTotal,                                 pSample->SamplesTotal,
280                                 pSample->Channels,                                 pSample->Channels,
281                                 pSample->BitDepth,                                 pSample->BitDepth,
# Line 240  void ExtractSamples(gig::File* gig, char Line 288  void ExtractSamples(gig::File* gig, char
288    
289          pSample = gig->GetNextSample();          pSample = gig->GetNextSample();
290      }      }
291      if (pWave) delete[] (uint8_t*) pWave;      gig::Sample::DestroyDecompressionBuffer(decompressionBuffer);
292  #ifndef HAVE_SNDFILE  #if USE_DISK_STREAMING
293        if (pWave) delete[] pWave;
294    #else
295        if (pIntWave) delete[] pIntWave;
296    #endif
297    #if !HAVE_SNDFILE // use libaudiofile
298      closeAFlib();      closeAFlib();
299  #endif // !HAVE_SNDFILE  #endif // !HAVE_SNDFILE
300  }  }
# Line 254  int writeWav(const char* filename, void* Line 307  int writeWav(const char* filename, void*
307      switch (bitdepth) {      switch (bitdepth) {
308          case 8:          case 8:
309              format |= SF_FORMAT_PCM_S8;              format |= SF_FORMAT_PCM_S8;
             cout << "8 bit" << endl << flush;  
310              break;              break;
311          case 16:          case 16:
312              format |= SF_FORMAT_PCM_16;              format |= SF_FORMAT_PCM_16;
             cout << "16 bit" << endl << flush;  
313              break;              break;
314          case 24:          case 24:
315              format |= SF_FORMAT_PCM_24;              format |= SF_FORMAT_PCM_24;
             cout << "24 bit" << endl << flush;  
316              break;              break;
317          case 32:          case 32:
318              format |= SF_FORMAT_PCM_32;              format |= SF_FORMAT_PCM_32;
             cout << "32 bit" << endl << flush;  
319              break;              break;
320          default:          default:
321              cerr << "Error: Bithdepth " << ToString(bitdepth) << " not supported by libsndfile, ignoring sample!\n" << flush;              cerr << "Error: Bithdepth " << ToString(bitdepth) << " not supported by libsndfile, ignoring sample!\n" << flush;
# Line 281  int writeWav(const char* filename, void* Line 330  int writeWav(const char* filename, void*
330          cerr << "Error: Unable to open output file \'" << filename << "\'.\n" << flush;          cerr << "Error: Unable to open output file \'" << filename << "\'.\n" << flush;
331          return -1;          return -1;
332      }      }
333      if (sf_write_short(hfile, (short*)samples, channels * samplecount) != channels * samplecount) {      sf_count_t res = bitdepth == 24 ?
334            sf_write_int(hfile, static_cast<int*>(samples), channels * samplecount) :
335            sf_write_short(hfile, static_cast<short*>(samples), channels * samplecount);
336        if (res != channels * samplecount) {
337          cerr << sf_strerror(hfile) << endl << flush;          cerr << sf_strerror(hfile) << endl << flush;
338          sf_close(hfile);          sf_close(hfile);
339          return -1;          return -1;
340      }      }
341      sf_close(hfile);      sf_close(hfile);
342  #else  #else // use libaudiofile
343      AFfilesetup setup = _afNewFileSetup();      AFfilesetup setup = _afNewFileSetup();
344      _afInitFileFormat(setup, AF_FILE_WAVE);      _afInitFileFormat(setup, AF_FILE_WAVE);
345      _afInitChannels(setup, AF_DEFAULT_TRACK, channels);      _afInitChannels(setup, AF_DEFAULT_TRACK, channels);
# Line 304  int writeWav(const char* filename, void* Line 356  int writeWav(const char* filename, void*
356      return 0; // success      return 0; // success
357  }  }
358    
359  #ifndef HAVE_SNDFILE  #if !HAVE_SNDFILE // use libaudiofile
360  void openAFlib() {  void openAFlib() {
361      hAFlib = dlopen("libaudiofile.so", RTLD_NOW);      hAFlib = dlopen("libaudiofile.so", RTLD_NOW);
362      if (!hAFlib) {      if (!hAFlib) {
# Line 328  void closeAFlib() { Line 380  void closeAFlib() {
380  }  }
381  #endif // !HAVE_SNDFILE  #endif // !HAVE_SNDFILE
382    
383    string Revision() {
384        string s = "$Revision: 1.9 $";
385        return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword
386    }
387    
388    void PrintVersion() {
389        cout << "gigextract revision " << Revision() << endl;
390        cout << "using " << gig::libraryName() << " " << gig::libraryVersion();
391        #if HAVE_SNDFILE
392        char versionBuffer[128];
393        sf_command(NULL, SFC_GET_LIB_VERSION, versionBuffer, 128);
394        cout << ", " << versionBuffer;
395        #else // use libaudiofile
396        cout << "\nbuilt against libaudiofile "
397             << LIBAUDIOFILE_MAJOR_VERSION << "." << LIBAUDIOFILE_MINOR_VERSION;
398        # ifdef LIBAUDIOFILE_MICRO_VERSION
399        cout << "." << LIBAUDIOFILE_MICRO_VERSION;
400        # endif // LIBAUDIOFILE_MICRO_VERSION
401        #endif // HAVE_SNDFILE
402        cout << endl;
403    }
404    
405  void PrintUsage() {  void PrintUsage() {
406      cout << "gigextract - extracts samples from a Gigasampler file." << endl;      cout << "gigextract - extracts samples from a Gigasampler file." << endl;
407      cout << endl;      cout << endl;
408      cout << "Usage: gigextract GIGFILE DESTDIR [SAMPLENR] [ [SAMPLENR] ...]" << endl;      cout << "Usage: gigextract [-v] GIGFILE DESTDIR [SAMPLENR] [ [SAMPLENR] ...]" << endl;
409      cout << endl;      cout << endl;
410      cout << "   GIGFILE  Input Gigasampler (.gig) file." << endl;      cout << "   GIGFILE  Input Gigasampler (.gig) file." << endl;
411      cout << endl;      cout << endl;
# Line 341  void PrintUsage() { Line 415  void PrintUsage() {
415      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;
416      cout << "            (use gigdump to look for available samples)." << endl;      cout << "            (use gigdump to look for available samples)." << endl;
417      cout << endl;      cout << endl;
418        cout << "   -v       Print version and exit." << endl;
419        cout << endl;
420  }  }
421    
422  string ToString(int i) {  string ToString(int i) {

Legend:
Removed from v.365  
changed lines
  Added in v.933

  ViewVC Help
Powered by ViewVC