Skip to content

Commit

Permalink
refactor and start working on a pulseaudio backend
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Apr 15, 2019
1 parent 23d6807 commit 3270366
Show file tree
Hide file tree
Showing 12 changed files with 930 additions and 372 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea
cmake-build*
.DS_Store
*.user
build
19 changes: 17 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,29 @@ cmake_minimum_required(VERSION 3.12)
project(libstdaudio)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -O0 -fsanitize=undefined")
if (APPLE)
set(CMAKE_EXE_LINKER_FLAGS "-framework CoreAudio")
endif ()

# add the headers to at least one target so that they get
# parsed as part of the project in some IDEs
set(headers
include/__audio_base.h
include/__audio_buffer.h
include/__audio_device.h
include/__audio_exception.h
include/__dynamic_library.h
include/audio
include/cpp/span.hpp
include/audio_backend/__coreaudio_backend.h
include/audio_backend/__null_backend.h
include/audio_backend/__portaudio_backend.h
include/audio_backend/__pulseaudio_backend.h
)
include_directories(include)

add_executable(white_noise examples/white_noise.cpp)
add_executable(white_noise examples/white_noise.cpp ${headers})
target_link_libraries(white_noise PRIVATE ${CMAKE_DL_LIBS})
add_executable(print_devices examples/print_devices.cpp)
target_link_libraries(print_devices PRIVATE ${CMAKE_DL_LIBS})
Expand Down
10 changes: 6 additions & 4 deletions examples/sine_wave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@ int main() {
float delta = 2.0f * frequency_hz * float(M_PI / device->get_sample_rate());
float phase = 0;

static int64_t i = 0;
device->connect([=](audio_device& device, audio_device_buffers& buffers) mutable {
auto buffer = *buffers.output_buffer();
for (int frame = 0; frame < buffer.size_frames(); ++frame) {
float next_sample = std::sin(phase);
float next_sample_1 = std::sin(phase);
float next_sample_2 = 0.f;std::sin(2.0f * frequency_hz * float(M_PI * double(i++) / device.get_sample_rate()));
phase += delta;
for (int channel = 0; channel < buffer.size_channels(); ++channel) {
buffer(frame, channel) = next_sample;
buffer(frame, channel) = next_sample_1 - next_sample_2;
}
}
});

device->start();
std::this_thread::sleep_for(std::chrono::seconds(2));
std::this_thread::sleep_for(std::chrono::seconds(20));
device->stop();
}
}
41 changes: 41 additions & 0 deletions include/__audio_base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include <optional>
#include <cassert>
#include <string_view>
#include <atomic>

// TODO: this is a temporary measure until std::span becomes available
#include "cpp/span.hpp"
using namespace TCB_SPAN_NAMESPACE_NAME;

#define _LIBSTDAUDIO_NAMESPACE std::experimental

#define _LIBSTDAUDIO_NAMESPACE_BEGIN namespace _LIBSTDAUDIO_NAMESPACE {
#define _LIBSTDAUDIO_NAMESPACE_END }

_LIBSTDAUDIO_NAMESPACE_BEGIN

struct audio_null_driver_t {};

#ifdef __APPLE__
struct __coreaudio_driver_t {};
using audio_default_driver_t = __coreaudio_driver_t;
#else

#if __has_include(<pulse/pulseaudio.h>)
struct __pulseaudio_driver_t {};
#endif
#if __has_include(<portaudio.h>)
struct __portaudio_driver_t {};
#endif

#if __has_include(<pulse/pulseaudio.h>)
using audio_default_driver_t = __pulseaudio_driver_t;
#elif __has_include(<portaudio.h>)
using audio_default_driver_t = __portaudio_driver_t;
#endif
#endif // __APPLE__

_LIBSTDAUDIO_NAMESPACE_END

2 changes: 2 additions & 0 deletions include/__audio_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)

#pragma once
#include <__audio_base.h>

_LIBSTDAUDIO_NAMESPACE_BEGIN

struct audio_buffer_order_interleaved {};
Expand Down
1 change: 1 addition & 0 deletions include/__audio_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)

#pragma once
#include <__audio_base.h>

_LIBSTDAUDIO_NAMESPACE_BEGIN

Expand Down
13 changes: 13 additions & 0 deletions include/__audio_exception.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once
#include <__audio_base.h>
#include <stdexcept>

_LIBSTDAUDIO_NAMESPACE_BEGIN

struct audio_device_exception : public runtime_error {
explicit audio_device_exception(const char* what)
: runtime_error(what) {
}
};

_LIBSTDAUDIO_NAMESPACE_END
70 changes: 70 additions & 0 deletions include/__dynamic_library.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#pragma once
#include <__audio_base.h>

#if defined(_WIN32)
#define __STDAUDIO_HAS_DYLIB 1
#include <windows.h>
#elif __has_include(<dlfcn.h>)
#define __STDAUDIO_HAS_DYLIB 1
#include <dlfcn.h>
#endif

_LIBSTDAUDIO_NAMESPACE_BEGIN
#if __STDAUDIO_HAS_DYLIB
class __dynamic_library
{
public:
explicit __dynamic_library(const char* const so) /* not noexcept - some DLL constructors can throw */
{
#ifdef _WIN32
impl = (void*)LoadLibraryA(so);
#else
impl = dlopen(so, RTLD_LAZY | RTLD_LOCAL | RTLD_NODELETE);
#endif
}

__dynamic_library(const __dynamic_library&) noexcept = delete;
__dynamic_library& operator=(const __dynamic_library&) noexcept = delete;
__dynamic_library(__dynamic_library&& other)
{
impl = other.impl;
other.impl = nullptr;
}

__dynamic_library& operator=(__dynamic_library&& other) noexcept
{
impl = other.impl;
other.impl = nullptr;
return *this;
}

~__dynamic_library()
{
if (impl)
{
#ifdef _WIN32
FreeLibrary((HMODULE)impl);
#else
dlclose(impl);
#endif
}
}

template <typename T>
T symbol(const char* const sym) const noexcept
{
#ifdef _WIN32
return (T)GetProcAddress((HMODULE)impl, sym);
#else
return (T)dlsym(impl, sym);
#endif
}

operator bool() const { return bool(impl); }

private:
void* impl{};
};
#endif

_LIBSTDAUDIO_NAMESPACE_END
39 changes: 5 additions & 34 deletions include/audio
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,7 @@
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)

#pragma once

#include <optional>
#include <cassert>
#include <string_view>
#include <atomic>

// TODO: this is a temporary measure until std::span becomes available
#include "cpp/span.hpp"
using namespace TCB_SPAN_NAMESPACE_NAME;

#define _LIBSTDAUDIO_NAMESPACE std::experimental

#define _LIBSTDAUDIO_NAMESPACE_BEGIN namespace _LIBSTDAUDIO_NAMESPACE {
#define _LIBSTDAUDIO_NAMESPACE_END }

_LIBSTDAUDIO_NAMESPACE_BEGIN

struct audio_null_driver_t {};

#ifdef __APPLE__
struct __coreaudio_driver_t {};
using audio_default_driver_t = __coreaudio_driver_t;
#else

#if __has_include(<portaudio.h>)
struct __portaudio_driver_t {};
using audio_default_driver_t = __portaudio_driver_t;
#endif

#endif // __APPLE__

_LIBSTDAUDIO_NAMESPACE_END

#include <__audio_base.h>
#include <__audio_buffer.h>
#include <__audio_device.h>
#include <audio_backend/__null_backend.h>
Expand All @@ -46,4 +14,7 @@ _LIBSTDAUDIO_NAMESPACE_END
#endif // __APPLE__
#if __has_include(<portaudio.h>)
#include <audio_backend/__portaudio_backend.h>
#endif // __APPLE__
#endif
#if __has_include(<pulse/pulseaudio.h>)
#include <audio_backend/__pulseaudio_backend.h>
#endif
10 changes: 4 additions & 6 deletions include/audio_backend/__coreaudio_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

#pragma once

#include <__audio_base.h>
#include <__audio_device.h>
#include <__audio_buffer.h>
#include <__audio_exception.h>
#include <cctype>
#include <string>
#include <iostream>
Expand Down Expand Up @@ -72,12 +76,6 @@ class __coreaudio_util {
}
};

struct audio_device_exception : public runtime_error {
explicit audio_device_exception(const char* what)
: runtime_error(what) {
}
};

using __coreaudio_device = audio_basic_device<__coreaudio_driver_t>;

template<>
Expand Down
Loading

0 comments on commit 3270366

Please sign in to comment.