sync-pair-aud.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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, 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 = video_output_get_info(obs_get_video());
  38. uint64_t fps_num = voi->fps_num;
  39. uint64_t fps_den = voi->fps_den;
  40. while (os_event_try(spa->event) == EAGAIN) {
  41. if (!os_sleepto_ns(last_time += 10000000))
  42. last_time = obs_get_video_frame_time();
  43. for (uint64_t i = 0; i < frames; i++) {
  44. uint64_t ts = last_time + util_mul_div64(i, 1000000000ULL, sample_rate);
  45. if (whitelist_time(ts, interval, fps_num, fps_den)) {
  46. cos_val += rate * M_PI_X2;
  47. if (cos_val > M_PI_X2)
  48. cos_val -= M_PI_X2;
  49. samples[i] = (float)(cos(cos_val) * 0.5);
  50. } else {
  51. samples[i] = 0.0f;
  52. }
  53. }
  54. struct obs_source_audio data;
  55. data.data[0] = (uint8_t *)samples;
  56. data.frames = frames;
  57. data.speakers = SPEAKERS_MONO;
  58. data.samples_per_sec = sample_rate;
  59. data.timestamp = last_time;
  60. data.format = AUDIO_FORMAT_FLOAT;
  61. obs_source_output_audio(spa->source, &data);
  62. }
  63. free(samples);
  64. return NULL;
  65. }
  66. /* ------------------------------------------------------------------------- */
  67. static const char *sync_pair_aud_getname(void *unused)
  68. {
  69. UNUSED_PARAMETER(unused);
  70. return "Sync Test Pair (Audio)";
  71. }
  72. static void sync_pair_aud_destroy(void *data)
  73. {
  74. struct sync_pair_aud *spa = data;
  75. if (spa) {
  76. if (spa->initialized_thread) {
  77. void *ret;
  78. os_event_signal(spa->event);
  79. pthread_join(spa->thread, &ret);
  80. }
  81. os_event_destroy(spa->event);
  82. bfree(spa);
  83. }
  84. }
  85. static void *sync_pair_aud_create(obs_data_t *settings, obs_source_t *source)
  86. {
  87. struct sync_pair_aud *spa = bzalloc(sizeof(struct sync_pair_aud));
  88. spa->source = source;
  89. if (os_event_init(&spa->event, OS_EVENT_TYPE_MANUAL) != 0)
  90. goto fail;
  91. if (pthread_create(&spa->thread, NULL, sync_pair_aud_thread, spa) != 0)
  92. goto fail;
  93. spa->initialized_thread = true;
  94. UNUSED_PARAMETER(settings);
  95. return spa;
  96. fail:
  97. sync_pair_aud_destroy(spa);
  98. return NULL;
  99. }
  100. struct obs_source_info sync_audio = {
  101. .id = "sync_audio",
  102. .type = OBS_SOURCE_TYPE_INPUT,
  103. .output_flags = OBS_SOURCE_AUDIO,
  104. .get_name = sync_pair_aud_getname,
  105. .create = sync_pair_aud_create,
  106. .destroy = sync_pair_aud_destroy,
  107. };