BellDSP.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #pragma once
  2. #include <stddef.h> // for size_t
  3. #include <stdint.h> // for uint32_t, uint8_t
  4. #include <functional> // for function
  5. #include <memory> // for shared_ptr, unique_ptr
  6. #include <mutex> // for mutex
  7. #include <vector> // for vector
  8. #include "StreamInfo.h" // for BitWidth
  9. namespace bell {
  10. class AudioPipeline;
  11. class CentralAudioBuffer;
  12. #define MAX_INT16 32767
  13. class BellDSP {
  14. public:
  15. BellDSP(std::shared_ptr<CentralAudioBuffer> centralAudioBuffer);
  16. ~BellDSP(){};
  17. class AudioEffect {
  18. public:
  19. AudioEffect() = default;
  20. ~AudioEffect() = default;
  21. size_t duration;
  22. virtual void apply(float* sampleData, size_t samples,
  23. size_t relativePosition) = 0;
  24. };
  25. class FadeEffect : public AudioEffect {
  26. private:
  27. std::function<void()> onFinish;
  28. bool isFadeIn;
  29. public:
  30. FadeEffect(size_t duration, bool isFadeIn,
  31. std::function<void()> onFinish = nullptr);
  32. ~FadeEffect(){};
  33. void apply(float* sampleData, size_t samples, size_t relativePosition);
  34. };
  35. void applyPipeline(std::shared_ptr<AudioPipeline> pipeline);
  36. void queryInstantEffect(std::unique_ptr<AudioEffect> instantEffect);
  37. std::shared_ptr<AudioPipeline> getActivePipeline();
  38. size_t process(uint8_t* data, size_t bytes, int channels, uint32_t sampleRate,
  39. BitWidth bitWidth);
  40. private:
  41. std::shared_ptr<AudioPipeline> activePipeline;
  42. std::shared_ptr<CentralAudioBuffer> buffer;
  43. std::mutex accessMutex;
  44. std::vector<float> dataLeft = std::vector<float>(1024);
  45. std::vector<float> dataRight = std::vector<float>(1024);
  46. std::unique_ptr<AudioEffect> underflowEffect = nullptr;
  47. std::unique_ptr<AudioEffect> startEffect = nullptr;
  48. std::unique_ptr<AudioEffect> instantEffect = nullptr;
  49. size_t samplesSinceInstantQueued;
  50. };
  51. }; // namespace bell