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.

96 lines
2.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. #include <string>
  19. #ifndef WAVFILE
  20. #define WAVFILE
  21. #include <stdio.h>
  22. #include "Sample.h"
  23. #if __APPLE__
  24. // this is the traditional way of setting 2 bytes alignment
  25. // else the apple compiler might use 4, or even 8
  26. #pragma options align=mac68k
  27. #endif
  28. struct CanonicalWavHeader
  29. {
  30. char RiffName[4];
  31. int RiffFileLength;
  32. char RiffTypeName[4];
  33. char FmtName[4];
  34. int FmtLength;
  35. short FmtTag;
  36. short FmtChannels;
  37. int FmtSamplerate;
  38. int FmtBytesPerSec;
  39. short FmtBlockAlign;
  40. short FmtBitsPerSample;
  41. };
  42. struct DataHeader
  43. {
  44. char DataName[4];
  45. int DataLengthBytes;
  46. };
  47. #if __APPLE__
  48. #pragma options align=reset
  49. #endif
  50. class WavFile
  51. {
  52. public:
  53. WavFile() : m_Stream(NULL), m_Samplerate(44100), m_DataStart(0) {}
  54. ~WavFile() {Close();}
  55. enum Mode{READ,WRITE};
  56. enum Channels{MONO,STEREO};
  57. int Open(string FileName, Mode mode, Channels channels=MONO);
  58. int Close();
  59. int Save(Sample &data);
  60. int Load(Sample &data);
  61. int Save(short *data, int Bytes);
  62. int Load(short *data);
  63. int SeekToChunk(int Pos);
  64. int LoadChunk(int NumSamples, Sample &ldata, Sample &rdata);
  65. int GetSize(); // in samples
  66. bool Recording() {return (m_Stream!=NULL);}
  67. void SetSamplerate(int s) { m_Samplerate=s; }
  68. int GetSamplerate() { return m_Header.FmtSamplerate; }
  69. bool IsStereo() { return (m_Header.FmtChannels==2); }
  70. bool IsOpen() { return m_Stream!=NULL; }
  71. private:
  72. FILE *m_Stream;
  73. int m_Samplerate;
  74. long m_DataStart;
  75. long m_CurSeekPos;
  76. CanonicalWavHeader m_Header;
  77. DataHeader m_DataHeader;
  78. };
  79. #endif