/[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 608 by schoenebeck, Fri Jun 3 14:35:44 2005 UTC revision 902 by persson, Sat Jul 22 14:22:01 2006 UTC
# Line 172  void ExtractSamples(gig::File* gig, char Line 172  void ExtractSamples(gig::File* gig, char
172      openAFlib();      openAFlib();
173  #endif // !HAVE_SNDFILE  #endif // !HAVE_SNDFILE
174      uint8_t* pWave  = NULL;      uint8_t* pWave  = NULL;
175        int* pIntWave = NULL;
176      long BufferSize = 0;      long BufferSize = 0;
177      int samples     = 0;      int samples     = 0;
178        gig::buffer_t decompressionBuffer;
179        decompressionBuffer.Size = 0;
180        unsigned long decompressionBufferSize = 0;
181      cout << "Seeking for available samples..." << flush;      cout << "Seeking for available samples..." << flush;
182      gig::Sample* pSample = gig->GetFirstSample();      gig::Sample* pSample = gig->GetFirstSample();
183      cout << "OK" << endl << flush;      cout << "OK" << endl << flush;
# Line 206  void ExtractSamples(gig::File* gig, char Line 210  void ExtractSamples(gig::File* gig, char
210    
211    
212  #if USE_DISK_STREAMING  #if USE_DISK_STREAMING
213          long neededsize = (pSample->Compressed) ? 10485760 /* 10 MB buffer */          long neededsize = pSample->BitDepth == 24 ?
214                                                  : pSample->SamplesTotal * pSample->FrameSize;              pSample->SamplesTotal * pSample->Channels * sizeof(int) :
215                pSample->SamplesTotal * pSample->FrameSize;
216          if (BufferSize < neededsize) {          if (BufferSize < neededsize) {
217              if (pWave) delete[] pWave;              if (pWave) delete[] pWave;
218              pWave = new uint8_t[neededsize];              pWave = new uint8_t[neededsize];
219              BufferSize = neededsize;              BufferSize = neededsize;
220          }          }
221            pIntWave = (int*)pWave;
222  #  if HASHED_READS_TEST  #  if HASHED_READS_TEST
223          unsigned long readsamples     = 0,          unsigned long readinthisrun   = 0,
                       readinthisrun   = 0,  
224                        samplepiecesize = 2000;                        samplepiecesize = 2000;
225          uint8_t* pSamplePiece = pWave;          uint8_t* pSamplePiece = pWave;
226          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
227              readinthisrun = pSample->Read(pSamplePiece, ++samplepiecesize);              readinthisrun = pSample->Read(pSamplePiece, ++samplepiecesize);
228              // 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;  
229          } while (readinthisrun == samplepiecesize);          } while (readinthisrun == samplepiecesize);
230    
         if (pSample->Compressed) { // hack  
             pSample->SamplesTotal = readsamples;  
             pSample->BitDepth = 16;  
         }  
231  #  else // read in one piece  #  else // read in one piece
232          if (pSample->Compressed) {          if (pSample->Compressed) {
233              pSample->SamplesTotal = pSample->Read(pWave, 10485760 >> 2); // assumes 16 bit stereo              if (decompressionBufferSize < pSample->SamplesTotal) {
234          }                  gig::Sample::DestroyDecompressionBuffer(decompressionBuffer);
235          else {                  decompressionBuffer = gig::Sample::CreateDecompressionBuffer(pSample->SamplesTotal);
236                    decompressionBufferSize = pSample->SamplesTotal;
237                }
238                pSample->Read(pWave, pSample->SamplesTotal, &decompressionBuffer);
239            } else {
240              pSample->Read(pWave, pSample->SamplesTotal);              pSample->Read(pWave, pSample->SamplesTotal);
241          }          }
242  #  endif // HASHED_READS_TEST  #  endif // HASHED_READS_TEST
243  #else // no disk streaming  #else // no disk streaming
244          if (pSample->Compressed) {          if (pSample->Compressed) {
245              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;
246            } else {
247                gig::buffer_t buffer = pSample->LoadSampleData(); // load wave into RAM
248                pWave = static_cast<uint8_t*>(buffer.pStart);
249                if (pSample->BitDepth == 24) {
250                    long neededsize = pSample->SamplesTotal * pSample->Channels;
251                    if (BufferSize < neededsize) {
252                        if (pIntWave) delete[] pIntWave;
253                        pIntWave = new int[neededsize];
254                        BufferSize = neededsize;
255                    }
256                }
257          }          }
         else pWave = (uint8_t*) pSample->LoadSampleData(); // load wave into RAM  
258  #endif // USE_DISK_STREAMING  #endif // USE_DISK_STREAMING
259          if (pWave) {          if (pWave) {
260    
261                // Both libsndfile and libaudiofile uses int for 24 bit
262                // samples. libgig however returns 3 bytes per sample, so
263                // we have to convert the wave data before writing.
264                if (pSample->BitDepth == 24) {
265                    int n = pSample->SamplesTotal * pSample->Channels;
266                    for (int i = n - 1 ; i >= 0 ; i--) {
267    #if HAVE_SNDFILE
268                        pIntWave[i] = pWave[i * 3] << 8 | pWave[i * 3 + 1] << 16 | pWave[i * 3 + 2] << 24;
269    #else
270                        pIntWave[i] = pWave[i * 3] | pWave[i * 3 + 1] << 8 | pWave[i * 3 + 2] << 16;
271    #endif
272                    }
273                }
274    
275              int res = writeWav(filename.c_str(),              int res = writeWav(filename.c_str(),
276                                 pWave,                                 pSample->BitDepth == 24 ? static_cast<void*>(pIntWave) : pWave,
277                                 pSample->SamplesTotal,                                 pSample->SamplesTotal,
278                                 pSample->Channels,                                 pSample->Channels,
279                                 pSample->BitDepth,                                 pSample->BitDepth,
# Line 258  void ExtractSamples(gig::File* gig, char Line 286  void ExtractSamples(gig::File* gig, char
286    
287          pSample = gig->GetNextSample();          pSample = gig->GetNextSample();
288      }      }
289      if (pWave) delete[] (uint8_t*) pWave;      gig::Sample::DestroyDecompressionBuffer(decompressionBuffer);
290    #if USE_DISK_STREAMING
291        if (pWave) delete[] pWave;
292    #else
293        if (pIntWave) delete[] pIntWave;
294    #endif
295  #if !HAVE_SNDFILE // use libaudiofile  #if !HAVE_SNDFILE // use libaudiofile
296      closeAFlib();      closeAFlib();
297  #endif // !HAVE_SNDFILE  #endif // !HAVE_SNDFILE
# Line 272  int writeWav(const char* filename, void* Line 305  int writeWav(const char* filename, void*
305      switch (bitdepth) {      switch (bitdepth) {
306          case 8:          case 8:
307              format |= SF_FORMAT_PCM_S8;              format |= SF_FORMAT_PCM_S8;
             cout << "8 bit" << endl << flush;  
308              break;              break;
309          case 16:          case 16:
310              format |= SF_FORMAT_PCM_16;              format |= SF_FORMAT_PCM_16;
             cout << "16 bit" << endl << flush;  
311              break;              break;
312          case 24:          case 24:
313              format |= SF_FORMAT_PCM_24;              format |= SF_FORMAT_PCM_24;
             cout << "24 bit" << endl << flush;  
314              break;              break;
315          case 32:          case 32:
316              format |= SF_FORMAT_PCM_32;              format |= SF_FORMAT_PCM_32;
             cout << "32 bit" << endl << flush;  
317              break;              break;
318          default:          default:
319              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 299  int writeWav(const char* filename, void* Line 328  int writeWav(const char* filename, void*
328          cerr << "Error: Unable to open output file \'" << filename << "\'.\n" << flush;          cerr << "Error: Unable to open output file \'" << filename << "\'.\n" << flush;
329          return -1;          return -1;
330      }      }
331      if (sf_write_short(hfile, (short*)samples, channels * samplecount) != channels * samplecount) {      sf_count_t res = bitdepth == 24 ?
332            sf_write_int(hfile, static_cast<int*>(samples), channels * samplecount) :
333            sf_write_short(hfile, static_cast<short*>(samples), channels * samplecount);
334        if (res != channels * samplecount) {
335          cerr << sf_strerror(hfile) << endl << flush;          cerr << sf_strerror(hfile) << endl << flush;
336          sf_close(hfile);          sf_close(hfile);
337          return -1;          return -1;
# Line 347  void closeAFlib() { Line 379  void closeAFlib() {
379  #endif // !HAVE_SNDFILE  #endif // !HAVE_SNDFILE
380    
381  string Revision() {  string Revision() {
382      string s = "$Revision: 1.7 $";      string s = "$Revision: 1.8 $";
383      return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword      return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword
384  }  }
385    

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

  ViewVC Help
Powered by ViewVC