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; |
244 |
pSample = gig->GetNextSample(); |
pSample = gig->GetNextSample(); |
245 |
} |
} |
246 |
if (pWave) delete[] (uint8_t*) pWave; |
if (pWave) delete[] (uint8_t*) pWave; |
247 |
|
#ifndef HAVE_SNDFILE |
248 |
closeAFlib(); |
closeAFlib(); |
249 |
|
#endif // !HAVE_SNDFILE |
250 |
} |
} |
251 |
|
|
252 |
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) { |
253 |
|
#if HAVE_SNDFILE |
254 |
|
SNDFILE* hfile; |
255 |
|
SF_INFO sfinfo; |
256 |
|
int format = SF_FORMAT_WAV; |
257 |
|
switch (bitdepth) { |
258 |
|
case 8: |
259 |
|
format |= SF_FORMAT_PCM_S8; |
260 |
|
cout << "8 bit" << endl << flush; |
261 |
|
break; |
262 |
|
case 16: |
263 |
|
format |= SF_FORMAT_PCM_16; |
264 |
|
cout << "16 bit" << endl << flush; |
265 |
|
break; |
266 |
|
case 24: |
267 |
|
format |= SF_FORMAT_PCM_24; |
268 |
|
cout << "24 bit" << endl << flush; |
269 |
|
break; |
270 |
|
case 32: |
271 |
|
format |= SF_FORMAT_PCM_32; |
272 |
|
cout << "32 bit" << endl << flush; |
273 |
|
break; |
274 |
|
default: |
275 |
|
cerr << "Error: Bithdepth " << ToString(bitdepth) << " not supported by libsndfile, ignoring sample!\n" << flush; |
276 |
|
return -1; |
277 |
|
} |
278 |
|
memset(&sfinfo, 0, sizeof (sfinfo)); |
279 |
|
sfinfo.samplerate = rate; |
280 |
|
sfinfo.frames = samplecount; |
281 |
|
sfinfo.channels = channels; |
282 |
|
sfinfo.format = format; |
283 |
|
if (!(hfile = sf_open(filename, SFM_WRITE, &sfinfo))) { |
284 |
|
cerr << "Error: Unable to open output file \'" << filename << "\'.\n" << flush; |
285 |
|
return -1; |
286 |
|
} |
287 |
|
if (sf_write_short(hfile, (short*)samples, channels * samplecount) != channels * samplecount) { |
288 |
|
cerr << sf_strerror(hfile) << endl << flush; |
289 |
|
sf_close(hfile); |
290 |
|
return -1; |
291 |
|
} |
292 |
|
sf_close(hfile); |
293 |
|
#else |
294 |
AFfilesetup setup = _afNewFileSetup(); |
AFfilesetup setup = _afNewFileSetup(); |
295 |
_afInitFileFormat(setup, AF_FILE_WAVE); |
_afInitFileFormat(setup, AF_FILE_WAVE); |
296 |
_afInitChannels(setup, AF_DEFAULT_TRACK, channels); |
_afInitChannels(setup, AF_DEFAULT_TRACK, channels); |
302 |
if (_afWriteFrames(hFile, AF_DEFAULT_TRACK, samples, samplecount) < 0) return -1; |
if (_afWriteFrames(hFile, AF_DEFAULT_TRACK, samples, samplecount) < 0) return -1; |
303 |
_afCloseFile(hFile); |
_afCloseFile(hFile); |
304 |
_afFreeFileSetup(setup); |
_afFreeFileSetup(setup); |
305 |
|
#endif // HAVE_SNDFILE |
306 |
|
|
307 |
return 0; |
return 0; // success |
308 |
} |
} |
309 |
|
|
310 |
|
#ifndef HAVE_SNDFILE |
311 |
void openAFlib() { |
void openAFlib() { |
312 |
hAFlib = dlopen("libaudiofile.so", RTLD_NOW); |
hAFlib = dlopen("libaudiofile.so", RTLD_NOW); |
313 |
if (!hAFlib) { |
if (!hAFlib) { |
329 |
void closeAFlib() { |
void closeAFlib() { |
330 |
if (hAFlib) dlclose(hAFlib); |
if (hAFlib) dlclose(hAFlib); |
331 |
} |
} |
332 |
|
#endif // !HAVE_SNDFILE |
333 |
|
|
334 |
void PrintUsage() { |
void PrintUsage() { |
335 |
cout << "gigextract - extracts samples from a Gigasampler file." << endl; |
cout << "gigextract - extracts samples from a Gigasampler file." << endl; |