如何使用pythonnet从python订阅c#stream?

2024-09-30 16:26:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要用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返回stream并从python订阅它?在

2)另一个变体是在c端分析流,并向python脚本激发一些事件。我该怎么做?(我指的是事件的射击和处理方面)


Tags: thetosampleinstancenewreaddatabuffer