main.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include <iostream>
  2. #include <vector>
  3. #include <random>
  4. #include <nlohmann/json.hpp>
  5. #include "signalstats.h"
  6. #include "ThreadPool.h"
  7. #include "RingQueueManualMutex.hpp"
  8. #include "spdlog/spdlog.h"
  9. #include <QApplication>
  10. #include <QCoreApplication>
  11. #include <QAudioFormat>
  12. #include <QAudioOutput>
  13. #include <QTimer>
  14. #include <QFile>
  15. void test1();
  16. void test2();
  17. void test3();
  18. void test4(int argc, char** argv);
  19. int main(int argc, char* argv[])
  20. {
  21. // 初始化Python解释器
  22. // signalstats_wrapper::initialize();
  23. // CPPTP.add_task(test1);
  24. // std::this_thread::sleep_for(std::chrono::seconds(1));
  25. // CPPTP.add_task(test2);
  26. // std::this_thread::sleep_for(std::chrono::seconds(5));
  27. // test3();
  28. // std::this_thread::sleep_for(std::chrono::seconds(2)); // 等待任务执行
  29. // signalstats_wrapper::finalize();
  30. test4(argc, argv);
  31. return 0;
  32. }
  33. void test1()
  34. {
  35. try {
  36. // 创建测试音频信号 (C++版本)
  37. const int sample_rate = 44100;
  38. const int duration_seconds = 1;
  39. const int num_samples = sample_rate * duration_seconds;
  40. std::vector<double> audio_signal(num_samples);
  41. std::random_device rd;
  42. std::mt19937 gen(rd());
  43. std::normal_distribution<> dist(0.0, 1.0);
  44. for (auto& sample : audio_signal) {
  45. sample = dist(gen);
  46. }
  47. std::cout << "生成测试音频信号完成" << std::endl;
  48. // 调用C++接口函数
  49. nlohmann::json output;
  50. std::vector<std::string> window_params = {"tukey", "0.25"};
  51. nlohmann::json& result = signalstats::detect_signal(
  52. output,
  53. audio_signal,
  54. sample_rate,
  55. 3e-3, // silence_threshold
  56. -70.0, // db_threshold
  57. -70.0, // cv_threshold
  58. window_params,
  59. 256, // nperseg
  60. 32, // noverlap
  61. 256 // nfft
  62. );
  63. std::cout << "源数据: " << std::endl;
  64. for(const auto it : audio_signal)
  65. {
  66. std::cout << it << " ";
  67. }
  68. std::cout << std::endl;
  69. // 处理输出结果
  70. if (!output.empty()) {
  71. std::cout << "信号检测完成,输出结果: " << output.dump(4) << std::endl;
  72. // 这里可以添加具体的结果解析逻辑
  73. } else {
  74. std::cout << "信号检测完成,但无输出结果" << std::endl;
  75. }
  76. std::cout << "程序执行完成" << std::endl;
  77. } catch (const std::exception& e) {
  78. std::cerr << "错误: " << e.what() << std::endl;
  79. return;
  80. }
  81. // signalstats_wrapper::finalize();
  82. }
  83. void test2()
  84. {
  85. // signalstats_wrapper::initialize();
  86. try {
  87. // 创建测试音频信号 (C++版本)
  88. const int sample_rate = 44100;
  89. const int duration_seconds = 1;
  90. const int num_samples = sample_rate * duration_seconds;
  91. std::vector<double> audio_signal(num_samples);
  92. std::random_device rd;
  93. std::mt19937 gen(rd());
  94. std::normal_distribution<> dist(0.0, 1.0);
  95. for (auto& sample : audio_signal) {
  96. sample = dist(gen);
  97. }
  98. std::cout << "生成测试音频信号完成" << std::endl;
  99. // 调用C++接口函数
  100. nlohmann::json output;
  101. std::vector<std::string> window_params = {"tukey", "0.25"};
  102. nlohmann::json& result = signalstats::detect_signal(
  103. output,
  104. audio_signal,
  105. sample_rate,
  106. 3e-3, // silence_threshold
  107. -70.0, // db_threshold
  108. -70.0, // cv_threshold
  109. window_params,
  110. 256, // nperseg
  111. 32, // noverlap
  112. 256 // nfft
  113. );
  114. // 处理输出结果
  115. if (!output.empty()) {
  116. std::cout << "信号检测完成,输出结果: " << output.dump(4) << std::endl;
  117. // 这里可以添加具体的结果解析逻辑
  118. } else {
  119. std::cout << "信号检测完成,但无输出结果" << std::endl;
  120. }
  121. std::cout << "程序执行完成" << std::endl;
  122. } catch (const std::exception& e) {
  123. std::cerr << "错误: " << e.what() << std::endl;
  124. return;
  125. }
  126. // signalstats_wrapper::finalize();
  127. }
  128. /* 测试环形队列 */
  129. void test3()
  130. {
  131. RingQueueManualMutex<int> queue(10);
  132. for(int i = 1; i <= 20; ++i)
  133. {
  134. int pop = queue.push(i);
  135. SPDLOG_INFO("Push: {}, pop:{}, Queue Size: {}", i, pop, queue.QueueSize());
  136. }
  137. }
  138. void test4(int argc, char** argv)
  139. {
  140. QCoreApplication a(argc, argv);
  141. QAudioFormat format;
  142. format.setSampleRate(44100);
  143. format.setChannelCount(2);
  144. format.setSampleSize(16);
  145. format.setCodec("audio/pcm");
  146. format.setByteOrder(QAudioFormat::LittleEndian);
  147. format.setSampleType(QAudioFormat::SignedInt);
  148. QAudioOutput* audioOutput = new QAudioOutput(format);
  149. QIODevice* device = audioOutput->start();
  150. if (!device) {
  151. SPDLOG_ERROR("Failed to start audio output device");
  152. return;
  153. }
  154. QFile* pFile = new QFile(QCoreApplication::applicationDirPath() + "/test.wav");
  155. if(!pFile->open(QIODevice::ReadOnly)) {
  156. SPDLOG_ERROR("Failed to open audio file: {}", pFile->errorString().toStdString());
  157. return;
  158. }
  159. int readSize = 4410 * 4;
  160. char* buffer = new char[readSize]; // 1 second buffer for stereo 16-bit audio
  161. QTimer* ptimer = new QTimer();
  162. ptimer->setInterval(100);
  163. ptimer->setSingleShot(false);
  164. ptimer->setTimerType(Qt::PreciseTimer);
  165. QObject::connect(ptimer, &QTimer::timeout, [&, device, pFile, buffer]() {
  166. SPDLOG_INFO("Timer triggered, reading audio data");
  167. pFile->read(buffer, readSize);
  168. device->write(buffer, readSize);
  169. if(pFile->atEnd()) {
  170. pFile->seek(0); // 重新开始播放
  171. SPDLOG_INFO("Reached end of file, restarting playback");
  172. }
  173. });
  174. ptimer->start();
  175. a.exec();
  176. }