Get printvideo audio working on Windows and MacOS

This commit is contained in:
Justine Tunney 2024-09-06 06:45:27 -07:00
parent 07fde68d52
commit 5d3b91d8b9
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
18 changed files with 93470 additions and 212 deletions

56
dsp/audio/BUILD.mk Normal file
View file

@ -0,0 +1,56 @@
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
#── vi: set noet ft=make ts=8 sw=8 fenc=utf-8 :vi ────────────────────┘
PKGS += DSP_AUDIO
DSP_AUDIO_ARTIFACTS += DSP_AUDIO_A
DSP_AUDIO = $(DSP_AUDIO_A_DEPS) $(DSP_AUDIO_A)
DSP_AUDIO_A = o/$(MODE)/dsp/audio/audio.a
DSP_AUDIO_A_FILES := $(wildcard dsp/audio/*)
DSP_AUDIO_A_HDRS = $(filter %.h,$(DSP_AUDIO_A_FILES)) dsp/audio/cosmoaudio/cosmoaudio.h
DSP_AUDIO_A_SRCS = $(filter %.c,$(DSP_AUDIO_A_FILES))
DSP_AUDIO_A_DATA = \
dsp/audio/cosmoaudio/miniaudio.h \
dsp/audio/cosmoaudio/cosmoaudio.c \
dsp/audio/cosmoaudio/cosmoaudio.h \
dsp/audio/cosmoaudio/cosmoaudio.dll \
DSP_AUDIO_A_OBJS = \
$(DSP_AUDIO_A_SRCS:%.c=o/$(MODE)/%.o) \
$(DSP_AUDIO_A_DATA:%=o/$(MODE)/%.zip.o) \
DSP_AUDIO_A_CHECKS = \
$(DSP_AUDIO_A).pkg \
$(DSP_AUDIO_A_HDRS:%=o/$(MODE)/%.ok)
DSP_AUDIO_A_DIRECTDEPS = \
LIBC_CALLS \
LIBC_DLOPEN \
LIBC_INTRIN \
LIBC_NEXGEN32E \
LIBC_STR \
LIBC_SYSV \
LIBC_PROC \
LIBC_THREAD \
DSP_AUDIO_A_DEPS := \
$(call uniq,$(foreach x,$(DSP_AUDIO_A_DIRECTDEPS),$($(x))))
$(DSP_AUDIO_A): dsp/audio/ \
$(DSP_AUDIO_A).pkg \
$(DSP_AUDIO_A_OBJS)
$(DSP_AUDIO_A).pkg: \
$(DSP_AUDIO_A_OBJS) \
$(foreach x,$(DSP_AUDIO_A_DIRECTDEPS),$($(x)_A).pkg)
DSP_AUDIO_LIBS = $(foreach x,$(DSP_AUDIO_ARTIFACTS),$($(x)))
DSP_AUDIO_SRCS = $(foreach x,$(DSP_AUDIO_ARTIFACTS),$($(x)_SRCS))
DSP_AUDIO_HDRS = $(foreach x,$(DSP_AUDIO_ARTIFACTS),$($(x)_HDRS))
DSP_AUDIO_CHECKS = $(foreach x,$(DSP_AUDIO_ARTIFACTS),$($(x)_CHECKS))
DSP_AUDIO_OBJS = $(foreach x,$(DSP_AUDIO_ARTIFACTS),$($(x)_OBJS))
$(DSP_AUDIO_OBJS): $(BUILD_FILES) dsp/audio/BUILD.mk
.PHONY: o/$(MODE)/dsp/audio
o/$(MODE)/dsp/audio: $(DSP_AUDIO_CHECKS)

353
dsp/audio/audio.c Normal file
View file

@ -0,0 +1,353 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2024 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "dsp/audio/cosmoaudio/cosmoaudio.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/stat.h"
#include "libc/calls/struct/timespec.h"
#include "libc/dce.h"
#include "libc/dlopen/dlfcn.h"
#include "libc/errno.h"
#include "libc/limits.h"
#include "libc/proc/posix_spawn.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/o.h"
#include "libc/temp.h"
#include "libc/thread/thread.h"
__static_yoink("dsp/audio/cosmoaudio/miniaudio.h");
__static_yoink("dsp/audio/cosmoaudio/cosmoaudio.h");
__static_yoink("dsp/audio/cosmoaudio/cosmoaudio.c");
__static_yoink("dsp/audio/cosmoaudio/cosmoaudio.dll");
static const struct Source {
const char *zip;
const char *name;
} srcs[] = {
{"/zip/dsp/audio/cosmoaudio/miniaudio.h", "miniaudio.h"},
{"/zip/dsp/audio/cosmoaudio/cosmoaudio.h", "cosmoaudio.h"},
{"/zip/dsp/audio/cosmoaudio/cosmoaudio.c", "cosmoaudio.c"}, // must last
};
static struct {
pthread_once_t once;
typeof(cosmoaudio_open) *open;
typeof(cosmoaudio_close) *close;
typeof(cosmoaudio_write) *write;
typeof(cosmoaudio_read) *read;
} g_audio;
static const char *get_tmp_dir(void) {
const char *tmpdir;
if (!(tmpdir = getenv("TMPDIR")) || !*tmpdir)
if (!(tmpdir = getenv("HOME")) || !*tmpdir)
tmpdir = ".";
return tmpdir;
}
static bool get_app_dir(char *path, size_t size) {
strlcpy(path, get_tmp_dir(), size);
strlcat(path, "/.cosmo/", size);
if (makedirs(path, 0755))
return false;
return true;
}
static bool get_dso_path(char *path, size_t size) {
if (!get_app_dir(path, size))
return false;
strlcat(path, "cosmoaudio", size);
if (IsWindows()) {
strlcat(path, ".dll", size);
} else if (IsXnu()) {
strlcat(path, ".dylib", size);
} else {
strlcat(path, ".so", size);
}
return true;
}
static int is_file_newer_than_time(const char *path, const char *other) {
struct stat st1, st2;
if (stat(path, &st1))
// PATH should always exist when calling this function
return -1;
if (stat(other, &st2)) {
if (errno == ENOENT) {
// PATH should replace OTHER because OTHER doesn't exist yet
return true;
} else {
// some other error happened, so we can't do anything
return -1;
}
}
// PATH should replace OTHER if PATH was modified more recently
return timespec_cmp(st1.st_mtim, st2.st_mtim) > 0;
}
static int is_file_newer_than_bytes(const char *path, const char *other) {
int other_fd;
if ((other_fd = open(other, O_RDONLY | O_CLOEXEC)) == -1) {
if (errno == ENOENT) {
return true;
} else {
return -1;
}
}
int path_fd;
if ((path_fd = open(path, O_RDONLY | O_CLOEXEC)) == -1) {
close(other_fd);
return -1;
}
int res;
long i = 0;
for (;;) {
char path_buf[512];
ssize_t path_rc = pread(path_fd, path_buf, sizeof(path_buf), i);
if (path_rc == -1) {
res = -1;
break;
}
char other_buf[512];
ssize_t other_rc = pread(other_fd, other_buf, sizeof(other_buf), i);
if (other_rc == -1) {
res = -1;
break;
}
if (!path_rc || !other_rc) {
if (!path_rc && !other_rc)
res = false;
else
res = true;
break;
}
size_t size = path_rc;
if (other_rc < path_rc)
size = other_rc;
if (memcmp(path_buf, other_buf, size)) {
res = true;
break;
}
i += size;
}
if (close(path_fd))
res = -1;
if (close(other_fd))
res = -1;
return res;
}
static int is_file_newer_than(const char *path, const char *other) {
if (startswith(path, "/zip/"))
// to keep builds deterministic, embedded zip files always have
// the same timestamp from back in 2022 when it was implemented
return is_file_newer_than_bytes(path, other);
else
return is_file_newer_than_time(path, other);
}
static bool extract(const char *zip, const char *to) {
int fdin, fdout;
char stage[PATH_MAX];
strlcpy(stage, to, sizeof(stage));
if (strlcat(stage, ".XXXXXX", sizeof(stage)) >= sizeof(stage)) {
errno = ENAMETOOLONG;
return false;
}
if ((fdout = mkostemp(stage, O_CLOEXEC)) == -1) {
return false;
}
if ((fdin = open(zip, O_RDONLY | O_CLOEXEC)) == -1) {
close(fdout);
unlink(stage);
return false;
}
if (copyfd(fdin, fdout, -1) == -1) {
close(fdin);
close(fdout);
unlink(stage);
return false;
}
if (close(fdout)) {
close(fdin);
unlink(stage);
return false;
}
if (close(fdin)) {
unlink(stage);
return false;
}
if (rename(stage, to)) {
unlink(stage);
return false;
}
return true;
}
static bool deploy(const char *dso) {
switch (is_file_newer_than("/zip/dsp/audio/cosmoaudio/cosmoaudio.dll", dso)) {
case 0:
return true;
case 1:
return extract("/zip/dsp/audio/cosmoaudio/cosmoaudio.dll", dso);
default:
return false;
}
}
static bool build(const char *dso) {
// extract source code
char src[PATH_MAX];
bool needs_rebuild = false;
for (int i = 0; i < sizeof(srcs) / sizeof(*srcs); ++i) {
get_app_dir(src, PATH_MAX);
strlcat(src, srcs[i].name, sizeof(src));
switch (is_file_newer_than(srcs[i].zip, src)) {
case -1:
return false;
case 0:
break;
case 1:
needs_rebuild = true;
if (!extract(srcs[i].zip, src))
return false;
break;
default:
__builtin_unreachable();
}
}
// determine if we need to build
if (!needs_rebuild) {
switch (is_file_newer_than(src, dso)) {
case -1:
return false;
case 0:
break;
case 1:
needs_rebuild = true;
break;
default:
__builtin_unreachable();
}
}
// compile dynamic shared object
if (needs_rebuild) {
int fd;
char tmpdso[PATH_MAX];
strlcpy(tmpdso, dso, sizeof(tmpdso));
strlcat(tmpdso, ".XXXXXX", sizeof(tmpdso));
if ((fd = mkostemp(tmpdso, O_CLOEXEC)) != -1) {
close(fd);
} else {
return false;
}
char *args[] = {
"cc", //
"-I.", //
"-O2", //
"-fPIC", //
"-shared", //
"-pthread", //
"-DNDEBUG", //
IsAarch64() ? "-ffixed-x28" : "-DIGNORE1", //
src, //
"-o", //
tmpdso, //
"-ldl", //
"-lm", //
NULL,
};
int pid, ws;
errno_t err = posix_spawnp(&pid, "cc", NULL, NULL, args, environ);
if (err)
return false;
while (waitpid(pid, &ws, 0) == -1) {
if (errno != EINTR)
return false;
}
if (ws)
return false;
if (rename(tmpdso, dso))
return false;
}
return true;
}
static void cosmoaudio_setup(void) {
void *handle;
if (!(handle = cosmo_dlopen("cosmoaudio.so", RTLD_LOCAL))) {
if (issetugid())
return;
char dso[PATH_MAX];
if (!get_dso_path(dso, sizeof(dso)))
return;
if (IsWindows())
if (deploy(dso))
if ((handle = cosmo_dlopen(dso, RTLD_LOCAL)))
goto WeAreGood;
if (!build(dso))
return;
if (!(handle = cosmo_dlopen(dso, RTLD_LOCAL)))
return;
}
WeAreGood:
g_audio.open = cosmo_dlsym(handle, "cosmoaudio_open");
g_audio.close = cosmo_dlsym(handle, "cosmoaudio_close");
g_audio.write = cosmo_dlsym(handle, "cosmoaudio_write");
g_audio.read = cosmo_dlsym(handle, "cosmoaudio_read");
}
static void cosmoaudio_init(void) {
pthread_once(&g_audio.once, cosmoaudio_setup);
}
COSMOAUDIO_ABI int cosmoaudio_open(struct CosmoAudio **cap, int sampleRate,
int channels) {
cosmoaudio_init();
if (!g_audio.open)
return COSMOAUDIO_ERROR;
return g_audio.open(cap, sampleRate, channels);
}
COSMOAUDIO_ABI int cosmoaudio_close(struct CosmoAudio *ca) {
cosmoaudio_init();
if (!g_audio.close)
return COSMOAUDIO_ERROR;
return g_audio.close(ca);
}
COSMOAUDIO_ABI int cosmoaudio_write(struct CosmoAudio *ca, const float *data,
int frames) {
cosmoaudio_init();
if (!g_audio.write)
return COSMOAUDIO_ERROR;
return g_audio.write(ca, data, frames);
}
COSMOAUDIO_ABI int cosmoaudio_read(struct CosmoAudio *ca, float *data,
int frames) {
cosmoaudio_init();
if (!g_audio.read)
return COSMOAUDIO_ERROR;
return g_audio.read(ca, data, frames);
}

3
dsp/audio/cosmoaudio/.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
*.o
/Debug
/Release

View file

@ -0,0 +1,86 @@
# Makefile for MSVC x64 Command Line Developer Tools
#
# nmake /f Makefile.msvc check
# nmake /f Makefile.msvc MODE=debug check
#
# Compiler and linker
CC=cl
LINK=link
# Build mode (can be overridden from command line)
!IFNDEF MODE
MODE=release
!ENDIF
# Library dependencies.
TEST_LIBS=OneCore.lib
# Compiler flags
CFLAGS_COMMON=/nologo /W4 /Gy /EHsc
CFLAGS_DEBUG=/Od /Zi /MDd /D_DEBUG
CFLAGS_RELEASE=/O2 /MD /DNDEBUG
!IF "$(MODE)"=="debug"
CFLAGS=$(CFLAGS_COMMON) $(CFLAGS_DEBUG)
LDFLAGS=/DEBUG
OUT_DIR=Debug
!ELSE
CFLAGS=$(CFLAGS_COMMON) $(CFLAGS_RELEASE) /GL
LDFLAGS=/RELEASE /OPT:REF /OPT:ICF /LTCG /INCREMENTAL:NO
OUT_DIR=Release
!ENDIF
# Additional flags for DLL
DLL_CFLAGS=$(CFLAGS) /D_USRDLL /D_WINDLL
# Linker flags
LDFLAGS=/NOLOGO /SUBSYSTEM:CONSOLE $(LDFLAGS)
# Output file names
DLL_TARGET=$(OUT_DIR)\cosmoaudio.dll
TEST_TARGET=$(OUT_DIR)\test.exe
# Source files
DLL_SOURCES=cosmoaudio.c
TEST_SOURCES=test.c
# Object files
DLL_OBJECTS=$(OUT_DIR)\cosmoaudio.obj
TEST_OBJECTS=$(OUT_DIR)\test.obj
# Default target
all: $(OUT_DIR) $(DLL_TARGET) $(TEST_TARGET)
# Create output directory
$(OUT_DIR):
if not exist $(OUT_DIR) mkdir $(OUT_DIR)
# Rule to build the DLL
$(DLL_TARGET): $(OUT_DIR) $(DLL_OBJECTS)
$(LINK) /DLL $(LDFLAGS) /OUT:$(DLL_TARGET) $(DLL_OBJECTS)
# Rule to build the test program
$(TEST_TARGET): $(OUT_DIR) $(TEST_OBJECTS) $(DLL_TARGET)
$(LINK) $(LDFLAGS) /OUT:$(TEST_TARGET) $(TEST_OBJECTS) $(DLL_TARGET:.dll=.lib) $(TEST_LIBS)
# Rules to compile .c files to .obj files with header dependencies
{.}.c{$(OUT_DIR)}.obj:
$(CC) $(DLL_CFLAGS) /c /Fo$(OUT_DIR)\ $<
$(OUT_DIR)\test.obj: $(OUT_DIR) test.c cosmoaudio.h
$(CC) $(CFLAGS) /c /Fo$(OUT_DIR)\ test.c
$(OUT_DIR)\cosmoaudio.obj: $(OUT_DIR) cosmoaudio.c miniaudio.h cosmoaudio.h
$(CC) $(DLL_CFLAGS) /c /Fo$(OUT_DIR)\ cosmoaudio.c
# Clean target
clean:
if exist $(OUT_DIR) rmdir /s /q $(OUT_DIR)
# Run tests (now called 'check')
check: $(TEST_TARGET)
$(TEST_TARGET)
# Phony targets
.PHONY: all clean check

View file

@ -0,0 +1,251 @@
#define COSMOAUDIO_BUILD
#include "cosmoaudio.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MA_STATIC
#define MA_NO_DECODING
#define MA_NO_ENCODING
#ifdef NDEBUG
#define MA_DR_MP3_NO_STDIO
#endif
#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"
#ifndef NDEBUG
#define LOG(...) fprintf(stderr, __VA_ARGS__)
#else
#define LOG(...) (void)0
#endif
struct CosmoAudio {
ma_device device;
ma_pcm_rb input;
ma_pcm_rb output;
ma_uint32 sampleRate;
ma_uint32 channels;
};
static int read_ring_buffer(ma_pcm_rb* rb, float* pOutput, ma_uint32 frameCount,
ma_uint32 channels) {
ma_result result;
ma_uint32 framesRead;
ma_uint32 framesToRead;
for (framesRead = 0; framesRead < frameCount; framesRead += framesToRead) {
framesToRead = frameCount - framesRead;
void* pMappedBuffer;
result = ma_pcm_rb_acquire_read(rb, &framesToRead, &pMappedBuffer);
if (result != MA_SUCCESS) {
LOG("ma_pcm_rb_acquire_read failed: %s\n", ma_result_description(result));
return COSMOAUDIO_ERROR;
}
if (!framesToRead)
break;
memcpy(pOutput + framesRead * channels, pMappedBuffer,
framesToRead * channels * sizeof(float));
result = ma_pcm_rb_commit_read(rb, framesToRead);
if (result != MA_SUCCESS) {
if (result == MA_AT_END)
break;
LOG("ma_pcm_rb_commit_read failed: %s\n", ma_result_description(result));
return COSMOAUDIO_ERROR;
}
}
return framesRead;
}
static int write_ring_buffer(ma_pcm_rb* rb, const float* pInput,
ma_uint32 frameCount, ma_uint32 channels) {
ma_result result;
ma_uint32 framesWritten;
ma_uint32 framesToWrite;
for (framesWritten = 0; framesWritten < frameCount;
framesWritten += framesToWrite) {
framesToWrite = frameCount - framesWritten;
void* pMappedBuffer;
result = ma_pcm_rb_acquire_write(rb, &framesToWrite, &pMappedBuffer);
if (result != MA_SUCCESS) {
LOG("ma_pcm_rb_acquire_write failed: %s\n",
ma_result_description(result));
return COSMOAUDIO_ERROR;
}
if (!framesToWrite)
break;
memcpy(pMappedBuffer, pInput + framesWritten * channels,
framesToWrite * channels * sizeof(float));
result = ma_pcm_rb_commit_write(rb, framesToWrite);
if (result != MA_SUCCESS) {
if (result == MA_AT_END)
break;
LOG("ma_pcm_rb_commit_write failed: %s\n", ma_result_description(result));
return COSMOAUDIO_ERROR;
}
}
return framesWritten;
}
static void data_callback_f32(ma_device* pDevice, float* pOutput,
const float* pInput, ma_uint32 frameCount) {
struct CosmoAudio* ca = (struct CosmoAudio*)pDevice->pUserData;
read_ring_buffer(&ca->output, pOutput, frameCount, ca->channels);
write_ring_buffer(&ca->input, pInput, frameCount, ca->channels);
}
static void data_callback(ma_device* pDevice, void* pOutput, const void* pInput,
ma_uint32 frameCount) {
data_callback_f32(pDevice, (float*)pOutput, (const float*)pInput, frameCount);
}
/**
* Opens access to speaker and microphone.
*
* @param cap will receive pointer to allocated CosmoAudio object on success,
* which must be freed by caller with cosmoaudio_close()
* @param sampleRate is sample rate in Hz, e.g. 44100
* @param channels is number of channels (1 for mono, 2 for stereo)
* @return 0 on success, or negative error code on failure
*/
COSMOAUDIO_ABI int cosmoaudio_open(struct CosmoAudio** cap, int sampleRate,
int channels) {
// Allocate cosmo audio object.
struct CosmoAudio* ca;
if (!(ca = (struct CosmoAudio*)malloc(sizeof(struct CosmoAudio))))
return COSMOAUDIO_ERROR;
ca->channels = channels;
ca->sampleRate = sampleRate;
// Initialize device.
ma_result result;
ma_device_config deviceConfig = ma_device_config_init(ma_device_type_duplex);
deviceConfig.sampleRate = sampleRate;
deviceConfig.capture.channels = channels;
deviceConfig.capture.format = ma_format_f32;
deviceConfig.capture.shareMode = ma_share_mode_shared;
deviceConfig.playback.channels = channels;
deviceConfig.playback.format = ma_format_f32;
deviceConfig.dataCallback = data_callback;
deviceConfig.pUserData = ca;
result = ma_device_init(NULL, &deviceConfig, &ca->device);
if (result != MA_SUCCESS) {
free(ca);
return COSMOAUDIO_ERROR;
}
// Initialize the speaker ring buffer.
result = ma_pcm_rb_init(ma_format_f32, channels,
ca->device.playback.internalPeriodSizeInFrames * 10,
NULL, NULL, &ca->output);
if (result != MA_SUCCESS) {
ma_device_uninit(&ca->device);
free(ca);
return COSMOAUDIO_ERROR;
}
ma_pcm_rb_set_sample_rate(&ca->output, sampleRate);
// Initialize the microphone ring buffer.
result = ma_pcm_rb_init(ma_format_f32, channels,
ca->device.capture.internalPeriodSizeInFrames * 10,
NULL, NULL, &ca->input);
if (result != MA_SUCCESS) {
ma_pcm_rb_uninit(&ca->output);
ma_device_uninit(&ca->device);
free(ca);
return COSMOAUDIO_ERROR;
}
ma_pcm_rb_set_sample_rate(&ca->output, sampleRate);
// Start audio playback.
if (ma_device_start(&ca->device) != MA_SUCCESS) {
ma_pcm_rb_uninit(&ca->input);
ma_pcm_rb_uninit(&ca->output);
ma_device_uninit(&ca->device);
free(ca);
return COSMOAUDIO_ERROR;
}
*cap = ca;
return COSMOAUDIO_SUCCESS;
}
/**
* Closes audio device and frees all associated resources.
*
* @param ca is CosmoAudio object returned earlier by cosmoaudio_open()
* @return 0 on success, or negative error code on failure
*/
COSMOAUDIO_ABI int cosmoaudio_close(struct CosmoAudio* ca) {
ma_device_uninit(&ca->device);
ma_pcm_rb_uninit(&ca->output);
free(ca);
return COSMOAUDIO_SUCCESS;
}
/**
* Writes raw audio data to speaker.
*
* The data is written to a ring buffer in real-time, which is then
* played back very soon on the audio device. This has tolerence for
* a certain amount of buffering, but expects that this function is
* repeatedly called at a regular time interval. The caller should
* have its own sleep loop for this purpose.
*
* @param ca is CosmoAudio object returned earlier by cosmoaudio_open()
* @param data is pointer to raw audio samples, expected to be in the range
* -1.0 to 1.0, where channels are interleaved
* @param frames is the number of frames (i.e. number of samples divided by
* number of channels) from `data` to write to audio device
* @return number of frames written, or negative error code on failure
*/
COSMOAUDIO_ABI int cosmoaudio_write(struct CosmoAudio* ca, const float* data,
int frames) {
return write_ring_buffer(&ca->output, data, frames, ca->channels);
}
/**
* Reads raw audio data from microphone.
*
* The data is read from a ring buffer in real-time, which is then
* played back very soon on the audio device. This has tolerence for
* a certain amount of buffering, but expects that this function is
* repeatedly called at a regular time interval. The caller should
* have its own sleep loop for this purpose.
*
* @param ca is CosmoAudio object returned earlier by cosmoaudio_open()
* @param data is pointer to raw audio samples, expected to be in the range
* -1.0 to 1.0, where channels are interleaved
* @param frames is the number of frames (i.e. number of samples divided by
* number of channels) from `data` to read from microphone
* @return number of frames read, or negative error code on failure
*/
COSMOAUDIO_ABI int cosmoaudio_read(struct CosmoAudio* ca, float* data,
int frames) {
int read;
for (int i = 0; i < frames; i += read) {
int remaining = frames - i;
read = read_ring_buffer(&ca->input, data + i * ca->channels, remaining,
ca->channels);
if (read < 0)
return read;
}
return frames;
}
#ifdef _MSC_VER
#include <Windows.h>
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call,
LPVOID lpReserved) {
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
(void)hModule;
(void)lpReserved;
(void)ul_reason_for_call;
}
#endif

Binary file not shown.

View file

@ -0,0 +1,41 @@
#ifndef COSMOAUDIO_H_
#define COSMOAUDIO_H_
#ifdef _MSC_VER
#define COSMOAUDIO_ABI
#ifdef COSMOAUDIO_BUILD
#define COSMOAUDIO_API __declspec(dllexport)
#else
#define COSMOAUDIO_API __declspec(dllimport)
#endif
#else
#define COSMOAUDIO_API
#ifdef __x86_64__
#define COSMOAUDIO_ABI __attribute__((__ms_abi__))
#else
#define COSMOAUDIO_ABI
#endif
#endif
#define COSMOAUDIO_SUCCESS 0
#define COSMOAUDIO_ERROR -1
#ifdef __cplusplus
extern "C" {
#endif
struct CosmoAudio;
COSMOAUDIO_API int cosmoaudio_open(struct CosmoAudio **, int,
int) COSMOAUDIO_ABI;
COSMOAUDIO_API int cosmoaudio_close(struct CosmoAudio *) COSMOAUDIO_ABI;
COSMOAUDIO_API int cosmoaudio_write(struct CosmoAudio *, const float *,
int) COSMOAUDIO_ABI;
COSMOAUDIO_API int cosmoaudio_read(struct CosmoAudio *, float *,
int) COSMOAUDIO_ABI;
#ifdef __cplusplus
}
#endif
#endif /* COSMOAUDIO_H_ */

View file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,36 @@
#include <errno.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <time.h>
#include "cosmoaudio.h"
#ifndef M_PIf
#define M_PIf 3.14159265358979323846f
#endif
int main() {
int hz = 44100;
int channels = 2;
struct CosmoAudio *ca;
if (cosmoaudio_open(&ca, hz, channels) != COSMOAUDIO_SUCCESS) {
fprintf(stderr, "%s: failed to open audio\n", argv[0]);
return 1;
}
int n = 1000;
int sample = 0;
float *buf = (float *)malloc(sizeof(float) * channels * n);
for (;;) {
for (int i = 0; i < 128; i++) {
float freq = 440;
float t = (float)sample++ / hz;
if (sample == hz)
sample = 0;
buf[i * channels] = sinf(freq * 2.f * M_PIf * t);
buf[i * channels + 1] = sinf(freq * 2.f * M_PIf * t);
}
cosmoaudio_write(ca, buf, 128);
}
}