libcm is a C development framework with an emphasis on audio signal processing applications.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

cmApBuf.h 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /// \file cmApBuf.h
  2. /// \brief Thread-safe audio buffer class.
  3. ///
  4. /// This file defines an audio buffer class which handles
  5. /// buffering incoming (recording) and outgoing (playback)
  6. /// samples in a thread-safe manner.
  7. ///
  8. /// Usage example and testing code:
  9. /// See cmApBufTest() and cmAudioSysTest().
  10. /// \snippet cmApBuf.c cmApBufExample
  11. ///
  12. /// Notes on channel flags:
  13. /// Disabled channels: kChFl is cleared
  14. /// cmApBufGet()
  15. /// in - return NULL buffer pointers
  16. /// out - return NULL buffer points
  17. ///
  18. /// cmApBufUpdate()
  19. /// in - incoming samples are set to 0.
  20. /// out - outgoing samples are set to 0.
  21. ///
  22. /// Muted channels: kMuteFl is set
  23. /// cmApBufUpdate()
  24. /// in - incoming samples are set to 0.
  25. /// out - outgoing samples are set to 0.
  26. ///
  27. /// Tone channels: kToneFl is set
  28. /// cmApBufUpdate()
  29. /// in - incoming samples are filled with a 1k sine tone
  30. /// out - outgoing samples are filled with a 1k sine tone
  31. ///
  32. #ifndef cmApBuf_h
  33. #define cmApBuf_h
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. typedef cmRC_t cmAbRC_t; ///< Result code type
  38. enum
  39. {
  40. kOkAbRC = 0
  41. };
  42. /// Allocate and initialize an audio buffer.
  43. /// devCnt - count of devices this buffer will handle.
  44. /// meterMs - length of the meter buffers in milliseconds
  45. cmAbRC_t cmApBufInitialize( unsigned devCnt, unsigned meterMs );
  46. /// Deallocate and release any resource held by an audio buffer allocated via cmApBufInitialize().
  47. cmAbRC_t cmApBufFinalize();
  48. /// Configure a buffer for a given device.
  49. cmAbRC_t cmApBufSetup(
  50. unsigned devIdx, ///< device to setup
  51. double srate, ///< device sample rate (only required for synthesizing the correct test-tone frequency)
  52. unsigned dspFrameCnt, /// dspFrameCnt - count of samples in channel buffers returned via cmApBufGet()
  53. unsigned cycleCnt, ///< number of audio port cycles to store
  54. unsigned inChCnt, ///< input channel count on this device
  55. unsigned inFramesPerCycle, ///< maximum number of incoming sample frames on an audio port cycle
  56. unsigned outChCnt, ///< output channel count on this device
  57. unsigned outFramesPerCycle ///< maximum number of outgoing sample frames in an audio port cycle
  58. );
  59. // Prime the buffer with 'audioCycleCnt' * outFramesPerCycle samples ready to be played
  60. cmAbRC_t cmApBufPrimeOutput( unsigned devIdx, unsigned audioCycleCnt );
  61. /// This function is called asynchronously by the audio device driver to transfer incoming samples to the
  62. /// the buffer and to send outgoing samples to the DAC. This function is
  63. /// intended to be called from the audio port callback function (\see cmApCallbackPtr_t).
  64. /// This function is thread-safe under the condition where the audio device uses
  65. /// different threads for input and output.
  66. ///
  67. /// Enable Flag:
  68. /// Input: If an input channel is disabled then the incoming samples are replaced with zeros.
  69. /// Output: If an output channel is disabled then the packet samples are set to zeros.
  70. ///
  71. /// Tone Flag:
  72. /// Input: If the tone flag is set on an input channel then the incoming samples are set to a sine tone.
  73. /// Output: If the tone flag is set on an output channel then the packet samples are set to a sine tone.
  74. ///
  75. /// The enable flag has higher precedence than the tone flag therefore disabled channels
  76. /// will be set to zero even if the tone flag is set.
  77. cmAbRC_t cmApBufUpdate(
  78. cmApAudioPacket_t* inPktArray, ///< full audio packets from incoming audio (from ADC)
  79. unsigned inPktCnt, ///< count of incoming audio packets
  80. cmApAudioPacket_t* outPktArray, ///< empty audio packet for outgoing audio (to DAC)
  81. unsigned outPktCnt ///< count of outgoing audio packets
  82. );
  83. /// Channel flags
  84. enum
  85. {
  86. kInApFl = 0x01, ///< Identify an input channel
  87. kOutApFl = 0x02, ///< Identify an output channel
  88. kEnableApFl = 0x04, ///< Set to enable a channel, Clear to disable.
  89. kChApFl = 0x08, ///< Used to enable/disable a channel
  90. kMuteApFl = 0x10, ///< Mute this channel
  91. kToneApFl = 0x20, ///< Generate a tone on this channel
  92. kMeterApFl = 0x40, ///< Turn meter's on/off
  93. kPassApFl = 0x80 ///< Pass input channels throught to the output. Must use cmApBufGetIO() to implement this functionality.
  94. };
  95. /// Return the meter window period as set by cmApBufInitialize()
  96. unsigned cmApBufMeterMs();
  97. /// Returns the channel count set via cmApBufSetup().
  98. unsigned cmApBufChannelCount( unsigned devIdx, unsigned flags );
  99. /// Set chIdx to -1 to enable all channels on this device.
  100. /// Set flags to {kInApFl | kOutApFl} | {kChApFl | kToneApFl | kMeterFl} | { kEnableApFl=on | 0=off }
  101. void cmApBufSetFlag( unsigned devIdx, unsigned chIdx, unsigned flags );
  102. /// Return true if the the flags is set.
  103. bool cmApBufIsFlag( unsigned devIdx, unsigned chIdx, unsigned flags );
  104. /// Set chIdx to -1 to enable all channels on this device.
  105. void cmApBufEnableChannel( unsigned devIdx, unsigned chIdx, unsigned flags );
  106. /// Returns true if an input/output channel is enabled on the specified device.
  107. bool cmApBufIsChannelEnabled(unsigned devIdx, unsigned chIdx, unsigned flags );
  108. /// Set the state of the tone generator on the specified channel.
  109. /// Set chIdx to -1 to apply the change to all channels on this device.
  110. /// Set flags to {kInApFl | kOutApFl} | { kEnableApFl=on | 0=off }
  111. void cmApBufEnableTone( unsigned devIdx, unsigned chIdx, unsigned flags );
  112. /// Returns true if an input/output tone is enabled on the specified device.
  113. bool cmApBufIsToneEnabled(unsigned devIdx, unsigned chIdx, unsigned flags );
  114. /// Mute a specified channel.
  115. /// Set chIdx to -1 to apply the change to all channels on this device.
  116. /// Set flags to {kInApFl | kOutApFl} | { kEnableApFl=on | 0=off }
  117. void cmApBufEnableMute( unsigned devIdx, unsigned chIdx, unsigned flags );
  118. /// Returns true if an input/output channel is muted on the specified device.
  119. bool cmApBufIsMuteEnabled(unsigned devIdx, unsigned chIdx, unsigned flags );
  120. /// Set the specified channel to pass through.
  121. /// Set chIdx to -1 to apply the change to all channels on this device.
  122. /// Set flags to {kInApFl | kOutApFl} | { kEnableApFl=on | 0=off }
  123. void cmApBufEnablePass( unsigned devIdx, unsigned chIdx, unsigned flags );
  124. /// Returns true if pass through is enabled on the specified channel.
  125. bool cmApBufIsPassEnabled(unsigned devIdx, unsigned chIdx, unsigned flags );
  126. /// Turn meter data collection on and off.
  127. /// Set chIdx to -1 to apply the change to all channels on this device.
  128. /// Set flags to {kInApFl | kOutApFl} | { kEnableApFl=on | 0=off }
  129. void cmApBufEnableMeter( unsigned devIdx, unsigned chIdx, unsigned flags );
  130. /// Returns true if an input/output tone is enabled on the specified device.
  131. bool cmApBufIsMeterEnabled(unsigned devIdx, unsigned chIdx, unsigned flags );
  132. /// Return the meter value for the requested channel.
  133. /// Set flags to kInApFl | kOutApFl.
  134. cmApSample_t cmApBufMeter(unsigned devIdx, unsigned chIdx, unsigned flags );
  135. /// Set chIdx to -1 to apply the gain to all channels on the specified device.
  136. void cmApBufSetGain( unsigned devIdx, unsigned chIdx, unsigned flags, double gain );
  137. /// Return the current gain seting for the specified channel.
  138. double cmApBufGain( unsigned devIdx, unsigned chIdx, unsigned flags );
  139. /// Get the meter and fault status of the channel input or output channel array of a device.
  140. /// Set 'flags' to { kInApFl | kOutApFl }.
  141. /// The returns value is the count of channels actually written to meterArray.
  142. /// If 'faultCntPtr' is non-NULL then it is set to the faultCnt of the associated devices input or output buffer.
  143. unsigned cmApBufGetStatus( unsigned devIdx, unsigned flags, double* meterArray, unsigned meterCnt, unsigned* faultCntPtr );
  144. /// Do all enabled input/output channels on this device have samples available?
  145. /// 'flags' can be set to either or both kInApFl and kOutApFl
  146. bool cmApBufIsDeviceReady( unsigned devIdx, unsigned flags );
  147. /// This function is called by the application to get full incoming sample buffers and
  148. /// to fill empty outgoing sample buffers.
  149. /// Upon return each element in bufArray[bufChCnt] holds a pointer to a buffer assoicated
  150. /// with an audio channel or to NULL if the channel is disabled.
  151. /// 'flags' can be set to kInApFl or kOutApFl but not both.
  152. /// The buffers pointed to by bufArray[] each contain 'dspFrameCnt' samples. Where
  153. /// 'dspFrameCnt' was set in the earlier call to cmApBufSetup() for this device.
  154. /// (see cmApBufInitialize()).
  155. /// Note that this function just returns audio information it does not
  156. /// change any cmApBuf() internal states.
  157. void cmApBufGet( unsigned devIdx, unsigned flags, cmApSample_t* bufArray[], unsigned bufChCnt );
  158. /// This function replaces calls to cmApBufGet() and implements pass-through and output
  159. /// buffer zeroing:
  160. ///
  161. /// 1) cmApBufGet(in);
  162. /// 2) cmApBufGet(out);
  163. /// 3) Copy through channels marked for 'pass' and set the associated oBufArray[i] channel to NULL.
  164. /// 4) Zero all other enabled output channels.
  165. ///
  166. /// Notes:
  167. /// 1) The oBufArray[] channels that are disabled or marked for pass-through will
  168. /// be set to NULL.
  169. /// 2) The client is required to use this function to implement pass-through internally.
  170. /// 3) This function just returns audio information it does not
  171. /// change any cmApBuf() internal states.
  172. void cmApBufGetIO( unsigned iDevIdx, cmApSample_t* iBufArray[], unsigned iBufChCnt, unsigned oDevIdx, cmApSample_t* oBufArray[], unsigned oBufChCnt );
  173. /// The application calls this function each time it completes processing of a bufArray[]
  174. /// returned from cmApBufGet(). 'flags' can be set to either or both kInApFl and kOutApFl.
  175. /// This function should only be called from the client thread.
  176. void cmApBufAdvance( unsigned devIdx, unsigned flags );
  177. /// Copy all available samples incoming samples from an input device to an output device.
  178. /// The source code for this example is a good example of how an application should use cmApBufGet()
  179. /// and cmApBufAdvance().
  180. void cmApBufInputToOutput( unsigned inDevIdx, unsigned outDevIdx );
  181. /// Print the current buffer state.
  182. void cmApBufReport( cmRpt_t* rpt );
  183. /// Run a buffer usage simulation to test the class. cmAudioPortTest.c calls this function.
  184. void cmApBufTest( cmRpt_t* rpt );
  185. #ifdef __cplusplus
  186. }
  187. #endif
  188. #endif