--- libgig/trunk/src/gigextract.cpp 2004/04/27 09:06:07 55 +++ libgig/trunk/src/gigextract.cpp 2005/05/16 18:40:45 549 @@ -2,8 +2,8 @@ * * * libgig - C++ cross-platform Gigasampler format file loader library * * * - * Copyright (C) 2003, 2004 by Christian Schoenebeck * - * * + * Copyright (C) 2003-2005 by Christian Schoenebeck * + * * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -39,13 +39,27 @@ #include #include +#include +#include #include #include #include #include #include #include -#include + +// only libsndfile is available for Windows, so we use that for writing the sound files +#ifdef WIN32 +# define HAVE_SNDFILE 1 +#endif // WIN32 + +// we prefer libsndfile before libaudiofile +#if HAVE_SNDFILE +# include +#else +# include +#endif // HAVE_SNDFILE + #include "gig.h" using namespace std; @@ -53,9 +67,14 @@ typedef map OrderMap; OrderMap* pOrderedSamples = NULL; +string Revision(); +void PrintVersion(); void PrintUsage(); void ExtractSamples(gig::File* gig, char* destdir, OrderMap* ordered); int writeWav(const char* filename, void* samples, long samplecount, int channels, int bitdepth, long rate); +string ToString(int i); + +#ifndef HAVE_SNDFILE void* hAFlib; // handle to libaudiofile void openAFlib(void); void closeAFlib(void); @@ -69,9 +88,18 @@ int(*_afWriteFrames)(AFfilehandle,int,const void*,int); AFfilehandle(*_afOpenFile)(const char*,const char*,AFfilesetup); int(*_afCloseFile)(AFfilehandle file); -string ToString(int i); +#endif // !HAVE_SNDFILE int main(int argc, char *argv[]) { + if (argc >= 2) { + if (argv[1][0] == '-') { + switch (argv[1][1]) { + case 'v': + PrintVersion(); + return EXIT_SUCCESS; + } + } + } if (argc < 3) { PrintUsage(); return EXIT_FAILURE; @@ -133,8 +161,10 @@ } void ExtractSamples(gig::File* gig, char* destdir, OrderMap* ordered) { +#ifndef HAVE_SNDFILE hAFlib = NULL; openAFlib(); +#endif // !HAVE_SNDFILE uint8_t* pWave = NULL; long BufferSize = 0; int samples = 0; @@ -170,11 +200,6 @@ #if USE_DISK_STREAMING - if (pSample->Compressed) { // hack - pSample->BitDepth = 16; - pSample->FrameSize = 4; - } - long neededsize = (pSample->Compressed) ? 10485760 /* 10 MB buffer */ : pSample->SamplesTotal * pSample->FrameSize; if (BufferSize < neededsize) { @@ -189,12 +214,14 @@ uint8_t* pSamplePiece = pWave; do { // we read the sample in small pieces and increment the size with each run just to test streaming capability readinthisrun = pSample->Read(pSamplePiece, ++samplepiecesize); - pSamplePiece += readinthisrun * pSample->FrameSize; + // 24 bit is truncated to 16 by Sample::Read at the moment + pSamplePiece += readinthisrun * (2 * pSample->Channels); // readinthisrun * pSample->FrameSize; readsamples += readinthisrun; } while (readinthisrun == samplepiecesize); if (pSample->Compressed) { // hack pSample->SamplesTotal = readsamples; + pSample->BitDepth = 16; } # else // read in one piece if (pSample->Compressed) { @@ -226,10 +253,53 @@ pSample = gig->GetNextSample(); } if (pWave) delete[] (uint8_t*) pWave; +#ifndef HAVE_SNDFILE closeAFlib(); +#endif // !HAVE_SNDFILE } int writeWav(const char* filename, void* samples, long samplecount, int channels, int bitdepth, long rate) { +#if HAVE_SNDFILE + SNDFILE* hfile; + SF_INFO sfinfo; + int format = SF_FORMAT_WAV; + switch (bitdepth) { + case 8: + format |= SF_FORMAT_PCM_S8; + cout << "8 bit" << endl << flush; + break; + case 16: + format |= SF_FORMAT_PCM_16; + cout << "16 bit" << endl << flush; + break; + case 24: + format |= SF_FORMAT_PCM_24; + cout << "24 bit" << endl << flush; + break; + case 32: + format |= SF_FORMAT_PCM_32; + cout << "32 bit" << endl << flush; + break; + default: + cerr << "Error: Bithdepth " << ToString(bitdepth) << " not supported by libsndfile, ignoring sample!\n" << flush; + return -1; + } + memset(&sfinfo, 0, sizeof (sfinfo)); + sfinfo.samplerate = rate; + sfinfo.frames = samplecount; + sfinfo.channels = channels; + sfinfo.format = format; + if (!(hfile = sf_open(filename, SFM_WRITE, &sfinfo))) { + cerr << "Error: Unable to open output file \'" << filename << "\'.\n" << flush; + return -1; + } + if (sf_write_short(hfile, (short*)samples, channels * samplecount) != channels * samplecount) { + cerr << sf_strerror(hfile) << endl << flush; + sf_close(hfile); + return -1; + } + sf_close(hfile); +#else AFfilesetup setup = _afNewFileSetup(); _afInitFileFormat(setup, AF_FILE_WAVE); _afInitChannels(setup, AF_DEFAULT_TRACK, channels); @@ -241,10 +311,12 @@ if (_afWriteFrames(hFile, AF_DEFAULT_TRACK, samples, samplecount) < 0) return -1; _afCloseFile(hFile); _afFreeFileSetup(setup); +#endif // HAVE_SNDFILE - return 0; + return 0; // success } +#ifndef HAVE_SNDFILE void openAFlib() { hAFlib = dlopen("libaudiofile.so", RTLD_NOW); if (!hAFlib) { @@ -266,11 +338,36 @@ void closeAFlib() { if (hAFlib) dlclose(hAFlib); } +#endif // !HAVE_SNDFILE + +string Revision() { + string s = "$Revision: 1.6 $"; + return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword +} + +void PrintVersion() { + cout << "gigextract revision " << Revision() << endl; + cout << "using " << gig::libraryName() << " " << gig::libraryVersion(); + #if HAVE_SNDFILE + char versionBuffer[128]; + sf_command(NULL, SFC_GET_LIB_VERSION, versionBuffer, 128); + cout << ", " << versionBuffer; + #endif // HAVE_SNDFILE + cout << endl; + #if !HAVE_SNDFILE + cout << "built against libaudiofile " + << LIBAUDIOFILE_MAJOR_VERSION << "." << LIBAUDIOFILE_MINOR_VERSION + # ifdef LIBAUDIOFILE_MICRO_VERSION + << "." << LIBAUDIOFILE_MICRO_VERSION + # endif // LIBAUDIOFILE_MICRO_VERSION + << endl; + #endif // !HAVE_SNDFILE +} void PrintUsage() { cout << "gigextract - extracts samples from a Gigasampler file." << endl; cout << endl; - cout << "Usage: gigextract GIGFILE DESTDIR [SAMPLENR] [ [SAMPLENR] ...]" << endl; + cout << "Usage: gigextract [-v] GIGFILE DESTDIR [SAMPLENR] [ [SAMPLENR] ...]" << endl; cout << endl; cout << " GIGFILE Input Gigasampler (.gig) file." << endl; cout << endl; @@ -280,6 +377,8 @@ cout << " If no sample indices are given, all samples will be extracted" << endl; cout << " (use gigdump to look for available samples)." << endl; cout << endl; + cout << " -v Print version and exit." << endl; + cout << endl; } string ToString(int i) {