SoundSampleQueue.hpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #ifndef MUMSI_SOUNDSAMPLEQUEUE_HPP
  2. #define MUMSI_SOUNDSAMPLEQUEUE_HPP
  3. #include <mutex>
  4. #include <memory>
  5. #include <cstring>
  6. #include <algorithm>
  7. template<typename SAMPLE_TYPE>
  8. class SoundSampleQueue {
  9. public:
  10. SoundSampleQueue() {
  11. start = 0;
  12. stop = 0;
  13. buffer = new SAMPLE_TYPE[10000000];
  14. }
  15. ~SoundSampleQueue() {
  16. delete[] buffer;
  17. }
  18. void push(SAMPLE_TYPE *data, int length) {
  19. std::lock_guard<std::mutex> lock(accessMutex);
  20. std::memcpy(&buffer[stop], data, length);
  21. stop += length;
  22. // printf("pos: %d\n", stop);
  23. }
  24. int pop(SAMPLE_TYPE *data, int maxLength) {
  25. std::lock_guard<std::mutex> lock(accessMutex);
  26. int samplesToTake = std::min(stop - start, maxLength);
  27. std::memcpy(data, &buffer[stop - samplesToTake], samplesToTake);
  28. stop -= samplesToTake;
  29. //todo maksymalna pojemność bufora
  30. return samplesToTake;
  31. }
  32. private:
  33. int start;
  34. int stop;
  35. std::mutex accessMutex;
  36. SAMPLE_TYPE *buffer;
  37. };
  38. #endif //MUMSI_SOUNDSAMPLEQUEUE_HPP