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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 933 - (hide annotations) (download)
Fri Nov 24 12:50:05 2006 UTC (17 years, 5 months ago) by schoenebeck
File size: 16878 byte(s)
* preparations for release 3.1.0

1 schoenebeck 2 /***************************************************************************
2     * *
3 schoenebeck 933 * libgig - C++ cross-platform Gigasampler format file access library *
4 schoenebeck 2 * *
5 schoenebeck 933 * Copyright (C) 2003-2006 by Christian Schoenebeck *
6 schoenebeck 518 * <cuse@users.sourceforge.net> *
7 schoenebeck 2 * *
8 schoenebeck 933 * This program is part of libgig. *
9     * *
10 schoenebeck 2 * 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 *
12     * the Free Software Foundation; either version 2 of the License, or *
13     * (at your option) any later version. *
14     * *
15     * This program is distributed in the hope that it will be useful, *
16     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18     * GNU General Public License for more details. *
19     * *
20     * You should have received a copy of the GNU General Public License *
21     * along with this program; if not, write to the Free Software *
22     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
23     * MA 02111-1307 USA *
24     ***************************************************************************/
25    
26     #ifdef HAVE_CONFIG_H
27     #include <config.h>
28     #endif
29    
30     // just for testing the disk streaming capability of libgig;
31     // you will also need to set this to 1 at the moment if you want to
32     // extract compressed samples, as I haven't implemented the
33     // decompression algorithm in gig::Sample::LoadSampleData() yet
34     #define USE_DISK_STREAMING 1
35    
36     // only when USE_DISK_STREAMING is set to 1:
37     // just for testing the disk stream capability; with this option set
38     // gigextract will read the samples in smaller pieces, just to stress
39     // gig::Sample::Read() method a bit
40     #define HASHED_READS_TEST 1
41    
42     #include <iostream>
43     #include <cstdlib>
44 schoenebeck 220 #include <string.h>
45 schoenebeck 518 #include <string>
46 schoenebeck 2 #include <stdlib.h>
47     #include <sys/types.h>
48     #include <sys/stat.h>
49     #include <dirent.h>
50     #include <errno.h>
51     #include <dlfcn.h>
52 schoenebeck 220
53     // only libsndfile is available for Windows, so we use that for writing the sound files
54     #ifdef WIN32
55     # define HAVE_SNDFILE 1
56     #endif // WIN32
57    
58 schoenebeck 608 // 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 schoenebeck 220 // we prefer libsndfile before libaudiofile
65     #if HAVE_SNDFILE
66     # include <sndfile.h>
67     #else
68     # include <audiofile.h>
69     #endif // HAVE_SNDFILE
70    
71 schoenebeck 2 #include "gig.h"
72    
73     using namespace std;
74    
75     typedef map<unsigned int, bool> OrderMap;
76     OrderMap* pOrderedSamples = NULL;
77    
78 schoenebeck 518 string Revision();
79     void PrintVersion();
80 schoenebeck 2 void PrintUsage();
81     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);
83 schoenebeck 220 string ToString(int i);
84    
85 schoenebeck 608 #if !HAVE_SNDFILE // use libaudiofile
86 schoenebeck 2 void* hAFlib; // handle to libaudiofile
87     void openAFlib(void);
88     void closeAFlib(void);
89     // pointers to libaudiofile functions
90     AFfilesetup(*_afNewFileSetup)(void);
91     void(*_afFreeFileSetup)(AFfilesetup);
92     void(*_afInitChannels)(AFfilesetup,int,int);
93     void(*_afInitSampleFormat)(AFfilesetup,int,int,int);
94     void(*_afInitFileFormat)(AFfilesetup,int);
95     void(*_afInitRate)(AFfilesetup,int,double);
96     int(*_afWriteFrames)(AFfilehandle,int,const void*,int);
97     AFfilehandle(*_afOpenFile)(const char*,const char*,AFfilesetup);
98     int(*_afCloseFile)(AFfilehandle file);
99 schoenebeck 220 #endif // !HAVE_SNDFILE
100 schoenebeck 2
101     int main(int argc, char *argv[]) {
102 schoenebeck 518 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 schoenebeck 2 if (argc < 3) {
112     PrintUsage();
113     return EXIT_FAILURE;
114     }
115     if (argc > 3) { // extracting specific samples
116     pOrderedSamples = new OrderMap;
117     for (int i = 3; i < argc; i++) {
118     unsigned int index = atoi(argv[i]);
119     (*pOrderedSamples)[index] = true;
120     }
121     }
122     FILE* hFile = fopen(argv[1], "r");
123     if (!hFile) {
124     cout << "Invalid input file argument!" << endl;
125     return EXIT_FAILURE;
126     }
127     fclose(hFile);
128     DIR* dir = opendir(argv[2]);
129     if (!dir) {
130     cout << "Unable to open DESTDIR: ";
131     switch (errno) {
132     case EACCES: cout << "Permission denied." << endl;
133     break;
134     case EMFILE: cout << "Too many file descriptors in use by process." << endl;
135     break;
136     case ENFILE: cout << "Too many files are currently open in the system." << endl;
137     break;
138     case ENOENT: cout << "Directory does not exist, or name is an empty string." << endl;
139     break;
140     case ENOMEM: cout << "Insufficient memory to complete the operation." << endl;
141     break;
142     case ENOTDIR: cout << "Is not a directory." << endl;
143     break;
144     default: cout << "Unknown error" << endl;
145     }
146     return EXIT_FAILURE;
147     }
148     if (dir) closedir(dir);
149     try {
150     RIFF::File* riff = new RIFF::File(argv[1]);
151     gig::File* gig = new gig::File(riff);
152     cout << "Extracting samples from \"" << argv[1] << "\" to directory \"" << argv[2] << "\"." << endl << flush;
153     ExtractSamples(gig, argv[2], pOrderedSamples);
154     cout << "Extraction finished." << endl << flush;
155     delete gig;
156     delete riff;
157     if (pOrderedSamples) delete pOrderedSamples;
158     }
159     catch (RIFF::Exception e) {
160     e.PrintMessage();
161     return EXIT_FAILURE;
162     }
163     catch (...) {
164     cout << "Unknown exception while trying to parse file." << endl;
165     return EXIT_FAILURE;
166     }
167    
168     return EXIT_SUCCESS;
169     }
170    
171     void ExtractSamples(gig::File* gig, char* destdir, OrderMap* ordered) {
172 schoenebeck 608 #if !HAVE_SNDFILE // use libaudiofile
173 schoenebeck 2 hAFlib = NULL;
174     openAFlib();
175 schoenebeck 220 #endif // !HAVE_SNDFILE
176 schoenebeck 2 uint8_t* pWave = NULL;
177 persson 902 int* pIntWave = NULL;
178 schoenebeck 2 long BufferSize = 0;
179     int samples = 0;
180 persson 902 gig::buffer_t decompressionBuffer;
181     decompressionBuffer.Size = 0;
182     unsigned long decompressionBufferSize = 0;
183 schoenebeck 2 cout << "Seeking for available samples..." << flush;
184     gig::Sample* pSample = gig->GetFirstSample();
185     cout << "OK" << endl << flush;
186     while (pSample) {
187     samples++;
188     if (ordered) {
189     if ((*ordered)[samples] == false) {
190     pSample = gig->GetNextSample();
191     continue;
192     }
193     }
194     string name = pSample->pInfo->Name;
195     string filename = destdir;
196     if (filename[filename.size() - 1] != '/') filename += "/";
197     filename += ToString(samples);
198     filename += "_";
199     if (name == "") {
200     name = "(NO NAME)";
201     filename += "NONAME";
202     }
203     else {
204     filename += name;
205     name.insert(0, "\"");
206     name += "\"";
207     }
208     filename += ".wav";
209     if (pSample->Compressed) cout << "Decompressing ";
210     else cout << "Extracting ";
211     cout << "Sample " << samples << ") " << name << " (" << pSample->BitDepth <<"Bits, " << pSample->SamplesPerSecond << "Hz, " << pSample->Channels << " Channels, " << pSample->SamplesTotal << " Samples)..." << flush;
212    
213    
214     #if USE_DISK_STREAMING
215 persson 902 long neededsize = pSample->BitDepth == 24 ?
216     pSample->SamplesTotal * pSample->Channels * sizeof(int) :
217     pSample->SamplesTotal * pSample->FrameSize;
218 schoenebeck 2 if (BufferSize < neededsize) {
219     if (pWave) delete[] pWave;
220     pWave = new uint8_t[neededsize];
221     BufferSize = neededsize;
222     }
223 persson 902 pIntWave = (int*)pWave;
224 schoenebeck 2 # if HASHED_READS_TEST
225 persson 902 unsigned long readinthisrun = 0,
226 schoenebeck 2 samplepiecesize = 2000;
227     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
229     readinthisrun = pSample->Read(pSamplePiece, ++samplepiecesize);
230 persson 902 pSamplePiece += readinthisrun * pSample->FrameSize;
231 schoenebeck 2 } while (readinthisrun == samplepiecesize);
232    
233     # else // read in one piece
234     if (pSample->Compressed) {
235 persson 902 if (decompressionBufferSize < pSample->SamplesTotal) {
236     gig::Sample::DestroyDecompressionBuffer(decompressionBuffer);
237     decompressionBuffer = gig::Sample::CreateDecompressionBuffer(pSample->SamplesTotal);
238     decompressionBufferSize = pSample->SamplesTotal;
239     }
240     pSample->Read(pWave, pSample->SamplesTotal, &decompressionBuffer);
241     } else {
242 schoenebeck 2 pSample->Read(pWave, pSample->SamplesTotal);
243     }
244     # endif // HASHED_READS_TEST
245     #else // no disk streaming
246     if (pSample->Compressed) {
247 persson 902 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 schoenebeck 2 }
260     #endif // USE_DISK_STREAMING
261     if (pWave) {
262 persson 902
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 schoenebeck 2 int res = writeWav(filename.c_str(),
278 persson 902 pSample->BitDepth == 24 ? static_cast<void*>(pIntWave) : pWave,
279 schoenebeck 2 pSample->SamplesTotal,
280     pSample->Channels,
281     pSample->BitDepth,
282     pSample->SamplesPerSecond);
283     if (res < 0) cout << "Couldn't write sample data." << endl;
284     else cout << "ok" << endl;
285     pSample->ReleaseSampleData(); // free wave from RAM
286     }
287     else cout << "Failed to load sample data." << endl;
288    
289     pSample = gig->GetNextSample();
290     }
291 persson 902 gig::Sample::DestroyDecompressionBuffer(decompressionBuffer);
292     #if USE_DISK_STREAMING
293     if (pWave) delete[] pWave;
294     #else
295     if (pIntWave) delete[] pIntWave;
296     #endif
297 schoenebeck 608 #if !HAVE_SNDFILE // use libaudiofile
298 schoenebeck 2 closeAFlib();
299 schoenebeck 220 #endif // !HAVE_SNDFILE
300 schoenebeck 2 }
301    
302     int writeWav(const char* filename, void* samples, long samplecount, int channels, int bitdepth, long rate) {
303 schoenebeck 220 #if HAVE_SNDFILE
304     SNDFILE* hfile;
305     SF_INFO sfinfo;
306     int format = SF_FORMAT_WAV;
307     switch (bitdepth) {
308     case 8:
309     format |= SF_FORMAT_PCM_S8;
310     break;
311     case 16:
312     format |= SF_FORMAT_PCM_16;
313     break;
314     case 24:
315     format |= SF_FORMAT_PCM_24;
316     break;
317     case 32:
318     format |= SF_FORMAT_PCM_32;
319     break;
320     default:
321     cerr << "Error: Bithdepth " << ToString(bitdepth) << " not supported by libsndfile, ignoring sample!\n" << flush;
322     return -1;
323     }
324     memset(&sfinfo, 0, sizeof (sfinfo));
325     sfinfo.samplerate = rate;
326     sfinfo.frames = samplecount;
327     sfinfo.channels = channels;
328     sfinfo.format = format;
329     if (!(hfile = sf_open(filename, SFM_WRITE, &sfinfo))) {
330     cerr << "Error: Unable to open output file \'" << filename << "\'.\n" << flush;
331     return -1;
332     }
333 persson 902 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 schoenebeck 220 cerr << sf_strerror(hfile) << endl << flush;
338     sf_close(hfile);
339     return -1;
340     }
341     sf_close(hfile);
342 schoenebeck 608 #else // use libaudiofile
343 schoenebeck 2 AFfilesetup setup = _afNewFileSetup();
344     _afInitFileFormat(setup, AF_FILE_WAVE);
345     _afInitChannels(setup, AF_DEFAULT_TRACK, channels);
346     _afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, bitdepth);
347     _afInitRate(setup, AF_DEFAULT_TRACK, rate);
348     if (setup == AF_NULL_FILESETUP) return -1;
349     AFfilehandle hFile = _afOpenFile(filename, "w", setup);
350     if (hFile == AF_NULL_FILEHANDLE) return -1;
351     if (_afWriteFrames(hFile, AF_DEFAULT_TRACK, samples, samplecount) < 0) return -1;
352     _afCloseFile(hFile);
353     _afFreeFileSetup(setup);
354 schoenebeck 220 #endif // HAVE_SNDFILE
355 schoenebeck 55
356 schoenebeck 220 return 0; // success
357 schoenebeck 2 }
358    
359 schoenebeck 608 #if !HAVE_SNDFILE // use libaudiofile
360 schoenebeck 2 void openAFlib() {
361     hAFlib = dlopen("libaudiofile.so", RTLD_NOW);
362     if (!hAFlib) {
363     cout << "Unable to load library libaudiofile.so: " << dlerror() << endl;
364     return;
365     }
366     _afNewFileSetup = (AFfilesetup(*)(void)) dlsym(hAFlib, "afNewFileSetup");
367     _afFreeFileSetup = (void(*)(AFfilesetup)) dlsym(hAFlib, "afFreeFileSetup");
368     _afInitChannels = (void(*)(AFfilesetup,int,int)) dlsym(hAFlib, "afInitChannels");
369     _afInitSampleFormat = (void(*)(AFfilesetup,int,int,int)) dlsym(hAFlib, "afInitSampleFormat");
370     _afInitFileFormat = (void(*)(AFfilesetup,int)) dlsym(hAFlib, "afInitFileFormat");
371     _afInitRate = (void(*)(AFfilesetup,int,double)) dlsym(hAFlib, "afInitRate");
372     _afWriteFrames = (int(*)(AFfilehandle,int,const void*,int)) dlsym(hAFlib, "afWriteFrames");
373     _afOpenFile = (AFfilehandle(*)(const char*,const char*,AFfilesetup)) dlsym(hAFlib, "afOpenFile");
374     _afCloseFile = (int(*)(AFfilehandle file)) dlsym(hAFlib, "afCloseFile");
375     if (dlerror()) cout << "Failed to load function from libaudiofile.so: " << dlerror() << endl;
376     }
377    
378     void closeAFlib() {
379     if (hAFlib) dlclose(hAFlib);
380     }
381 schoenebeck 220 #endif // !HAVE_SNDFILE
382 schoenebeck 2
383 schoenebeck 518 string Revision() {
384 schoenebeck 933 string s = "$Revision: 1.9 $";
385 schoenebeck 518 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 schoenebeck 549 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 schoenebeck 608 #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 schoenebeck 549 #endif // HAVE_SNDFILE
402     cout << endl;
403 schoenebeck 518 }
404    
405 schoenebeck 2 void PrintUsage() {
406     cout << "gigextract - extracts samples from a Gigasampler file." << endl;
407     cout << endl;
408 schoenebeck 518 cout << "Usage: gigextract [-v] GIGFILE DESTDIR [SAMPLENR] [ [SAMPLENR] ...]" << endl;
409 schoenebeck 2 cout << endl;
410     cout << " GIGFILE Input Gigasampler (.gig) file." << endl;
411     cout << endl;
412     cout << " DESTDIR Destination directory where all .wav files will be written to." << endl;
413     cout << endl;
414     cout << " SAMPLENR Index (/indices) of Sample(s) which should be extracted." << endl;
415     cout << " If no sample indices are given, all samples will be extracted" << endl;
416     cout << " (use gigdump to look for available samples)." << endl;
417     cout << endl;
418 schoenebeck 518 cout << " -v Print version and exit." << endl;
419     cout << endl;
420 schoenebeck 2 }
421    
422     string ToString(int i) {
423     static char strbuf[1024];
424     sprintf(strbuf,"%d",i);
425     string s = strbuf;
426     return s;
427     }

  ViewVC Help
Powered by ViewVC