首页 > 解决方案 > 如何使用 pythonnet 从 python 订阅 c# 流?

问题描述

我需要用 python 分析 loopbackcapture 流。

我没有找到任何用于 wasapi loopbackcapture 的 python 包装,所以我不得不使用 c#(我没有任何经验)。

现在我有 c# 程序集 dll,它包装到 wasapiLoopbackCapture 并将其记录到文件中。但我需要实时进行分析。

 using (WasapiCapture soundIn = new WasapiLoopbackCapture())
        {

            //initialize the soundIn instance
            soundIn.Initialize();

            //create a SoundSource around the the soundIn instance
            SoundInSource soundInSource = new SoundInSource(soundIn) { FillWithZeros = false };

            //create a source, that converts the data provided by the soundInSource to any other format
            IWaveSource convertedSource = soundInSource
                .ChangeSampleRate(sampleRate) // sample rate
                .ToSampleSource()
                .ToWaveSource(bitsPerSample); //bits per sample

            //channels...
            convertedSource = convertedSource.ToMono()

            //create a new wavefile
            WaveWriter waveWriter = new WaveWriter(output_file, convertedSource.WaveFormat)
            //register an event handler for the DataAvailable event of the soundInSource
            soundInSource.DataAvailable += (s, e) =>
                {
                    //read data from the converedSource
                    byte[] buffer = new byte[convertedSource.WaveFormat.BytesPerSecond / 2];
                    int read;

                    //keep reading as long as we still get some data
                    while ((read = convertedSource.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        //write the read data to a file
                        waveWriter.Write(buffer, 0, read);
                    }
                };
}

1) 是否可以从 c# 返回流并从 python 订阅它?

2) 另一种变体是在 c# 端分析流并将一些事件触发到 python 脚本。我怎样才能做到这一点?(我的意思是事件的触发和处理方面)

标签: c#pythonpython.netwasapi

解决方案


推荐阅读