00001 #ifndef _PRO_AUDIO
00002 #define _PRO_AUDIO
00003
00004 #include <string>
00005 #include <map>
00006
00036
00038 class AudioSample {
00039 public:
00041 AudioSample(unsigned char * data, unsigned int size, unsigned short channels, unsigned int sampleRate, unsigned short bitsPerSample) :
00042 m_data(data), m_size(size), m_channels(channels), m_sampleRate(sampleRate), m_bitsPerSample(bitsPerSample) { }
00044 AudioSample(const AudioSample & source);
00046 ~AudioSample() { delete[] m_data; }
00047
00049 unsigned char * data() { return m_data; };
00051 const unsigned char * data() const { return m_data; };
00053 unsigned int size() const { return m_size; }
00055 unsigned int frames() const { return m_size/m_channels/(m_bitsPerSample>>3); }
00057 unsigned int sizeFrame() const { return m_channels*(m_bitsPerSample>>3); }
00059 unsigned short channels() const { return m_channels; }
00061 unsigned int sampleRate() const { return m_sampleRate; }
00063 unsigned short bitsPerSample() const { return m_bitsPerSample; }
00065 bool bitsPerSample(unsigned short bits);
00067 unsigned short bytesPerSample() const { return m_bitsPerSample>>3; }
00068
00070 void volume(float f);
00071
00073 static AudioSample* loadWav(const std::string & fname);
00075 static AudioSample* readWav(FILE* stream, size_t (*readFunc)( void *, size_t, size_t, FILE *));
00076 protected:
00078 unsigned char * m_data;
00080 unsigned int m_size;
00082 unsigned short m_channels;
00084 unsigned int m_sampleRate;
00086 unsigned short m_bitsPerSample;
00087 };
00088
00089
00090
00092 class DeviceAudio {
00093 public:
00095
00096 static DeviceAudio& singleton() { return *s_instance; }
00098 static void destroy() { if(s_instance) delete s_instance; s_instance=0; };
00099
00101 void volume(float left, float right) { m_volL=left; m_volR=right; }
00103 void volume(float leftAndRight) { m_volL=m_volR=leftAndRight; }
00105
00106 bool loaderRegister(AudioSample *(*loadFunc)(const std::string &), const std::string & suffix);
00108 bool loaderAvailable(const std::string & suffix) const;
00109
00111 virtual unsigned int sampleFromFile(const std::string & filename, float volume=1.0f);
00113 virtual unsigned int sampleFromMemory(const AudioSample & sample, float volume=1.0f)=0;
00115 virtual bool sampleDestroy(unsigned int sample)=0;
00117 virtual const AudioSample* sample(unsigned int handle) const { return 0; }
00118
00120
00126 virtual unsigned int soundPlay(unsigned int sample, float volumeL=1.0f, float volumeR=1.0f, float disparity=0.0f, float pitch=1.0f )=0;
00128
00134 virtual unsigned int soundLoop(unsigned int sample, float volumeL=1.0f, float volumeR=1.0f, float disparity=0.0f, float pitch=1.0f )=0;
00136
00142 virtual bool soundUpdate(unsigned int sound, float volumeL, float volumeR, float disparity=0.0f, float pitch=1.0f )=0;
00144 virtual bool soundStop(unsigned int sound)=0;
00146 virtual void soundStop()=0;
00148 virtual unsigned int soundActive() const=0;
00149
00150 protected:
00152 DeviceAudio();
00154 virtual ~DeviceAudio() { s_instance = 0; }
00155
00157 unsigned int m_freqOut;
00159 float m_volL;
00161 float m_volR;
00163 std::map<std::string, AudioSample * (*)(const std::string &)> mm_loader;
00164
00166 static DeviceAudio * s_instance;
00167 };
00168
00169 #endif // _PRO_AUDIO