/Android/FrequencyDomain/app/src/main/jni/SuperpoweredAudioBuffers.h

https://gitlab.com/kidaa/Low-Latency-Android-Audio-iOS-Audio-Engine · C Header · 194 lines · 51 code · 14 blank · 129 comment · 0 complexity · 0d0c6aedd2fd6c461d7663ce367b4e91 MD5 · raw file

  1. #ifndef Header_SuperpoweredAudioBuffers
  2. #define Header_SuperpoweredAudioBuffers
  3. struct bufferPoolInternals;
  4. struct pointerListInternals;
  5. struct SuperpoweredAudiobufferlistElement;
  6. /**
  7. @brief This object manages an audio buffer pool.
  8. It reduces the number of memory allocation requests, increasing efficiency of audio applications.
  9. Check the SuperpoweredOfflineProcessingExample project on how to use.
  10. */
  11. class SuperpoweredAudiobufferPool {
  12. public:
  13. /**
  14. @brief Creates a buffer pool.
  15. @param bytesPerSample Sample size. For example: 2 for 16-bit, 4 for 32-bit audio.
  16. @param optimalCapacityBytes The optimal memory usage of the entire pool.
  17. @param freeUnusedSeconds If a buffer is unused, how many seconds to wait with freeing the memory used.
  18. */
  19. SuperpoweredAudiobufferPool(unsigned char bytesPerSample, int optimalCapacityBytes, int freeUnusedSeconds = 1);
  20. ~SuperpoweredAudiobufferPool();
  21. /**
  22. @brief Creates a buffer, with 1 retain count.
  23. @return The buffer's identifier.
  24. @param sizeInSamples The buffer's size in samples, for 2 channels. For example, if you need a buffer for 512 samples of stereo audio, pass 512.
  25. */
  26. unsigned int createBuffer(unsigned int sizeInSamples);
  27. /**
  28. @brief Release a buffer, similar to Objective-C.
  29. */
  30. void releaseBuffer(SuperpoweredAudiobufferlistElement *buffer);
  31. /**
  32. @brief Retains a buffer, similar to Objective-C.
  33. */
  34. void retainBuffer(SuperpoweredAudiobufferlistElement *buffer);
  35. /**
  36. @brief Returns with a pointer to the buffer's storage.
  37. */
  38. short int *int16Audio(SuperpoweredAudiobufferlistElement *buffer);
  39. /**
  40. @brief Returns with a pointer to the buffer's storage.
  41. */
  42. float *floatAudio(SuperpoweredAudiobufferlistElement *buffer);
  43. /**
  44. @brief Creates a buffer to be placed into a buffer list.
  45. @see SuperpoweredAudiobufferlistElement
  46. */
  47. bool createSuperpoweredAudiobufferlistElement(SuperpoweredAudiobufferlistElement *item, unsigned int samplePosition, unsigned int sizeInSamples);
  48. private:
  49. bufferPoolInternals *internals;
  50. SuperpoweredAudiobufferPool(const SuperpoweredAudiobufferPool&);
  51. SuperpoweredAudiobufferPool& operator=(const SuperpoweredAudiobufferPool&);
  52. };
  53. /**
  54. @brief This object manages an audio buffer list.
  55. Instead of circular buffers and too many memmove/memcpy, this object maintains an audio buffer "chain". You can append, insert, truncate, slice, extend, etc. this without the expensive memory operations.
  56. Check the SuperpoweredOfflineProcessingExample project on how to use.
  57. @param sampleLength The number of samples inside this list.
  58. */
  59. class SuperpoweredAudiopointerList {
  60. public:
  61. int sampleLength;
  62. /**
  63. @brief Creates an audio buffer list.
  64. @param bufPool The buffer pool, which will manage the memory allocation.
  65. */
  66. SuperpoweredAudiopointerList(SuperpoweredAudiobufferPool *bufPool);
  67. ~SuperpoweredAudiopointerList();
  68. /**
  69. @brief Append a buffer to the end of the list.
  70. */
  71. void append(SuperpoweredAudiobufferlistElement *buffer);
  72. /**
  73. @brief Insert a buffer before the beginning of the list.
  74. */
  75. void insert(SuperpoweredAudiobufferlistElement *buffer);
  76. /**
  77. @brief Sets the last samples in the list to 1.0f, good for debugging purposes.
  78. */
  79. void markLastSamples();
  80. /**
  81. @brief Remove everything from the list.
  82. */
  83. void clear();
  84. /**
  85. @brief Appends all buffers to another buffer list.
  86. */
  87. void copyAllBuffersTo(SuperpoweredAudiopointerList *anotherList);
  88. /**
  89. @brief Removes samples from the beginning or the end.
  90. @param numSamples The number of samples to remove.
  91. @param fromTheBeginning From the end or the beginning.
  92. */
  93. void truncate(int numSamples, bool fromTheBeginning);
  94. /**
  95. @brief Returns the buffer list beginning's sample position in an audio file or stream.
  96. */
  97. int startSamplePosition();
  98. /**
  99. @brief Returns the buffer list end's sample position in an audio file or stream, plus 1.
  100. */
  101. int nextSamplePosition();
  102. /**
  103. @brief Creates a "virtual slice" from this list.
  104. @param fromSample The slice will start from this sample.
  105. @param lengthSamples The slice will contain this number of samples.
  106. */
  107. bool makeSlice(int fromSample, int lengthSamples);
  108. /**
  109. @brief Returns the slice beginning's sample position in an audio file or stream.
  110. */
  111. int samplePositionOfSliceBeginning();
  112. /**
  113. @brief This the slice's forward enumerator method to go through all buffers in it.
  114. @param audio The pointer to the audio.
  115. @param lengthSamples Returns with the number of samples in audio.
  116. @param samplesUsed Returns with the number of original number of samples, creating this chunk of audio. Good for time-stretching for example, to track the movement of the playhead.
  117. */
  118. bool nextSliceItem(float **audio, int *lengthSamples, float *samplesUsed = 0);
  119. /**
  120. @brief This the slice's forward enumerator method to go through all buffers in it.
  121. @param audio The pointer to the audio.
  122. @param lengthSamples Returns with the number of samples in audio.
  123. @param samplesUsed Returns with the number of original number of samples, creating this chunk of audio. Good for time-stretching for example, to track the movement of the playhead.
  124. */
  125. bool nextSliceItem(short int **audio, int *lengthSamples, float *samplesUsed = 0);
  126. /**
  127. @brief Returns the slice enumerator to the first buffer.
  128. */
  129. void rewindSlice();
  130. /**
  131. @brief Jumps the enumerator to the last buffer.
  132. */
  133. void forwardToLastSliceBuffer();
  134. /**
  135. @brief This the slice's backwards (reverse) enumerator method to go through all buffers in it.
  136. @param audio The pointer to the audio.
  137. @param lengthSamples Returns with the number of samples in audio.
  138. @param samplesUsed Returns with the number of original number of samples, creating this chunk of audio. Good for time-stretching for example, to track the movement of the playhead.
  139. */
  140. bool prevSliceItem(float **audio, int *lengthSamples, float *samplesUsed = 0);
  141. /**
  142. @brief This the slice's backwards (reverse) enumerator method to go through all buffers in it.
  143. @param audio The pointer to the audio.
  144. @param lengthSamples Returns with the number of samples in audio.
  145. @param samplesUsed Returns with the number of original number of samples, creating this chunk of audio. Good for time-stretching for example, to track the movement of the playhead.
  146. */
  147. bool prevSliceItem(short int **audio, int *lengthSamples, float *samplesUsed = 0);
  148. private:
  149. pointerListInternals *internals;
  150. SuperpoweredAudiopointerList(const SuperpoweredAudiopointerList&);
  151. SuperpoweredAudiopointerList& operator=(const SuperpoweredAudiopointerList&);
  152. };
  153. /**
  154. @brief An audio buffer list item.
  155. @param bufferID The buffer's identifier in a buffer pool.
  156. @param startSample The first sample in the buffer.
  157. @param endSample The last sample in the buffer.
  158. @param samplePosition The buffer beginning's sample position in an audio file or stream.
  159. @param samplesUsed How many "original" samples were used to create this chunk of audio. Good for time-stretching for example, to track the movement of the playhead.
  160. */
  161. typedef struct SuperpoweredAudiobufferlistElement {
  162. int bufferID, startSample, endSample, samplePosition;
  163. float samplesUsed;
  164. } SuperpoweredAudiobufferlistElement;
  165. #endif