main.cpp 6.7 KB

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