2
0

main.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <memory.h>
  2. #include <atomic>
  3. #include <cmath>
  4. #include <fstream>
  5. #include <iostream>
  6. #include <map>
  7. #include <memory>
  8. #include <vector>
  9. #include "AudioCodecs.h"
  10. #include "AudioContainers.h"
  11. #include "BellHTTPServer.h"
  12. #include "BellTask.h"
  13. #include "CentralAudioBuffer.h"
  14. #include "Compressor.h"
  15. #include "DecoderGlobals.h"
  16. #include "EncodedAudioStream.h"
  17. #include "HTTPClient.h"
  18. #include "PortAudioSink.h"
  19. #include <BellDSP.h>
  20. #include <BellLogger.h>
  21. std::shared_ptr<bell::CentralAudioBuffer> audioBuffer;
  22. std::atomic<bool> isPaused = false;
  23. class AudioPlayer : bell::Task {
  24. public:
  25. std::unique_ptr<PortAudioSink> audioSink;
  26. std::unique_ptr<bell::BellDSP> dsp;
  27. AudioPlayer() : bell::Task("player", 1024, 0, 0) {
  28. this->audioSink = std::make_unique<PortAudioSink>();
  29. this->audioSink->setParams(44100, 2, 16);
  30. this->dsp = std::make_unique<bell::BellDSP>(audioBuffer);
  31. startTask();
  32. }
  33. void runTask() override {
  34. while (true) {
  35. if (audioBuffer->hasAtLeast(64) || isPaused) {
  36. auto chunk = audioBuffer->readChunk();
  37. if (chunk != nullptr && chunk->pcmSize > 0) {
  38. // this->dsp->process(chunk->pcmData, chunk->pcmSize, 2, 44100,
  39. // bell::BitWidth::BW_16);
  40. this->audioSink->feedPCMFrames(chunk->pcmData, chunk->pcmSize);
  41. }
  42. }
  43. }
  44. }
  45. };
  46. int main() {
  47. bell::setDefaultLogger();
  48. bell::createDecoders();
  49. size_t size = sizeof(void*);
  50. audioBuffer = std::make_shared<bell::CentralAudioBuffer>(512);
  51. auto task = AudioPlayer();
  52. auto url = "http://193.222.135.71/378";
  53. // std::ifstream file("aactest.aac", std::ios::binary);
  54. auto req = bell::HTTPClient::get(url);
  55. auto container = AudioContainers::guessAudioContainer(req->stream());
  56. auto codec = AudioCodecs::getCodec(container.get());
  57. uint32_t dataLen;
  58. while (true) {
  59. uint8_t* data = codec->decode(container.get(), dataLen);
  60. if (!data) {
  61. std::cout << "data invalid" << std::endl;
  62. continue;
  63. }
  64. size_t toWrite = dataLen;
  65. while (toWrite > 0) {
  66. toWrite -= audioBuffer->writePCM(data + dataLen - toWrite, toWrite, 0);
  67. }
  68. // std::cout << dataLen << std::endl;
  69. }
  70. // return 0;
  71. // std::vector<uint8_t> frameData(1024 * 10);
  72. // /*
  73. // while (true) {
  74. // size_t bytes =audioStream->decodeFrame(frameData.data());
  75. // std::cout << bytes <<std::endl;
  76. // size_t toWrite = bytes;
  77. // if (!isPaused) {
  78. // while (toWrite > 0) {
  79. // toWrite -= audioBuffer->writePCM(frameData.data() + bytes - toWrite,
  80. // toWrite, 0);
  81. // }
  82. // }
  83. // }*/
  84. return 0;
  85. }