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 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 * |
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 |
|
// we prefer libsndfile before libaudiofile |
57 |
|
#if HAVE_SNDFILE |
58 |
|
# include <sndfile.h> |
59 |
|
#else |
60 |
|
# include <audiofile.h> |
61 |
|
#endif // HAVE_SNDFILE |
62 |
|
|
63 |
#include "gig.h" |
#include "gig.h" |
64 |
|
|
65 |
using namespace std; |
using namespace std; |
67 |
typedef map<unsigned int, bool> OrderMap; |
typedef map<unsigned int, bool> OrderMap; |
68 |
OrderMap* pOrderedSamples = NULL; |
OrderMap* pOrderedSamples = NULL; |
69 |
|
|
70 |
|
string Revision(); |
71 |
|
void PrintVersion(); |
72 |
void PrintUsage(); |
void PrintUsage(); |
73 |
void ExtractSamples(gig::File* gig, char* destdir, OrderMap* ordered); |
void ExtractSamples(gig::File* gig, char* destdir, OrderMap* ordered); |
74 |
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); |
75 |
|
string ToString(int i); |
76 |
|
|
77 |
|
#ifndef HAVE_SNDFILE |
78 |
void* hAFlib; // handle to libaudiofile |
void* hAFlib; // handle to libaudiofile |
79 |
void openAFlib(void); |
void openAFlib(void); |
80 |
void closeAFlib(void); |
void closeAFlib(void); |
88 |
int(*_afWriteFrames)(AFfilehandle,int,const void*,int); |
int(*_afWriteFrames)(AFfilehandle,int,const void*,int); |
89 |
AFfilehandle(*_afOpenFile)(const char*,const char*,AFfilesetup); |
AFfilehandle(*_afOpenFile)(const char*,const char*,AFfilesetup); |
90 |
int(*_afCloseFile)(AFfilehandle file); |
int(*_afCloseFile)(AFfilehandle file); |
91 |
string ToString(int i); |
#endif // !HAVE_SNDFILE |
92 |
|
|
93 |
int main(int argc, char *argv[]) { |
int main(int argc, char *argv[]) { |
94 |
|
if (argc >= 2) { |
95 |
|
if (argv[1][0] == '-') { |
96 |
|
switch (argv[1][1]) { |
97 |
|
case 'v': |
98 |
|
PrintVersion(); |
99 |
|
return EXIT_SUCCESS; |
100 |
|
} |
101 |
|
} |
102 |
|
} |
103 |
if (argc < 3) { |
if (argc < 3) { |
104 |
PrintUsage(); |
PrintUsage(); |
105 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
161 |
} |
} |
162 |
|
|
163 |
void ExtractSamples(gig::File* gig, char* destdir, OrderMap* ordered) { |
void ExtractSamples(gig::File* gig, char* destdir, OrderMap* ordered) { |
164 |
|
#ifndef HAVE_SNDFILE |
165 |
hAFlib = NULL; |
hAFlib = NULL; |
166 |
openAFlib(); |
openAFlib(); |
167 |
|
#endif // !HAVE_SNDFILE |
168 |
uint8_t* pWave = NULL; |
uint8_t* pWave = NULL; |
169 |
long BufferSize = 0; |
long BufferSize = 0; |
170 |
int samples = 0; |
int samples = 0; |
200 |
|
|
201 |
|
|
202 |
#if USE_DISK_STREAMING |
#if USE_DISK_STREAMING |
|
if (pSample->Compressed) { // hack |
|
|
pSample->BitDepth = 16; |
|
|
pSample->FrameSize = 4; |
|
|
} |
|
|
|
|
203 |
long neededsize = (pSample->Compressed) ? 10485760 /* 10 MB buffer */ |
long neededsize = (pSample->Compressed) ? 10485760 /* 10 MB buffer */ |
204 |
: pSample->SamplesTotal * pSample->FrameSize; |
: pSample->SamplesTotal * pSample->FrameSize; |
205 |
if (BufferSize < neededsize) { |
if (BufferSize < neededsize) { |
214 |
uint8_t* pSamplePiece = pWave; |
uint8_t* pSamplePiece = pWave; |
215 |
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 |
216 |
readinthisrun = pSample->Read(pSamplePiece, ++samplepiecesize); |
readinthisrun = pSample->Read(pSamplePiece, ++samplepiecesize); |
217 |
pSamplePiece += readinthisrun * pSample->FrameSize; |
// 24 bit is truncated to 16 by Sample::Read at the moment |
218 |
|
pSamplePiece += readinthisrun * (2 * pSample->Channels); // readinthisrun * pSample->FrameSize; |
219 |
readsamples += readinthisrun; |
readsamples += readinthisrun; |
220 |
} while (readinthisrun == samplepiecesize); |
} while (readinthisrun == samplepiecesize); |
221 |
|
|
222 |
if (pSample->Compressed) { // hack |
if (pSample->Compressed) { // hack |
223 |
pSample->SamplesTotal = readsamples; |
pSample->SamplesTotal = readsamples; |
224 |
|
pSample->BitDepth = 16; |
225 |
} |
} |
226 |
# else // read in one piece |
# else // read in one piece |
227 |
if (pSample->Compressed) { |
if (pSample->Compressed) { |
253 |
pSample = gig->GetNextSample(); |
pSample = gig->GetNextSample(); |
254 |
} |
} |
255 |
if (pWave) delete[] (uint8_t*) pWave; |
if (pWave) delete[] (uint8_t*) pWave; |
256 |
|
#ifndef HAVE_SNDFILE |
257 |
closeAFlib(); |
closeAFlib(); |
258 |
|
#endif // !HAVE_SNDFILE |
259 |
} |
} |
260 |
|
|
261 |
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) { |
262 |
|
#if HAVE_SNDFILE |
263 |
|
SNDFILE* hfile; |
264 |
|
SF_INFO sfinfo; |
265 |
|
int format = SF_FORMAT_WAV; |
266 |
|
switch (bitdepth) { |
267 |
|
case 8: |
268 |
|
format |= SF_FORMAT_PCM_S8; |
269 |
|
cout << "8 bit" << endl << flush; |
270 |
|
break; |
271 |
|
case 16: |
272 |
|
format |= SF_FORMAT_PCM_16; |
273 |
|
cout << "16 bit" << endl << flush; |
274 |
|
break; |
275 |
|
case 24: |
276 |
|
format |= SF_FORMAT_PCM_24; |
277 |
|
cout << "24 bit" << endl << flush; |
278 |
|
break; |
279 |
|
case 32: |
280 |
|
format |= SF_FORMAT_PCM_32; |
281 |
|
cout << "32 bit" << endl << flush; |
282 |
|
break; |
283 |
|
default: |
284 |
|
cerr << "Error: Bithdepth " << ToString(bitdepth) << " not supported by libsndfile, ignoring sample!\n" << flush; |
285 |
|
return -1; |
286 |
|
} |
287 |
|
memset(&sfinfo, 0, sizeof (sfinfo)); |
288 |
|
sfinfo.samplerate = rate; |
289 |
|
sfinfo.frames = samplecount; |
290 |
|
sfinfo.channels = channels; |
291 |
|
sfinfo.format = format; |
292 |
|
if (!(hfile = sf_open(filename, SFM_WRITE, &sfinfo))) { |
293 |
|
cerr << "Error: Unable to open output file \'" << filename << "\'.\n" << flush; |
294 |
|
return -1; |
295 |
|
} |
296 |
|
if (sf_write_short(hfile, (short*)samples, channels * samplecount) != channels * samplecount) { |
297 |
|
cerr << sf_strerror(hfile) << endl << flush; |
298 |
|
sf_close(hfile); |
299 |
|
return -1; |
300 |
|
} |
301 |
|
sf_close(hfile); |
302 |
|
#else |
303 |
AFfilesetup setup = _afNewFileSetup(); |
AFfilesetup setup = _afNewFileSetup(); |
304 |
_afInitFileFormat(setup, AF_FILE_WAVE); |
_afInitFileFormat(setup, AF_FILE_WAVE); |
305 |
_afInitChannels(setup, AF_DEFAULT_TRACK, channels); |
_afInitChannels(setup, AF_DEFAULT_TRACK, channels); |
311 |
if (_afWriteFrames(hFile, AF_DEFAULT_TRACK, samples, samplecount) < 0) return -1; |
if (_afWriteFrames(hFile, AF_DEFAULT_TRACK, samples, samplecount) < 0) return -1; |
312 |
_afCloseFile(hFile); |
_afCloseFile(hFile); |
313 |
_afFreeFileSetup(setup); |
_afFreeFileSetup(setup); |
314 |
|
#endif // HAVE_SNDFILE |
315 |
return 0; |
|
316 |
|
return 0; // success |
317 |
} |
} |
318 |
|
|
319 |
|
#ifndef HAVE_SNDFILE |
320 |
void openAFlib() { |
void openAFlib() { |
321 |
hAFlib = dlopen("libaudiofile.so", RTLD_NOW); |
hAFlib = dlopen("libaudiofile.so", RTLD_NOW); |
322 |
if (!hAFlib) { |
if (!hAFlib) { |
338 |
void closeAFlib() { |
void closeAFlib() { |
339 |
if (hAFlib) dlclose(hAFlib); |
if (hAFlib) dlclose(hAFlib); |
340 |
} |
} |
341 |
|
#endif // !HAVE_SNDFILE |
342 |
|
|
343 |
|
string Revision() { |
344 |
|
string s = "$Revision: 1.5 $"; |
345 |
|
return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword |
346 |
|
} |
347 |
|
|
348 |
|
void PrintVersion() { |
349 |
|
cout << "gigextract revision " << Revision() << endl; |
350 |
|
cout << "using " << gig::libraryName() << " " << gig::libraryVersion() << endl; |
351 |
|
} |
352 |
|
|
353 |
void PrintUsage() { |
void PrintUsage() { |
354 |
cout << "gigextract - extracts samples from a Gigasampler file." << endl; |
cout << "gigextract - extracts samples from a Gigasampler file." << endl; |
355 |
cout << endl; |
cout << endl; |
356 |
cout << "Usage: gigextract GIGFILE DESTDIR [SAMPLENR] [ [SAMPLENR] ...]" << endl; |
cout << "Usage: gigextract [-v] GIGFILE DESTDIR [SAMPLENR] [ [SAMPLENR] ...]" << endl; |
357 |
cout << endl; |
cout << endl; |
358 |
cout << " GIGFILE Input Gigasampler (.gig) file." << endl; |
cout << " GIGFILE Input Gigasampler (.gig) file." << endl; |
359 |
cout << endl; |
cout << endl; |
363 |
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; |
364 |
cout << " (use gigdump to look for available samples)." << endl; |
cout << " (use gigdump to look for available samples)." << endl; |
365 |
cout << endl; |
cout << endl; |
366 |
|
cout << " -v Print version and exit." << endl; |
367 |
|
cout << endl; |
368 |
} |
} |
369 |
|
|
370 |
string ToString(int i) { |
string ToString(int i) { |