--- libgig/trunk/src/gigextract.cpp 2003/10/25 20:15:04 2 +++ libgig/trunk/src/gigextract.cpp 2005/02/10 19:16:31 365 @@ -2,8 +2,8 @@ * * * libgig - C++ cross-platform Gigasampler format file loader library * * * - * Copyright (C) 2003 by Christian Schoenebeck * - * * + * Copyright (C) 2003, 2004 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,26 @@ #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; @@ -56,6 +69,9 @@ 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,7 +85,7 @@ 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 < 3) { @@ -133,8 +149,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 +188,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 +202,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 +241,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 +299,12 @@ if (_afWriteFrames(hFile, AF_DEFAULT_TRACK, samples, samplecount) < 0) return -1; _afCloseFile(hFile); _afFreeFileSetup(setup); - - return 0; +#endif // HAVE_SNDFILE + + return 0; // success } +#ifndef HAVE_SNDFILE void openAFlib() { hAFlib = dlopen("libaudiofile.so", RTLD_NOW); if (!hAFlib) { @@ -266,6 +326,7 @@ void closeAFlib() { if (hAFlib) dlclose(hAFlib); } +#endif // !HAVE_SNDFILE void PrintUsage() { cout << "gigextract - extracts samples from a Gigasampler file." << endl;