confirmed working with P/invoke on windows .NET Core C#

main
Patrick W3AXL 1 year ago
parent d6c59be715
commit a9a2a17d23

3
.gitignore vendored

@ -33,3 +33,6 @@
# Build directory # Build directory
build/ build/
# VS stuff
.vs/

@ -16,6 +16,11 @@ project(dvmvocoder)
set(CMAKE_STATIC_LIBRARY_PREFIX "") set(CMAKE_STATIC_LIBRARY_PREFIX "")
set(CMAKE_SHARED_LIBRARY_PREFIX "") set(CMAKE_SHARED_LIBRARY_PREFIX "")
# Set up export symbols (Windows only)
if(WIN32)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif()
# #
# Build Vocoder as a Shared Library (dll or so) # Build Vocoder as a Shared Library (dll or so)
# #

@ -16,83 +16,84 @@
#include "MBEDecoder.h" #include "MBEDecoder.h"
using namespace edac; using namespace edac;
using namespace vocoder;
// --------------------------------------------------------------------------- namespace vocoder {
// Constants
// ---------------------------------------------------------------------------
const int MBEDecoder::dW[72] = { 0,0,3,2,1,1,0,0,1,1,0,0,3,2,1,1,3,2,1,1,0,0,3,2,0,0,3,2,1,1,0,0,1,1,0,0,3,2,1,1,3,2,1,1,0,0,3,2,0,0,3,2,1,1,0,0,1,1,0,0,3,2,1,1,3,3,2,1,0,0,3,3, }; // ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
const int MBEDecoder::dX[72] = { 10,22,11,9,10,22,11,23,8,20,9,21,10,8,9,21,8,6,7,19,8,20,9,7,6,18,7,5,6,18,7,19,4,16,5,17,6,4,5,17,4,2,3,15,4,16,5,3,2,14,3,1,2,14,3,15,0,12,1,13,2,0,1,13,0,12,10,11,0,12,1,13, }; const int MBEDecoder::dW[72] = { 0,0,3,2,1,1,0,0,1,1,0,0,3,2,1,1,3,2,1,1,0,0,3,2,0,0,3,2,1,1,0,0,1,1,0,0,3,2,1,1,3,2,1,1,0,0,3,2,0,0,3,2,1,1,0,0,1,1,0,0,3,2,1,1,3,3,2,1,0,0,3,3, };
const int MBEDecoder::rW[36] = { const int MBEDecoder::dX[72] = { 10,22,11,9,10,22,11,23,8,20,9,21,10,8,9,21,8,6,7,19,8,20,9,7,6,18,7,5,6,18,7,19,4,16,5,17,6,4,5,17,4,2,3,15,4,16,5,3,2,14,3,1,2,14,3,15,0,12,1,13,2,0,1,13,0,12,10,11,0,12,1,13, };
const int MBEDecoder::rW[36] = {
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
0, 1, 0, 1, 0, 2, 0, 1, 0, 1, 0, 2,
0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2,
0, 2, 0, 2, 0, 2 0, 2, 0, 2, 0, 2
}; };
const int MBEDecoder::rX[36] = { const int MBEDecoder::rX[36] = {
23, 10, 22, 9, 21, 8, 23, 10, 22, 9, 21, 8,
20, 7, 19, 6, 18, 5, 20, 7, 19, 6, 18, 5,
17, 4, 16, 3, 15, 2, 17, 4, 16, 3, 15, 2,
14, 1, 13, 0, 12, 10, 14, 1, 13, 0, 12, 10,
11, 9, 10, 8, 9, 7, 11, 9, 10, 8, 9, 7,
8, 6, 7, 5, 6, 4 8, 6, 7, 5, 6, 4
}; };
// bit 0 // bit 0
const int MBEDecoder::rY[36] = { const int MBEDecoder::rY[36] = {
0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2,
0, 2, 0, 3, 0, 3, 0, 2, 0, 3, 0, 3,
1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3,
1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3,
1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3,
1, 3, 1, 3, 1, 3 1, 3, 1, 3, 1, 3
}; };
const int MBEDecoder::rZ[36] = { const int MBEDecoder::rZ[36] = {
5, 3, 4, 2, 3, 1, 5, 3, 4, 2, 3, 1,
2, 0, 1, 13, 0, 12, 2, 0, 1, 13, 0, 12,
22, 11, 21, 10, 20, 9, 22, 11, 21, 10, 20, 9,
19, 8, 18, 7, 17, 6, 19, 8, 18, 7, 17, 6,
16, 5, 15, 4, 14, 3, 16, 5, 15, 4, 14, 3,
13, 2, 12, 1, 11, 0 13, 2, 12, 1, 11, 0
}; };
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Public Class Members // Public Class Members
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
/* Initializes a new instance of the MBEDecoder class. */ /* Initializes a new instance of the MBEDecoder class. */
MBEDecoder::MBEDecoder(MBE_DECODER_MODE mode) : MBEDecoder::MBEDecoder(MBE_DECODER_MODE mode) :
m_mbelibParms(NULL), m_mbelibParms(NULL),
m_mbeMode(mode), m_mbeMode(mode),
m_gainAdjust(1.0f) m_gainAdjust(1.0f)
{ {
m_mbelibParms = new mbelibParms(); m_mbelibParms = new mbelibParms();
mbe_initMbeParms(m_mbelibParms->m_cur_mp, m_mbelibParms->m_prev_mp, m_mbelibParms->m_prev_mp_enhanced); mbe_initMbeParms(m_mbelibParms->m_cur_mp, m_mbelibParms->m_prev_mp, m_mbelibParms->m_prev_mp_enhanced);
::memset(gainMaxBuf, 0, sizeof(float) * 200); ::memset(gainMaxBuf, 0, sizeof(float) * 200);
gainMaxBufPtr = gainMaxBuf; gainMaxBufPtr = gainMaxBuf;
gainMaxIdx = 0; gainMaxIdx = 0;
} }
/* Finalizes a instance of the MBEDecoder class. */ /* Finalizes a instance of the MBEDecoder class. */
MBEDecoder::~MBEDecoder() MBEDecoder::~MBEDecoder()
{ {
delete m_mbelibParms; delete m_mbelibParms;
} }
/* Decodes the given MBE codewords to deinterleaved MBE bits using the decoder mode. */ /* Decodes the given MBE codewords to deinterleaved MBE bits using the decoder mode. */
int32_t MBEDecoder::decodeBits(uint8_t* codeword, char* mbeBits) int32_t MBEDecoder::decodeBits(uint8_t* codeword, char* mbeBits)
{ {
int32_t errs = 0; int32_t errs = 0;
float samples[160U]; float samples[160U];
::memset(samples, 0x00U, 160U * sizeof(float)); ::memset(samples, 0x00U, 160U * sizeof(float));
@ -150,12 +151,12 @@ int32_t MBEDecoder::decodeBits(uint8_t* codeword, char* mbeBits)
} }
return errs; return errs;
} }
/* Decodes the given MBE codewords to PCM samples using the decoder mode. */ /* Decodes the given MBE codewords to PCM samples using the decoder mode. */
int32_t MBEDecoder::decodeF(uint8_t* codeword, float samples[]) int32_t MBEDecoder::decodeF(uint8_t* codeword, float samples[])
{ {
int32_t errs = 0; int32_t errs = 0;
switch (m_mbeMode) switch (m_mbeMode)
{ {
@ -213,12 +214,12 @@ int32_t MBEDecoder::decodeF(uint8_t* codeword, float samples[])
} }
return errs; return errs;
} }
/* Decodes the given MBE codewords to PCM samples using the decoder mode. */ /* Decodes the given MBE codewords to PCM samples using the decoder mode. */
int32_t MBEDecoder::decode(uint8_t* codeword, int16_t samples[]) int32_t MBEDecoder::decode(uint8_t* codeword, int16_t samples[])
{ {
float samplesF[160U]; float samplesF[160U];
::memset(samplesF, 0x00U, 160U * sizeof(float)); ::memset(samplesF, 0x00U, 160U * sizeof(float));
int32_t errs = decodeF(codeword, samplesF); int32_t errs = decodeF(codeword, samplesF);
@ -313,4 +314,30 @@ int32_t MBEDecoder::decode(uint8_t* codeword, int16_t samples[])
} }
return errs; return errs;
} }
// Extern methods for C#/C++ interop
MBEDecoder* MBEDecoder_Create(MBE_DECODER_MODE mode)
{
return new MBEDecoder(mode);
}
int32_t MBEDecoder_Decode(MBEDecoder* pDecoder, uint8_t* codeword, int16_t* samples)
{
if (pDecoder != NULL)
{
return pDecoder->decode(codeword, samples);
}
return -1;
}
void MBEDecoder_Delete(MBEDecoder* pDecoder)
{
if (pDecoder != NULL)
{
delete pDecoder;
pDecoder = NULL;
}
}
} // namespace vocoder

@ -138,9 +138,17 @@ namespace vocoder
}; };
// Extern methods for C#/C++ interop // Extern methods for C#/C++ interop
extern "C" MBEDecoder* MBEDecoder_Create(MBE_DECODER_MODE mode) { return new MBEDecoder(mode); } extern "C" {
extern "C" int32_t MBEDecoder_Decode(MBEDecoder* pDecoder, uint8_t* codeword, int16_t* samples) { return pDecoder->decode(codeword, samples); } #ifdef _WIN32
extern "C" void MBEDecoder_Delete(MBEDecoder* pDecoder) { delete pDecoder; } extern __declspec(dllexport) MBEDecoder* MBEDecoder_Create(MBE_DECODER_MODE mode);
extern __declspec(dllexport) int32_t MBEDecoder_Decode(MBEDecoder* pDecoder, uint8_t* codeword, int16_t* samples);
extern __declspec(dllexport) void MBEDecoder_Delete(MBEDecoder* pDecoder);
#else
extern MBEDecoder* MBEDecoder_Create(MBE_DECODER_MODE mode);
extern int32_t MBEDecoder_Decode(MBEDecoder* pDecoder, uint8_t* codeword, int16_t* samples);
extern void MBEDecoder_Delete(MBEDecoder* pDecoder);
#endif
}
} // namespace vocoder } // namespace vocoder

@ -44,17 +44,18 @@
#include <cassert> #include <cassert>
using namespace edac; using namespace edac;
using namespace vocoder;
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma warning(disable: 4244) #pragma warning(disable: 4244)
#endif #endif
// --------------------------------------------------------------------------- namespace vocoder {
// Constants
// ---------------------------------------------------------------------------
static const short b0_lookup[] = { // ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
static const short b0_lookup[] = {
0, 0, 0, 1, 1, 2, 2, 2, 0, 0, 0, 1, 1, 2, 2, 2,
3, 3, 4, 4, 4, 5, 5, 5, 3, 3, 4, 4, 4, 5, 5, 5,
6, 6, 7, 7, 7, 8, 8, 8, 6, 6, 7, 7, 7, 8, 8, 8,
@ -159,13 +160,13 @@ static const short b0_lookup[] = {
118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118,
118, 118, 118, 119, 119, 119, 119, 119, 118, 118, 118, 119, 119, 119, 119, 119,
119, 119, 119 119, 119, 119
}; };
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Global Functions // Global Functions
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
/** /**
* @brief * @brief
* @param[in] imbe_param * @param[in] imbe_param
* @param b * @param b
@ -173,8 +174,8 @@ static const short b0_lookup[] = {
* @param prev_mp Previous MBE parameters. * @param prev_mp Previous MBE parameters.
* @param gainAdjust Gain adjustment. * @param gainAdjust Gain adjustment.
*/ */
static void encodeAMBE(const IMBE_PARAM* imbe_param, int b[], mbe_parms* cur_mp, mbe_parms* prev_mp, float gainAdjust) static void encodeAMBE(const IMBE_PARAM* imbe_param, int b[], mbe_parms* cur_mp, mbe_parms* prev_mp, float gainAdjust)
{ {
static const float SQRT_2 = sqrtf(2.0); static const float SQRT_2 = sqrtf(2.0);
static const int b0_lmax = sizeof(b0_lookup) / sizeof(b0_lookup[0]); static const int b0_lmax = sizeof(b0_lookup) / sizeof(b0_lookup[0]);
// int b[9]; // int b[9];
@ -458,15 +459,15 @@ static void encodeAMBE(const IMBE_PARAM* imbe_param, int b[], mbe_parms* cur_mp,
mbe_dequantizeAmbe2250Parms(cur_mp, prev_mp, b); mbe_dequantizeAmbe2250Parms(cur_mp, prev_mp, b);
mbe_moveMbeParms(cur_mp, prev_mp); mbe_moveMbeParms(cur_mp, prev_mp);
} }
/** /**
* @brief * @brief
* @param bits * @param bits
* @param b * @param b
*/ */
static void encode49bit(uint8_t bits[49], const int b[9]) static void encode49bit(uint8_t bits[49], const int b[9])
{ {
bits[0] = (b[0] >> 6) & 1; bits[0] = (b[0] >> 6) & 1;
bits[1] = (b[0] >> 5) & 1; bits[1] = (b[0] >> 5) & 1;
bits[2] = (b[0] >> 4) & 1; bits[2] = (b[0] >> 4) & 1;
@ -516,15 +517,15 @@ static void encode49bit(uint8_t bits[49], const int b[9])
bits[46] = b[7] & 1; bits[46] = b[7] & 1;
bits[47] = (b[8] >> 1) & 1; bits[47] = (b[8] >> 1) & 1;
bits[48] = b[8] & 1; bits[48] = b[8] & 1;
} }
/** /**
* @brief * @brief
* @param[in] in * @param[in] in
* @param out * @param out
*/ */
static void encodeDmrAMBE(const uint8_t* in, uint8_t* out) static void encodeDmrAMBE(const uint8_t* in, uint8_t* out)
{ {
unsigned int aOrig = 0U; unsigned int aOrig = 0U;
unsigned int bOrig = 0U; unsigned int bOrig = 0U;
unsigned int cOrig = 0U; unsigned int cOrig = 0U;
@ -571,25 +572,25 @@ static void encodeDmrAMBE(const uint8_t* in, uint8_t* out)
unsigned int cPos = AMBE_C_TABLE[i]; unsigned int cPos = AMBE_C_TABLE[i];
WRITE_BIT(out, cPos, cOrig & MASK); WRITE_BIT(out, cPos, cOrig & MASK);
} }
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Public Class Members // Public Class Members
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
/* Initializes a new instance of the MBEEncoder class. */ /* Initializes a new instance of the MBEEncoder class. */
MBEEncoder::MBEEncoder(MBE_ENCODER_MODE mode) : MBEEncoder::MBEEncoder(MBE_ENCODER_MODE mode) :
m_mbeMode(mode), m_mbeMode(mode),
m_gainAdjust(0.0f) m_gainAdjust(0.0f)
{ {
mbe_parms enh_mp; mbe_parms enh_mp;
mbe_initMbeParms(&m_curMBEParms, &m_prevMBEParms, &enh_mp); mbe_initMbeParms(&m_curMBEParms, &m_prevMBEParms, &enh_mp);
} }
/* Encodes the given MBE bits to deinterleaved MBE bits using the decoder mode. */ /* Encodes the given MBE bits to deinterleaved MBE bits using the decoder mode. */
void MBEEncoder::encodeBits(uint8_t* bits, uint8_t* codeword) void MBEEncoder::encodeBits(uint8_t* bits, uint8_t* codeword)
{ {
assert(bits != nullptr); assert(bits != nullptr);
assert(codeword != nullptr); assert(codeword != nullptr);
@ -635,12 +636,12 @@ void MBEEncoder::encodeBits(uint8_t* bits, uint8_t* codeword)
} }
break; break;
} }
} }
/* Encodes the given PCM samples using the encoder mode to MBE codewords. */ /* Encodes the given PCM samples using the encoder mode to MBE codewords. */
void MBEEncoder::encode(int16_t* samples, uint8_t* codeword) void MBEEncoder::encode(int16_t* samples, uint8_t* codeword)
{ {
assert(samples != nullptr); assert(samples != nullptr);
assert(codeword != nullptr); assert(codeword != nullptr);
@ -715,4 +716,25 @@ void MBEEncoder::encode(int16_t* samples, uint8_t* codeword)
encodeDmrAMBE(rawAmbe, dmrAMBE); encodeDmrAMBE(rawAmbe, dmrAMBE);
::memcpy(codeword, dmrAMBE, 9U); ::memcpy(codeword, dmrAMBE, 9U);
} }
} }
// Extern methods for C#/C++ interop
MBEEncoder* MBEEncoder_Create(MBE_ENCODER_MODE mode)
{
return new MBEEncoder(mode);
}
void MBEEncoder_Encode(MBEEncoder* pEncoder, int16_t* samples, uint8_t* codeword)
{
if (pEncoder != NULL)
{
pEncoder->encode(samples, codeword);
}
}
void MBEEncoder_Delete(MBEEncoder* pEncoder)
{
delete pEncoder;
pEncoder = NULL;
}
} // namespace vocoder

@ -81,9 +81,17 @@ namespace vocoder
}; };
// Extern methods for C#/C++ interop // Extern methods for C#/C++ interop
extern "C" MBEEncoder* MBEEncoder_Create(MBE_ENCODER_MODE mode) { return new MBEEncoder(mode); } extern "C" {
extern "C" void MBEEncoder_Encode(MBEEncoder* pEncoder, int16_t* samples, uint8_t* codeword) { pEncoder->encode(samples, codeword); } #ifdef _WIN32
extern "C" void MBEEncoder_Delete(MBEEncoder* pEncoder) { delete pEncoder; } __declspec(dllexport) MBEEncoder* MBEEncoder_Create(MBE_ENCODER_MODE mode);
__declspec(dllexport) void MBEEncoder_Encode(MBEEncoder* pEncoder, int16_t* samples, uint8_t* codeword);
__declspec(dllexport) void MBEEncoder_Delete(MBEEncoder* pEncoder);
#else
MBEEncoder* MBEEncoder_Create(MBE_ENCODER_MODE mode);
void MBEEncoder_Encode(MBEEncoder* pEncoder, int16_t* samples, uint8_t* codeword);
void MBEEncoder_Delete(MBEEncoder* pEncoder);
#endif
}
} // namespace vocoder } // namespace vocoder

Loading…
Cancel
Save

Powered by TurnKey Linux.