Main Page   Class Hierarchy   Compound List   File List   Compound Members   Related Pages  

gapefilereader.cpp

00001 //gapefilereader.cpp
00002 //Dave Chisholm dkc@ccrma.stanford.edu 12/1/00
00003 #include "gapefilereader.h"
00004 #include "WvIn.h"
00005 #include "AifWvIn.h"
00006 #include "SndWvIn.h"
00007 #include "RawWvIn.h"
00008 #include "WavWvIn.h"
00009 
00010 GapeFileReader::GapeFileReader(const QString& filename, int mode)
00011 : GapeUnit(NULL) {
00012     char buffer[512];
00013     validFile = true;
00014     this->mode = mode;
00015     char* charmode;
00016     if (mode == GapeConsts::ONEPLAY) {
00017         charmode = "oneshot";
00018     } else {
00019         charmode = "looping";
00020     }
00021 
00022 
00023     //we must use a bufer for our file name b/c stk doesn't use the const keyword...
00024     strncpy(buffer, filename.latin1(), 512);
00025 
00026     try {
00027         if (filename.find(".aif",-5,false) >= 0) {//we have an aiff format file
00028             data = new AifWvIn(buffer,charmode);
00029         } else if (filename.find(".raw",-4,false) >= 0) {//we have a raw sound file
00030             data = new RawWvIn(buffer,charmode);
00031         } else if (filename.find(".wav",-4,false) >= 0) {//we have a wav format sound file
00032             data = new WavWvIn(buffer,charmode);
00033         } else if (filename.find(".snd",-4,false) >= 0) {//we have an snd format sound file
00034             data = new SndWvIn(buffer,charmode);
00035         } else {
00036             //validFile = false;
00037             QString errorMessage("Vague filename for GapeFileReader (should end in .raw/wav/snd/aif/aiff) - ");
00038             errorMessage.append(filename);
00039             errorMessage.append("\n Opening as raw file \n");
00040             GapeConsts::reportError(errorMessage.latin1());
00041             data = new WavWvIn(buffer,charmode);
00042         }
00043     } catch(StkError& e) {
00044         validFile = false;
00045         QString errorMessage("Error opening file - ");
00046         errorMessage.append(filename);
00047         GapeConsts::reportError(errorMessage.latin1());
00048     }
00049     setNumChannels(data->getNumChannels());
00050     if (numChannels > GAPE_MAX_NUM_CHANNELS) {
00051         validFile = false;
00052         QString errorMessage("Too many channels in file for GAPE to support - ");
00053         errorMessage.append(filename);
00054         GapeConsts::reportError(errorMessage.latin1());
00055     }
00056 
00057     //need to deal with stk's confusing rate stuff - rate here doesn't indicate
00058     //the sample rate, instead it indicates some time scaling coefficientl
00059     //if (mode != GapeConsts::ONEPLAY)
00060         data->setRate(data->getRate() * SRATE/GapeConsts::sampleRate);
00061         //data->setRate(1);
00062 }
00063 
00064 GapeFileReader::GapeFileReader(GapeController* c, const QString& filename, int mode)
00065 : GapeUnit(c) {
00066     GapeFileReader(filename, mode);
00067 }
00068 
00069 GapeFileReader::~GapeFileReader() {
00070     delete data;
00071     data = NULL;
00072 }
00073 
00074 void GapeFileReader::receiveTick(const GapeFloat* values, int numValues) {
00075     if (muted || !validFile || data->isFinished()) {
00076         emit emitTick(values,numValues);
00077         return;
00078     }
00079 
00080     if ((values != NULL) && (numValues != 0)) { //in this case we must mix the incoming sample and our file's next sample
00081         GapeFloat sample[GAPE_MAX_NUM_CHANNELS] = {0.0};//aggregate initialization initializes all data to 0
00082         memset(sample,0,GAPE_MAX_NUM_CHANNELS * sizeof(GapeFloat));
00083 
00084         if (numValues > GAPE_MAX_NUM_CHANNELS) {
00085             numValues = GAPE_MAX_NUM_CHANNELS;
00086         }
00087 
00088         for (int i = 0;i < numValues;i++) {
00089             sample[i] = values[i];
00090         }
00091 
00092         GapeFloat* fileSamples= data->mtick();
00093         for (int j = 0;j < numChannels;j++) {
00094             sample[j] += fileSamples[j] * gain;
00095         }
00096 
00097         int emitSize  = (numValues > numChannels) ? numValues : numChannels;//we emit as many channels as we need
00098         emit emitTick(sample,emitSize);
00099 
00100     } else {//otherwise we just send out the next file sample
00101         emit emitTick(data->mtick(),numChannels);
00102     }
00103 }

Generated at Thu Jun 21 13:28:49 2001 for GAPE by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001