00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifdef HAVE_CONFIG_H
00025 #include <config.h>
00026 #endif
00027
00028
00029
00030
00031
00032 #define USE_DISK_STREAMING 1
00033
00034
00035
00036
00037
00038 #define HASHED_READS_TEST 1
00039
00040 #include <iostream>
00041 #include <cstdlib>
00042 #include <string.h>
00043 #include <string>
00044 #include <stdlib.h>
00045 #include <sys/types.h>
00046 #include <sys/stat.h>
00047 #include <dirent.h>
00048 #include <errno.h>
00049 #include <dlfcn.h>
00050
00051
00052 #ifdef WIN32
00053 # define HAVE_SNDFILE 1
00054 #endif // WIN32
00055
00056
00057 #if HAVE_SNDFILE
00058 # include <sndfile.h>
00059 #else
00060 # include <audiofile.h>
00061 #endif // HAVE_SNDFILE
00062
00063 #include "gig.h"
00064
00065 using namespace std;
00066
00067 typedef map<unsigned int, bool> OrderMap;
00068 OrderMap* pOrderedSamples = NULL;
00069
00070 string Revision();
00071 void PrintVersion();
00072 void PrintUsage();
00073 void ExtractSamples(gig::File* gig, char* destdir, OrderMap* ordered);
00074 int writeWav(const char* filename, void* samples, long samplecount, int channels, int bitdepth, long rate);
00075 string ToString(int i);
00076
00077 #ifndef HAVE_SNDFILE
00078 void* hAFlib;
00079 void openAFlib(void);
00080 void closeAFlib(void);
00081
00082 AFfilesetup(*_afNewFileSetup)(void);
00083 void(*_afFreeFileSetup)(AFfilesetup);
00084 void(*_afInitChannels)(AFfilesetup,int,int);
00085 void(*_afInitSampleFormat)(AFfilesetup,int,int,int);
00086 void(*_afInitFileFormat)(AFfilesetup,int);
00087 void(*_afInitRate)(AFfilesetup,int,double);
00088 int(*_afWriteFrames)(AFfilehandle,int,const void*,int);
00089 AFfilehandle(*_afOpenFile)(const char*,const char*,AFfilesetup);
00090 int(*_afCloseFile)(AFfilehandle file);
00091 #endif // !HAVE_SNDFILE
00092
00093 int main(int argc, char *argv[]) {
00094 if (argc >= 2) {
00095 if (argv[1][0] == '-') {
00096 switch (argv[1][1]) {
00097 case 'v':
00098 PrintVersion();
00099 return EXIT_SUCCESS;
00100 }
00101 }
00102 }
00103 if (argc < 3) {
00104 PrintUsage();
00105 return EXIT_FAILURE;
00106 }
00107 if (argc > 3) {
00108 pOrderedSamples = new OrderMap;
00109 for (int i = 3; i < argc; i++) {
00110 unsigned int index = atoi(argv[i]);
00111 (*pOrderedSamples)[index] = true;
00112 }
00113 }
00114 FILE* hFile = fopen(argv[1], "r");
00115 if (!hFile) {
00116 cout << "Invalid input file argument!" << endl;
00117 return EXIT_FAILURE;
00118 }
00119 fclose(hFile);
00120 DIR* dir = opendir(argv[2]);
00121 if (!dir) {
00122 cout << "Unable to open DESTDIR: ";
00123 switch (errno) {
00124 case EACCES: cout << "Permission denied." << endl;
00125 break;
00126 case EMFILE: cout << "Too many file descriptors in use by process." << endl;
00127 break;
00128 case ENFILE: cout << "Too many files are currently open in the system." << endl;
00129 break;
00130 case ENOENT: cout << "Directory does not exist, or name is an empty string." << endl;
00131 break;
00132 case ENOMEM: cout << "Insufficient memory to complete the operation." << endl;
00133 break;
00134 case ENOTDIR: cout << "Is not a directory." << endl;
00135 break;
00136 default: cout << "Unknown error" << endl;
00137 }
00138 return EXIT_FAILURE;
00139 }
00140 if (dir) closedir(dir);
00141 try {
00142 RIFF::File* riff = new RIFF::File(argv[1]);
00143 gig::File* gig = new gig::File(riff);
00144 cout << "Extracting samples from \"" << argv[1] << "\" to directory \"" << argv[2] << "\"." << endl << flush;
00145 ExtractSamples(gig, argv[2], pOrderedSamples);
00146 cout << "Extraction finished." << endl << flush;
00147 delete gig;
00148 delete riff;
00149 if (pOrderedSamples) delete pOrderedSamples;
00150 }
00151 catch (RIFF::Exception e) {
00152 e.PrintMessage();
00153 return EXIT_FAILURE;
00154 }
00155 catch (...) {
00156 cout << "Unknown exception while trying to parse file." << endl;
00157 return EXIT_FAILURE;
00158 }
00159
00160 return EXIT_SUCCESS;
00161 }
00162
00163 void ExtractSamples(gig::File* gig, char* destdir, OrderMap* ordered) {
00164 #ifndef HAVE_SNDFILE
00165 hAFlib = NULL;
00166 openAFlib();
00167 #endif // !HAVE_SNDFILE
00168 uint8_t* pWave = NULL;
00169 long BufferSize = 0;
00170 int samples = 0;
00171 cout << "Seeking for available samples..." << flush;
00172 gig::Sample* pSample = gig->GetFirstSample();
00173 cout << "OK" << endl << flush;
00174 while (pSample) {
00175 samples++;
00176 if (ordered) {
00177 if ((*ordered)[samples] == false) {
00178 pSample = gig->GetNextSample();
00179 continue;
00180 }
00181 }
00182 string name = pSample->pInfo->Name;
00183 string filename = destdir;
00184 if (filename[filename.size() - 1] != '/') filename += "/";
00185 filename += ToString(samples);
00186 filename += "_";
00187 if (name == "") {
00188 name = "(NO NAME)";
00189 filename += "NONAME";
00190 }
00191 else {
00192 filename += name;
00193 name.insert(0, "\"");
00194 name += "\"";
00195 }
00196 filename += ".wav";
00197 if (pSample->Compressed) cout << "Decompressing ";
00198 else cout << "Extracting ";
00199 cout << "Sample " << samples << ") " << name << " (" << pSample->BitDepth <<"Bits, " << pSample->SamplesPerSecond << "Hz, " << pSample->Channels << " Channels, " << pSample->SamplesTotal << " Samples)..." << flush;
00200
00201
00202 #if USE_DISK_STREAMING
00203 long neededsize = (pSample->Compressed) ? 10485760
00204 : pSample->SamplesTotal * pSample->FrameSize;
00205 if (BufferSize < neededsize) {
00206 if (pWave) delete[] pWave;
00207 pWave = new uint8_t[neededsize];
00208 BufferSize = neededsize;
00209 }
00210 # if HASHED_READS_TEST
00211 unsigned long readsamples = 0,
00212 readinthisrun = 0,
00213 samplepiecesize = 2000;
00214 uint8_t* pSamplePiece = pWave;
00215 do {
00216 readinthisrun = pSample->Read(pSamplePiece, ++samplepiecesize);
00217
00218 pSamplePiece += readinthisrun * (2 * pSample->Channels);
00219 readsamples += readinthisrun;
00220 } while (readinthisrun == samplepiecesize);
00221
00222 if (pSample->Compressed) {
00223 pSample->SamplesTotal = readsamples;
00224 pSample->BitDepth = 16;
00225 }
00226 # else // read in one piece
00227 if (pSample->Compressed) {
00228 pSample->SamplesTotal = pSample->Read(pWave, 10485760 >> 2);
00229 }
00230 else {
00231 pSample->Read(pWave, pSample->SamplesTotal);
00232 }
00233 # endif // HASHED_READS_TEST
00234 #else // no disk streaming
00235 if (pSample->Compressed) {
00236 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;
00237 }
00238 else pWave = (uint8_t*) pSample->LoadSampleData();
00239 #endif // USE_DISK_STREAMING
00240 if (pWave) {
00241 int res = writeWav(filename.c_str(),
00242 pWave,
00243 pSample->SamplesTotal,
00244 pSample->Channels,
00245 pSample->BitDepth,
00246 pSample->SamplesPerSecond);
00247 if (res < 0) cout << "Couldn't write sample data." << endl;
00248 else cout << "ok" << endl;
00249 pSample->ReleaseSampleData();
00250 }
00251 else cout << "Failed to load sample data." << endl;
00252
00253 pSample = gig->GetNextSample();
00254 }
00255 if (pWave) delete[] (uint8_t*) pWave;
00256 #ifndef HAVE_SNDFILE
00257 closeAFlib();
00258 #endif // !HAVE_SNDFILE
00259 }
00260
00261 int writeWav(const char* filename, void* samples, long samplecount, int channels, int bitdepth, long rate) {
00262 #if HAVE_SNDFILE
00263 SNDFILE* hfile;
00264 SF_INFO sfinfo;
00265 int format = SF_FORMAT_WAV;
00266 switch (bitdepth) {
00267 case 8:
00268 format |= SF_FORMAT_PCM_S8;
00269 cout << "8 bit" << endl << flush;
00270 break;
00271 case 16:
00272 format |= SF_FORMAT_PCM_16;
00273 cout << "16 bit" << endl << flush;
00274 break;
00275 case 24:
00276 format |= SF_FORMAT_PCM_24;
00277 cout << "24 bit" << endl << flush;
00278 break;
00279 case 32:
00280 format |= SF_FORMAT_PCM_32;
00281 cout << "32 bit" << endl << flush;
00282 break;
00283 default:
00284 cerr << "Error: Bithdepth " << ToString(bitdepth) << " not supported by libsndfile, ignoring sample!\n" << flush;
00285 return -1;
00286 }
00287 memset(&sfinfo, 0, sizeof (sfinfo));
00288 sfinfo.samplerate = rate;
00289 sfinfo.frames = samplecount;
00290 sfinfo.channels = channels;
00291 sfinfo.format = format;
00292 if (!(hfile = sf_open(filename, SFM_WRITE, &sfinfo))) {
00293 cerr << "Error: Unable to open output file \'" << filename << "\'.\n" << flush;
00294 return -1;
00295 }
00296 if (sf_write_short(hfile, (short*)samples, channels * samplecount) != channels * samplecount) {
00297 cerr << sf_strerror(hfile) << endl << flush;
00298 sf_close(hfile);
00299 return -1;
00300 }
00301 sf_close(hfile);
00302 #else
00303 AFfilesetup setup = _afNewFileSetup();
00304 _afInitFileFormat(setup, AF_FILE_WAVE);
00305 _afInitChannels(setup, AF_DEFAULT_TRACK, channels);
00306 _afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, bitdepth);
00307 _afInitRate(setup, AF_DEFAULT_TRACK, rate);
00308 if (setup == AF_NULL_FILESETUP) return -1;
00309 AFfilehandle hFile = _afOpenFile(filename, "w", setup);
00310 if (hFile == AF_NULL_FILEHANDLE) return -1;
00311 if (_afWriteFrames(hFile, AF_DEFAULT_TRACK, samples, samplecount) < 0) return -1;
00312 _afCloseFile(hFile);
00313 _afFreeFileSetup(setup);
00314 #endif // HAVE_SNDFILE
00315
00316 return 0;
00317 }
00318
00319 #ifndef HAVE_SNDFILE
00320 void openAFlib() {
00321 hAFlib = dlopen("libaudiofile.so", RTLD_NOW);
00322 if (!hAFlib) {
00323 cout << "Unable to load library libaudiofile.so: " << dlerror() << endl;
00324 return;
00325 }
00326 _afNewFileSetup = (AFfilesetup(*)(void)) dlsym(hAFlib, "afNewFileSetup");
00327 _afFreeFileSetup = (void(*)(AFfilesetup)) dlsym(hAFlib, "afFreeFileSetup");
00328 _afInitChannels = (void(*)(AFfilesetup,int,int)) dlsym(hAFlib, "afInitChannels");
00329 _afInitSampleFormat = (void(*)(AFfilesetup,int,int,int)) dlsym(hAFlib, "afInitSampleFormat");
00330 _afInitFileFormat = (void(*)(AFfilesetup,int)) dlsym(hAFlib, "afInitFileFormat");
00331 _afInitRate = (void(*)(AFfilesetup,int,double)) dlsym(hAFlib, "afInitRate");
00332 _afWriteFrames = (int(*)(AFfilehandle,int,const void*,int)) dlsym(hAFlib, "afWriteFrames");
00333 _afOpenFile = (AFfilehandle(*)(const char*,const char*,AFfilesetup)) dlsym(hAFlib, "afOpenFile");
00334 _afCloseFile = (int(*)(AFfilehandle file)) dlsym(hAFlib, "afCloseFile");
00335 if (dlerror()) cout << "Failed to load function from libaudiofile.so: " << dlerror() << endl;
00336 }
00337
00338 void closeAFlib() {
00339 if (hAFlib) dlclose(hAFlib);
00340 }
00341 #endif // !HAVE_SNDFILE
00342
00343 string Revision() {
00344 string s = "$Revision: 1.6 $";
00345 return s.substr(11, s.size() - 13);
00346 }
00347
00348 void PrintVersion() {
00349 cout << "gigextract revision " << Revision() << endl;
00350 cout << "using " << gig::libraryName() << " " << gig::libraryVersion();
00351 #if HAVE_SNDFILE
00352 char versionBuffer[128];
00353 sf_command(NULL, SFC_GET_LIB_VERSION, versionBuffer, 128);
00354 cout << ", " << versionBuffer;
00355 #endif // HAVE_SNDFILE
00356 cout << endl;
00357 #if !HAVE_SNDFILE
00358 cout << "built against libaudiofile "
00359 << LIBAUDIOFILE_MAJOR_VERSION << "." << LIBAUDIOFILE_MINOR_VERSION
00360 # ifdef LIBAUDIOFILE_MICRO_VERSION
00361 << "." << LIBAUDIOFILE_MICRO_VERSION
00362 # endif // LIBAUDIOFILE_MICRO_VERSION
00363 << endl;
00364 #endif // !HAVE_SNDFILE
00365 }
00366
00367 void PrintUsage() {
00368 cout << "gigextract - extracts samples from a Gigasampler file." << endl;
00369 cout << endl;
00370 cout << "Usage: gigextract [-v] GIGFILE DESTDIR [SAMPLENR] [ [SAMPLENR] ...]" << endl;
00371 cout << endl;
00372 cout << " GIGFILE Input Gigasampler (.gig) file." << endl;
00373 cout << endl;
00374 cout << " DESTDIR Destination directory where all .wav files will be written to." << endl;
00375 cout << endl;
00376 cout << " SAMPLENR Index (/indices) of Sample(s) which should be extracted." << endl;
00377 cout << " If no sample indices are given, all samples will be extracted" << endl;
00378 cout << " (use gigdump to look for available samples)." << endl;
00379 cout << endl;
00380 cout << " -v Print version and exit." << endl;
00381 cout << endl;
00382 }
00383
00384 string ToString(int i) {
00385 static char strbuf[1024];
00386 sprintf(strbuf,"%d",i);
00387 string s = strbuf;
00388 return s;
00389 }