00001 #ifndef UTILITY_H
00002 #define UTILITY_H
00003
00004
00005 #ifdef _MSC_VER
00006 #pragma warning(disable : 4786)
00007 #endif
00008
00009
00010 #include <map>
00011 #include <string>
00012 #include <utility>
00013 #include "audiere.h"
00014 #include "types.h"
00015
00016
00017 #if defined(_MSC_VER) && _MSC_VER <= 1200 && !defined(_STLPORT_VERSION)
00018
00019
00020
00021
00022 namespace std {
00023
00024 #ifdef min
00025 #undef min
00026 #endif
00027
00028 #ifdef max
00029 #undef max
00030 #endif
00031
00032 template<typename T>
00033 inline T min(T a, T b) {
00034 return (a < b ? a : b);
00035 }
00036
00037 template<typename T>
00038 inline T max(T a, T b) {
00039 return (a > b ? a : b);
00040 }
00041 }
00042
00043 #else
00044
00045 #include <algorithm>
00046
00047 #endif
00048
00049
00050 namespace audiere {
00051
00052
00053 template<typename T>
00054 T clamp(T min, T x, T max) {
00055 return std::max(std::min(x, max), min);
00056 }
00057
00058
00059 class ParameterList {
00060 public:
00061 ParameterList(const char* parameters);
00062 std::string getValue(const std::string& key, const std::string& defValue) const;
00063 bool getBoolean(const std::string& key, bool def) const;
00064 int getInt(const std::string& key, int def) const;
00065
00066 private:
00067 std::map<std::string, std::string> m_values;
00068 };
00069
00070 int strcmp_case(const char* a, const char* b);
00071
00072
00073 inline int GetFrameSize(SampleSource* source) {
00074 int channel_count, sample_rate;
00075 SampleFormat sample_format;
00076 source->getFormat(channel_count, sample_rate, sample_format);
00077 return GetSampleSize(sample_format) * channel_count;
00078 }
00079
00080 inline int GetFrameSize(const SampleSourcePtr& source) {
00081 return GetFrameSize(source.get());
00082 }
00083
00084
00085 inline u16 read16_le(const u8* b) {
00086 return b[0] + (b[1] << 8);
00087 }
00088
00089 inline u16 read16_be(const u8* b) {
00090 return (b[0] << 8) + b[1];
00091 }
00092
00093 inline u32 read32_le(const u8* b) {
00094 return read16_le(b) + (read16_le(b + 2) << 16);
00095 }
00096
00097 inline u32 read32_be(const u8* b) {
00098 return (read16_be(b) << 16) + read16_be(b + 2);
00099 }
00100
00102 inline u32 readLD_be(const u8* b) {
00103 u32 mantissa = read32_be(b + 2);
00104 u8 exp = 30 - b[1];
00105 u32 last = 0;
00106 while (exp--) {
00107 last = mantissa;
00108 mantissa >>= 1;
00109 }
00110 if (last & 0x1) {
00111 mantissa++;
00112 }
00113 return mantissa;
00114 }
00115
00116
00117 inline int GetFileLength(File* file) {
00118 int pos = file->tell();
00119 file->seek(0, File::END);
00120 int length = file->tell();
00121 file->seek(pos, File::BEGIN);
00122 return length;
00123 }
00124
00125
00126 inline SampleSource* OpenBufferStream(
00127 void* samples, int sample_count,
00128 int channel_count, int sample_rate, SampleFormat sample_format)
00129 {
00130 return CreateSampleBuffer(
00131 samples, sample_count,
00132 channel_count, sample_rate, sample_format)->openStream();
00133 }
00134
00135
00136 class QueueBuffer {
00137 public:
00138 QueueBuffer() {
00139 m_capacity = 256;
00140 m_size = 0;
00141
00142 m_buffer = (u8*)malloc(m_capacity);
00143 }
00144
00145 ~QueueBuffer() {
00146 m_buffer = (u8*)realloc(m_buffer, 0);
00147 }
00148
00149 int getSize() {
00150 return m_size;
00151 }
00152
00153 void write(const void* buffer, int size) {
00154 bool need_realloc = false;
00155 while (size + m_size > m_capacity) {
00156 m_capacity *= 2;
00157 need_realloc = true;
00158 }
00159
00160 if (need_realloc) {
00161 m_buffer = (u8*)realloc(m_buffer, m_capacity);
00162 }
00163
00164 memcpy(m_buffer + m_size, buffer, size);
00165 m_size += size;
00166 }
00167
00168 int read(void* buffer, int size) {
00169 int to_read = std::min(size, m_size);
00170 memcpy(buffer, m_buffer, to_read);
00171 memmove(m_buffer, m_buffer + to_read, m_size - to_read);
00172 m_size -= to_read;
00173 return to_read;
00174 }
00175
00176 void clear() {
00177 m_size = 0;
00178 }
00179
00180 private:
00181 u8* m_buffer;
00182 int m_capacity;
00183 int m_size;
00184
00185
00186 QueueBuffer(const QueueBuffer&);
00187 QueueBuffer& operator=(const QueueBuffer&);
00188 };
00189
00190
00191 class SizedBuffer {
00192 public:
00193 SizedBuffer() {
00194 m_capacity = 256;
00195 m_buffer = malloc(m_capacity);
00196 }
00197
00198 ~SizedBuffer() {
00199 m_buffer = realloc(m_buffer, 0);
00200 }
00201
00202 void ensureSize(int size) {
00203 bool need_realloc = false;
00204 while (m_capacity < size) {
00205 m_capacity *= 2;
00206 need_realloc = true;
00207 }
00208 if (need_realloc) {
00209 m_buffer = realloc(m_buffer, m_capacity);
00210 }
00211 }
00212
00213 void* get() {
00214 return m_buffer;
00215 }
00216
00217 private:
00218 void* m_buffer;
00219 int m_capacity;
00220
00221
00222 SizedBuffer(const SizedBuffer&);
00223 SizedBuffer& operator=(const SizedBuffer&);
00224 };
00225
00226 }
00227
00228
00229 #endif