Skip to content

Commit

Permalink
feat: Adding AVX implementation for mixing audio. (#745)
Browse files Browse the repository at this point in the history
Co-authored-by: Brain <[email protected]>
  • Loading branch information
RealTimeChris and braindigitalis authored Aug 3, 2023
1 parent 5520897 commit b633491
Show file tree
Hide file tree
Showing 7 changed files with 546 additions and 6 deletions.
56 changes: 56 additions & 0 deletions cmake/DetectArchitecture.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
include(CheckCXXSourceRuns)

function(check_instruction_set INSTRUCTION_SET_NAME INSTRUCTION_SET_FLAG INSTRUCTION_SET_INTRINSIC)

set(INSTRUCTION_SET_CODE "
#include <immintrin.h>
#include <stdint.h>
int main()
{
${INSTRUCTION_SET_INTRINSIC};
return 0;
}
")

set(CMAKE_REQUIRED_FLAGS "${INSTRUCTION_SET_FLAG}")
CHECK_CXX_SOURCE_RUNS("${INSTRUCTION_SET_CODE}" "${INSTRUCTION_SET_NAME}")
if(${INSTRUCTION_SET_NAME})
set(AVX_TYPE "${INSTRUCTION_SET_NAME}" PARENT_SCOPE)
set(AVX_FLAG "${INSTRUCTION_SET_FLAG}" PARENT_SCOPE)
set(AVX_NAME "${INSTRUCTION_SET_NAME}" PARENT_SCOPE)
else()
message(STATUS "Instruction set ${INSTRUCTION_SET_NAME} not supported. Falling back to the previous instruction set.")
return()
endif()
endfunction()

if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set(INSTRUCTION_SETS
"T_AVX?/arch:AVX?auto result = _mm_testz_ps(__m128{}, __m128{})"
"T_AVX2?/arch:AVX2?auto result = _mm256_extract_epi64(__m256i{}, 0)"
"T_AVX512?/arch:AVX512?auto result = _mm512_add_ps(__m512i{}, __m512i{}).auto result2 = _mm512_cmplt_epu8_mask(__m512i{}, __m512i{})"
)
else()
set(INSTRUCTION_SETS
"T_AVX?-mavx.-mpclmul.-mbmi?auto result = _mm_testz_ps(__m128{}, __m128{})"
"T_AVX2?-mavx2.-mavx.-mpclmul.-mbmi?auto result = _mm256_extract_epi64(__m256i{}, 0)"
"T_AVX512?-mavx512bw.-mavx512f.-mavx2.-mavx.-mpclmul.-mbmi?auto result = _mm512_add_ps(__m512i{}, __m512i{}).auto result2 = _mm512_cmplt_epu8_mask(__m512i{}, __m512i{})"
)
endif()

set(CMAKE_REQUIRED_FLAGS_SAVE "${CMAKE_REQUIRED_FLAGS}")

set(AVX_NAME "T_Fallback")

foreach(INSTRUCTION_SET IN LISTS INSTRUCTION_SETS)
string(REPLACE "?" ";" CURRENT_LIST "${INSTRUCTION_SET}")
list(GET CURRENT_LIST 0 INSTRUCTION_SET_NAME)
list(GET CURRENT_LIST 1 INSTRUCTION_SET_FLAG)
string(REPLACE "." ";" INSTRUCTION_SET_FLAG "${INSTRUCTION_SET_FLAG}")
list(GET CURRENT_LIST 2 INSTRUCTION_SET_INTRINSIC)
string(REPLACE "." ";" INSTRUCTION_SET_INTRINSIC "${INSTRUCTION_SET_INTRINSIC}")
check_instruction_set("${INSTRUCTION_SET_NAME}" "${INSTRUCTION_SET_FLAG}" "${INSTRUCTION_SET_INTRINSIC}")
endforeach()

message(STATUS "Detected CPU Architecture: ${AVX_NAME}")
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS_SAVE}")
1 change: 1 addition & 0 deletions cmake/LINUXx86ToolChain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ SET(OPENSSL_SSL_LIBRARY /usr/lib/i386-linux-gnu/libssl.so)

SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32 " CACHE INTERNAL "" FORCE)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32 " CACHE INTERNAL "" FORCE)
set(T_AVX_EXITCODE "0" CACHE STRING INTERNAL FORCE)

EXECUTE_PROCESS(COMMAND sudo dpkg --add-architecture i386)
EXECUTE_PROCESS(COMMAND sudo apt-get update)
Expand Down
38 changes: 38 additions & 0 deletions include/dpp/discordvoiceclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <dpp/dispatcher.h>
#include <dpp/cluster.h>
#include <dpp/discordevents.h>
#include <dpp/isa_detection.h>
#include <dpp/socket.h>
#include <queue>
#include <thread>
Expand All @@ -58,6 +59,23 @@ namespace dpp {

using json = nlohmann::json;

/*
* @brief For holding a moving average of the number of current voice users, for applying a smooth gain ramp.
*/
struct DPP_EXPORT moving_averager {
moving_averager() = default;

moving_averager(uint64_t collection_count_new);

moving_averager operator+=(int64_t value);

operator float();

protected:
std::deque<int64_t> values{};
uint64_t collectionCount{};
};

// Forward declaration
class cluster;

Expand Down Expand Up @@ -473,6 +491,21 @@ class DPP_EXPORT discord_voice_client : public websocket_client
*/
bool terminating;

/**
* @brief The gain value for the end of the current voice iteration.
*/
float end_gain;

/**
* @brief The gain value for the current voice iteration.
*/
float current_gain;

/**
* @brief The amount to increment each successive sample for, for the current voice iteration.
*/
float increment;

/**
* @brief Heartbeat interval for sending heartbeat keepalive
*/
Expand Down Expand Up @@ -503,6 +536,11 @@ class DPP_EXPORT discord_voice_client : public websocket_client
*/
snowflake server_id;

/**
* @brief Moving averager.
*/
moving_averager moving_average;

/**
* @brief Channel ID
*/
Expand Down
Loading

0 comments on commit b633491

Please sign in to comment.