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;
00116
00118
00124 virtual unsigned int soundPlay(unsigned int sample, float volumeL=1.0f, float volumeR=1.0f, float disparity=0.0f, float pitch=1.0f )=0;
00126
00132 virtual unsigned int soundLoop(unsigned int sample, float volumeL=1.0f, float volumeR=1.0f, float disparity=0.0f, float pitch=1.0f )=0;
00134
00140 virtual bool soundUpdate(unsigned int sound, float volumeL, float volumeR, float disparity=0.0f, float pitch=1.0f )=0;
00142 virtual bool soundStop(unsigned int sound)=0;
00144 virtual void soundStop()=0;
00146 virtual unsigned int soundActive() const=0;
00147
00148 protected:
00150 DeviceAudio();
00152 virtual ~DeviceAudio() { s_instance = 0; }
00153
00155 unsigned int m_freqOut;
00157 float m_volL;
00159 float m_volR;
00161 std::map<std::string, AudioSample * (*)(const std::string &)> mm_loader;
00162
00164 static DeviceAudio * s_instance;
00165 };
00166
00167 #endif // _PRO_AUDIO