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:
parent
e891b3fae8
commit
f827ba38ef
@ -38,8 +38,8 @@ struct audio_line {
|
||||
* buffer is depleted, it's destroyed */
|
||||
bool alive;
|
||||
|
||||
struct audio_line **prev_next;
|
||||
struct audio_line *next;
|
||||
struct audio_line **prev_next;
|
||||
struct audio_line *next;
|
||||
};
|
||||
|
||||
static inline void audio_line_destroy_data(struct audio_line *line)
|
||||
@ -130,7 +130,7 @@ static void mix_audio_lines(struct audio_output *audio, uint64_t audio_time,
|
||||
while (line) {
|
||||
struct audio_line *next = line->next;
|
||||
|
||||
if (line->base_timestamp < prev_time) {
|
||||
if (line->buffer.size && line->base_timestamp < prev_time) {
|
||||
clear_excess_audio_data(line,
|
||||
line->base_timestamp - prev_time);
|
||||
line->base_timestamp = prev_time;
|
||||
@ -222,7 +222,7 @@ int audio_output_open(audio_t *audio, media_t media, struct audio_info *info)
|
||||
goto fail;
|
||||
if (pthread_mutex_init(&out->line_mutex, &attr) != 0)
|
||||
goto fail;
|
||||
if (event_init(&out->stop_event, true) != 0)
|
||||
if (event_init(&out->stop_event, EVENT_TYPE_MANUAL) != 0)
|
||||
goto fail;
|
||||
if (!ao_add_to_media(out))
|
||||
goto fail;
|
||||
@ -243,6 +243,7 @@ audio_line_t audio_output_createline(audio_t audio, const char *name)
|
||||
struct audio_line *line = bmalloc(sizeof(struct audio_line));
|
||||
memset(line, 0, sizeof(struct audio_line));
|
||||
line->alive = true;
|
||||
line->audio = audio;
|
||||
|
||||
if (pthread_mutex_init(&line->mutex, NULL) != 0) {
|
||||
blog(LOG_ERROR, "audio_output_createline: Failed to create "
|
||||
@ -393,8 +394,8 @@ static inline void mul_vol_float(struct audio_line *line, float volume,
|
||||
static void audio_line_place_data(struct audio_line *line,
|
||||
const struct audio_data *data, size_t position)
|
||||
{
|
||||
size_t total_size = data->frames * line->audio->block_size;
|
||||
size_t total_num = data->frames * line->audio->channels;
|
||||
size_t total_size = data->frames * line->audio->block_size;
|
||||
|
||||
da_copy_array(line->volume_buffer, data->data, total_size);
|
||||
|
||||
|
@ -115,9 +115,9 @@ int video_output_open(video_t *video, media_t media, struct video_info *info)
|
||||
|
||||
if (pthread_mutex_init(&out->data_mutex, NULL) != 0)
|
||||
goto fail;
|
||||
if (event_init(&out->stop_event, true) != 0)
|
||||
if (event_init(&out->stop_event, EVENT_TYPE_MANUAL) != 0)
|
||||
goto fail;
|
||||
if (event_init(&out->update_event, false) != 0)
|
||||
if (event_init(&out->update_event, EVENT_TYPE_AUTO) != 0)
|
||||
goto fail;
|
||||
if (!vo_add_to_media(out))
|
||||
goto fail;
|
||||
|
@ -332,14 +332,6 @@ static void source_output_audio_line(obs_source_t source,
|
||||
{
|
||||
struct audio_data in = *data;
|
||||
|
||||
if (!in.timestamp) {
|
||||
in.timestamp = os_gettime_ns();
|
||||
if (!source->timing_set) {
|
||||
source->timing_set = true;
|
||||
source->timing_adjust = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (!source->timing_set) {
|
||||
source->timing_set = true;
|
||||
source->timing_adjust = in.timestamp - os_gettime_ns();
|
||||
|
@ -53,9 +53,14 @@ struct event_data {
|
||||
bool manual;
|
||||
};
|
||||
|
||||
enum event_type {
|
||||
EVENT_TYPE_AUTO,
|
||||
EVENT_TYPE_MANUAL
|
||||
};
|
||||
|
||||
typedef struct event_data event_t;
|
||||
|
||||
static inline int event_init(event_t *event, bool manual)
|
||||
static inline int event_init(event_t *event, enum event_type type)
|
||||
{
|
||||
int code = 0;
|
||||
|
||||
@ -65,7 +70,7 @@ static inline int event_init(event_t *event, bool manual)
|
||||
if ((code = pthread_cond_init(&event->cond, NULL)) < 0)
|
||||
pthread_mutex_destroy(&event->mutex);
|
||||
|
||||
event->manual = manual;
|
||||
event->manual = (type == EVENT_TYPE_MANUAL);
|
||||
event->signalled = false;
|
||||
|
||||
return code;
|
||||
|
@ -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
|
@ -22,11 +22,13 @@
|
||||
<ClInclude Include="..\..\..\test\test-input\test-filter.h" />
|
||||
<ClInclude Include="..\..\..\test\test-input\test-input-exports.h" />
|
||||
<ClInclude Include="..\..\..\test\test-input\test-random.h" />
|
||||
<ClInclude Include="..\..\..\test\test-input\test-sinewave.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\..\..\test\test-input\test-filter.c" />
|
||||
<ClCompile Include="..\..\..\test\test-input\test-input.c" />
|
||||
<ClCompile Include="..\..\..\test\test-input\test-random.c" />
|
||||
<ClCompile Include="..\..\..\test\test-input\test-sinewave.c" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{760ECBBC-EA7C-464A-B60E-945A0BB1B100}</ProjectGuid>
|
||||
@ -102,7 +104,7 @@
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>libobs.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>pthreads.lib;libobs.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
<PostBuildEvent>
|
||||
<Command>copy "$(OutDir)$(TargetName)$(TargetExt)" "../../../build/plugins/32bit/$(TargetName)$(TargetExt)"</Command>
|
||||
@ -122,7 +124,7 @@
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>libobs.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>pthreads.lib;libobs.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
<PostBuildEvent>
|
||||
<Command>copy "$(OutDir)$(TargetName)$(TargetExt)" "../../../build/plugins/64bit/$(TargetName)$(TargetExt)"</Command>
|
||||
@ -146,7 +148,7 @@
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>libobs.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>pthreads.lib;libobs.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
<PostBuildEvent>
|
||||
<Command>copy "$(OutDir)$(TargetName)$(TargetExt)" "../../../build/plugins/32bit/$(TargetName)$(TargetExt)"</Command>
|
||||
@ -170,7 +172,7 @@
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>libobs.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>pthreads.lib;libobs.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
<PostBuildEvent>
|
||||
<Command>copy "$(OutDir)$(TargetName)$(TargetExt)" "../../../build/plugins/64bit/$(TargetName)$(TargetExt)"</Command>
|
||||
|
@ -24,6 +24,9 @@
|
||||
<ClInclude Include="..\..\..\test\test-input\test-input-exports.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\..\test\test-input\test-sinewave.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\..\..\test\test-input\test-random.c">
|
||||
@ -35,5 +38,8 @@
|
||||
<ClCompile Include="..\..\..\test\test-input\test-input.c">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\..\test\test-input\test-sinewave.c">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
Loading…
x
Reference in New Issue
Block a user