00001
00012 #ifndef AUDIERE_H
00013 #define AUDIERE_H
00014
00015
00016 #include <vector>
00017 #include <string>
00018
00019 #ifdef _MSC_VER
00020 #pragma warning(disable : 4786)
00021 #endif
00022
00023
00024 #ifndef __cplusplus
00025 #error Audiere requires C++
00026 #endif
00027
00028
00029
00030 #if defined(WIN32) || defined(_WIN32)
00031 #define ADR_CALL __stdcall
00032 #else
00033 #define ADR_CALL
00034 #endif
00035
00036
00037 #define ADR_FUNCTION(ret, name) extern "C" ret ADR_CALL name
00038
00039
00040 namespace audiere {
00041
00042 class RefCounted {
00043 protected:
00051 ~RefCounted() { }
00052
00053 public:
00057 virtual void ADR_CALL ref() = 0;
00058
00063 virtual void ADR_CALL unref() = 0;
00064 };
00065
00066
00067 template<typename T>
00068 class RefPtr {
00069 public:
00070 RefPtr(T* ptr = 0) {
00071 m_ptr = 0;
00072 *this = ptr;
00073 }
00074
00075 RefPtr(const RefPtr<T>& ptr) {
00076 m_ptr = 0;
00077 *this = ptr;
00078 }
00079
00080 ~RefPtr() {
00081 if (m_ptr) {
00082 m_ptr->unref();
00083 m_ptr = 0;
00084 }
00085 }
00086
00087 RefPtr<T>& operator=(T* ptr) {
00088 if (ptr != m_ptr) {
00089 if (m_ptr) {
00090 m_ptr->unref();
00091 }
00092 m_ptr = ptr;
00093 if (m_ptr) {
00094 m_ptr->ref();
00095 }
00096 }
00097 return *this;
00098 }
00099
00100 RefPtr<T>& operator=(const RefPtr<T>& ptr) {
00101 *this = ptr.m_ptr;
00102 return *this;
00103 }
00104
00105 T* operator->() const {
00106 return m_ptr;
00107 }
00108
00109 T& operator*() const {
00110 return *m_ptr;
00111 }
00112
00113 operator bool() const {
00114 return (m_ptr != 0);
00115 }
00116
00117 T* get() const {
00118 return m_ptr;
00119 }
00120
00121 private:
00122 T* m_ptr;
00123 };
00124
00125
00130 template<class Interface>
00131 class RefImplementation : public Interface {
00132 protected:
00133 RefImplementation() {
00134 m_ref_count = 0;
00135 }
00136
00141 virtual ~RefImplementation() { }
00142
00143 public:
00144 void ADR_CALL ref() {
00145 ++m_ref_count;
00146 }
00147
00148 void ADR_CALL unref() {
00149 if (--m_ref_count == 0) {
00150 delete this;
00151 }
00152 }
00153
00154 private:
00155 int m_ref_count;
00156 };
00157
00158
00167 class File : public RefCounted {
00168 protected:
00169 ~File() { }
00170
00171 public:
00175 enum SeekMode {
00176 BEGIN,
00177 CURRENT,
00178 END,
00179 };
00180
00189 virtual int ADR_CALL read(void* buffer, int size) = 0;
00190
00202 virtual bool ADR_CALL seek(int position, SeekMode mode) = 0;
00203
00209 virtual int ADR_CALL tell() = 0;
00210 };
00211 typedef RefPtr<File> FilePtr;
00212
00213
00215 enum SampleFormat {
00216 SF_U8,
00217 SF_S16,
00218 };
00219
00220
00231 class SampleSource : public RefCounted {
00232 protected:
00233 ~SampleSource() { }
00234
00235 public:
00240 virtual void ADR_CALL getFormat(
00241 int& channel_count,
00242 int& sample_rate,
00243 SampleFormat& sample_format) = 0;
00244
00254 virtual int ADR_CALL read(int frame_count, void* buffer) = 0;
00255
00261 virtual void ADR_CALL reset() = 0;
00262
00266 virtual bool ADR_CALL isSeekable() = 0;
00267
00272 virtual int ADR_CALL getLength() = 0;
00273
00280 virtual void ADR_CALL setPosition(int position) = 0;
00281
00287 virtual int ADR_CALL getPosition() = 0;
00288 };
00289 typedef RefPtr<SampleSource> SampleSourcePtr;
00290
00291
00300 class OutputStream : public RefCounted {
00301 protected:
00302 ~OutputStream() { }
00303
00304 public:
00309 virtual void ADR_CALL play() = 0;
00310
00315 virtual void ADR_CALL stop() = 0;
00316
00320 virtual bool ADR_CALL isPlaying() = 0;
00321
00329 virtual void ADR_CALL reset() = 0;
00330
00336 virtual void ADR_CALL setRepeat(bool repeat) = 0;
00337
00341 virtual bool ADR_CALL getRepeat() = 0;
00342
00348 virtual void ADR_CALL setVolume(float volume) = 0;
00349
00355 virtual float ADR_CALL getVolume() = 0;
00356
00362 virtual void ADR_CALL setPan(float pan) = 0;
00363
00367 virtual float ADR_CALL getPan() = 0;
00368
00374 virtual void ADR_CALL setPitchShift(float shift) = 0;
00375
00379 virtual float ADR_CALL getPitchShift() = 0;
00380
00384 virtual bool ADR_CALL isSeekable() = 0;
00385
00390 virtual int ADR_CALL getLength() = 0;
00391
00398 virtual void ADR_CALL setPosition(int position) = 0;
00399
00405 virtual int ADR_CALL getPosition() = 0;
00406 };
00407 typedef RefPtr<OutputStream> OutputStreamPtr;
00408
00409
00418 class AudioDevice : public RefCounted {
00419 protected:
00420 ~AudioDevice() { }
00421
00422 public:
00428 virtual void ADR_CALL update() = 0;
00429
00443 virtual OutputStream* ADR_CALL openStream(SampleSource* source) = 0;
00444
00467 virtual OutputStream* ADR_CALL openBuffer(
00468 void* samples,
00469 int frame_count,
00470 int channel_count,
00471 int sample_rate,
00472 SampleFormat sample_format) = 0;
00473 };
00474 typedef RefPtr<AudioDevice> AudioDevicePtr;
00475
00476
00486 class SampleBuffer : public RefCounted {
00487 protected:
00488 ~SampleBuffer() { }
00489
00490 public:
00491
00496 virtual void ADR_CALL getFormat(
00497 int& channel_count,
00498 int& sample_rate,
00499 SampleFormat& sample_format) = 0;
00500
00504 virtual int ADR_CALL getLength() = 0;
00505
00511 virtual const void* ADR_CALL getSamples() = 0;
00512
00517 virtual SampleSource* ADR_CALL openStream() = 0;
00518 };
00519 typedef RefPtr<SampleBuffer> SampleBufferPtr;
00520
00521
00525 enum SoundEffectType {
00526 SINGLE,
00527 MULTIPLE,
00528 };
00529
00530
00539 class SoundEffect : public RefCounted {
00540 protected:
00541 ~SoundEffect() { }
00542
00543 public:
00550 virtual void ADR_CALL play() = 0;
00551
00556 virtual void ADR_CALL stop() = 0;
00557 };
00558 typedef RefPtr<SoundEffect> SoundEffectPtr;
00559
00560
00562 namespace hidden {
00563
00564
00565
00566 ADR_FUNCTION(const char*, AdrGetVersion)();
00567
00576 ADR_FUNCTION(const char*, AdrGetSupportedFileFormats)();
00577
00578 ADR_FUNCTION(int, AdrGetSampleSize)(SampleFormat format);
00579
00580 ADR_FUNCTION(AudioDevice*, AdrOpenDevice)(
00581 const char* name,
00582 const char* parameters);
00583
00584 ADR_FUNCTION(SampleSource*, AdrOpenSampleSource)(const char* filename);
00585 ADR_FUNCTION(SampleSource*, AdrOpenSampleSourceFromFile)(File* file);
00586 ADR_FUNCTION(SampleSource*, AdrCreateTone)(double frequency);
00587 ADR_FUNCTION(SampleSource*, AdrCreateSquareWave)(double frequency);
00588 ADR_FUNCTION(SampleSource*, AdrCreateWhiteNoise)();
00589 ADR_FUNCTION(SampleSource*, AdrCreatePinkNoise)();
00590
00591 ADR_FUNCTION(OutputStream*, AdrOpenSound)(
00592 AudioDevice* device,
00593 SampleSource* source,
00594 bool streaming);
00595
00596 ADR_FUNCTION(SampleBuffer*, AdrCreateSampleBuffer)(
00597 void* samples,
00598 int frame_count,
00599 int channel_count,
00600 int sample_rate,
00601 SampleFormat sample_format);
00602 ADR_FUNCTION(SampleBuffer*, AdrCreateSampleBufferFromSource)(
00603 SampleSource* source);
00604
00605 ADR_FUNCTION(SoundEffect*, AdrOpenSoundEffect)(
00606 AudioDevice* device,
00607 SampleSource* source,
00608 SoundEffectType type);
00609 }
00610
00611
00612
00613
00614
00620 inline const char* GetVersion() {
00621 return hidden::AdrGetVersion();
00622 }
00623
00624
00626 struct FileFormatDesc {
00628 std::string description;
00629
00631 std::vector<std::string> extensions;
00632 };
00633
00634 inline void _SplitString(
00635 std::vector<std::string>& out,
00636 const char* in,
00637 char delim)
00638 {
00639 out.clear();
00640 while (*in) {
00641 const char* next = strchr(in, delim);
00642 if (next) {
00643 out.push_back(std::string(in, next));
00644 } else {
00645 out.push_back(in);
00646 }
00647
00648 in = (next ? next + 1 : "");
00649 }
00650 }
00651
00655 inline void GetSupportedFileFormats(std::vector<FileFormatDesc>& formats) {
00656 std::vector<std::string> descriptions;
00657 _SplitString(descriptions, hidden::AdrGetSupportedFileFormats(), ';');
00658
00659 formats.resize(descriptions.size());
00660 for (unsigned i = 0; i < formats.size(); ++i) {
00661 const char* d = descriptions[i].c_str();
00662 const char* colon = strchr(d, ':');
00663 formats[i].description.assign(d, colon);
00664
00665 _SplitString(formats[i].extensions, colon + 1, ',');
00666 }
00667 }
00668
00677 inline int GetSampleSize(SampleFormat format) {
00678 return hidden::AdrGetSampleSize(format);
00679 }
00680
00693 inline AudioDevice* OpenDevice(
00694 const char* name = 0,
00695 const char* parameters = 0)
00696 {
00697 return hidden::AdrOpenDevice(name, parameters);
00698 }
00699
00707 inline SampleSource* OpenSampleSource(const char* filename) {
00708 return hidden::AdrOpenSampleSource(filename);
00709 }
00710
00721 inline SampleSource* OpenSampleSource(File* file) {
00722 return hidden::AdrOpenSampleSourceFromFile(file);
00723 }
00724
00732 inline SampleSource* CreateTone(double frequency) {
00733 return hidden::AdrCreateTone(frequency);
00734 }
00735
00743 inline SampleSource* CreateSquareWave(double frequency) {
00744 return hidden::AdrCreateSquareWave(frequency);
00745 }
00746
00753 inline SampleSource* CreateWhiteNoise() {
00754 return hidden::AdrCreateWhiteNoise();
00755 }
00756
00763 inline SampleSource* CreatePinkNoise() {
00764 return hidden::AdrCreatePinkNoise();
00765 }
00766
00787 inline OutputStream* OpenSound(
00788 AudioDevice* device,
00789 SampleSource* source,
00790 bool streaming = false)
00791 {
00792 return hidden::AdrOpenSound(device, source, streaming);
00793 }
00794
00799 inline OutputStream* OpenSound(
00800 AudioDevice* device,
00801 const char* filename,
00802 bool streaming = false)
00803 {
00804 return OpenSound(device, OpenSampleSource(filename), streaming);
00805 }
00806
00811 inline OutputStream* OpenSound(
00812 AudioDevice* device,
00813 File* file,
00814 bool streaming = false)
00815 {
00816 return OpenSound(device, OpenSampleSource(file), streaming);
00817 }
00818
00836 inline SampleBuffer* CreateSampleBuffer(
00837 void* samples,
00838 int frame_count,
00839 int channel_count,
00840 int sample_rate,
00841 SampleFormat sample_format)
00842 {
00843 return hidden::AdrCreateSampleBuffer(
00844 samples, frame_count,
00845 channel_count, sample_rate, sample_format);
00846 }
00847
00857 inline SampleBuffer* CreateSampleBuffer(SampleSource* source) {
00858 return hidden::AdrCreateSampleBufferFromSource(source);
00859 }
00860
00875 inline SoundEffect* OpenSoundEffect(
00876 AudioDevice* device,
00877 SampleSource* source,
00878 SoundEffectType type)
00879 {
00880 return hidden::AdrOpenSoundEffect(device, source, type);
00881 }
00882
00887 inline SoundEffect* OpenSoundEffect(
00888 AudioDevice* device,
00889 const char* filename,
00890 SoundEffectType type)
00891 {
00892 return OpenSoundEffect(device, OpenSampleSource(filename), type);
00893 }
00894
00899 inline SoundEffect* OpenSoundEffect(
00900 AudioDevice* device,
00901 File* file,
00902 SoundEffectType type)
00903 {
00904 return OpenSoundEffect(device, OpenSampleSource(file), type);
00905 }
00906 }
00907
00908
00909 #endif