c1.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "c1.h"
  2. #include "AudioRecord.h"
  3. #include <chrono>
  4. #include <thread>
  5. #include "spdlog/spdlog.h"
  6. #include "RingQueueManualMutex.hpp"
  7. AudioLeftRightData::AudioLeftRightData(int totalSize)
  8. {
  9. // if (totalSize == 0)
  10. // {
  11. // SPDLOG_ERROR("AudioLeftRightData::AudioLeftRightData totalSize is 0");
  12. // return;
  13. // }
  14. this->totalSize = totalSize;
  15. vecLeftData.reserve(totalSize);
  16. vecRightData.reserve(totalSize);
  17. startTime = QDateTime();
  18. endTime = QDateTime();
  19. numMSecond = 0;
  20. }
  21. AudioLeftRightData::~AudioLeftRightData()
  22. {
  23. }
  24. AudioLeftRightData::AudioLeftRightData(const AudioLeftRightData& obj)
  25. {
  26. *this = obj; // 使用赋值运算符进行深拷贝
  27. }
  28. AudioLeftRightData& AudioLeftRightData::operator=(const AudioLeftRightData &obj)
  29. {
  30. if (this == &obj)
  31. {
  32. return *this; // 防止自赋值
  33. }
  34. // 释放原有资源
  35. vecLeftData.clear();
  36. vecRightData.clear();
  37. // 复制数据
  38. totalSize = obj.totalSize;
  39. vecLeftData = obj.vecLeftData;
  40. vecRightData = obj.vecRightData;
  41. // 复制时间戳
  42. startTime = obj.startTime;
  43. endTime = obj.endTime;
  44. numMSecond = obj.numMSecond;
  45. return *this;
  46. }
  47. void c1::test1()
  48. {
  49. RingQueueManualMutex<AudioLeftRightData*> listData(10);
  50. SPDLOG_INFO("开始添加 listData 数据");
  51. for(int i = 0; i < 10; ++i)
  52. {
  53. AudioLeftRightData* data = new AudioLeftRightData(48000);
  54. for(int j = 0; j < 48000; ++j)
  55. {
  56. data->vecLeftData.push_back(0.5); // 模拟数据
  57. data->vecRightData.push_back(0.5); // 模拟数据
  58. }
  59. listData.push(data);
  60. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  61. }
  62. SPDLOG_INFO("添加 listData 数据完成, 当前数据大小: {}", listData.QueueSize());
  63. std::this_thread::sleep_for(std::chrono::milliseconds(10000));
  64. /* 开始释放数据 */
  65. SPDLOG_INFO("开始释放 listData 数据");
  66. while (!listData.isEmpty())
  67. {
  68. AudioLeftRightData* data = listData.front_pop();
  69. if(data != nullptr)
  70. {
  71. delete data;
  72. data = nullptr;
  73. }
  74. }
  75. SPDLOG_INFO("释放 listData 数据完成");
  76. std::this_thread::sleep_for(std::chrono::milliseconds(5000));
  77. SPDLOG_INFO("结束 test1");
  78. }