1 |
/*************************************************************************** |
2 |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file loader library * |
4 |
* * |
5 |
* Copyright (C) 2003-2005 by Christian Schoenebeck * |
6 |
* <cuse@users.sourceforge.net> * |
7 |
* * |
8 |
* 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 * |
10 |
* the Free Software Foundation; either version 2 of the License, or * |
11 |
* (at your option) any later version. * |
12 |
* * |
13 |
* This program is distributed in the hope that it will be useful, * |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
* GNU General Public License for more details. * |
17 |
* * |
18 |
* You should have received a copy of the GNU General Public License * |
19 |
* along with this program; if not, write to the Free Software * |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
* MA 02111-1307 USA * |
22 |
***************************************************************************/ |
23 |
|
24 |
#ifdef HAVE_CONFIG_H |
25 |
#include <config.h> |
26 |
#endif |
27 |
|
28 |
// just for testing the disk streaming capability of libgig; |
29 |
// you will also need to set this to 1 at the moment if you want to |
30 |
// extract compressed samples, as I haven't implemented the |
31 |
// decompression algorithm in gig::Sample::LoadSampleData() yet |
32 |
#define USE_DISK_STREAMING 1 |
33 |
|
34 |
// only when USE_DISK_STREAMING is set to 1: |
35 |
// just for testing the disk stream capability; with this option set |
36 |
// gigextract will read the samples in smaller pieces, just to stress |
37 |
// gig::Sample::Read() method a bit |
38 |
#define HASHED_READS_TEST 1 |
39 |
|
40 |
#include <iostream> |
41 |
#include <cstdlib> |
42 |
#include <string.h> |
43 |
#include <string> |
44 |
#include <stdlib.h> |
45 |
#include <sys/types.h> |
46 |
#include <sys/stat.h> |
47 |
#include <dirent.h> |
48 |
#include <errno.h> |
49 |
#include <dlfcn.h> |
50 |
|
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" |
70 |
|
71 |
using namespace std; |
72 |
|
73 |
typedef map<unsigned int, bool> OrderMap; |
74 |
OrderMap* pOrderedSamples = NULL; |
75 |
|
76 |
string Revision(); |
77 |
void PrintVersion(); |
78 |
void PrintUsage(); |
79 |
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); |
81 |
string ToString(int i); |
82 |
|
83 |
#if !HAVE_SNDFILE // use libaudiofile |
84 |
void* hAFlib; // handle to libaudiofile |
85 |
void openAFlib(void); |
86 |
void closeAFlib(void); |
87 |
// pointers to libaudiofile functions |
88 |
AFfilesetup(*_afNewFileSetup)(void); |
89 |
void(*_afFreeFileSetup)(AFfilesetup); |
90 |
void(*_afInitChannels)(AFfilesetup,int,int); |
91 |
void(*_afInitSampleFormat)(AFfilesetup,int,int,int); |
92 |
void(*_afInitFileFormat)(AFfilesetup,int); |
93 |
void(*_afInitRate)(AFfilesetup,int,double); |
94 |
int(*_afWriteFrames)(AFfilehandle,int,const void*,int); |
95 |
AFfilehandle(*_afOpenFile)(const char*,const char*,AFfilesetup); |
96 |
int(*_afCloseFile)(AFfilehandle file); |
97 |
#endif // !HAVE_SNDFILE |
98 |
|
99 |
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) { |
110 |
PrintUsage(); |
111 |
return EXIT_FAILURE; |
112 |
} |
113 |
if (argc > 3) { // extracting specific samples |
114 |
pOrderedSamples = new OrderMap; |
115 |
for (int i = 3; i < argc; i++) { |
116 |
unsigned int index = atoi(argv[i]); |
117 |
(*pOrderedSamples)[index] = true; |
118 |
} |
119 |
} |
120 |
FILE* hFile = fopen(argv[1], "r"); |
121 |
if (!hFile) { |
122 |
cout << "Invalid input file argument!" << endl; |
123 |
return EXIT_FAILURE; |
124 |
} |
125 |
fclose(hFile); |
126 |
DIR* dir = opendir(argv[2]); |
127 |
if (!dir) { |
128 |
cout << "Unable to open DESTDIR: "; |
129 |
switch (errno) { |
130 |
case EACCES: cout << "Permission denied." << endl; |
131 |
break; |
132 |
case EMFILE: cout << "Too many file descriptors in use by process." << endl; |
133 |
break; |
134 |
case ENFILE: cout << "Too many files are currently open in the system." << endl; |
135 |
break; |
136 |
case ENOENT: cout << "Directory does not exist, or name is an empty string." << endl; |
137 |
break; |
138 |
case ENOMEM: cout << "Insufficient memory to complete the operation." << endl; |
139 |
break; |
140 |
case ENOTDIR: cout << "Is not a directory." << endl; |
141 |
break; |
142 |
default: cout << "Unknown error" << endl; |
143 |
} |
144 |
return EXIT_FAILURE; |
145 |
} |
146 |
if (dir) closedir(dir); |
147 |
try { |
148 |
RIFF::File* riff = new RIFF::File(argv[1]); |
149 |
gig::File* gig = new gig::File(riff); |
150 |
cout << "Extracting samples from \"" << argv[1] << "\" to directory \"" << argv[2] << "\"." << endl << flush; |
151 |
ExtractSamples(gig, argv[2], pOrderedSamples); |
152 |
cout << "Extraction finished." << endl << flush; |
153 |
delete gig; |
154 |
delete riff; |
155 |
if (pOrderedSamples) delete pOrderedSamples; |
156 |
} |
157 |
catch (RIFF::Exception e) { |
158 |
e.PrintMessage(); |
159 |
return EXIT_FAILURE; |
160 |
} |
161 |
catch (...) { |
162 |
cout << "Unknown exception while trying to parse file." << endl; |
163 |
return EXIT_FAILURE; |
164 |
} |
165 |
|
166 |
return EXIT_SUCCESS; |
167 |
} |
168 |
|
169 |
void ExtractSamples(gig::File* gig, char* destdir, OrderMap* ordered) { |
170 |
#if !HAVE_SNDFILE // use libaudiofile |
171 |
hAFlib = NULL; |
172 |
openAFlib(); |
173 |
#endif // !HAVE_SNDFILE |
174 |
uint8_t* pWave = NULL; |
175 |
long BufferSize = 0; |
176 |
int samples = 0; |
177 |
cout << "Seeking for available samples..." << flush; |
178 |
gig::Sample* pSample = gig->GetFirstSample(); |
179 |
cout << "OK" << endl << flush; |
180 |
while (pSample) { |
181 |
samples++; |
182 |
if (ordered) { |
183 |
if ((*ordered)[samples] == false) { |
184 |
pSample = gig->GetNextSample(); |
185 |
continue; |
186 |
} |
187 |
} |
188 |
string name = pSample->pInfo->Name; |
189 |
string filename = destdir; |
190 |
if (filename[filename.size() - 1] != '/') filename += "/"; |
191 |
filename += ToString(samples); |
192 |
filename += "_"; |
193 |
if (name == "") { |
194 |
name = "(NO NAME)"; |
195 |
filename += "NONAME"; |
196 |
} |
197 |
else { |
198 |
filename += name; |
199 |
name.insert(0, "\""); |
200 |
name += "\""; |
201 |
} |
202 |
filename += ".wav"; |
203 |
if (pSample->Compressed) cout << "Decompressing "; |
204 |
else cout << "Extracting "; |
205 |
cout << "Sample " << samples << ") " << name << " (" << pSample->BitDepth <<"Bits, " << pSample->SamplesPerSecond << "Hz, " << pSample->Channels << " Channels, " << pSample->SamplesTotal << " Samples)..." << flush; |
206 |
|
207 |
|
208 |
#if USE_DISK_STREAMING |
209 |
long neededsize = (pSample->Compressed) ? 10485760 /* 10 MB buffer */ |
210 |
: pSample->SamplesTotal * pSample->FrameSize; |
211 |
if (BufferSize < neededsize) { |
212 |
if (pWave) delete[] pWave; |
213 |
pWave = new uint8_t[neededsize]; |
214 |
BufferSize = neededsize; |
215 |
} |
216 |
# if HASHED_READS_TEST |
217 |
unsigned long readsamples = 0, |
218 |
readinthisrun = 0, |
219 |
samplepiecesize = 2000; |
220 |
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 |
222 |
readinthisrun = pSample->Read(pSamplePiece, ++samplepiecesize); |
223 |
// 24 bit is truncated to 16 by Sample::Read at the moment |
224 |
pSamplePiece += readinthisrun * (2 * pSample->Channels); // readinthisrun * pSample->FrameSize; |
225 |
readsamples += readinthisrun; |
226 |
} while (readinthisrun == samplepiecesize); |
227 |
|
228 |
if (pSample->Compressed) { // hack |
229 |
pSample->SamplesTotal = readsamples; |
230 |
pSample->BitDepth = 16; |
231 |
} |
232 |
# else // read in one piece |
233 |
if (pSample->Compressed) { |
234 |
pSample->SamplesTotal = pSample->Read(pWave, 10485760 >> 2); // assumes 16 bit stereo |
235 |
} |
236 |
else { |
237 |
pSample->Read(pWave, pSample->SamplesTotal); |
238 |
} |
239 |
# endif // HASHED_READS_TEST |
240 |
#else // no disk streaming |
241 |
if (pSample->Compressed) { |
242 |
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; |
243 |
} |
244 |
else pWave = (uint8_t*) pSample->LoadSampleData(); // load wave into RAM |
245 |
#endif // USE_DISK_STREAMING |
246 |
if (pWave) { |
247 |
int res = writeWav(filename.c_str(), |
248 |
pWave, |
249 |
pSample->SamplesTotal, |
250 |
pSample->Channels, |
251 |
pSample->BitDepth, |
252 |
pSample->SamplesPerSecond); |
253 |
if (res < 0) cout << "Couldn't write sample data." << endl; |
254 |
else cout << "ok" << endl; |
255 |
pSample->ReleaseSampleData(); // free wave from RAM |
256 |
} |
257 |
else cout << "Failed to load sample data." << endl; |
258 |
|
259 |
pSample = gig->GetNextSample(); |
260 |
} |
261 |
if (pWave) delete[] (uint8_t*) pWave; |
262 |
#if !HAVE_SNDFILE // use libaudiofile |
263 |
closeAFlib(); |
264 |
#endif // !HAVE_SNDFILE |
265 |
} |
266 |
|
267 |
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(); |
310 |
_afInitFileFormat(setup, AF_FILE_WAVE); |
311 |
_afInitChannels(setup, AF_DEFAULT_TRACK, channels); |
312 |
_afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, bitdepth); |
313 |
_afInitRate(setup, AF_DEFAULT_TRACK, rate); |
314 |
if (setup == AF_NULL_FILESETUP) return -1; |
315 |
AFfilehandle hFile = _afOpenFile(filename, "w", setup); |
316 |
if (hFile == AF_NULL_FILEHANDLE) return -1; |
317 |
if (_afWriteFrames(hFile, AF_DEFAULT_TRACK, samples, samplecount) < 0) return -1; |
318 |
_afCloseFile(hFile); |
319 |
_afFreeFileSetup(setup); |
320 |
#endif // HAVE_SNDFILE |
321 |
|
322 |
return 0; // success |
323 |
} |
324 |
|
325 |
#if !HAVE_SNDFILE // use libaudiofile |
326 |
void openAFlib() { |
327 |
hAFlib = dlopen("libaudiofile.so", RTLD_NOW); |
328 |
if (!hAFlib) { |
329 |
cout << "Unable to load library libaudiofile.so: " << dlerror() << endl; |
330 |
return; |
331 |
} |
332 |
_afNewFileSetup = (AFfilesetup(*)(void)) dlsym(hAFlib, "afNewFileSetup"); |
333 |
_afFreeFileSetup = (void(*)(AFfilesetup)) dlsym(hAFlib, "afFreeFileSetup"); |
334 |
_afInitChannels = (void(*)(AFfilesetup,int,int)) dlsym(hAFlib, "afInitChannels"); |
335 |
_afInitSampleFormat = (void(*)(AFfilesetup,int,int,int)) dlsym(hAFlib, "afInitSampleFormat"); |
336 |
_afInitFileFormat = (void(*)(AFfilesetup,int)) dlsym(hAFlib, "afInitFileFormat"); |
337 |
_afInitRate = (void(*)(AFfilesetup,int,double)) dlsym(hAFlib, "afInitRate"); |
338 |
_afWriteFrames = (int(*)(AFfilehandle,int,const void*,int)) dlsym(hAFlib, "afWriteFrames"); |
339 |
_afOpenFile = (AFfilehandle(*)(const char*,const char*,AFfilesetup)) dlsym(hAFlib, "afOpenFile"); |
340 |
_afCloseFile = (int(*)(AFfilehandle file)) dlsym(hAFlib, "afCloseFile"); |
341 |
if (dlerror()) cout << "Failed to load function from libaudiofile.so: " << dlerror() << endl; |
342 |
} |
343 |
|
344 |
void closeAFlib() { |
345 |
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() { |
372 |
cout << "gigextract - extracts samples from a Gigasampler file." << endl; |
373 |
cout << endl; |
374 |
cout << "Usage: gigextract [-v] GIGFILE DESTDIR [SAMPLENR] [ [SAMPLENR] ...]" << endl; |
375 |
cout << endl; |
376 |
cout << " GIGFILE Input Gigasampler (.gig) file." << endl; |
377 |
cout << endl; |
378 |
cout << " DESTDIR Destination directory where all .wav files will be written to." << endl; |
379 |
cout << endl; |
380 |
cout << " SAMPLENR Index (/indices) of Sample(s) which should be extracted." << endl; |
381 |
cout << " If no sample indices are given, all samples will be extracted" << endl; |
382 |
cout << " (use gigdump to look for available samples)." << endl; |
383 |
cout << endl; |
384 |
cout << " -v Print version and exit." << endl; |
385 |
cout << endl; |
386 |
} |
387 |
|
388 |
string ToString(int i) { |
389 |
static char strbuf[1024]; |
390 |
sprintf(strbuf,"%d",i); |
391 |
string s = strbuf; |
392 |
return s; |
393 |
} |