00001
00002 #ifndef SAMPLE_DOT_AITCH
00003 #define SAMPLE_DOT_AITCH
00004
00005 #include "taptaudio.h"
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <SDL.h>
00015
00016 #include "sndfile.h"
00017 #include "taptdebug.h"
00018
00019
00020 class PCMSample : public ASSample {
00021 protected:
00022 void *data;
00023 int channels;
00024 unsigned long bytes_size;
00025 double rate;
00026 AUDIO_FORMAT fmt;
00027
00028
00029 PCMSample() : data(0) {}
00030
00031 public:
00032 PCMSample(void* data_, unsigned channels_, unsigned long bytes_size_, double rate_, AUDIO_FORMAT fmt_)
00033 :
00034 data(data_), channels(channels_), bytes_size(bytes_size_), rate(rate_), fmt(fmt_) {}
00035
00036 virtual unsigned getChannels() {return channels;}
00037 virtual AUDIO_FORMAT getFormat() {return fmt;}
00038 virtual unsigned long numBytes() {return bytes_size;}
00039 virtual const void* getBytes() {return data;}
00040 virtual double getSamRate() {return rate;}
00041
00042 static void mono2stereo(void *newdata,
00043 const void *olddata,
00044 unsigned long olddata_bytes,
00045 unsigned oldframesize);
00046
00047 static void stereo2mono(Uint8 *newdata,
00048 const Uint8 *olddata,
00049 unsigned long olddata_bytes,
00050 unsigned framesize);
00051
00052 };
00053
00054
00055
00056
00057 class FileSample : public PCMSample {
00058 protected:
00059 virtual void load_sndfile(SNDFILE* sf, SF_INFO &sfinfo, bool forcestereo);
00060 virtual ~FileSample();
00061 public:
00062 FileSample (const std::string& path, bool forcestereo = false);
00063
00064 };
00065
00066
00067
00068
00069 template <class T>
00070 class FileSampleT : public FileSample {
00071 protected:
00072 virtual ~FileSampleT();
00073 virtual void load_sndfile(SNDFILE* sf, SF_INFO &sfinfo, bool forcestereo);
00074 public:
00075 FileSampleT (const std::string& path, bool forcestereo = false)
00076 :
00077 FileSample(path, forcestereo) {}
00078 };
00079
00080
00081 class Recording : public PCMSample {
00082 protected:
00083 virtual ~Recording();
00084 unsigned long reserved;
00085 public:
00086 Recording(unsigned long initial_size = 28672,
00087 unsigned chans = 2,
00088 AUDIO_FORMAT format = AF_Int16,
00089 double samplerate = 44100);
00090
00091
00092
00093
00094
00095 void append(void *more, unsigned long sz);
00096
00097 void fill(const void *more, unsigned long sz);
00098 void skipfill(const void *more, unsigned long sz, unsigned block);
00099 void trim();
00100 void fillSilence(unsigned long sz);
00101 bool save(const std::string &path);
00102
00103 virtual bool isFull() {return false;}
00104
00105 };
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 template <class T>
00118 sf_count_t lsf_read(SNDFILE *sndfile, T* data, sf_count_t items) {
00119 return sf_read_raw(sndfile, data, sizeof(T) * items);
00120 }
00121
00122 template <>
00123 sf_count_t lsf_read(SNDFILE *sndfile, short* data, sf_count_t items);
00124 template <>
00125 sf_count_t lsf_read(SNDFILE *sndfile, int* data, sf_count_t items);
00126 template <>
00127 sf_count_t lsf_read(SNDFILE *sndfile, float* data, sf_count_t items);
00128 template <>
00129 sf_count_t lsf_read(SNDFILE *sndfile, double* data, sf_count_t items);
00130
00131 template <class T>
00132 void FileSampleT<T>::load_sndfile(SNDFILE* sf, SF_INFO &sfinfo, bool forcestereo) {
00133 T* tdata;
00134 channels = sfinfo.channels;
00135 unsigned frame_sz = sizeof(T) * channels;
00136 bytes_size = frame_sz * sfinfo.frames;
00137 rate = sfinfo.samplerate;
00138 data = tdata = new T[sfinfo.frames * channels];
00139
00140 sf_count_t numread = lsf_read(sf, tdata, channels * sfinfo.frames );
00141
00142 if (numread != channels * sfinfo.frames) {
00143
00144 }
00145
00146 if (channels == 1 && forcestereo) {
00147 T *newdata = new T[sfinfo.frames * 2];
00148 mono2stereo(newdata, data, bytes_size, frame_sz );
00149 delete[] tdata;
00150 data = newdata;
00151 bytes_size *= 2;
00152 channels = 2;
00153 }
00154 }
00155
00156 template <class T>
00157 FileSampleT<T>::~FileSampleT() {
00158 delete[] static_cast<T*>(data);
00159 data = 0;
00160 }
00161
00162 #endif