これは、 CallbackReadでsrc_callback_readを呼び出すときのライブラリSecret Rabbit Code (別名 libsamplerate)の .NET ラッパーです: AccessViolationException - Attempted to read or write protected memory .
Cヘッダーから:
/* Opaque data type SRC_STATE. */
typedef struct SRC_STATE_tag SRC_STATE ;
typedef long (*src_callback_t) (void *cb_data, float **data) ;
SRC_STATE* src_callback_new (src_callback_t func, int converter_type, int channels, int *error, void* cb_data) ;
long src_callback_read (SRC_STATE *state, double src_ratio, long frames, float *
次に、次を作成しました。
C ライブラリを呼び出す C++/CLI クラス ライブラリ:
public delegate long SRCCallback ( void *cb_data, float **data ) ;
static IntPtr CallbackNew ( SRCCallback ^func, RabbitConverter converter_type, int channels, int *error, void* cb_data )
{
src_callback_t* cb = ( src_callback_t* ) &func;
SRC_STATE* state = src_callback_new ( *cb, ( int ) converter_type, channels, error, cb_data );
IntPtr ptr = IntPtr ( state );
return ptr;
}
static long CallbackRead ( IntPtr src_state, double src_ratio, long frames , float * data )
{
void* toPointer = src_state.ToPointer();
SRC_STATE* state = ( SRC_STATE* ) toPointer;
long l = src_callback_read ( state, src_ratio, frames, data );
return l;
}
C++/CLI ライブラリを使用する C# アプリケーション:
private static readonly unsafe SRCCallback _callback = target;
private static unsafe void Callback()
{
var input = GetSine(1000.0d, 44100, 1);
fixed (float* f = input)
{
RabbitError error = RabbitError.SRC_ERR_NO_ERROR;
var @void = Rabbit.CallbackNew(_callback, RabbitConverter.SRC_SINC_BEST_QUALITY, 1, (int*) (&error),
(void*) IntPtr.Zero);
var callbackRead = Rabbit.CallbackRead(@void , 1d, input.Length/2, f);
if (callbackRead != input.Length)
{
// throw new InvalidOperationException();
}
}
}
private static unsafe int target(void* cb_data, float** data)
{
return 9;
}