BellDSP.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "BellDSP.h"
  2. #include <iostream>
  3. #include "CentralAudioBuffer.h"
  4. using namespace bell;
  5. BellDSP::FadeEffect::FadeEffect(size_t duration, bool isFadeIn,
  6. std::function<void()> onFinish) {
  7. this->duration = duration;
  8. this->onFinish = onFinish;
  9. this->isFadeIn = isFadeIn;
  10. }
  11. void BellDSP::FadeEffect::apply(float* audioData, size_t samples,
  12. size_t relativePosition) {
  13. float effect = (this->duration - relativePosition) / (float)this->duration;
  14. if (isFadeIn) {
  15. effect = relativePosition / (float)this->duration;
  16. }
  17. for (int x = 0; x <= samples; x++) {
  18. audioData[x] *= effect;
  19. }
  20. if (relativePosition + samples > this->duration && onFinish != nullptr) {
  21. onFinish();
  22. }
  23. }
  24. BellDSP::BellDSP(std::shared_ptr<CentralAudioBuffer> buffer) {
  25. this->buffer = buffer;
  26. };
  27. void BellDSP::applyPipeline(std::shared_ptr<AudioPipeline> pipeline) {
  28. std::scoped_lock lock(accessMutex);
  29. activePipeline = pipeline;
  30. }
  31. void BellDSP::queryInstantEffect(std::unique_ptr<AudioEffect> instantEffect) {
  32. this->instantEffect = std::move(instantEffect);
  33. samplesSinceInstantQueued = 0;
  34. }
  35. size_t BellDSP::process(uint8_t* data, size_t bytes, int channels,
  36. uint32_t sampleRate, BitWidth bitWidth) {
  37. if (bytes > 1024 * 2 * channels) {
  38. return 0;
  39. }
  40. size_t bytesPerSample = channels * 2;
  41. size_t samplesLeftInBuffer = buffer->audioBuffer->size() / bytesPerSample;
  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. }