main.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include <iostream>
  2. #include <vector>
  3. #include <random>
  4. #include <nlohmann/json.hpp>
  5. #include "signalstats_wrapper.h"
  6. #include "ThreadPool.h"
  7. void test1();
  8. void test2();
  9. int main() {
  10. // 初始化Python解释器
  11. // signalstats_wrapper::initialize();
  12. // try {
  13. // // 创建测试音频信号 (C++版本)
  14. // const int sample_rate = 44100;
  15. // const int duration_seconds = 1;
  16. // const int num_samples = sample_rate * duration_seconds;
  17. // std::vector<double> audio_signal(num_samples);
  18. // std::random_device rd;
  19. // std::mt19937 gen(rd());
  20. // std::normal_distribution<> dist(0.0, 1.0);
  21. // for (auto& sample : audio_signal) {
  22. // sample = dist(gen);
  23. // }
  24. // std::cout << "生成测试音频信号完成" << std::endl;
  25. // // 调用C++接口函数
  26. // nlohmann::json output;
  27. // std::vector<std::string> window_params = {"tukey", "0.25"};
  28. // nlohmann::json& result = signalstats_wrapper::detect_signal_wrapper(
  29. // output,
  30. // audio_signal,
  31. // sample_rate,
  32. // 3e-3, // silence_threshold
  33. // -70.0, // db_threshold
  34. // -70.0, // cv_threshold
  35. // window_params,
  36. // 256, // nperseg
  37. // 32, // noverlap
  38. // 256, // nfft
  39. // true // debug
  40. // );
  41. // // 处理输出结果
  42. // if (!output.empty()) {
  43. // std::cout << "信号检测完成,输出结果: " << output.dump(4) << std::endl;
  44. // // 这里可以添加具体的结果解析逻辑
  45. // } else {
  46. // std::cout << "信号检测完成,但无输出结果" << std::endl;
  47. // }
  48. // std::cout << "程序执行完成" << std::endl;
  49. // } catch (const std::exception& e) {
  50. // std::cerr << "错误: " << e.what() << std::endl;
  51. // return 1;
  52. // }
  53. // test1();
  54. // 初始化Python解释器
  55. // signalstats_wrapper::initialize();
  56. CPPTP.add_task(test1);
  57. // std::this_thread::sleep_for(std::chrono::seconds(1));
  58. CPPTP.add_task(test2);
  59. // std::this_thread::sleep_for(std::chrono::seconds(5));
  60. std::this_thread::sleep_for(std::chrono::seconds(10)); // 等待任务执行
  61. // signalstats_wrapper::finalize();
  62. return 0;
  63. }
  64. void test1()
  65. {
  66. signalstats_wrapper::initialize();
  67. try {
  68. // 创建测试音频信号 (C++版本)
  69. const int sample_rate = 44100;
  70. const int duration_seconds = 1;
  71. const int num_samples = sample_rate * duration_seconds;
  72. std::vector<double> audio_signal(num_samples);
  73. std::random_device rd;
  74. std::mt19937 gen(rd());
  75. std::normal_distribution<> dist(0.0, 1.0);
  76. for (auto& sample : audio_signal) {
  77. sample = dist(gen);
  78. }
  79. std::cout << "生成测试音频信号完成" << std::endl;
  80. // 调用C++接口函数
  81. nlohmann::json output;
  82. std::vector<std::string> window_params = {"tukey", "0.25"};
  83. nlohmann::json& result = signalstats_wrapper::detect_signal_wrapper(
  84. output,
  85. audio_signal,
  86. sample_rate,
  87. 3e-3, // silence_threshold
  88. -70.0, // db_threshold
  89. -70.0, // cv_threshold
  90. window_params,
  91. 256, // nperseg
  92. 32, // noverlap
  93. 256, // nfft
  94. true // debug
  95. );
  96. // 处理输出结果
  97. if (!output.empty()) {
  98. std::cout << "信号检测完成,输出结果: " << output.dump(4) << std::endl;
  99. // 这里可以添加具体的结果解析逻辑
  100. } else {
  101. std::cout << "信号检测完成,但无输出结果" << std::endl;
  102. }
  103. std::cout << "程序执行完成" << std::endl;
  104. } catch (const std::exception& e) {
  105. std::cerr << "错误: " << e.what() << std::endl;
  106. return;
  107. }
  108. signalstats_wrapper::finalize();
  109. }
  110. void test2()
  111. {
  112. signalstats_wrapper::initialize();
  113. try {
  114. // 创建测试音频信号 (C++版本)
  115. const int sample_rate = 44100;
  116. const int duration_seconds = 1;
  117. const int num_samples = sample_rate * duration_seconds;
  118. std::vector<double> audio_signal(num_samples);
  119. std::random_device rd;
  120. std::mt19937 gen(rd());
  121. std::normal_distribution<> dist(0.0, 1.0);
  122. for (auto& sample : audio_signal) {
  123. sample = dist(gen);
  124. }
  125. std::cout << "生成测试音频信号完成" << std::endl;
  126. // 调用C++接口函数
  127. nlohmann::json output;
  128. std::vector<std::string> window_params = {"tukey", "0.25"};
  129. nlohmann::json& result = signalstats_wrapper::detect_signal_wrapper(
  130. output,
  131. audio_signal,
  132. sample_rate,
  133. 3e-3, // silence_threshold
  134. -70.0, // db_threshold
  135. -70.0, // cv_threshold
  136. window_params,
  137. 256, // nperseg
  138. 32, // noverlap
  139. 256, // nfft
  140. true // debug
  141. );
  142. // 处理输出结果
  143. if (!output.empty()) {
  144. std::cout << "信号检测完成,输出结果: " << output.dump(4) << std::endl;
  145. // 这里可以添加具体的结果解析逻辑
  146. } else {
  147. std::cout << "信号检测完成,但无输出结果" << std::endl;
  148. }
  149. std::cout << "程序执行完成" << std::endl;
  150. } catch (const std::exception& e) {
  151. std::cerr << "错误: " << e.what() << std::endl;
  152. return;
  153. }
  154. signalstats_wrapper::finalize();
  155. }