sync-pair-aud.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <math.h>
  2. #include <util/bmem.h>
  3. #include <util/threading.h>
  4. #include <util/platform.h>
  5. #include <obs.h>
  6. struct sync_pair_aud {
  7. bool initialized_thread;
  8. pthread_t thread;
  9. os_event_t *event;
  10. obs_source_t *source;
  11. };
  12. /* middle C */
  13. static const double rate = 261.63/48000.0;
  14. #ifndef M_PI
  15. #define M_PI 3.1415926535897932384626433832795
  16. #endif
  17. #define M_PI_X2 M_PI*2
  18. extern uint64_t starting_time;
  19. static inline bool whitelist_time(uint64_t ts, uint64_t interval,
  20. uint64_t fps_num, uint64_t fps_den)
  21. {
  22. if (!starting_time)
  23. return false;
  24. uint64_t count = (ts - starting_time) / interval;
  25. uint64_t sec = count * fps_den / fps_num;
  26. return sec % 2 == 1;
  27. }
  28. static void *sync_pair_aud_thread(void *pdata)
  29. {
  30. struct sync_pair_aud *spa = pdata;
  31. uint32_t sample_rate = audio_output_get_sample_rate(obs_get_audio());
  32. uint32_t frames = sample_rate / 100;
  33. uint64_t last_time = obs_get_video_frame_time();
  34. double cos_val = 0.0;
  35. float *samples = malloc(frames * sizeof(float));
  36. uint64_t interval = video_output_get_frame_time(obs_get_video());
  37. const struct video_output_info *voi =
  38. video_output_get_info(obs_get_video());
  39. uint64_t fps_num = voi->fps_num;
  40. uint64_t fps_den = voi->fps_den;
  41. while (os_event_try(spa->event) == EAGAIN) {
  42. if (!os_sleepto_ns(last_time += 10000000))
  43. last_time = obs_get_video_frame_time();
  44. for (uint64_t i = 0; i < frames; i++) {
  45. uint64_t ts = last_time +
  46. i * 1000000000ULL / sample_rate;
  47. if (whitelist_time(ts, interval, fps_num, fps_den)) {
  48. cos_val += rate * M_PI_X2;
  49. if (cos_val > M_PI_X2)
  50. cos_val -= M_PI_X2;
  51. samples[i] = (float)(cos(cos_val) * 0.5);
  52. } else {
  53. samples[i] = 0.0f;
  54. }
  55. }
  56. struct obs_source_audio data;
  57. data.data[0] = (uint8_t*)samples;
  58. data.frames = frames;
  59. data.speakers = SPEAKERS_MONO;
  60. data.samples_per_sec = sample_rate;
  61. data.timestamp = last_time;
  62. data.format = AUDIO_FORMAT_FLOAT;
  63. obs_source_output_audio(spa->source, &data);
  64. }
  65. free(samples);
  66. return NULL;
  67. }
  68. /* ------------------------------------------------------------------------- */
  69. static const char *sync_pair_aud_getname(void *unused)
  70. {
  71. UNUSED_PARAMETER(unused);
  72. return "Sync Test Pair (Audio)";
  73. }
  74. static void sync_pair_aud_destroy(void *data)
  75. {
  76. struct sync_pair_aud *spa = data;
  77. if (spa) {
  78. if (spa->initialized_thread) {
  79. void *ret;
  80. os_event_signal(spa->event);
  81. pthread_join(spa->thread, &ret);
  82. }
  83. os_event_destroy(spa->event);
  84. bfree(spa);
  85. }
  86. }
  87. static void *sync_pair_aud_create(obs_data_t *settings,
  88. obs_source_t *source)
  89. {
  90. struct sync_pair_aud *spa = bzalloc(sizeof(struct sync_pair_aud));
  91. spa->source = source;
  92. if (os_event_init(&spa->event, OS_EVENT_TYPE_MANUAL) != 0)
  93. goto fail;
  94. if (pthread_create(&spa->thread, NULL, sync_pair_aud_thread, spa) != 0)
  95. goto fail;
  96. spa->initialized_thread = true;
  97. UNUSED_PARAMETER(settings);
  98. return spa;
  99. fail:
  100. sync_pair_aud_destroy(spa);
  101. return NULL;
  102. }
  103. struct obs_source_info sync_audio = {
  104. .id = "sync_audio",
  105. .type = OBS_SOURCE_TYPE_INPUT,
  106. .output_flags = OBS_SOURCE_AUDIO,
  107. .get_name = sync_pair_aud_getname,
  108. .create = sync_pair_aud_create,
  109. .destroy = sync_pair_aud_destroy,
  110. };