You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
3.3KB

  1. /* SpiralSynth
  2. * Copyleft (C) 2000 David Griffiths <dave@pawfal.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #ifndef WAVFILE
  19. #define WAVFILE
  20. #include <string>
  21. #include <stdio.h>
  22. #include "Sample.h"
  23. #include "../config.h"
  24. #ifdef USE_LIBSNDFILE
  25. #include <sndfile.h>
  26. #endif
  27. #ifndef USE_LIBSNDFILE
  28. #if __APPLE__
  29. // this is the traditional way of setting 2 bytes alignment
  30. // else the apple compiler might use 4, or even 8
  31. #pragma options align=mac68k
  32. #endif
  33. struct CanonicalWavHeader
  34. {
  35. char RiffName[4];
  36. int RiffFileLength;
  37. char RiffTypeName[4];
  38. char FmtName[4];
  39. int FmtLength;
  40. short FmtTag;
  41. short FmtChannels;
  42. int FmtSamplerate;
  43. int FmtBytesPerSec;
  44. short FmtBlockAlign;
  45. short FmtBitsPerSample;
  46. };
  47. struct DataHeader
  48. {
  49. char DataName[4];
  50. int DataLengthBytes;
  51. };
  52. #if __APPLE__
  53. #pragma options align=reset
  54. #endif
  55. #endif
  56. class WavFile
  57. {
  58. public:
  59. #ifdef USE_LIBSNDFILE
  60. WavFile() : m_FileHandle(NULL), m_BitsPerSample(16), m_CurSeekPos(0) {m_FileInfo.samplerate = 44100; m_FileInfo.format = 0;}
  61. #else
  62. WavFile() : m_Stream(NULL), m_Samplerate(44100), m_BitsPerSample(16), m_DataStart(0) {}
  63. #endif
  64. ~WavFile() {Close();}
  65. enum Mode{READ,WRITE};
  66. enum Channels{MONO,STEREO};
  67. int Open(std::string FileName, Mode mode=READ, Channels channels=MONO);
  68. int Close();
  69. int Save(Sample &data);
  70. int Save(short *data, int Bytes);
  71. int Save(float *left, float *right, int Length);
  72. int Load(Sample &data);
  73. int Load(short *data);
  74. int SeekToChunk(int Pos);
  75. int LoadChunk(int NumSamples, Sample &ldata, Sample &rdata);
  76. #ifdef USE_LIBSNDFILE
  77. int GetSize() {return m_FileInfo.frames;} // in samples
  78. void SetSamplerate(int s) { m_FileInfo.samplerate=s; }
  79. int GetSamplerate() { return m_FileInfo.samplerate; }
  80. void SetBitsPerSample(int s) { m_BitsPerSample=s; }
  81. int GetBitsPerSample() { return m_BitsPerSample; }
  82. bool IsStereo() { return (m_FileInfo.channels > 1); }
  83. bool IsOpen() { return m_FileHandle!=NULL; }
  84. bool Recording() {return (m_FileHandle!=NULL);}
  85. #else
  86. int GetSize(); // in samples
  87. void SetSamplerate(int s) { m_Samplerate=s; }
  88. int GetSamplerate() { return m_Header.FmtSamplerate; }
  89. void SetBitsPerSample(int s) { m_BitsPerSample=s; }
  90. int GetBitsPerSample() { return m_Header.FmtBitsPerSample; }
  91. bool IsStereo() { return (m_Header.FmtChannels==2); }
  92. bool IsOpen() { return m_Stream!=NULL; }
  93. bool Recording() {return (m_Stream!=NULL);}
  94. #endif
  95. private:
  96. #ifdef USE_LIBSNDFILE
  97. SNDFILE *m_FileHandle;
  98. SF_INFO m_FileInfo;
  99. int m_BitsPerSample;
  100. sf_count_t m_CurSeekPos;
  101. #else
  102. FILE *m_Stream;
  103. int m_Samplerate;
  104. int m_BitsPerSample;
  105. long m_DataStart;
  106. long m_CurSeekPos;
  107. CanonicalWavHeader m_Header;
  108. DataHeader m_DataHeader;
  109. #endif
  110. };
  111. #endif