Added a sinewave audio test source
- Added a test audio sinewave test source that should just play a sine wave of the middle C note. Using unsigned 8 bit mono to test ffmpeg's audio resampler, seems to work pretty good. - Fixed a boolean trap in threading.h for the event_init function, it now uses enum event_type, which can be EVENT_TYPE_MANUAL or EVENT_TYPE_AUTO, to specify whether the event is automatically reset or not. - Changed display names of test sources to something a little less vague. - Removed te whole "if timestamp is 0 just use current system time" when outputting source audio, if you want to use system time you should just use system time yourself. Using 0 as some sort of "indicator" like that just makes things confusing, and prevents you from legitimately using 0 as a timestamp for your audio data.
This commit is contained in:
@@ -3,6 +3,7 @@ include_directories(SYSTEM ${obs_SOURCE_DIR}/libobs)
|
||||
add_library(test-input MODULE
|
||||
test-filter.c
|
||||
test-input.c
|
||||
test-sinewave.c
|
||||
test-random.c)
|
||||
|
||||
target_link_libraries(test-input
|
||||
|
@@ -19,4 +19,5 @@ endif
|
||||
libtest_input_la_LIBADD = $(top_srcdir)/libobs/libobs.la
|
||||
libtest_input_la_SOURCES = test-filter.c \
|
||||
test-input.c \
|
||||
test-random.c
|
||||
test-random.c \
|
||||
test-sinewave.c
|
||||
|
@@ -1,7 +1,7 @@
|
||||
#include <obs.h>
|
||||
#include "test-input-exports.h"
|
||||
|
||||
const char *inputs[] = {"random"};
|
||||
const char *inputs[] = {"random", "sinewave"};
|
||||
const char *filters[] = {"test"};
|
||||
|
||||
uint32_t module_version(uint32_t in_version)
|
||||
|
@@ -3,7 +3,7 @@
|
||||
|
||||
const char *random_getname(const char *locale)
|
||||
{
|
||||
return "Random Source";
|
||||
return "20x20 Random Pixel Texture Source (Test)";
|
||||
}
|
||||
|
||||
struct random_tex *random_create(const char *settings, obs_source_t source)
|
||||
|
86
test/test-input/test-sinewave.c
Normal file
86
test/test-input/test-sinewave.c
Normal file
@@ -0,0 +1,86 @@
|
||||
#include <math.h>
|
||||
#include "test-sinewave.h"
|
||||
|
||||
const double rate = 261.63/48000.0;
|
||||
|
||||
#define M_PI 3.1415926535897932384626433832795
|
||||
|
||||
static void *sinewave_thread(void *pdata)
|
||||
{
|
||||
struct sinewave_data *swd = pdata;
|
||||
uint64_t last_time = os_gettime_ns();
|
||||
uint64_t ts = 0;
|
||||
double sin_val = 0.0;
|
||||
uint8_t bytes[480];
|
||||
|
||||
while (event_try(&swd->event) == EAGAIN) {
|
||||
os_sleepto_ns(last_time += 10000000);
|
||||
|
||||
for (size_t i = 0; i < 480; i++) {
|
||||
sin_val += rate * M_PI;
|
||||
if (sin_val > M_PI)
|
||||
sin_val -= M_PI;
|
||||
|
||||
double wave = sin(sin_val);
|
||||
bytes[i] = (uint8_t)(wave * 255.0);
|
||||
}
|
||||
|
||||
struct source_audio data;
|
||||
data.data = bytes;
|
||||
data.frames = 480;
|
||||
data.speakers = SPEAKERS_MONO;
|
||||
data.samples_per_sec = 48000;
|
||||
data.timestamp = ts;
|
||||
data.format = AUDIO_FORMAT_U8BIT;
|
||||
obs_source_output_audio(swd->source, &data);
|
||||
|
||||
ts += 10000000;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
const char *sinewave_getname(const char *locale)
|
||||
{
|
||||
return "Sinewave Sound Source (Test)";
|
||||
}
|
||||
|
||||
struct sinewave_data *sinewave_create(const char *settings, obs_source_t source)
|
||||
{
|
||||
struct sinewave_data *swd = bmalloc(sizeof(struct sinewave_data));
|
||||
memset(swd, 0, sizeof(struct sinewave_data));
|
||||
swd->source = source;
|
||||
|
||||
if (event_init(&swd->event, EVENT_TYPE_MANUAL) != 0)
|
||||
goto fail;
|
||||
if (pthread_create(&swd->thread, NULL, sinewave_thread, swd) != 0)
|
||||
goto fail;
|
||||
|
||||
swd->initialized_thread = true;
|
||||
return swd;
|
||||
|
||||
fail:
|
||||
sinewave_destroy(swd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void sinewave_destroy(struct sinewave_data *swd)
|
||||
{
|
||||
if (swd) {
|
||||
if (swd->initialized_thread) {
|
||||
void *ret;
|
||||
event_signal(&swd->event);
|
||||
pthread_join(swd->thread, &ret);
|
||||
}
|
||||
|
||||
event_destroy(&swd->event);
|
||||
bfree(swd);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t sinewave_get_output_flags(struct sinewave_data *swd)
|
||||
{
|
||||
return SOURCE_AUDIO;
|
||||
}
|
28
test/test-input/test-sinewave.h
Normal file
28
test/test-input/test-sinewave.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <util/bmem.h>
|
||||
#include <util/threading.h>
|
||||
#include <util/platform.h>
|
||||
#include <obs.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct sinewave_data {
|
||||
bool initialized_thread;
|
||||
pthread_t thread;
|
||||
event_t event;
|
||||
obs_source_t source;
|
||||
};
|
||||
|
||||
EXPORT const char *sinewave_getname(const char *locale);
|
||||
|
||||
EXPORT struct sinewave_data *sinewave_create(const char *settings,
|
||||
obs_source_t source);
|
||||
EXPORT void sinewave_destroy(struct sinewave_data *swd);
|
||||
EXPORT uint32_t sinewave_get_output_flags(struct sinewave_data *swd);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
Reference in New Issue
Block a user