39 |
|
|
40 |
#include <iostream> |
#include <iostream> |
41 |
#include <cstdlib> |
#include <cstdlib> |
42 |
|
#include <string.h> |
43 |
#include <stdlib.h> |
#include <stdlib.h> |
44 |
#include <sys/types.h> |
#include <sys/types.h> |
45 |
#include <sys/stat.h> |
#include <sys/stat.h> |
46 |
#include <dirent.h> |
#include <dirent.h> |
47 |
#include <errno.h> |
#include <errno.h> |
48 |
#include <dlfcn.h> |
#include <dlfcn.h> |
49 |
#include <audiofile.h> |
|
50 |
|
// only libsndfile is available for Windows, so we use that for writing the sound files |
51 |
|
#ifdef WIN32 |
52 |
|
# define HAVE_SNDFILE 1 |
53 |
|
#endif // WIN32 |
54 |
|
|
55 |
|
// we prefer libsndfile before libaudiofile |
56 |
|
#if HAVE_SNDFILE |
57 |
|
# include <sndfile.h> |
58 |
|
#else |
59 |
|
# include <audiofile.h> |
60 |
|
#endif // HAVE_SNDFILE |
61 |
|
|
62 |
#include "gig.h" |
#include "gig.h" |
63 |
|
|
64 |
using namespace std; |
using namespace std; |
69 |
void PrintUsage(); |
void PrintUsage(); |
70 |
void ExtractSamples(gig::File* gig, char* destdir, OrderMap* ordered); |
void ExtractSamples(gig::File* gig, char* destdir, OrderMap* ordered); |
71 |
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); |
72 |
|
string ToString(int i); |
73 |
|
|
74 |
|
#ifndef HAVE_SNDFILE |
75 |
void* hAFlib; // handle to libaudiofile |
void* hAFlib; // handle to libaudiofile |
76 |
void openAFlib(void); |
void openAFlib(void); |
77 |
void closeAFlib(void); |
void closeAFlib(void); |
85 |
int(*_afWriteFrames)(AFfilehandle,int,const void*,int); |
int(*_afWriteFrames)(AFfilehandle,int,const void*,int); |
86 |
AFfilehandle(*_afOpenFile)(const char*,const char*,AFfilesetup); |
AFfilehandle(*_afOpenFile)(const char*,const char*,AFfilesetup); |
87 |
int(*_afCloseFile)(AFfilehandle file); |
int(*_afCloseFile)(AFfilehandle file); |
88 |
string ToString(int i); |
#endif // !HAVE_SNDFILE |
89 |
|
|
90 |
int main(int argc, char *argv[]) { |
int main(int argc, char *argv[]) { |
91 |
if (argc < 3) { |
if (argc < 3) { |
149 |
} |
} |
150 |
|
|
151 |
void ExtractSamples(gig::File* gig, char* destdir, OrderMap* ordered) { |
void ExtractSamples(gig::File* gig, char* destdir, OrderMap* ordered) { |
152 |
|
#ifndef HAVE_SNDFILE |
153 |
hAFlib = NULL; |
hAFlib = NULL; |
154 |
openAFlib(); |
openAFlib(); |
155 |
|
#endif // !HAVE_SNDFILE |
156 |
uint8_t* pWave = NULL; |
uint8_t* pWave = NULL; |
157 |
long BufferSize = 0; |
long BufferSize = 0; |
158 |
int samples = 0; |
int samples = 0; |
188 |
|
|
189 |
|
|
190 |
#if USE_DISK_STREAMING |
#if USE_DISK_STREAMING |
|
if (pSample->Compressed) { // hack |
|
|
pSample->BitDepth = 16; |
|
|
pSample->FrameSize = 4; |
|
|
} |
|
|
|
|
191 |
long neededsize = (pSample->Compressed) ? 10485760 /* 10 MB buffer */ |
long neededsize = (pSample->Compressed) ? 10485760 /* 10 MB buffer */ |
192 |
: pSample->SamplesTotal * pSample->FrameSize; |
: pSample->SamplesTotal * pSample->FrameSize; |
193 |
if (BufferSize < neededsize) { |
if (BufferSize < neededsize) { |
202 |
uint8_t* pSamplePiece = pWave; |
uint8_t* pSamplePiece = pWave; |
203 |
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 |
204 |
readinthisrun = pSample->Read(pSamplePiece, ++samplepiecesize); |
readinthisrun = pSample->Read(pSamplePiece, ++samplepiecesize); |
205 |
pSamplePiece += readinthisrun * pSample->FrameSize; |
// 24 bit is truncated to 16 by Sample::Read at the moment |
206 |
|
pSamplePiece += readinthisrun * (2 * pSample->Channels); // readinthisrun * pSample->FrameSize; |
207 |
readsamples += readinthisrun; |
readsamples += readinthisrun; |
208 |
} while (readinthisrun == samplepiecesize); |
} while (readinthisrun == samplepiecesize); |
209 |
|
|
210 |
if (pSample->Compressed) { // hack |
if (pSample->Compressed) { // hack |
211 |
pSample->SamplesTotal = readsamples; |
pSample->SamplesTotal = readsamples; |
212 |
|
pSample->BitDepth = 16; |
213 |
} |
} |
214 |
# else // read in one piece |
# else // read in one piece |
215 |
if (pSample->Compressed) { |
if (pSample->Compressed) { |
241 |
pSample = gig->GetNextSample(); |
pSample = gig->GetNextSample(); |
242 |
} |
} |
243 |
if (pWave) delete[] (uint8_t*) pWave; |
if (pWave) delete[] (uint8_t*) pWave; |
244 |
|
#ifndef HAVE_SNDFILE |
245 |
closeAFlib(); |
closeAFlib(); |
246 |
|
#endif // !HAVE_SNDFILE |
247 |
} |
} |
248 |
|
|
249 |
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) { |
250 |
|
#if HAVE_SNDFILE |
251 |
|
SNDFILE* hfile; |
252 |
|
SF_INFO sfinfo; |
253 |
|
int format = SF_FORMAT_WAV; |
254 |
|
switch (bitdepth) { |
255 |
|
case 8: |
256 |
|
format |= SF_FORMAT_PCM_S8; |
257 |
|
cout << "8 bit" << endl << flush; |
258 |
|
break; |
259 |
|
case 16: |
260 |
|
format |= SF_FORMAT_PCM_16; |
261 |
|
cout << "16 bit" << endl << flush; |
262 |
|
break; |
263 |
|
case 24: |
264 |
|
format |= SF_FORMAT_PCM_24; |
265 |
|
cout << "24 bit" << endl << flush; |
266 |
|
break; |
267 |
|
case 32: |
268 |
|
format |= SF_FORMAT_PCM_32; |
269 |
|
cout << "32 bit" << endl << flush; |
270 |
|
break; |
271 |
|
default: |
272 |
|
cerr << "Error: Bithdepth " << ToString(bitdepth) << " not supported by libsndfile, ignoring sample!\n" << flush; |
273 |
|
return -1; |
274 |
|
} |
275 |
|
memset(&sfinfo, 0, sizeof (sfinfo)); |
276 |
|
sfinfo.samplerate = rate; |
277 |
|
sfinfo.frames = samplecount; |
278 |
|
sfinfo.channels = channels; |
279 |
|
sfinfo.format = format; |
280 |
|
if (!(hfile = sf_open(filename, SFM_WRITE, &sfinfo))) { |
281 |
|
cerr << "Error: Unable to open output file \'" << filename << "\'.\n" << flush; |
282 |
|
return -1; |
283 |
|
} |
284 |
|
if (sf_write_short(hfile, (short*)samples, channels * samplecount) != channels * samplecount) { |
285 |
|
cerr << sf_strerror(hfile) << endl << flush; |
286 |
|
sf_close(hfile); |
287 |
|
return -1; |
288 |
|
} |
289 |
|
sf_close(hfile); |
290 |
|
#else |
291 |
AFfilesetup setup = _afNewFileSetup(); |
AFfilesetup setup = _afNewFileSetup(); |
292 |
_afInitFileFormat(setup, AF_FILE_WAVE); |
_afInitFileFormat(setup, AF_FILE_WAVE); |
293 |
_afInitChannels(setup, AF_DEFAULT_TRACK, channels); |
_afInitChannels(setup, AF_DEFAULT_TRACK, channels); |
299 |
if (_afWriteFrames(hFile, AF_DEFAULT_TRACK, samples, samplecount) < 0) return -1; |
if (_afWriteFrames(hFile, AF_DEFAULT_TRACK, samples, samplecount) < 0) return -1; |
300 |
_afCloseFile(hFile); |
_afCloseFile(hFile); |
301 |
_afFreeFileSetup(setup); |
_afFreeFileSetup(setup); |
302 |
|
#endif // HAVE_SNDFILE |
303 |
|
|
304 |
return 0; |
return 0; // success |
305 |
} |
} |
306 |
|
|
307 |
|
#ifndef HAVE_SNDFILE |
308 |
void openAFlib() { |
void openAFlib() { |
309 |
hAFlib = dlopen("libaudiofile.so", RTLD_NOW); |
hAFlib = dlopen("libaudiofile.so", RTLD_NOW); |
310 |
if (!hAFlib) { |
if (!hAFlib) { |
326 |
void closeAFlib() { |
void closeAFlib() { |
327 |
if (hAFlib) dlclose(hAFlib); |
if (hAFlib) dlclose(hAFlib); |
328 |
} |
} |
329 |
|
#endif // !HAVE_SNDFILE |
330 |
|
|
331 |
void PrintUsage() { |
void PrintUsage() { |
332 |
cout << "gigextract - extracts samples from a Gigasampler file." << endl; |
cout << "gigextract - extracts samples from a Gigasampler file." << endl; |