sync-pair-aud.c 3.1 KB

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