main.cpp 6.7 KB

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