2
0

BellDSP.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "BellDSP.h"
  2. #include <type_traits> // for remove_extent_t
  3. #include <utility> // for move
  4. #include "AudioPipeline.h" // for CentralAudioBuffer
  5. #include "CentralAudioBuffer.h" // for CentralAudioBuffer
  6. using namespace bell;
  7. BellDSP::FadeEffect::FadeEffect(size_t duration, bool isFadeIn,
  8. std::function<void()> onFinish) {
  9. this->duration = duration;
  10. this->onFinish = onFinish;
  11. this->isFadeIn = isFadeIn;
  12. }
  13. void BellDSP::FadeEffect::apply(float* audioData, size_t samples,
  14. size_t relativePosition) {
  15. float effect = (this->duration - relativePosition) / (float)this->duration;
  16. if (isFadeIn) {
  17. effect = relativePosition / (float)this->duration;
  18. }
  19. for (int x = 0; x <= samples; x++) {
  20. audioData[x] *= effect;
  21. }
  22. if (relativePosition + samples > this->duration && onFinish != nullptr) {
  23. onFinish();
  24. }
  25. }
  26. BellDSP::BellDSP(std::shared_ptr<CentralAudioBuffer> buffer) {
  27. this->buffer = buffer;
  28. };
  29. void BellDSP::applyPipeline(std::shared_ptr<AudioPipeline> pipeline) {
  30. std::scoped_lock lock(accessMutex);
  31. activePipeline = pipeline;
  32. }
  33. void BellDSP::queryInstantEffect(std::unique_ptr<AudioEffect> instantEffect) {
  34. this->instantEffect = std::move(instantEffect);
  35. samplesSinceInstantQueued = 0;
  36. }
  37. size_t BellDSP::process(uint8_t* data, size_t bytes, int channels,
  38. uint32_t sampleRate, BitWidth bitWidth) {
  39. if (bytes > 1024 * 2 * channels) {
  40. return 0;
  41. }
  42. // Create a StreamInfo object to pass to the pipeline
  43. auto streamInfo = std::make_unique<StreamInfo>();
  44. streamInfo->numChannels = channels;
  45. streamInfo->sampleRate = static_cast<bell::SampleRate>(sampleRate);
  46. streamInfo->bitwidth = bitWidth;
  47. streamInfo->numSamples = bytes / channels / 2;
  48. std::scoped_lock lock(accessMutex);
  49. int16_t* data16Bit = (int16_t*)data;
  50. int length16 = bytes / 4;
  51. for (size_t i = 0; i < length16; i++) {
  52. dataLeft[i] = (data16Bit[i * 2] / (float)MAX_INT16); // Normalize left
  53. dataRight[i] =
  54. (data16Bit[i * 2 + 1] / (float)MAX_INT16); // Normalize right
  55. }
  56. float* sampleData[] = {&dataLeft[0], &dataRight[0]};
  57. streamInfo->data = sampleData;
  58. if (activePipeline) {
  59. streamInfo = activePipeline->process(std::move(streamInfo));
  60. }
  61. if (this->instantEffect != nullptr) {
  62. this->instantEffect->apply(dataLeft.data(), length16,
  63. samplesSinceInstantQueued);
  64. if (streamInfo->numSamples > 1) {
  65. this->instantEffect->apply(dataRight.data(), length16,
  66. samplesSinceInstantQueued);
  67. }
  68. samplesSinceInstantQueued += length16;
  69. if (this->instantEffect->duration <= samplesSinceInstantQueued) {
  70. this->instantEffect = nullptr;
  71. }
  72. }
  73. for (size_t i = 0; i < length16; i++) {
  74. if (dataLeft[i] > 1.0f) {
  75. dataLeft[i] = 1.0f;
  76. }
  77. // Data has been downmixed to mono
  78. if (streamInfo->numChannels == 1) {
  79. data16Bit[i] = dataLeft[i] * MAX_INT16; // Denormalize left
  80. } else {
  81. data16Bit[i * 2] = dataLeft[i] * MAX_INT16; // Denormalize left
  82. data16Bit[i * 2 + 1] = dataRight[i] * MAX_INT16; // Denormalize right
  83. }
  84. }
  85. if (streamInfo->numChannels == 1) {
  86. return bytes / 2;
  87. }
  88. return bytes;
  89. }
  90. std::shared_ptr<AudioPipeline> BellDSP::getActivePipeline() {
  91. return activePipeline;
  92. }