diff options
Diffstat (limited to 'extern')
19 files changed, 1720 insertions, 15 deletions
diff --git a/extern/audaspace/CMakeLists.txt b/extern/audaspace/CMakeLists.txt index 84f5e8441c1..fe1bf5dc742 100644 --- a/extern/audaspace/CMakeLists.txt +++ b/extern/audaspace/CMakeLists.txt @@ -284,6 +284,15 @@ if(AUDASPACE_STANDALONE) option(WITH_PYTHON "Build With Python Library" TRUE) option(WITH_SDL "Build With SDL" TRUE) option(WITH_STRICT_DEPENDENCIES "Error and abort instead of warning if a library is not found." FALSE) + if(APPLE) + option(WITH_COREAUDIO "Build With CoreAudio" TRUE) + endif() + if(NOT WIN32 AND NOT APPLE) + option(WITH_PULSEAUDIO "Build With PulseAudio" TRUE) + endif() + if(WIN32) + option(WITH_WASAPI "Build With WASAPI" TRUE) + endif() if(WITH_STRICT_DEPENDENCIES) set(PACKAGE_OPTION REQUIRED) @@ -291,7 +300,7 @@ if(AUDASPACE_STANDALONE) endif() if(AUDASPACE_STANDALONE) - if(WIN32) + if(WIN32 OR APPLE) set(DEFAULT_PLUGIN_PATH "." CACHE STRING "Default plugin installation and loading path.") set(DOCUMENTATION_INSTALL_PATH "doc" CACHE PATH "Path where the documentation is installed.") else() @@ -302,14 +311,18 @@ endif() if(AUDASPACE_STANDALONE) cmake_dependent_option(SEPARATE_C "Build C Binding as separate library" TRUE "WITH_C" FALSE) + cmake_dependent_option(PLUGIN_COREAUDIO "Build CoreAudio Plugin" TRUE "WITH_COREAUDIO;SHARED_LIBRARY" FALSE) cmake_dependent_option(PLUGIN_FFMPEG "Build FFMPEG Plugin" TRUE "WITH_FFMPEG;SHARED_LIBRARY" FALSE) cmake_dependent_option(PLUGIN_JACK "Build JACK Plugin" TRUE "WITH_JACK;SHARED_LIBRARY" FALSE) cmake_dependent_option(PLUGIN_LIBSNDFILE "Build LibSndFile Plugin" TRUE "WITH_LIBSNDFILE;SHARED_LIBRARY" FALSE) cmake_dependent_option(PLUGIN_OPENAL "Build OpenAL Plugin" TRUE "WITH_OPENAL;SHARED_LIBRARY" FALSE) + cmake_dependent_option(PLUGIN_PULSEAUDIO "Build PulseAudio Plugin" TRUE "WITH_PULSEAUDIO;SHARED_LIBRARY" FALSE) cmake_dependent_option(PLUGIN_SDL "Build SDL Plugin" TRUE "WITH_SDL;SHARED_LIBRARY" FALSE) + cmake_dependent_option(PLUGIN_WASAPI "Build WASAPI Plugin" TRUE "WITH_WASAPI;SHARED_LIBRARY" FALSE) cmake_dependent_option(WITH_PYTHON_MODULE "Build Python Module" TRUE "WITH_PYTHON" FALSE) cmake_dependent_option(USE_SDL2 "Use SDL2 instead of 1 if available" TRUE "WITH_SDL" FALSE) cmake_dependent_option(DYNLOAD_JACK "Dynamically load JACK" FALSE "WITH_JACK" FALSE) + cmake_dependent_option(DYNLOAD_PULSEAUDIO "Dynamically load PulseAudio" FALSE "WITH_PULSEAUDIO" FALSE) cmake_dependent_option(WITH_BINDING_DOCS "Build C/Python HTML Documentation with Sphinx" TRUE "WITH_PYTHON_MODULE" FALSE) endif() @@ -380,16 +393,16 @@ if(WITH_C) bindings/C/AUD_Types.h ) -if(WITH_FFTW) - list(APPEND C_SRC + if(WITH_FFTW) + list(APPEND C_SRC bindings/C/AUD_HRTF.cpp bindings/C/AUD_ImpulseResponse.cpp - ) + ) list(APPEND C_HDR bindings/C/AUD_HRTF.h bindings/C/AUD_ImpulseResponse.h - ) + ) endif() if(NOT SEPARATE_C) @@ -400,6 +413,44 @@ if(WITH_FFTW) endif() endif() +# CoreAudio +if(WITH_COREAUDIO) + find_library(COREAUDIO_LIBRARY CoreAudio) + find_library(AUDIOUNIT_LIBRARY AudioUnit) + find_library(AUDIOTOOLBOX_LIBRARY AudioToolbox) + find_path(AUDIOUNIT_INCLUDE_DIR AudioUnit/AudioUnit.h) + find_path(AUDIOTOOLBOX_INCLUDE_DIR AudioToolbox/CoreAudioClock.h) + + if(COREAUDIO_LIBRARY AND AUDIOUNIT_LIBRARY AND AUDIOUNIT_INCLUDE_DIR) + set(COREAUDIO_LIBRARIES ${COREAUDIO_LIBRARY} ${AUDIOUNIT_LIBRARY} ${AUDIOTOOLBOX_LIBRARY}) + set(COREAUDIO_INCLUDE_DIRS ${AUDIOUNIT_INCLUDE_DIR} ${AUDIOTOOLBOX_INCLUDE_DIR}) + + set(COREAUDIO_SRC + plugins/coreaudio/CoreAudioDevice.cpp + plugins/coreaudio/CoreAudioSynchronizer.cpp + ) + set(COREAUDIO_HDR + plugins/coreaudio/CoreAudioDevice.h + plugins/coreaudio/CoreAudioSynchronizer.h + ) + + if(NOT PLUGIN_COREAUDIO) + list(APPEND INCLUDE ${COREAUDIO_INCLUDE_DIRS}) + list(APPEND LIBRARIES ${COREAUDIO_LIBRARIES}) + list(APPEND SRC ${COREAUDIO_SRC}) + list(APPEND HDR ${COREAUDIO_HDR}) + list(APPEND STATIC_PLUGINS CoreAudioDevice) + endif() + else() + if(WITH_STRICT_DEPENDENCIES) + message(FATAL_ERROR "CoreAudio not found!") + else() + set(WITH_COREAUDIO FALSE CACHE BOOL "Build With CoreAudio" FORCE) + message(WARNING "CoreAudio not found, plugin will not be built.") + endif() + endif() +endif() + # FFMPEG if(WITH_FFMPEG) if(AUDASPACE_STANDALONE) @@ -504,7 +555,7 @@ if(WITH_JACK) plugins/jack/JackSymbols.h ) - if(DYNLOAD_JACK) + if(DYNLOAD_JACK) add_definitions(-DDYNLOAD_JACK) endif() @@ -598,6 +649,42 @@ if(WITH_OPENAL) endif() endif() +# PulseAudio +if(WITH_PULSEAUDIO) + if(AUDASPACE_STANDALONE) + find_package(LibPulse ${PACKAGE_OPTION}) + endif() + + if(LIBPULSE_FOUND) + set(PULSEAUDIO_SRC + plugins/pulseaudio/PulseAudioDevice.cpp + plugins/pulseaudio/PulseAudioLibrary.cpp + ) + set(PULSEAUDIO_HDR + plugins/pulseaudio/PulseAudioDevice.h + plugins/pulseaudio/PulseAudioLibrary.h + plugins/pulseaudio/PulseAudioSymbols.h + ) + + if(DYNLOAD_PULSEAUDIO) + add_definitions(-DDYNLOAD_PULSEAUDIO) + endif() + + if(NOT PLUGIN_PULSEAUDIO) + list(APPEND INCLUDE ${LIBPULSE_INCLUDE_DIR}) + if(NOT DYNLOAD_PULSEAUDIO) + list(APPEND LIBRARIES ${LIBPULSE_LIBRARY}) + endif() + list(APPEND SRC ${PULSEAUDIO_SRC}) + list(APPEND HDR ${PULSEAUDIO_HDR}) + list(APPEND STATIC_PLUGINS PulseAudioDevice) + endif() + else() + set(WITH_PULSEAUDIO FALSE CACHE BOOL "Build With PulseAudio" FORCE) + message(WARNING "PulseAudio not found, plugin will not be built.") + endif() +endif() + # Python if(WITH_PYTHON) if(AUDASPACE_STANDALONE) @@ -672,6 +759,23 @@ if(WITH_SDL) endif() endif() +# WASAPI +if(WITH_WASAPI) + set(WASAPI_SRC + plugins/wasapi/WASAPIDevice.cpp + ) + set(WASAPI_HDR + plugins/wasapi/WASAPIDevice.h + ) + + if(NOT PLUGIN_WASAPI) + list(APPEND LIBRARIES ksuser) + list(APPEND SRC ${WASAPI_SRC}) + list(APPEND HDR ${WASAPI_HDR}) + list(APPEND STATIC_PLUGINS WASAPIDevice) + endif() +endif() + # library configuration if(SHARED_LIBRARY) @@ -757,6 +861,17 @@ endif() # plugins +if(WITH_COREAUDIO AND PLUGIN_COREAUDIO) + add_definitions(-DCOREAUDIO_PLUGIN) + include_directories(${INCLUDE} ${COREAUDIO_INCLUDE_DIRS}) + add_library(audcoreaudio SHARED ${COREAUDIO_SRC} ${COREAUDIO_HDR} ${HDR}) + if(WITH_VERSIONED_PLUGINS) + set_target_properties(audcoreaudio PROPERTIES SOVERSION ${AUDASPACE_VERSION}) + endif() + target_link_libraries(audcoreaudio audaspace ${COREAUDIO_LIBRARIES}) + install(TARGETS audcoreaudio DESTINATION ${DEFAULT_PLUGIN_PATH}) +endif() + if(WITH_FFMPEG AND PLUGIN_FFMPEG) add_definitions(-DFFMPEG_PLUGIN) include_directories(${INCLUDE} ${FFMPEG_INCLUDE_DIRS}) @@ -797,6 +912,19 @@ if(WITH_OPENAL AND PLUGIN_OPENAL) install(TARGETS audopenal DESTINATION ${DEFAULT_PLUGIN_PATH}) endif() +if(WITH_PULSEAUDIO AND PLUGIN_PULSEAUDIO) + add_definitions(-DPULSEAUDIO_PLUGIN) + include_directories(${INCLUDE} ${LIBPULSE_INCLUDE_DIR}) + add_library(audpulseaudio SHARED ${PULSEAUDIO_SRC} ${PULSEAUDIO_HDR} ${HDR}) + set_target_properties(audpulseaudio PROPERTIES SOVERSION ${AUDASPACE_VERSION}) + if(DYNLOAD_PULSEAUDIO) + target_link_libraries(audpulseaudio audaspace) + else() + target_link_libraries(audpulseaudio audaspace ${LIBPULSE_LIBRARY}) + endif() + install(TARGETS audpulseaudio DESTINATION ${DEFAULT_PLUGIN_PATH}) +endif() + if(WITH_SDL AND PLUGIN_SDL) add_definitions(-DSDL_PLUGIN) include_directories(${INCLUDE} ${SDL_INCLUDE_DIR}) @@ -806,6 +934,17 @@ if(WITH_SDL AND PLUGIN_SDL) install(TARGETS audsdl DESTINATION ${DEFAULT_PLUGIN_PATH}) endif() +if(WITH_WASAPI AND PLUGIN_WASAPI) + add_definitions(-DWASAPI_PLUGIN) + include_directories(${INCLUDE}) + add_library(audwasapi SHARED ${WASAPI_SRC} ${WASAPI_HDR} ${HDR}) + if(WITH_VERSIONED_PLUGINS) + set_target_properties(audwasapi PROPERTIES SOVERSION ${AUDASPACE_VERSION}) + endif() + target_link_libraries(audwasapi audaspace ksuser) + install(TARGETS audwasapi DESTINATION ${DEFAULT_PLUGIN_PATH}) +endif() + # dlls if(WIN32) diff --git a/extern/audaspace/bindings/C/AUD_Special.cpp b/extern/audaspace/bindings/C/AUD_Special.cpp index a83465620ab..ac876a01eb3 100644 --- a/extern/audaspace/bindings/C/AUD_Special.cpp +++ b/extern/audaspace/bindings/C/AUD_Special.cpp @@ -376,7 +376,7 @@ AUD_API AUD_Device* AUD_init(const char* device, AUD_DeviceSpecs specs, int buff { try { - std::shared_ptr<IDeviceFactory> factory = DeviceManager::getDeviceFactory(device); + std::shared_ptr<IDeviceFactory> factory = device ? DeviceManager::getDeviceFactory(device) : DeviceManager::getDefaultDeviceFactory(); if(factory) { diff --git a/extern/audaspace/blender_config.cmake b/extern/audaspace/blender_config.cmake index 12810e2b044..14d7d593838 100644 --- a/extern/audaspace/blender_config.cmake +++ b/extern/audaspace/blender_config.cmake @@ -10,19 +10,24 @@ set(WITH_FFMPEG ${WITH_CODEC_FFMPEG}) # "Build With FFMPEG" set(WITH_FFTW FALSE) # "Build With FFTW" set(WITH_LIBSNDFILE ${WITH_CODEC_SNDFILE}) # "Build With LibSndFile" set(SEPARATE_C FALSE) # "Build C Binding as separate library" +set(PLUGIN_COREAUDIO FALSE) # "Build CoreAudio Plugin" set(PLUGIN_FFMPEG FALSE) # "Build FFMPEG Plugin" set(PLUGIN_JACK FALSE) # "Build JACK Plugin" set(PLUGIN_LIBSNDFILE FALSE) # "Build LibSndFile Plugin" set(PLUGIN_OPENAL FALSE) # "Build OpenAL Plugin" +set(PLUGIN_PULSEAUDIO FALSE) # "Build PulseAudio Plugin" set(PLUGIN_SDL FALSE) # "Build SDL Plugin" +set(PLUGIN_WASAPI FALSE) # "Build WASAPI Plugin" set(WITH_PYTHON_MODULE FALSE) # "Build Python Module" set(DYNLOAD_JACK ${WITH_JACK_DYNLOAD}) # "Dynamically load JACK" +set(DYNLOAD_PULSEAUDIO ${WITH_PULSEAUDIO_DYNLOAD}) # "Dynamically load PulseAudio" set(WITH_BINDING_DOCS FALSE) # "Build C/Python HTML Documentation with Sphinx" set(DEFAULT_PLUGIN_PATH "plugins") # "Default plugin installation and loading path." set(FFMPEG_FOUND ${WITH_CODEC_FFMPEG}) set(JACK_FOUND ${WITH_JACK}) set(LIBSNDFILE_FOUND ${WITH_CODEC_SNDFILE}) set(OPENAL_FOUND ${WITH_OPENAL}) +set(LIBPULSE_FOUND ${WITH_PULSEAUDIO}) set(PYTHONLIBS_FOUND TRUE) set(NUMPY_FOUND ${WITH_PYTHON_NUMPY}) set(NUMPY_INCLUDE_DIRS ${PYTHON_NUMPY_INCLUDE_DIRS}) diff --git a/extern/audaspace/plugins/coreaudio/CoreAudioDevice.cpp b/extern/audaspace/plugins/coreaudio/CoreAudioDevice.cpp new file mode 100644 index 00000000000..113ceccad60 --- /dev/null +++ b/extern/audaspace/plugins/coreaudio/CoreAudioDevice.cpp @@ -0,0 +1,235 @@ +/******************************************************************************* + * Copyright 2009-2021 Jörg Müller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ + +#include "CoreAudioDevice.h" +#include "devices/DeviceManager.h" +#include "devices/IDeviceFactory.h" +#include "Exception.h" +#include "IReader.h" + +AUD_NAMESPACE_BEGIN + +OSStatus CoreAudioDevice::CoreAudio_mix(void* data, AudioUnitRenderActionFlags* flags, const AudioTimeStamp* time_stamp, UInt32 bus_number, UInt32 number_frames, AudioBufferList* buffer_list) +{ + CoreAudioDevice* device = (CoreAudioDevice*)data; + + for(int i = 0; i < buffer_list->mNumberBuffers; i++) + { + auto& buffer = buffer_list->mBuffers[i]; + + device->mix((data_t*)buffer.mData, buffer.mDataByteSize / AUD_DEVICE_SAMPLE_SIZE(device->m_specs)); + } + + return noErr; +} + +void CoreAudioDevice::playing(bool playing) +{ + if(m_playback != playing) + { + if(playing) + AudioOutputUnitStart(m_audio_unit); + else + AudioOutputUnitStop(m_audio_unit); + } + + m_playback = playing; +} + +CoreAudioDevice::CoreAudioDevice(DeviceSpecs specs, int buffersize) : +m_playback(false), +m_audio_unit(nullptr) +{ + AudioComponentDescription component_description = {}; + + component_description.componentType = kAudioUnitType_Output; + component_description.componentSubType = kAudioUnitSubType_DefaultOutput; + component_description.componentManufacturer = kAudioUnitManufacturer_Apple; + + AudioComponent component = AudioComponentFindNext(nullptr, &component_description); + + if(!component) + AUD_THROW(DeviceException, "The audio device couldn't be opened with CoreAudio."); + + OSStatus status = AudioComponentInstanceNew(component, &m_audio_unit); + + if(status != noErr) + AUD_THROW(DeviceException, "The audio device couldn't be opened with CoreAudio."); + + AudioStreamBasicDescription stream_basic_description = {}; + + if(specs.channels == CHANNELS_INVALID) + specs.channels = CHANNELS_STEREO; + if(specs.format == FORMAT_INVALID) + specs.format = FORMAT_FLOAT32; + if(specs.rate == RATE_INVALID) + specs.rate = RATE_48000; + + switch(specs.format) + { + case FORMAT_U8: + stream_basic_description.mFormatFlags = 0; + stream_basic_description.mBitsPerChannel = 8; + break; + case FORMAT_S16: + stream_basic_description.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger; + stream_basic_description.mBitsPerChannel = 16; + break; + case FORMAT_S24: + stream_basic_description.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger; + stream_basic_description.mBitsPerChannel = 24; + break; + case FORMAT_S32: + stream_basic_description.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger; + stream_basic_description.mBitsPerChannel = 32; + break; + case FORMAT_FLOAT32: + stream_basic_description.mFormatFlags = kLinearPCMFormatFlagIsFloat; + stream_basic_description.mBitsPerChannel = 32; + break; + case FORMAT_FLOAT64: + stream_basic_description.mFormatFlags = kLinearPCMFormatFlagIsFloat; + stream_basic_description.mBitsPerChannel = 64; + break; + default: + specs.format = FORMAT_FLOAT32; + stream_basic_description.mFormatFlags = kLinearPCMFormatFlagIsFloat; + stream_basic_description.mBitsPerChannel = 32; + break; + } + + stream_basic_description.mSampleRate = specs.rate; + stream_basic_description.mFormatID = kAudioFormatLinearPCM; + stream_basic_description.mFormatFlags |= kAudioFormatFlagsNativeEndian | kLinearPCMFormatFlagIsPacked; + stream_basic_description.mBytesPerPacket = stream_basic_description.mBytesPerFrame = AUD_DEVICE_SAMPLE_SIZE(specs); + stream_basic_description.mFramesPerPacket = 1; + stream_basic_description.mChannelsPerFrame = specs.channels; + + status = AudioUnitSetProperty(m_audio_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &stream_basic_description, sizeof(stream_basic_description)); + + if(status != noErr) + { + AudioComponentInstanceDispose(m_audio_unit); + AUD_THROW(DeviceException, "The audio device couldn't be opened with CoreAudio."); + } + + m_specs = specs; + + AURenderCallbackStruct render_callback_struct; + render_callback_struct.inputProc = CoreAudioDevice::CoreAudio_mix; + render_callback_struct.inputProcRefCon = this; + + status = AudioUnitSetProperty(m_audio_unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &render_callback_struct, sizeof(render_callback_struct)); + + if(status != noErr) + { + AudioComponentInstanceDispose(m_audio_unit); + AUD_THROW(DeviceException, "The audio device couldn't be opened with CoreAudio."); + } + + status = AudioUnitInitialize(m_audio_unit); + + if(status != noErr) + { + AudioComponentInstanceDispose(m_audio_unit); + AUD_THROW(DeviceException, "The audio device couldn't be opened with CoreAudio."); + } + + try + { + m_synchronizer = std::unique_ptr<CoreAudioSynchronizer>(new CoreAudioSynchronizer(m_audio_unit)); + } + catch(Exception&) + { + AudioComponentInstanceDispose(m_audio_unit); + throw; + } + + create(); +} + +CoreAudioDevice::~CoreAudioDevice() +{ + AudioOutputUnitStop(m_audio_unit); + AudioUnitUninitialize(m_audio_unit); + AudioComponentInstanceDispose(m_audio_unit); + + destroy(); +} + +ISynchronizer* CoreAudioDevice::getSynchronizer() +{ + return m_synchronizer.get(); +} + +class CoreAudioDeviceFactory : public IDeviceFactory +{ +private: + DeviceSpecs m_specs; + int m_buffersize; + +public: + CoreAudioDeviceFactory() : + m_buffersize(AUD_DEFAULT_BUFFER_SIZE) + { + m_specs.format = FORMAT_FLOAT32; + m_specs.channels = CHANNELS_STEREO; + m_specs.rate = RATE_48000; + } + + virtual std::shared_ptr<IDevice> openDevice() + { + return std::shared_ptr<IDevice>(new CoreAudioDevice(m_specs, m_buffersize)); + } + + virtual int getPriority() + { + return 1 << 15; + } + + virtual void setSpecs(DeviceSpecs specs) + { + m_specs = specs; + } + + virtual void setBufferSize(int buffersize) + { + m_buffersize = buffersize; + } + + virtual void setName(std::string name) + { + } +}; + +void CoreAudioDevice::registerPlugin() +{ + DeviceManager::registerDevice("CoreAudio", std::shared_ptr<IDeviceFactory>(new CoreAudioDeviceFactory)); +} + +#ifdef COREAUDIO_PLUGIN +extern "C" AUD_PLUGIN_API void registerPlugin() +{ + CoreAudioDevice::registerPlugin(); +} + +extern "C" AUD_PLUGIN_API const char* getName() +{ + return "CoreAudio"; +} +#endif + +AUD_NAMESPACE_END diff --git a/extern/audaspace/plugins/coreaudio/CoreAudioDevice.h b/extern/audaspace/plugins/coreaudio/CoreAudioDevice.h new file mode 100644 index 00000000000..3770228db6f --- /dev/null +++ b/extern/audaspace/plugins/coreaudio/CoreAudioDevice.h @@ -0,0 +1,100 @@ +/******************************************************************************* + * Copyright 2009-2021 Jörg Müller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ + +#pragma once + +#ifdef COREAUDIO_PLUGIN +#define AUD_BUILD_PLUGIN +#endif + +/** + * @file CoreAudioDevice.h + * @ingroup plugin + * The CoreAudioDevice class. + */ + +#include "CoreAudioSynchronizer.h" +#include "devices/SoftwareDevice.h" + +#include <memory> + +#include <AudioUnit/AudioUnit.h> + +AUD_NAMESPACE_BEGIN + +/** + * This device plays back through CoreAudio, the Apple audio API. + */ +class AUD_PLUGIN_API CoreAudioDevice : public SoftwareDevice +{ +private: + /** + * Whether there is currently playback. + */ + bool m_playback; + + /** + * The CoreAudio AudioUnit. + */ + AudioUnit m_audio_unit; + + /** + * The Synchronizer. + */ + std::unique_ptr<CoreAudioSynchronizer> m_synchronizer; + + /** + * Mixes the next bytes into the buffer. + * \param data The CoreAudio device. + * \param flags Unused flags. + * \param time_stamp Unused time stamp. + * \param bus_number Unused bus number. + * \param number_frames Unused number of frames. + * \param buffer_list The list of buffers to be filled. + */ + AUD_LOCAL static OSStatus CoreAudio_mix(void* data, AudioUnitRenderActionFlags* flags, const AudioTimeStamp* time_stamp, UInt32 bus_number, UInt32 number_frames, AudioBufferList* buffer_list); + + // delete copy constructor and operator= + CoreAudioDevice(const CoreAudioDevice&) = delete; + CoreAudioDevice& operator=(const CoreAudioDevice&) = delete; + +protected: + virtual void playing(bool playing); + +public: + /** + * Opens the CoreAudio audio device for playback. + * \param specs The wanted audio specification. + * \param buffersize The size of the internal buffer. + * \note The specification really used for opening the device may differ. + * \exception Exception Thrown if the audio device cannot be opened. + */ + CoreAudioDevice(DeviceSpecs specs, int buffersize = AUD_DEFAULT_BUFFER_SIZE); + + /** + * Closes the CoreAudio audio device. + */ + virtual ~CoreAudioDevice(); + + virtual ISynchronizer* getSynchronizer(); + + /** + * Registers this plugin. + */ + static void registerPlugin(); +}; + +AUD_NAMESPACE_END diff --git a/extern/audaspace/plugins/coreaudio/CoreAudioSynchronizer.cpp b/extern/audaspace/plugins/coreaudio/CoreAudioSynchronizer.cpp new file mode 100644 index 00000000000..fcfa7fde9be --- /dev/null +++ b/extern/audaspace/plugins/coreaudio/CoreAudioSynchronizer.cpp @@ -0,0 +1,127 @@ +/******************************************************************************* + * Copyright 2009-2016 Jörg Müller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ + +#include "CoreAudioSynchronizer.h" + +#include "CoreAudioDevice.h" +#include "Exception.h" + +AUD_NAMESPACE_BEGIN + +CoreAudioSynchronizer::CoreAudioSynchronizer(AudioUnit& audio_unit) : + m_clock_ref(nullptr), + m_playing(false) +{ + OSStatus status = CAClockNew(0, &m_clock_ref); + + if(status != noErr) + AUD_THROW(DeviceException, "Could not create a CoreAudio clock."); + + CAClockTimebase timebase = kCAClockTimebase_AudioOutputUnit; + + status = CAClockSetProperty(m_clock_ref, kCAClockProperty_InternalTimebase, sizeof(timebase), &timebase); + + if(status != noErr) + { + CAClockDispose(m_clock_ref); + AUD_THROW(DeviceException, "Could not create a CoreAudio clock."); + } + + status = CAClockSetProperty(m_clock_ref, kCAClockProperty_TimebaseSource, sizeof(audio_unit), &audio_unit); + + if(status != noErr) + { + CAClockDispose(m_clock_ref); + AUD_THROW(DeviceException, "Could not create a CoreAudio clock."); + } + + CAClockSyncMode sync_mode = kCAClockSyncMode_Internal; + + status = CAClockSetProperty(m_clock_ref, kCAClockProperty_SyncMode, sizeof(sync_mode), &sync_mode); + + if(status != noErr) + { + CAClockDispose(m_clock_ref); + AUD_THROW(DeviceException, "Could not create a CoreAudio clock."); + } +} + +CoreAudioSynchronizer::~CoreAudioSynchronizer() +{ + CAClockDispose(m_clock_ref); +} + +void CoreAudioSynchronizer::seek(std::shared_ptr<IHandle> handle, double time) +{ + if(m_playing) + CAClockStop(m_clock_ref); + + CAClockTime clock_time; + clock_time.format = kCAClockTimeFormat_Seconds; + clock_time.time.seconds = time; + CAClockSetCurrentTime(m_clock_ref, &clock_time); + + handle->seek(time); + + if(m_playing) + CAClockStart(m_clock_ref); +} + +double CoreAudioSynchronizer::getPosition(std::shared_ptr<IHandle> handle) +{ + CAClockTime clock_time; + + OSStatus status; + + if(m_playing) + status = CAClockGetCurrentTime(m_clock_ref, kCAClockTimeFormat_Seconds, &clock_time); + else + status = CAClockGetStartTime(m_clock_ref, kCAClockTimeFormat_Seconds, &clock_time); + + if(status != noErr) + return 0; + + return clock_time.time.seconds; +} + +void CoreAudioSynchronizer::play() +{ + if(m_playing) + return; + + m_playing = true; + CAClockStart(m_clock_ref); +} + +void CoreAudioSynchronizer::stop() +{ + if(!m_playing) + return; + + m_playing = false; + CAClockStop(m_clock_ref); +} + +void CoreAudioSynchronizer::setSyncCallback(ISynchronizer::syncFunction function, void* data) +{ +} + +int CoreAudioSynchronizer::isPlaying() +{ + return m_playing; +} + +AUD_NAMESPACE_END diff --git a/extern/audaspace/plugins/coreaudio/CoreAudioSynchronizer.h b/extern/audaspace/plugins/coreaudio/CoreAudioSynchronizer.h new file mode 100644 index 00000000000..4f9d9b28ea5 --- /dev/null +++ b/extern/audaspace/plugins/coreaudio/CoreAudioSynchronizer.h @@ -0,0 +1,64 @@ +/******************************************************************************* + * Copyright 2009-2016 Jörg Müller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ + +#pragma once + +#ifdef COREAUDIO_PLUGIN +#define AUD_BUILD_PLUGIN +#endif + +/** + * @file CoreAudioSynchronizer.h + * @ingroup plugin + * The CoreAudioSynchronizer class. + */ + +#include "devices/ISynchronizer.h" + +#include <AudioUnit/AudioUnit.h> +#include <AudioToolbox/CoreAudioClock.h> + +AUD_NAMESPACE_BEGIN + +/** + * This class is a Synchronizer implementation using a CoreAudio clock. + */ +class AUD_PLUGIN_API CoreAudioSynchronizer : public ISynchronizer +{ +private: + /// The CoreAudio clock referene. + CAClockRef m_clock_ref; + + /// Whether the clock is currently playing. + bool m_playing; + +public: + /** + * Creates a new CoreAudioSynchronizer. + * @param device The device that should be synchronized. + */ + CoreAudioSynchronizer(AudioUnit& audio_unit); + virtual ~CoreAudioSynchronizer(); + + virtual void seek(std::shared_ptr<IHandle> handle, double time); + virtual double getPosition(std::shared_ptr<IHandle> handle); + virtual void play(); + virtual void stop(); + virtual void setSyncCallback(syncFunction function, void* data); + virtual int isPlaying(); +}; + +AUD_NAMESPACE_END diff --git a/extern/audaspace/plugins/jack/JackLibrary.h b/extern/audaspace/plugins/jack/JackLibrary.h index 4e210852702..533eca7272a 100644 --- a/extern/audaspace/plugins/jack/JackLibrary.h +++ b/extern/audaspace/plugins/jack/JackLibrary.h @@ -21,9 +21,8 @@ #endif /** - * @file JackDevice.h + * @file JackLibrary.h * @ingroup plugin - * The JackDevice class. */ #include "Audaspace.h" diff --git a/extern/audaspace/plugins/pulseaudio/PulseAudioDevice.cpp b/extern/audaspace/plugins/pulseaudio/PulseAudioDevice.cpp new file mode 100644 index 00000000000..0a50d5db2c7 --- /dev/null +++ b/extern/audaspace/plugins/pulseaudio/PulseAudioDevice.cpp @@ -0,0 +1,282 @@ +/******************************************************************************* + * Copyright 2009-2016 Jörg Müller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ + +#include "PulseAudioDevice.h" +#include "PulseAudioLibrary.h" +#include "devices/DeviceManager.h" +#include "devices/IDeviceFactory.h" +#include "Exception.h" +#include "IReader.h" + +AUD_NAMESPACE_BEGIN + +void PulseAudioDevice::PulseAudio_state_callback(pa_context *context, void *data) +{ + PulseAudioDevice* device = (PulseAudioDevice*)data; + + device->m_state = AUD_pa_context_get_state(context); + + AUD_pa_threaded_mainloop_signal(device->m_mainloop, 0); +} + +void PulseAudioDevice::PulseAudio_request(pa_stream *stream, size_t num_bytes, void *data) +{ + PulseAudioDevice* device = (PulseAudioDevice*)data; + + void* buffer; + + AUD_pa_stream_begin_write(stream, &buffer, &num_bytes); + + device->mix((data_t*)buffer, num_bytes / AUD_DEVICE_SAMPLE_SIZE(device->m_specs)); + + AUD_pa_stream_write(stream, buffer, num_bytes, nullptr, 0, PA_SEEK_RELATIVE); +} + +void PulseAudioDevice::PulseAudio_underflow(pa_stream *stream, void *data) +{ + PulseAudioDevice* device = (PulseAudioDevice*)data; + + DeviceSpecs specs = device->getSpecs(); + + if(++device->m_underflows > 4 && device->m_buffersize < AUD_DEVICE_SAMPLE_SIZE(specs) * specs.rate * 2) + { + device->m_buffersize <<= 1; + device->m_underflows = 0; + + pa_buffer_attr buffer_attr; + + buffer_attr.fragsize = -1U; + buffer_attr.maxlength = -1U; + buffer_attr.minreq = -1U; + buffer_attr.prebuf = -1U; + buffer_attr.tlength = device->m_buffersize; + + AUD_pa_stream_set_buffer_attr(stream, &buffer_attr, nullptr, nullptr); + } +} + +void PulseAudioDevice::playing(bool playing) +{ + m_playback = playing; + + AUD_pa_stream_cork(m_stream, playing ? 0 : 1, nullptr, nullptr); +} + +PulseAudioDevice::PulseAudioDevice(std::string name, DeviceSpecs specs, int buffersize) : + m_playback(false), + m_state(PA_CONTEXT_UNCONNECTED), + m_buffersize(buffersize), + m_underflows(0) +{ + m_mainloop = AUD_pa_threaded_mainloop_new(); + + AUD_pa_threaded_mainloop_lock(m_mainloop); + + m_context = AUD_pa_context_new(AUD_pa_threaded_mainloop_get_api(m_mainloop), name.c_str()); + + if(!m_context) + { + AUD_pa_threaded_mainloop_unlock(m_mainloop); + AUD_pa_threaded_mainloop_free(m_mainloop); + + AUD_THROW(DeviceException, "Could not connect to PulseAudio."); + } + + AUD_pa_context_set_state_callback(m_context, PulseAudio_state_callback, this); + + AUD_pa_context_connect(m_context, nullptr, PA_CONTEXT_NOFLAGS, nullptr); + + AUD_pa_threaded_mainloop_start(m_mainloop); + + while(m_state != PA_CONTEXT_READY) + { + switch(m_state) + { + case PA_CONTEXT_FAILED: + case PA_CONTEXT_TERMINATED: + AUD_pa_threaded_mainloop_unlock(m_mainloop); + AUD_pa_threaded_mainloop_stop(m_mainloop); + + AUD_pa_context_disconnect(m_context); + AUD_pa_context_unref(m_context); + + AUD_pa_threaded_mainloop_free(m_mainloop); + + AUD_THROW(DeviceException, "Could not connect to PulseAudio."); + break; + default: + AUD_pa_threaded_mainloop_wait(m_mainloop); + break; + } + } + + if(specs.channels == CHANNELS_INVALID) + specs.channels = CHANNELS_STEREO; + if(specs.format == FORMAT_INVALID) + specs.format = FORMAT_FLOAT32; + if(specs.rate == RATE_INVALID) + specs.rate = RATE_48000; + + m_specs = specs; + + pa_sample_spec sample_spec; + + sample_spec.channels = specs.channels; + sample_spec.format = PA_SAMPLE_FLOAT32; + sample_spec.rate = specs.rate; + + switch(m_specs.format) + { + case FORMAT_U8: + sample_spec.format = PA_SAMPLE_U8; + break; + case FORMAT_S16: + sample_spec.format = PA_SAMPLE_S16NE; + break; + case FORMAT_S24: + sample_spec.format = PA_SAMPLE_S24NE; + break; + case FORMAT_S32: + sample_spec.format = PA_SAMPLE_S32NE; + break; + case FORMAT_FLOAT32: + sample_spec.format = PA_SAMPLE_FLOAT32; + break; + case FORMAT_FLOAT64: + m_specs.format = FORMAT_FLOAT32; + break; + default: + break; + } + + m_stream = AUD_pa_stream_new(m_context, "Playback", &sample_spec, nullptr); + + if(!m_stream) + { + AUD_pa_threaded_mainloop_unlock(m_mainloop); + AUD_pa_threaded_mainloop_stop(m_mainloop); + + AUD_pa_context_disconnect(m_context); + AUD_pa_context_unref(m_context); + + AUD_pa_threaded_mainloop_free(m_mainloop); + + AUD_THROW(DeviceException, "Could not create PulseAudio stream."); + } + + AUD_pa_stream_set_write_callback(m_stream, PulseAudio_request, this); + AUD_pa_stream_set_underflow_callback(m_stream, PulseAudio_underflow, this); + + pa_buffer_attr buffer_attr; + + buffer_attr.fragsize = -1U; + buffer_attr.maxlength = -1U; + buffer_attr.minreq = -1U; + buffer_attr.prebuf = -1U; + buffer_attr.tlength = buffersize; + + if(AUD_pa_stream_connect_playback(m_stream, nullptr, &buffer_attr, static_cast<pa_stream_flags_t>(PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_ADJUST_LATENCY | PA_STREAM_AUTO_TIMING_UPDATE), nullptr, nullptr) < 0) + { + AUD_pa_threaded_mainloop_unlock(m_mainloop); + AUD_pa_threaded_mainloop_stop(m_mainloop); + + AUD_pa_context_disconnect(m_context); + AUD_pa_context_unref(m_context); + + AUD_pa_threaded_mainloop_free(m_mainloop); + + AUD_THROW(DeviceException, "Could not connect PulseAudio stream."); + } + + AUD_pa_threaded_mainloop_unlock(m_mainloop); + + create(); +} + +PulseAudioDevice::~PulseAudioDevice() +{ + AUD_pa_threaded_mainloop_stop(m_mainloop); + + AUD_pa_context_disconnect(m_context); + AUD_pa_context_unref(m_context); + + AUD_pa_threaded_mainloop_free(m_mainloop); + + destroy(); +} + +class PulseAudioDeviceFactory : public IDeviceFactory +{ +private: + DeviceSpecs m_specs; + int m_buffersize; + std::string m_name; + +public: + PulseAudioDeviceFactory() : + m_buffersize(AUD_DEFAULT_BUFFER_SIZE), + m_name("Audaspace") + { + m_specs.format = FORMAT_FLOAT32; + m_specs.channels = CHANNELS_STEREO; + m_specs.rate = RATE_48000; + } + + virtual std::shared_ptr<IDevice> openDevice() + { + return std::shared_ptr<IDevice>(new PulseAudioDevice(m_name, m_specs, m_buffersize)); + } + + virtual int getPriority() + { + return 1 << 15; + } + + virtual void setSpecs(DeviceSpecs specs) + { + m_specs = specs; + } + + virtual void setBufferSize(int buffersize) + { + m_buffersize = buffersize; + } + + virtual void setName(std::string name) + { + m_name = name; + } +}; + +void PulseAudioDevice::registerPlugin() +{ + if(loadPulseAudio()) + DeviceManager::registerDevice("PulseAudio", std::shared_ptr<IDeviceFactory>(new PulseAudioDeviceFactory)); +} + +#ifdef PULSEAUDIO_PLUGIN +extern "C" AUD_PLUGIN_API void registerPlugin() +{ + PulseAudioDevice::registerPlugin(); +} + +extern "C" AUD_PLUGIN_API const char* getName() +{ + return "PulseAudio"; +} +#endif + +AUD_NAMESPACE_END diff --git a/extern/audaspace/plugins/pulseaudio/PulseAudioDevice.h b/extern/audaspace/plugins/pulseaudio/PulseAudioDevice.h new file mode 100644 index 00000000000..9efae5128b1 --- /dev/null +++ b/extern/audaspace/plugins/pulseaudio/PulseAudioDevice.h @@ -0,0 +1,105 @@ +/******************************************************************************* + * Copyright 2009-2016 Jörg Müller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ + +#pragma once + +#ifdef PULSEAUDIO_PLUGIN +#define AUD_BUILD_PLUGIN +#endif + +/** + * @file PulseAudioDevice.h + * @ingroup plugin + * The PulseAudioDevice class. + */ + +#include "devices/SoftwareDevice.h" + +#include <pulse/pulseaudio.h> + +AUD_NAMESPACE_BEGIN + +/** + * This device plays back through PulseAudio, the simple direct media layer. + */ +class AUD_PLUGIN_API PulseAudioDevice : public SoftwareDevice +{ +private: + /** + * Whether there is currently playback. + */ + volatile bool m_playback; + + pa_threaded_mainloop* m_mainloop; + pa_context* m_context; + pa_stream* m_stream; + pa_context_state_t m_state; + + int m_buffersize; + uint32_t m_underflows; + + /** + * Reports the state of the PulseAudio server connection. + * \param context The PulseAudio context. + * \param data The PulseAudio device. + */ + AUD_LOCAL static void PulseAudio_state_callback(pa_context* context, void* data); + + /** + * Supplies the next samples to PulseAudio. + * \param stream The PulseAudio stream. + * \param num_bytes The length in bytes to be supplied. + * \param data The PulseAudio device. + */ + AUD_LOCAL static void PulseAudio_request(pa_stream* stream, size_t num_bytes, void* data); + + /** + * Reports an underflow from the PulseAudio server. + * Automatically adjusts the latency if this happens too often. + * @param stream The PulseAudio stream. + * \param data The PulseAudio device. + */ + AUD_LOCAL static void PulseAudio_underflow(pa_stream* stream, void* data); + + // delete copy constructor and operator= + PulseAudioDevice(const PulseAudioDevice&) = delete; + PulseAudioDevice& operator=(const PulseAudioDevice&) = delete; + +protected: + virtual void playing(bool playing); + +public: + /** + * Opens the PulseAudio audio device for playback. + * \param specs The wanted audio specification. + * \param buffersize The size of the internal buffer. + * \note The specification really used for opening the device may differ. + * \exception Exception Thrown if the audio device cannot be opened. + */ + PulseAudioDevice(std::string name, DeviceSpecs specs, int buffersize = AUD_DEFAULT_BUFFER_SIZE); + + /** + * Closes the PulseAudio audio device. + */ + virtual ~PulseAudioDevice(); + + /** + * Registers this plugin. + */ + static void registerPlugin(); +}; + +AUD_NAMESPACE_END diff --git a/extern/audaspace/plugins/pulseaudio/PulseAudioLibrary.cpp b/extern/audaspace/plugins/pulseaudio/PulseAudioLibrary.cpp new file mode 100644 index 00000000000..66a0fae5ab6 --- /dev/null +++ b/extern/audaspace/plugins/pulseaudio/PulseAudioLibrary.cpp @@ -0,0 +1,59 @@ +/******************************************************************************* + * Copyright 2009-2016 Jörg Müller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ + +#define PULSEAUDIO_LIBRARY_IMPLEMENTATION + +#include <string> +#include <array> + +#include "PulseAudioLibrary.h" + +#ifdef DYNLOAD_PULSEAUDIO +#include "plugin/PluginManager.h" +#endif + +AUD_NAMESPACE_BEGIN + +bool loadPulseAudio() +{ +#ifdef DYNLOAD_PULSEAUDIO + std::array<const std::string, 2> names = {"libpulse.so", "libpulse.so.0"}; + + void* handle = nullptr; + + for(auto& name : names) + { + handle = PluginManager::openLibrary(name); + if(handle) + break; + } + + if (!handle) + return false; + +#define PULSEAUDIO_SYMBOL(sym) AUD_##sym = reinterpret_cast<decltype(&sym)>(PluginManager::lookupLibrary(handle, #sym)) +#else +#define PULSEAUDIO_SYMBOL(sym) AUD_##sym = &sym +#endif + +#include "PulseAudioSymbols.h" + +#undef PULSEAUDIO_SYMBOL + + return AUD_pa_context_new != nullptr; +} + +AUD_NAMESPACE_END diff --git a/extern/audaspace/plugins/pulseaudio/PulseAudioLibrary.h b/extern/audaspace/plugins/pulseaudio/PulseAudioLibrary.h new file mode 100644 index 00000000000..ae6c5951d11 --- /dev/null +++ b/extern/audaspace/plugins/pulseaudio/PulseAudioLibrary.h @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright 2009-2016 Jörg Müller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ + +#pragma once + +#ifdef PULSEAUDIO_PLUGIN +#define AUD_BUILD_PLUGIN +#endif + +/** + * @file PulseAudioLibrary.h + * @ingroup plugin + */ + +#include "Audaspace.h" + +#include <pulse/pulseaudio.h> + +AUD_NAMESPACE_BEGIN + +#ifdef PULSEAUDIO_LIBRARY_IMPLEMENTATION +#define PULSEAUDIO_SYMBOL(sym) decltype(&sym) AUD_##sym +#else +#define PULSEAUDIO_SYMBOL(sym) extern decltype(&sym) AUD_##sym +#endif + +#include "PulseAudioSymbols.h" + +#undef PULSEAUDIO_SYMBOL + +bool loadPulseAudio(); + +AUD_NAMESPACE_END diff --git a/extern/audaspace/plugins/pulseaudio/PulseAudioSymbols.h b/extern/audaspace/plugins/pulseaudio/PulseAudioSymbols.h new file mode 100644 index 00000000000..9cefbc0c7e2 --- /dev/null +++ b/extern/audaspace/plugins/pulseaudio/PulseAudioSymbols.h @@ -0,0 +1,41 @@ +/******************************************************************************* + * Copyright 2009-2016 Jörg Müller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ + +PULSEAUDIO_SYMBOL(pa_context_connect); +PULSEAUDIO_SYMBOL(pa_context_disconnect); +PULSEAUDIO_SYMBOL(pa_context_get_state); +PULSEAUDIO_SYMBOL(pa_context_new); +PULSEAUDIO_SYMBOL(pa_context_set_state_callback); +PULSEAUDIO_SYMBOL(pa_context_unref); + +PULSEAUDIO_SYMBOL(pa_stream_begin_write); +PULSEAUDIO_SYMBOL(pa_stream_connect_playback); +PULSEAUDIO_SYMBOL(pa_stream_cork); +PULSEAUDIO_SYMBOL(pa_stream_new); +PULSEAUDIO_SYMBOL(pa_stream_set_buffer_attr); +PULSEAUDIO_SYMBOL(pa_stream_set_underflow_callback); +PULSEAUDIO_SYMBOL(pa_stream_set_write_callback); +PULSEAUDIO_SYMBOL(pa_stream_write); + +PULSEAUDIO_SYMBOL(pa_threaded_mainloop_free); +PULSEAUDIO_SYMBOL(pa_threaded_mainloop_get_api); +PULSEAUDIO_SYMBOL(pa_threaded_mainloop_lock); +PULSEAUDIO_SYMBOL(pa_threaded_mainloop_new); +PULSEAUDIO_SYMBOL(pa_threaded_mainloop_signal); +PULSEAUDIO_SYMBOL(pa_threaded_mainloop_start); +PULSEAUDIO_SYMBOL(pa_threaded_mainloop_stop); +PULSEAUDIO_SYMBOL(pa_threaded_mainloop_unlock); +PULSEAUDIO_SYMBOL(pa_threaded_mainloop_wait); diff --git a/extern/audaspace/plugins/wasapi/WASAPIDevice.cpp b/extern/audaspace/plugins/wasapi/WASAPIDevice.cpp new file mode 100644 index 00000000000..241f694feb5 --- /dev/null +++ b/extern/audaspace/plugins/wasapi/WASAPIDevice.cpp @@ -0,0 +1,401 @@ +/******************************************************************************* + * Copyright 2009-2016 Jörg Müller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ + +#include "WASAPIDevice.h" +#include "devices/DeviceManager.h" +#include "devices/IDeviceFactory.h" +#include "Exception.h" +#include "IReader.h" + +AUD_NAMESPACE_BEGIN + +template <class T> void SafeRelease(T **ppT) +{ + if(*ppT) + { + (*ppT)->Release(); + *ppT = NULL; + } +} + +void WASAPIDevice::start() +{ + lock(); + + if(!m_playing) + { + if(m_thread.joinable()) + m_thread.join(); + + m_playing = true; + + m_thread = std::thread(&WASAPIDevice::updateStream, this); + } + + unlock(); +} + +void WASAPIDevice::updateStream() +{ + UINT32 buffer_size; + data_t* buffer; + + if(FAILED(m_audio_client->GetBufferSize(&buffer_size))) + return; + + IAudioRenderClient* render_client = nullptr; + const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient); + + if(FAILED(m_audio_client->GetService(IID_IAudioRenderClient, reinterpret_cast<void**>(&render_client)))) + return; + + UINT32 padding; + + if(FAILED(m_audio_client->GetCurrentPadding(&padding))) + { + SafeRelease(&render_client); + return; + } + + UINT32 length = buffer_size - padding; + + if(FAILED(render_client->GetBuffer(length, &buffer))) + { + SafeRelease(&render_client); + return; + } + + lock(); + + mix((data_t*)buffer, length); + + unlock(); + + if(FAILED(render_client->ReleaseBuffer(length, 0))) + { + SafeRelease(&render_client); + return; + } + + m_audio_client->Start(); + + auto sleepDuration = std::chrono::milliseconds(buffer_size * 1000 / int(m_specs.rate) / 2); + + for(;;) + { + if(FAILED(m_audio_client->GetCurrentPadding(&padding))) + { + m_audio_client->Stop(); + SafeRelease(&render_client); + return; + } + + length = buffer_size - padding; + + if(FAILED(render_client->GetBuffer(length, &buffer))) + { + m_audio_client->Stop(); + SafeRelease(&render_client); + return; + } + + lock(); + + mix((data_t*)buffer, length); + + unlock(); + + if(FAILED(render_client->ReleaseBuffer(length, 0))) + { + m_audio_client->Stop(); + SafeRelease(&render_client); + return; + } + + // stop thread + if(!m_playing) + { + m_audio_client->Stop(); + SafeRelease(&render_client); + return; + } + + std::this_thread::sleep_for(sleepDuration); + } +} + +void WASAPIDevice::playing(bool playing) +{ + if(!m_playing && playing) + start(); + else + m_playing = playing; +} + +WASAPIDevice::WASAPIDevice(DeviceSpecs specs, int buffersize) : + m_playing(false), + + m_imm_device_enumerator(nullptr), + m_imm_device(nullptr), + m_audio_client(nullptr), + + m_wave_format_extensible({}) +{ + // initialize COM if it hasn't happened yet + CoInitializeEx(nullptr, COINIT_MULTITHREADED); + + const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator); + const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator); + const IID IID_IAudioClient = __uuidof(IAudioClient); + + WAVEFORMATEXTENSIBLE wave_format_extensible_closest_match; + WAVEFORMATEXTENSIBLE* closest_match_pointer = &wave_format_extensible_closest_match; + + HRESULT result; + + REFERENCE_TIME minimum_time = 0; + REFERENCE_TIME buffer_duration; + + if(FAILED(CoCreateInstance(CLSID_MMDeviceEnumerator, nullptr, CLSCTX_ALL, IID_IMMDeviceEnumerator, reinterpret_cast<void**>(&m_imm_device_enumerator)))) + goto error; + + if(FAILED(m_imm_device_enumerator->GetDefaultAudioEndpoint(eRender, eMultimedia, &m_imm_device))) + goto error; + + if(FAILED(m_imm_device->Activate(IID_IAudioClient, CLSCTX_ALL, nullptr, reinterpret_cast<void**>(&m_audio_client)))) + goto error; + + if(specs.channels == CHANNELS_INVALID) + specs.channels = CHANNELS_STEREO; + if(specs.format == FORMAT_INVALID) + specs.format = FORMAT_FLOAT32; + if(specs.rate == RATE_INVALID) + specs.rate = RATE_48000; + + switch(specs.format) + { + case FORMAT_U8: + case FORMAT_S16: + case FORMAT_S24: + case FORMAT_S32: + m_wave_format_extensible.SubFormat = KSDATAFORMAT_SUBTYPE_PCM; + break; + case FORMAT_FLOAT32: + m_wave_format_extensible.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT; + break; + default: + m_wave_format_extensible.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT; + specs.format = FORMAT_FLOAT32; + break; + } + + switch(specs.channels) + { + case CHANNELS_MONO: + m_wave_format_extensible.dwChannelMask = SPEAKER_FRONT_CENTER; + break; + case CHANNELS_STEREO: + m_wave_format_extensible.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT; + break; + case CHANNELS_STEREO_LFE: + m_wave_format_extensible.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_LOW_FREQUENCY; + break; + case CHANNELS_SURROUND4: + m_wave_format_extensible.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT; + break; + case CHANNELS_SURROUND5: + m_wave_format_extensible.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT; + break; + case CHANNELS_SURROUND51: + m_wave_format_extensible.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT; + break; + case CHANNELS_SURROUND61: + m_wave_format_extensible.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_LOW_FREQUENCY | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT | SPEAKER_SIDE_LEFT | SPEAKER_SIDE_RIGHT; + break; + case CHANNELS_SURROUND71: + m_wave_format_extensible.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT | SPEAKER_SIDE_LEFT | SPEAKER_SIDE_RIGHT; + break; + default: + specs.channels = CHANNELS_STEREO; + m_wave_format_extensible.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT; + break; + } + + m_wave_format_extensible.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE; + m_wave_format_extensible.Format.nChannels = specs.channels; + m_wave_format_extensible.Format.nSamplesPerSec = specs.rate; + m_wave_format_extensible.Format.nAvgBytesPerSec = specs.rate * AUD_DEVICE_SAMPLE_SIZE(specs); + m_wave_format_extensible.Format.nBlockAlign = AUD_DEVICE_SAMPLE_SIZE(specs); + m_wave_format_extensible.Format.wBitsPerSample = AUD_FORMAT_SIZE(specs.format) * 8; + m_wave_format_extensible.Format.cbSize = 22; + m_wave_format_extensible.Samples.wValidBitsPerSample = m_wave_format_extensible.Format.wBitsPerSample; + + result = m_audio_client->IsFormatSupported(AUDCLNT_SHAREMODE_SHARED, reinterpret_cast<const WAVEFORMATEX*>(&m_wave_format_extensible), reinterpret_cast<WAVEFORMATEX**>(&closest_match_pointer)); + + if(result == S_FALSE) + { + if(closest_match_pointer->Format.wFormatTag != WAVE_FORMAT_EXTENSIBLE) + goto error; + + specs.channels = Channels(closest_match_pointer->Format.nChannels); + specs.rate = closest_match_pointer->Format.nSamplesPerSec; + + if(closest_match_pointer->SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) + { + if(closest_match_pointer->Format.wBitsPerSample == 32) + specs.format = FORMAT_FLOAT32; + else if(closest_match_pointer->Format.wBitsPerSample == 64) + specs.format = FORMAT_FLOAT64; + else + goto error; + } + else if(closest_match_pointer->SubFormat == KSDATAFORMAT_SUBTYPE_PCM) + { + switch(closest_match_pointer->Format.wBitsPerSample) + { + case 8: + specs.format = FORMAT_U8; + break; + case 16: + specs.format = FORMAT_S16; + break; + case 24: + specs.format = FORMAT_S24; + break; + case 32: + specs.format = FORMAT_S32; + break; + default: + goto error; + break; + } + } + else + goto error; + + m_wave_format_extensible = *closest_match_pointer; + + if(closest_match_pointer != &wave_format_extensible_closest_match) + { + CoTaskMemFree(closest_match_pointer); + closest_match_pointer = &wave_format_extensible_closest_match; + } + } + else if(FAILED(result)) + goto error; + + if(FAILED(m_audio_client->GetDevicePeriod(nullptr, &minimum_time))) + goto error; + + buffer_duration = REFERENCE_TIME(buffersize) * REFERENCE_TIME(10000000) / REFERENCE_TIME(specs.rate); + + if(minimum_time > buffer_duration) + buffer_duration = minimum_time; + + m_specs = specs; + + if(FAILED(m_audio_client->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, buffer_duration, 0, reinterpret_cast<WAVEFORMATEX*>(&m_wave_format_extensible), nullptr))) + goto error; + + create(); + + return; + + error: + if(closest_match_pointer != &wave_format_extensible_closest_match) + CoTaskMemFree(closest_match_pointer); + SafeRelease(&m_imm_device); + SafeRelease(&m_imm_device_enumerator); + SafeRelease(&m_audio_client); + AUD_THROW(DeviceException, "The audio device couldn't be opened with WASAPI."); +} + +WASAPIDevice::~WASAPIDevice() +{ + lock(); + + stopAll(); + + unlock(); + + if(m_thread.joinable()) + m_thread.join(); + + SafeRelease(&m_audio_client); + SafeRelease(&m_imm_device); + SafeRelease(&m_imm_device_enumerator); + + destroy(); +} + +class WASAPIDeviceFactory : public IDeviceFactory +{ +private: + DeviceSpecs m_specs; + int m_buffersize; + +public: + WASAPIDeviceFactory() : + m_buffersize(AUD_DEFAULT_BUFFER_SIZE) + { + m_specs.format = FORMAT_S16; + m_specs.channels = CHANNELS_STEREO; + m_specs.rate = RATE_48000; + } + + virtual std::shared_ptr<IDevice> openDevice() + { + return std::shared_ptr<IDevice>(new WASAPIDevice(m_specs, m_buffersize)); + } + + virtual int getPriority() + { + return 1 << 15; + } + + virtual void setSpecs(DeviceSpecs specs) + { + m_specs = specs; + } + + virtual void setBufferSize(int buffersize) + { + m_buffersize = buffersize; + } + + virtual void setName(std::string name) + { + } +}; + +void WASAPIDevice::registerPlugin() +{ + DeviceManager::registerDevice("WASAPI", std::shared_ptr<IDeviceFactory>(new WASAPIDeviceFactory)); +} + +#ifdef WASAPI_PLUGIN +extern "C" AUD_PLUGIN_API void registerPlugin() +{ + WASAPIDevice::registerPlugin(); +} + +extern "C" AUD_PLUGIN_API const char* getName() +{ + return "WASAPI"; +} +#endif + +AUD_NAMESPACE_END diff --git a/extern/audaspace/plugins/wasapi/WASAPIDevice.h b/extern/audaspace/plugins/wasapi/WASAPIDevice.h new file mode 100644 index 00000000000..b7b520e802c --- /dev/null +++ b/extern/audaspace/plugins/wasapi/WASAPIDevice.h @@ -0,0 +1,99 @@ +/******************************************************************************* + * Copyright 2009-2016 Jörg Müller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ + +#pragma once + +#ifdef WASAPI_PLUGIN +#define AUD_BUILD_PLUGIN +#endif + +/** + * @file WASAPIDevice.h + * @ingroup plugin + * The WASAPIDevice class. + */ + +#include "devices/SoftwareDevice.h" + +#include <thread> + +#include <windows.h> +#include <audioclient.h> +#include <mmdeviceapi.h> +#include <mmreg.h> + +AUD_NAMESPACE_BEGIN + +/** + * This device plays back through WASAPI, the Windows audio API. + */ +class AUD_PLUGIN_API WASAPIDevice : public SoftwareDevice +{ +private: + /** + * Whether there is currently playback. + */ + bool m_playing; + + IMMDeviceEnumerator* m_imm_device_enumerator; + IMMDevice* m_imm_device; + IAudioClient* m_audio_client; + WAVEFORMATEXTENSIBLE m_wave_format_extensible; + + /** + * The streaming thread. + */ + std::thread m_thread; + + /** + * Starts the streaming thread. + */ + AUD_LOCAL void start(); + + /** + * Streaming thread main function. + */ + AUD_LOCAL void updateStream(); + + // delete copy constructor and operator= + WASAPIDevice(const WASAPIDevice&) = delete; + WASAPIDevice& operator=(const WASAPIDevice&) = delete; + +protected: + virtual void playing(bool playing); + +public: + /** + * Opens the WASAPI audio device for playback. + * \param specs The wanted audio specification. + * \param buffersize The size of the internal buffer. + * \note The specification really used for opening the device may differ. + * \exception Exception Thrown if the audio device cannot be opened. + */ + WASAPIDevice(DeviceSpecs specs, int buffersize = AUD_DEFAULT_BUFFER_SIZE); + + /** + * Closes the WASAPI audio device. + */ + virtual ~WASAPIDevice(); + + /** + * Registers this plugin. + */ + static void registerPlugin(); +}; + +AUD_NAMESPACE_END diff --git a/extern/audaspace/src/devices/NULLDevice.cpp b/extern/audaspace/src/devices/NULLDevice.cpp index c3290465563..fa8e457dbd2 100644 --- a/extern/audaspace/src/devices/NULLDevice.cpp +++ b/extern/audaspace/src/devices/NULLDevice.cpp @@ -187,7 +187,7 @@ public: void NULLDevice::registerPlugin() { - DeviceManager::registerDevice("Null", std::shared_ptr<IDeviceFactory>(new NULLDeviceFactory)); + DeviceManager::registerDevice("None", std::shared_ptr<IDeviceFactory>(new NULLDeviceFactory)); } AUD_NAMESPACE_END diff --git a/extern/audaspace/src/plugin/PluginManagerUnix.cpp.in b/extern/audaspace/src/plugin/PluginManagerUnix.cpp.in index d08804bc2e7..3ab24986a0c 100644 --- a/extern/audaspace/src/plugin/PluginManagerUnix.cpp.in +++ b/extern/audaspace/src/plugin/PluginManagerUnix.cpp.in @@ -83,7 +83,12 @@ void PluginManager::loadPlugins(const std::string& path) while(dirent* entry = readdir(dir)) { const std::string filename = entry->d_name; + +#ifdef __APPLE__ + const std::string end = ".dylib"; +#else const std::string end = ".so"; +#endif if(filename.length() >= end.length() && filename.substr(filename.length() - end.length()) == end) { diff --git a/extern/audaspace/src/plugin/PluginManagerWindows.cpp.in b/extern/audaspace/src/plugin/PluginManagerWindows.cpp.in index 62350fd24fd..dd2df15751c 100644 --- a/extern/audaspace/src/plugin/PluginManagerWindows.cpp.in +++ b/extern/audaspace/src/plugin/PluginManagerWindows.cpp.in @@ -27,9 +27,9 @@ void* PluginManager::openLibrary(const std::string& path) return reinterpret_cast<void*>(LoadLibrary(path.c_str())); } -void *PluginManager::lookupLibrary(void *handle, const std::string &name) +void* PluginManager::lookupLibrary(void *handle, const std::string &name) { - return GetProcAddress(reinterpret_cast<HMODULE>(handle), name.c_str()); + return reinterpret_cast<void*>(GetProcAddress(reinterpret_cast<HMODULE>(handle), name.c_str())); } void PluginManager::closeLibrary(void *handle) diff --git a/extern/mantaflow/CMakeLists.txt b/extern/mantaflow/CMakeLists.txt index 3220a45bef4..9b047eb1a3e 100644 --- a/extern/mantaflow/CMakeLists.txt +++ b/extern/mantaflow/CMakeLists.txt @@ -32,9 +32,7 @@ if(MSVC_CLANG AND WITH_OPENMP AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9.0.1 endif() # Exporting functions from the blender binary gives linker warnings on Apple arm64 systems. -# For now and until Apple arm64 is officially supported, these will just be silenced here. -# TODO (sebbas): Check if official arm64 devices give linker warnings without this block. - +# Silence them here. if(APPLE AND ("${CMAKE_OSX_ARCHITECTURES}" STREQUAL "arm64")) if(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") string(APPEND CMAKE_C_FLAGS " -fvisibility=hidden") |