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-2005 by Christian Schoenebeck * |
* Copyright (C) 2003-2013 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 * |
12 |
* the Free Software Foundation; either version 2 of the License, or * |
* the Free Software Foundation; either version 2 of the License, or * |
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> |
|
#include <dirent.h> |
|
49 |
#include <errno.h> |
#include <errno.h> |
50 |
#include <dlfcn.h> |
|
51 |
|
#include "gig.h" |
52 |
|
|
53 |
|
#ifdef _MSC_VER |
54 |
|
#define S_ISDIR(x) (S_IFDIR & (x)) |
55 |
|
#define S_IWUSR S_IWRITE |
56 |
|
#define S_IXUSR S_IEXEC |
57 |
|
#endif |
58 |
|
|
59 |
|
#if POSIX |
60 |
|
# include <dlfcn.h> |
61 |
|
#endif |
62 |
|
|
63 |
// only libsndfile is available for Windows, so we use that for writing the sound files |
// only libsndfile is available for Windows, so we use that for writing the sound files |
64 |
#ifdef WIN32 |
#ifdef WIN32 |
65 |
# define HAVE_SNDFILE 1 |
# define HAVE_SNDFILE 1 |
66 |
#endif // WIN32 |
#endif // WIN32 |
67 |
|
|
68 |
|
// abort compilation here if neither libsndfile nor libaudiofile are available |
69 |
|
#if !HAVE_SNDFILE && !HAVE_AUDIOFILE |
70 |
|
# error "Neither libsndfile nor libaudiofile seem to be available!" |
71 |
|
# error "(HAVE_SNDFILE and HAVE_AUDIOFILE are both false)" |
72 |
|
#endif |
73 |
|
|
74 |
// we prefer libsndfile before libaudiofile |
// we prefer libsndfile before libaudiofile |
75 |
#if HAVE_SNDFILE |
#if HAVE_SNDFILE |
76 |
# include <sndfile.h> |
# include <sndfile.h> |
78 |
# include <audiofile.h> |
# include <audiofile.h> |
79 |
#endif // HAVE_SNDFILE |
#endif // HAVE_SNDFILE |
80 |
|
|
|
#include "gig.h" |
|
|
|
|
81 |
using namespace std; |
using namespace std; |
82 |
|
|
83 |
typedef map<unsigned int, bool> OrderMap; |
typedef map<unsigned int, bool> OrderMap; |
87 |
void PrintVersion(); |
void PrintVersion(); |
88 |
void PrintUsage(); |
void PrintUsage(); |
89 |
void ExtractSamples(gig::File* gig, char* destdir, OrderMap* ordered); |
void ExtractSamples(gig::File* gig, char* destdir, OrderMap* ordered); |
90 |
int writeWav(const char* filename, void* samples, long samplecount, int channels, int bitdepth, long rate); |
int writeWav(gig::Sample* sample, const char* filename, void* samples, long samplecount, int channels, int bitdepth, long rate); |
91 |
string ToString(int i); |
string ToString(int i); |
92 |
|
|
93 |
#ifndef HAVE_SNDFILE |
#if !HAVE_SNDFILE // use libaudiofile |
94 |
void* hAFlib; // handle to libaudiofile |
void* hAFlib; // handle to libaudiofile |
95 |
void openAFlib(void); |
void openAFlib(void); |
96 |
void closeAFlib(void); |
void closeAFlib(void); |
133 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
134 |
} |
} |
135 |
fclose(hFile); |
fclose(hFile); |
136 |
DIR* dir = opendir(argv[2]); |
struct stat buf; |
137 |
if (!dir) { |
if (stat(argv[2], &buf) == -1) { |
138 |
cout << "Unable to open DESTDIR: "; |
cout << "Unable to open DESTDIR: "; |
139 |
switch (errno) { |
switch (errno) { |
140 |
case EACCES: cout << "Permission denied." << endl; |
case EACCES: cout << "Permission denied." << endl; |
141 |
break; |
break; |
|
case EMFILE: cout << "Too many file descriptors in use by process." << endl; |
|
|
break; |
|
|
case ENFILE: cout << "Too many files are currently open in the system." << endl; |
|
|
break; |
|
142 |
case ENOENT: cout << "Directory does not exist, or name is an empty string." << endl; |
case ENOENT: cout << "Directory does not exist, or name is an empty string." << endl; |
143 |
break; |
break; |
144 |
case ENOMEM: cout << "Insufficient memory to complete the operation." << endl; |
case ENOMEM: cout << "Insufficient memory to complete the operation." << endl; |
148 |
default: cout << "Unknown error" << endl; |
default: cout << "Unknown error" << endl; |
149 |
} |
} |
150 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
151 |
|
} else if (!S_ISDIR(buf.st_mode)) { |
152 |
|
cout << "Unable to open DESTDIR: Is not a directory." << endl; |
153 |
|
return EXIT_FAILURE; |
154 |
|
} else if (!(S_IWUSR & buf.st_mode) || !(S_IXUSR & buf.st_mode)) { |
155 |
|
cout << "Unable to open DESTDIR: Permission denied." << endl; |
156 |
|
return EXIT_FAILURE; |
157 |
} |
} |
|
if (dir) closedir(dir); |
|
158 |
try { |
try { |
159 |
RIFF::File* riff = new RIFF::File(argv[1]); |
RIFF::File* riff = new RIFF::File(argv[1]); |
160 |
gig::File* gig = new gig::File(riff); |
gig::File* gig = new gig::File(riff); |
177 |
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
178 |
} |
} |
179 |
|
|
180 |
|
static std::string getLoopTypeText(gig::loop_type_t type) { |
181 |
|
switch (type) { |
182 |
|
case gig::loop_type_normal: |
183 |
|
return "normal"; |
184 |
|
case gig::loop_type_bidirectional: |
185 |
|
return "pingpong"; |
186 |
|
case gig::loop_type_backward: |
187 |
|
return "backward"; |
188 |
|
default: |
189 |
|
return "INVALID"; |
190 |
|
} |
191 |
|
} |
192 |
|
|
193 |
void ExtractSamples(gig::File* gig, char* destdir, OrderMap* ordered) { |
void ExtractSamples(gig::File* gig, char* destdir, OrderMap* ordered) { |
194 |
#ifndef HAVE_SNDFILE |
#if !HAVE_SNDFILE // use libaudiofile |
195 |
hAFlib = NULL; |
hAFlib = NULL; |
196 |
openAFlib(); |
openAFlib(); |
197 |
#endif // !HAVE_SNDFILE |
#endif // !HAVE_SNDFILE |
198 |
uint8_t* pWave = NULL; |
uint8_t* pWave = NULL; |
199 |
|
int* pIntWave = NULL; |
200 |
long BufferSize = 0; |
long BufferSize = 0; |
201 |
int samples = 0; |
int samples = 0; |
202 |
|
gig::buffer_t decompressionBuffer; |
203 |
|
decompressionBuffer.Size = 0; |
204 |
cout << "Seeking for available samples..." << flush; |
cout << "Seeking for available samples..." << flush; |
205 |
gig::Sample* pSample = gig->GetFirstSample(); |
gig::Sample* pSample = gig->GetFirstSample(); |
206 |
cout << "OK" << endl << flush; |
cout << "OK" << endl << flush; |
229 |
filename += ".wav"; |
filename += ".wav"; |
230 |
if (pSample->Compressed) cout << "Decompressing "; |
if (pSample->Compressed) cout << "Decompressing "; |
231 |
else cout << "Extracting "; |
else cout << "Extracting "; |
232 |
cout << "Sample " << samples << ") " << name << " (" << pSample->BitDepth <<"Bits, " << pSample->SamplesPerSecond << "Hz, " << pSample->Channels << " Channels, " << pSample->SamplesTotal << " Samples)..." << flush; |
cout << "Sample " << samples << ") " << name << " (" << pSample->BitDepth <<"Bits, " << pSample->SamplesPerSecond << "Hz, " << pSample->Channels << " Channels, " << pSample->SamplesTotal << " Samples"; |
233 |
|
if (pSample->Loops > 0) { |
234 |
|
cout << ", LoopType " << getLoopTypeText(pSample->LoopType) |
235 |
|
<< ", LoopStart " << pSample->LoopStart |
236 |
|
<< ", LoopEnd " << pSample->LoopEnd; |
237 |
|
} |
238 |
|
cout << ")..." << flush; |
239 |
|
|
240 |
|
|
241 |
#if USE_DISK_STREAMING |
#if USE_DISK_STREAMING |
242 |
long neededsize = (pSample->Compressed) ? 10485760 /* 10 MB buffer */ |
long neededsize = pSample->BitDepth == 24 ? |
243 |
: pSample->SamplesTotal * pSample->FrameSize; |
pSample->SamplesTotal * pSample->Channels * sizeof(int) : |
244 |
|
pSample->SamplesTotal * pSample->FrameSize; |
245 |
if (BufferSize < neededsize) { |
if (BufferSize < neededsize) { |
246 |
if (pWave) delete[] pWave; |
if (pWave) delete[] pWave; |
247 |
pWave = new uint8_t[neededsize]; |
pWave = new uint8_t[neededsize]; |
248 |
BufferSize = neededsize; |
BufferSize = neededsize; |
249 |
} |
} |
250 |
|
pIntWave = (int*)pWave; |
251 |
# if HASHED_READS_TEST |
# if HASHED_READS_TEST |
252 |
unsigned long readsamples = 0, |
unsigned long readinthisrun = 0, |
|
readinthisrun = 0, |
|
253 |
samplepiecesize = 2000; |
samplepiecesize = 2000; |
254 |
uint8_t* pSamplePiece = pWave; |
uint8_t* pSamplePiece = pWave; |
255 |
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 |
256 |
readinthisrun = pSample->Read(pSamplePiece, ++samplepiecesize); |
readinthisrun = pSample->Read(pSamplePiece, ++samplepiecesize); |
257 |
// 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; |
|
258 |
} while (readinthisrun == samplepiecesize); |
} while (readinthisrun == samplepiecesize); |
259 |
|
|
|
if (pSample->Compressed) { // hack |
|
|
pSample->SamplesTotal = readsamples; |
|
|
pSample->BitDepth = 16; |
|
|
} |
|
260 |
# else // read in one piece |
# else // read in one piece |
261 |
if (pSample->Compressed) { |
if (pSample->Compressed) { |
262 |
pSample->SamplesTotal = pSample->Read(pWave, 10485760 >> 2); // assumes 16 bit stereo |
if (decompressionBufferSize < pSample->SamplesTotal) { |
263 |
} |
gig::Sample::DestroyDecompressionBuffer(decompressionBuffer); |
264 |
else { |
decompressionBuffer = gig::Sample::CreateDecompressionBuffer(pSample->SamplesTotal); |
265 |
|
decompressionBufferSize = pSample->SamplesTotal; |
266 |
|
} |
267 |
|
pSample->Read(pWave, pSample->SamplesTotal, &decompressionBuffer); |
268 |
|
} else { |
269 |
pSample->Read(pWave, pSample->SamplesTotal); |
pSample->Read(pWave, pSample->SamplesTotal); |
270 |
} |
} |
271 |
# endif // HASHED_READS_TEST |
# endif // HASHED_READS_TEST |
272 |
#else // no disk streaming |
#else // no disk streaming |
273 |
if (pSample->Compressed) { |
if (pSample->Compressed) { |
274 |
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; |
275 |
|
} else { |
276 |
|
gig::buffer_t buffer = pSample->LoadSampleData(); // load wave into RAM |
277 |
|
pWave = static_cast<uint8_t*>(buffer.pStart); |
278 |
|
if (pSample->BitDepth == 24) { |
279 |
|
long neededsize = pSample->SamplesTotal * pSample->Channels; |
280 |
|
if (BufferSize < neededsize) { |
281 |
|
if (pIntWave) delete[] pIntWave; |
282 |
|
pIntWave = new int[neededsize]; |
283 |
|
BufferSize = neededsize; |
284 |
|
} |
285 |
|
} |
286 |
} |
} |
|
else pWave = (uint8_t*) pSample->LoadSampleData(); // load wave into RAM |
|
287 |
#endif // USE_DISK_STREAMING |
#endif // USE_DISK_STREAMING |
288 |
if (pWave) { |
if (pWave) { |
289 |
int res = writeWav(filename.c_str(), |
|
290 |
pWave, |
// Both libsndfile and libaudiofile uses int for 24 bit |
291 |
|
// samples. libgig however returns 3 bytes per sample, so |
292 |
|
// we have to convert the wave data before writing. |
293 |
|
if (pSample->BitDepth == 24) { |
294 |
|
int n = pSample->SamplesTotal * pSample->Channels; |
295 |
|
for (int i = n - 1 ; i >= 0 ; i--) { |
296 |
|
#if HAVE_SNDFILE |
297 |
|
pIntWave[i] = pWave[i * 3] << 8 | pWave[i * 3 + 1] << 16 | pWave[i * 3 + 2] << 24; |
298 |
|
#else |
299 |
|
pIntWave[i] = pWave[i * 3] | pWave[i * 3 + 1] << 8 | pWave[i * 3 + 2] << 16; |
300 |
|
#endif |
301 |
|
} |
302 |
|
} |
303 |
|
|
304 |
|
int res = writeWav(pSample, |
305 |
|
filename.c_str(), |
306 |
|
pSample->BitDepth == 24 ? static_cast<void*>(pIntWave) : pWave, |
307 |
pSample->SamplesTotal, |
pSample->SamplesTotal, |
308 |
pSample->Channels, |
pSample->Channels, |
309 |
pSample->BitDepth, |
pSample->BitDepth, |
316 |
|
|
317 |
pSample = gig->GetNextSample(); |
pSample = gig->GetNextSample(); |
318 |
} |
} |
319 |
if (pWave) delete[] (uint8_t*) pWave; |
gig::Sample::DestroyDecompressionBuffer(decompressionBuffer); |
320 |
#ifndef HAVE_SNDFILE |
#if USE_DISK_STREAMING |
321 |
|
if (pWave) delete[] pWave; |
322 |
|
#else |
323 |
|
if (pIntWave) delete[] pIntWave; |
324 |
|
#endif |
325 |
|
#if !HAVE_SNDFILE // use libaudiofile |
326 |
closeAFlib(); |
closeAFlib(); |
327 |
#endif // !HAVE_SNDFILE |
#endif // !HAVE_SNDFILE |
328 |
} |
} |
329 |
|
|
330 |
int writeWav(const char* filename, void* samples, long samplecount, int channels, int bitdepth, long rate) { |
int writeWav(gig::Sample* sample, const char* filename, void* samples, long samplecount, int channels, int bitdepth, long rate) { |
331 |
#if HAVE_SNDFILE |
#if HAVE_SNDFILE |
332 |
SNDFILE* hfile; |
SNDFILE* hfile; |
333 |
SF_INFO sfinfo; |
SF_INFO sfinfo; |
334 |
|
SF_INSTRUMENT instr; |
335 |
int format = SF_FORMAT_WAV; |
int format = SF_FORMAT_WAV; |
336 |
switch (bitdepth) { |
switch (bitdepth) { |
337 |
case 8: |
case 8: |
338 |
format |= SF_FORMAT_PCM_S8; |
format |= SF_FORMAT_PCM_S8; |
|
cout << "8 bit" << endl << flush; |
|
339 |
break; |
break; |
340 |
case 16: |
case 16: |
341 |
format |= SF_FORMAT_PCM_16; |
format |= SF_FORMAT_PCM_16; |
|
cout << "16 bit" << endl << flush; |
|
342 |
break; |
break; |
343 |
case 24: |
case 24: |
344 |
format |= SF_FORMAT_PCM_24; |
format |= SF_FORMAT_PCM_24; |
|
cout << "24 bit" << endl << flush; |
|
345 |
break; |
break; |
346 |
case 32: |
case 32: |
347 |
format |= SF_FORMAT_PCM_32; |
format |= SF_FORMAT_PCM_32; |
|
cout << "32 bit" << endl << flush; |
|
348 |
break; |
break; |
349 |
default: |
default: |
350 |
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; |
351 |
return -1; |
return -1; |
352 |
} |
} |
353 |
memset(&sfinfo, 0, sizeof (sfinfo)); |
memset(&sfinfo, 0, sizeof (sfinfo)); |
354 |
|
memset(&instr, 0, sizeof (instr)); |
355 |
sfinfo.samplerate = rate; |
sfinfo.samplerate = rate; |
356 |
sfinfo.frames = samplecount; |
sfinfo.frames = samplecount; |
357 |
sfinfo.channels = channels; |
sfinfo.channels = channels; |
360 |
cerr << "Error: Unable to open output file \'" << filename << "\'.\n" << flush; |
cerr << "Error: Unable to open output file \'" << filename << "\'.\n" << flush; |
361 |
return -1; |
return -1; |
362 |
} |
} |
363 |
if (sf_write_short(hfile, (short*)samples, channels * samplecount) != channels * samplecount) { |
instr.basenote = sample->MIDIUnityNote; |
364 |
|
instr.detune = sample->FineTune; |
365 |
|
if (sample->Loops > 0) { |
366 |
|
instr.loop_count = 1; |
367 |
|
switch (sample->LoopType) { |
368 |
|
case gig::loop_type_normal: |
369 |
|
instr.loops[0].mode = SF_LOOP_FORWARD; |
370 |
|
break; |
371 |
|
case gig::loop_type_bidirectional: |
372 |
|
instr.loops[0].mode = SF_LOOP_ALTERNATING; |
373 |
|
break; |
374 |
|
case gig::loop_type_backward: |
375 |
|
instr.loops[0].mode = SF_LOOP_BACKWARD; |
376 |
|
break; |
377 |
|
default: |
378 |
|
instr.loops[0].mode = SF_LOOP_NONE; |
379 |
|
break; |
380 |
|
} |
381 |
|
instr.loops[0].start = sample->LoopStart; |
382 |
|
instr.loops[0].end = sample->LoopEnd; |
383 |
|
instr.loops[0].count = sample->LoopPlayCount; |
384 |
|
} |
385 |
|
sf_command(hfile, SFC_SET_INSTRUMENT, &instr, sizeof(instr)); |
386 |
|
sf_count_t res = bitdepth == 24 ? |
387 |
|
sf_write_int(hfile, static_cast<int*>(samples), channels * samplecount) : |
388 |
|
sf_write_short(hfile, static_cast<short*>(samples), channels * samplecount); |
389 |
|
if (res != channels * samplecount) { |
390 |
cerr << sf_strerror(hfile) << endl << flush; |
cerr << sf_strerror(hfile) << endl << flush; |
391 |
sf_close(hfile); |
sf_close(hfile); |
392 |
return -1; |
return -1; |
393 |
} |
} |
394 |
sf_close(hfile); |
sf_close(hfile); |
395 |
#else |
#else // use libaudiofile |
396 |
AFfilesetup setup = _afNewFileSetup(); |
AFfilesetup setup = _afNewFileSetup(); |
397 |
_afInitFileFormat(setup, AF_FILE_WAVE); |
_afInitFileFormat(setup, AF_FILE_WAVE); |
398 |
_afInitChannels(setup, AF_DEFAULT_TRACK, channels); |
_afInitChannels(setup, AF_DEFAULT_TRACK, channels); |
409 |
return 0; // success |
return 0; // success |
410 |
} |
} |
411 |
|
|
412 |
#ifndef HAVE_SNDFILE |
#if !HAVE_SNDFILE // use libaudiofile |
413 |
void openAFlib() { |
void openAFlib() { |
414 |
hAFlib = dlopen("libaudiofile.so", RTLD_NOW); |
hAFlib = dlopen("libaudiofile.so", RTLD_NOW); |
415 |
if (!hAFlib) { |
if (!hAFlib) { |
434 |
#endif // !HAVE_SNDFILE |
#endif // !HAVE_SNDFILE |
435 |
|
|
436 |
string Revision() { |
string Revision() { |
437 |
string s = "$Revision: 1.6 $"; |
string s = "$Revision$"; |
438 |
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 |
439 |
} |
} |
440 |
|
|
445 |
char versionBuffer[128]; |
char versionBuffer[128]; |
446 |
sf_command(NULL, SFC_GET_LIB_VERSION, versionBuffer, 128); |
sf_command(NULL, SFC_GET_LIB_VERSION, versionBuffer, 128); |
447 |
cout << ", " << versionBuffer; |
cout << ", " << versionBuffer; |
448 |
#endif // HAVE_SNDFILE |
#else // use libaudiofile |
449 |
cout << endl; |
cout << "\nbuilt against libaudiofile " |
450 |
#if !HAVE_SNDFILE |
<< LIBAUDIOFILE_MAJOR_VERSION << "." << LIBAUDIOFILE_MINOR_VERSION; |
|
cout << "built against libaudiofile " |
|
|
<< LIBAUDIOFILE_MAJOR_VERSION << "." << LIBAUDIOFILE_MINOR_VERSION |
|
451 |
# ifdef LIBAUDIOFILE_MICRO_VERSION |
# ifdef LIBAUDIOFILE_MICRO_VERSION |
452 |
<< "." << LIBAUDIOFILE_MICRO_VERSION |
cout << "." << LIBAUDIOFILE_MICRO_VERSION; |
453 |
# endif // LIBAUDIOFILE_MICRO_VERSION |
# endif // LIBAUDIOFILE_MICRO_VERSION |
454 |
<< endl; |
#endif // HAVE_SNDFILE |
455 |
#endif // !HAVE_SNDFILE |
cout << endl; |
456 |
} |
} |
457 |
|
|
458 |
void PrintUsage() { |
void PrintUsage() { |