00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _FM_GENERIC_H_
00019 #define _FM_GENERIC_H_
00020
00021
00035 #include <wx/wx.h>
00036 #include <wx/file.h>
00037
00038
00042 #define FM_NAME "FinalMix"
00043 #define FM_VENDOR "MD"
00044 #define FM_NAME_UPPER "FINALMIX"
00045 #define FM_VERSION_STRING "0.9.5"
00046
00047 #define FM_PEAK_TAG "FMPE"
00048 #define FM_PEAK_EXT "fpe"
00049
00050 #define FM_CLIP_TAG "FMCL"
00051 #define FM_CLIP_NAME "clipboard"
00052 #define FM_CLIP_EXT "fcl"
00053 #define FM_CLIP_SUFFIX "-" FM_VERSION_STRING
00054
00055 #define FM_PROJECT_TAG "FMPR"
00056 #define FM_PROJECT_EXT "fpr"
00057
00058 #define FULL_PERCENT 100
00059
00060
00064 enum exception_t {
00065 EXC_WARN = 0,
00066 EXC_RUNTIME,
00067 EXC_ASSERT,
00068 EXC_FATAL
00069 };
00070
00071
00075 class Exception {
00076 public:
00077 Exception(const enum exception_t severity);
00078 Exception(const enum exception_t severity, const wxString desc);
00080 enum exception_t GetSeverity() const;
00081 wxString GetDesc() const;
00082 private:
00083 enum exception_t sev;
00084 wxString dsc;
00085 };
00086
00087
00093 class ExceptionZero : public Exception {
00094 public:
00095 ExceptionZero(const wxString desc);
00096 };
00097
00098
00103 class ExceptionIO : public Exception {
00104 public:
00105 ExceptionIO();
00106 };
00107
00108
00112 enum status_t {
00113 OK = 0,
00114 IO_ERROR = -1,
00115 UNK_ERROR = -2,
00116 UNK_FORMAT = -3,
00117 STRUCT_ERR = -4,
00118 UNS_FORMAT = -5
00119 };
00120
00121
00125 enum encoding_t {
00126 PCM = 0
00127 };
00128
00129
00133 enum bitencoding_t {
00134 UNSIGNED = 0,
00135 SIGNED
00136 };
00137
00138
00142 enum samplewidth_t {
00143 BIT16 = 0,
00144 BIT8
00145 };
00146
00147
00153 enum endianess_t {
00154 LITTLE = 0,
00155 BIG
00156 };
00157
00158
00159 typedef wxUint8 volume_t;
00161 extern const volume_t volume_min;
00162 extern const volume_t volume_max;
00165 typedef wxUint8 sample_pcm8u_t;
00166 typedef wxInt8 sample_pcm8s_t;
00167 typedef wxUint16 sample_pcm16u_t;
00168 typedef wxInt16 sample_pcm16s_t;
00170 typedef wxUint32 sample_pcm8u_w_t;
00171 typedef wxInt32 sample_pcm8s_w_t;
00172 typedef wxUint64 sample_pcm16u_w_t;
00173 typedef wxInt64 sample_pcm16s_w_t;
00175 typedef wxFloat64 floatsample_t;
00178 typedef wxUint32 samplerate_t;
00179 typedef wxUint64 samples_t;
00185 typedef struct {
00186 wxUint8 bytes[10];
00187 } wxFloat80;
00193 extern const sample_pcm8u_t sample_pcm8u_min;
00194 extern const sample_pcm8u_t sample_pcm8u_max;
00195 extern const sample_pcm8u_t sample_pcm8u_0;
00196
00197 extern const sample_pcm8s_t sample_pcm8s_min;
00198 extern const sample_pcm8s_t sample_pcm8s_max;
00199 extern const sample_pcm8s_t sample_pcm8s_0;
00200
00201 extern const sample_pcm16u_t sample_pcm16u_min;
00202 extern const sample_pcm16u_t sample_pcm16u_max;
00203 extern const sample_pcm16u_t sample_pcm16u_0;
00204
00205 extern const sample_pcm16s_t sample_pcm16s_min;
00206 extern const sample_pcm16s_t sample_pcm16s_max;
00207 extern const sample_pcm16s_t sample_pcm16s_0;
00208
00209
00210 typedef wxUint16 version_t;
00212 extern const version_t fmfile_version_major;
00213 extern const version_t fmfile_version_minor;
00216 typedef wxUint64 length_t;
00217 typedef wxUint16 track_t;
00218 typedef wxUint32 ratio_t;
00219 typedef wxUint8 bool_t;
00225 typedef struct {
00226 enum encoding_t encoding;
00227 enum bitencoding_t bitencoding;
00228 enum endianess_t endianess;
00229 enum samplewidth_t samplewidth;
00230 samplerate_t rate;
00231 samples_t samples;
00232 } format_t;
00233
00234
00238 typedef struct {
00239 enum encoding_t encoding;
00240 enum bitencoding_t bitencoding;
00241 enum endianess_t endianess;
00242 enum samplewidth_t samplewidth;
00243 samplerate_t rate;
00244 track_t channels;
00245 } format_ex_t;
00246
00247
00255 #if defined(__WXMSW__)
00256 # define __FUNCTION__ "unknown"
00257 # define __MSW__INT64_BUG__ (signed __int64)
00258 # define __MSW__DIRDLG_STYLE__ wxDD_NEW_DIR_BUTTON
00259 #else
00260 # define __MSW__INT64_BUG__
00261 # define __MSW__DIRDLG_STYLE__ 0
00262 #endif
00263
00264
00275 #define ASSERT(expr) if (!(expr)) { throw Exception(EXC_ASSERT, wxString::Format("Assertion failed: %s (in %s() at %s on line %d)", #expr, __FUNCTION__, __FILE__, __LINE__)); }
00276
00277
00285 #define FATAL(desc) { throw Exception(EXC_FATAL, wxString::Format("Fatal exception: %s (in %s() at %s on line %d)", desc, __FUNCTION__, __FILE__, __LINE__)); }
00286
00287
00288 wxString SetFilename(const wxString path, const wxString name, const wxString ext, const wxString fsuffix);
00291 size_t SampleLen(const format_t & format);
00292 size_t SampleLen(const format_ex_t & format);
00294 void * AllocSamples(const format_t & format, const samples_t count);
00295 void * AllocSamples(const format_ex_t & format, const samples_t count);
00296 void FreeSamples(const format_t & format, const void * data);
00297 void FreeSamples(const format_ex_t & format, const void * data);
00299 void SamplesAverage(const format_t & format, const samples_t samples, const void * inbuf, const void * outbuf);
00300 void SamplesMax(const format_t & format, const samples_t samples, const void * inbuf, const void * outbuf);
00301
00302 floatsample_t NormSample(const format_t & format, const void * sample, const floatsample_t max);
00303 floatsample_t SamplesAverageNorm(const format_t & format, const samples_t samples, const void * inbuf, const floatsample_t max);
00304 floatsample_t SamplesMaxNorm(const format_t & format, const samples_t samples, const void * inbuf, const floatsample_t max);
00306 samples_t SamplesCmp(const format_t & infmt, const format_t & outfmt, const samples_t samples);
00307 samples_t SamplesCmpIn(const format_t & infmt, const format_t & outfmt, const samples_t insamples);
00308 samples_t SamplesConv(const format_t & infmt, const format_ex_t & outfmt, const samples_t samples, void * inbuf, void * outbuf);
00310 void ClearSamples(const format_ex_t & format, const samples_t samples, void * buf);
00311 bool AddSamples(const format_ex_t & format, const volume_t volume[], const samples_t tsamples, const samples_t samples, const void * tbuf, const void * buf);
00313 void FormatAlias(const format_ex_t & infmt, format_t & outfmt);
00327 inline void * GetSample(const format_t & format, const samples_t pos, const void * samples) {
00328 switch (format.encoding) {
00329 case PCM:
00330 switch (format.bitencoding) {
00331 case UNSIGNED:
00332 switch (format.samplewidth) {
00333 case BIT8:
00334 return (sample_pcm8u_t * ) samples + pos;
00335 case BIT16:
00336 return (sample_pcm16u_t * ) samples + pos;
00337 default:
00338 FATAL("Undefined format.samplewidth");
00339 }
00340 break;
00341 case SIGNED:
00342 switch (format.samplewidth) {
00343 case BIT8:
00344 return (sample_pcm8s_t * ) samples + pos;
00345 case BIT16:
00346 return (sample_pcm16s_t * ) samples + pos;
00347 default:
00348 FATAL("Undefined format.samplewidth");
00349 }
00350 break;
00351 default:
00352 FATAL("Undefined format.bitencoding");
00353 }
00354 default:
00355 FATAL("Undefined format.encoding");
00356 }
00357
00358 return NULL;
00359 }
00360
00361
00374 inline void * GetSample(const format_ex_t & format, const track_t chan, const samples_t pos, const void * samples) {
00375 switch (format.encoding) {
00376 case PCM:
00377 switch (format.bitencoding) {
00378 case UNSIGNED:
00379 switch (format.samplewidth) {
00380 case BIT8:
00381 return (sample_pcm8u_t * ) samples + pos * format.channels + chan;
00382 case BIT16:
00383 return (sample_pcm16u_t * ) samples + pos * format.channels + chan;
00384 default:
00385 FATAL("Undefined format.samplewidth");
00386 }
00387 break;
00388 case SIGNED:
00389 switch (format.samplewidth) {
00390 case BIT8:
00391 return (sample_pcm8s_t * ) samples + pos * format.channels + chan;
00392 case BIT16:
00393 return (sample_pcm16s_t * ) samples + pos * format.channels + chan;
00394 default:
00395 FATAL("Undefined format.samplewidth");
00396 }
00397 break;
00398 default:
00399 FATAL("Undefined format.bitencoding");
00400 }
00401 default:
00402 FATAL("Undefined format.encoding");
00403 }
00404
00405 return NULL;
00406 }
00407
00408
00419 inline void ReadValue(wxFile & file, void * buf, size_t size) {
00420 size_t ret;
00421
00422 ret = file.Read(buf, size);
00423 if (ret != size)
00424 throw ExceptionIO();
00425 }
00426
00427
00438 inline void ReadTag(wxFile & file, char * str, size_t size) {
00439 ReadValue(file, str, sizeof(char) * size);
00440 str[size] = 0;
00441 }
00442
00443
00455 inline void ReadString(wxFile & file, wxString & str) {
00456 wxUint64 len;
00457 char * tmp;
00458
00459 ReadValue(file, &len, sizeof(len));
00460 tmp = new char[len];
00461 ReadValue(file, tmp, sizeof(char) * len);
00462 tmp[len] = 0;
00463 str = tmp;
00464 delete[] tmp;
00465 }
00466
00467
00478 inline void WriteValue(wxFile & file, const void * buf, const size_t size) {
00479 size_t ret;
00480
00481 ret = file.Write(buf, size);
00482 if (ret != size)
00483 throw ExceptionIO();
00484 }
00485
00486
00496 inline void WriteTag(wxFile & file, const char * str) {
00497 off_t len = strlen(str);
00498
00499 WriteValue(file, str, sizeof(char) * len);
00500 }
00501
00502
00514 inline void WriteString(wxFile & file, const char * str) {
00515 wxUint64 len = strlen(str);
00516
00517 WriteValue(file, &len, sizeof(len));
00518 WriteValue(file, str, sizeof(char) * len);
00519 }
00520
00521
00531 inline void Seek(wxFile & file, off_t offset) {
00532 if (file.Seek(offset) == wxInvalidOffset)
00533 ExceptionIO();
00534 }
00535
00536
00547 template< class T > inline T min(const T a, const T b) {
00548 if (a < b)
00549 return a;
00550 else
00551 return b;
00552 }
00553
00554
00565 template< class T > inline T max(const T a, const T b) {
00566 if (a > b)
00567 return a;
00568 else
00569 return b;
00570 }
00571
00572 #endif