sample.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  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 along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. ///////////////////////////////////////////////////////////////////////////////
  19. //
  20. // sample.cpp
  21. //
  22. // History:
  23. // 06/23/95 JMI Started.
  24. //
  25. // 01/28/97 JMI Added Load(RFile*), Save(char*), and Save(RFile*).
  26. //
  27. // 05/09/97 JMI Added GetDuration(), GetTime(), and GetPos() functions.
  28. //
  29. //////////////////////////////////////////////////////////////////////////////
  30. //
  31. // See CPP for description of this API.
  32. //
  33. //////////////////////////////////////////////////////////////////////////////
  34. #ifndef SAMPLE_H
  35. #define SAMPLE_H
  36. //////////////////////////////////////////////////////////////////////////////
  37. // Headers.
  38. //////////////////////////////////////////////////////////////////////////////
  39. #include "System.h"
  40. // If PATHS_IN_INCLUDES macro is defined, we can utilized relative
  41. // paths to a header file. In this case we generally go off of our
  42. // RSPiX root directory. System.h MUST be included before this macro
  43. // is evaluated. System.h is the header that, based on the current
  44. // platform (or more so in this case on the compiler), defines
  45. // PATHS_IN_INCLUDES. Blue.h includes system.h so you can include that
  46. // instead.
  47. #ifdef PATHS_IN_INCLUDES
  48. #include "ORANGE/IFF/iff.h"
  49. #else
  50. #include "iff.h"
  51. #endif // PATHS_IN_INCLUDES
  52. //////////////////////////////////////////////////////////////////////////////
  53. // Macros.
  54. //////////////////////////////////////////////////////////////////////////////
  55. //////////////////////////////////////////////////////////////////////////////
  56. // Types.
  57. //////////////////////////////////////////////////////////////////////////////
  58. // Forward declare RSample for typedef.
  59. class RSample;
  60. // Handy-dandy typedef.
  61. typedef RSample* PSAMPLE;
  62. class RSample
  63. {
  64. public:
  65. // Default constructor.
  66. RSample();
  67. // Constructor especial.
  68. RSample( void *pData, long lBufSize,
  69. long lSamplesPerSec, short sBitsPerSample, short sNumChannels);
  70. // Destructor.
  71. ~RSample();
  72. ////////////////////////// Querries ///////////////////////////////////////
  73. public:
  74. // Returns the reference count. Non-zero == locked, Zero == not locked,
  75. // Negative == fucked.
  76. short IsLocked(void) { return m_sRefCnt; }
  77. // Get the duration of this sample in milliseconds.
  78. long GetDuration(void)
  79. {
  80. return GetTime(m_lBufSize);
  81. }
  82. // Get the time in milliseconds indicated by the specified byte position
  83. // in the sample.
  84. long GetTime(
  85. long lPos) // In: Position in bytes.
  86. {
  87. return lPos / (m_lSamplesPerSec * m_sNumChannels * m_sBitsPerSample / 8000);
  88. }
  89. // Get the position in bytes indicated by the specified time in milliseconds
  90. // in the sample.
  91. long GetPos(
  92. long lTime) // In: Time in milliseconds.
  93. {
  94. return lTime * (m_lSamplesPerSec * m_sNumChannels * m_sBitsPerSample / 8000);
  95. }
  96. ////////////////////////// Methods ////////////////////////////////////////
  97. public:
  98. // Reset object. Release sample data and reset variables.
  99. void Reset(void);
  100. // Open a file and read the header. Locks the RSample automatically.
  101. // Returns the size of the file's data on success, negative otherwise.
  102. long Open(char* pszSampleName, long lReadBufSize);
  103. // Read the specified amount of data from the open file.
  104. // Returns amount read on success, negative on failure.
  105. long Read(long lAmount);
  106. // Close the file opened with Open. Unlocks the RSample automatically.
  107. // Returns 0 on success.
  108. short Close(void);
  109. // Load an entire sample from a file.
  110. // Returns 0 on success.
  111. short Load(char* pszSampleName);
  112. // Same as above, but accepts an open RFile*.
  113. short Load( // Returns 0 on success.
  114. RFile* pfile); // Open RFile.
  115. // Saves entire sample to file specified in RIFF WAVE format.
  116. short Save( // Returns 0 on success.
  117. char* pszFileName); // Filename for sample file.
  118. // Saves entire sample to file specified in RIFF WAVE format.
  119. short Save( // Returns 0 on success.
  120. RFile* pfile); // Open RFile for sample.
  121. // Lock this sample for use. Returns 0 on success.
  122. short Lock(void);
  123. // Unlock this sample from use. Returns new reference count.
  124. short Unlock(void);
  125. // Convert current sample data from 8 bit unsigned to 16 bit signed.
  126. // Returns 0 on success.
  127. short Convert8to16(void);
  128. ////////////////////////// Internal Methods ///////////////////////////////
  129. protected:
  130. // Initialize instantiable members.
  131. void Init(void);
  132. // Read WAVE info from m_file.
  133. // Returns size of sample data on success, negative on error.
  134. long ReadWaveHeader(void);
  135. ////////////////////////// Member vars ////////////////////////////////////
  136. public:
  137. long m_lBufSize; // Amount of data in CSnd.
  138. long m_lSamplesPerSec; // Rate of data in buffer.
  139. short m_sNumChannels; // Number of channels (i.e., 1==mono,
  140. // 2==stereo).
  141. short m_sBitsPerSample; // Number of bits per sample.
  142. void* m_pData; // Buffer used for sample data.
  143. protected:
  144. short m_sOwnBuf; // TRUE if play buffer allocated by RSample.
  145. short m_sRefCnt; // Reference count of locks on this sample's
  146. // data.
  147. RIff m_iff; // File stream pointer for reading/streaming.
  148. /////////////////////// Static members /////////////////////////////////
  149. };
  150. #endif // SAMPLE_H
  151. //////////////////////////////////////////////////////////////////////////////
  152. // EOF
  153. //////////////////////////////////////////////////////////////////////////////