在.NET Core中录制音频

时间:2018-09-21 13:24:27

标签: .net audio .net-core cross-platform

正如标题所示,我正在寻找从.NET Core中的麦克风捕获音频的方法。对我来说重要的是它是跨平台的。我已经在Windows,Linux和OSX上的OpenCV中进行了大量视频处理。音频对我来说是一个难题。

3 个答案:

答案 0 :(得分:0)

不幸的是,我不知道为您提供帮助的API或库。

如果其他人也不知道,那么您有两种选择:

  1. 自己编写一个跨平台的api(看一下CoreFx或CoreFxLab仓库),在Windows上您可以使用P / Invoke来使用系统dll,在其他平台上您还需要其他东西
  2. 获取跨平台记录器可执行文件并与.net核心功能中的文件交互

答案 1 :(得分:0)

之前我已经在.NET Core上编写了一个记录器应用程序,但是它不再属于我的属性了……但是,这是我在使用它时使用的the library

LibsoundIO Sharp能够将配置扩展到诸如输入或输出的采样率之类的东西,并且具有类似以下的示例:

class Record
    {
        static SoundIORingBuffer ring_buffer = null;

        static SoundIOFormat [] prioritized_formats = {
            SoundIODevice.Float32NE,
            SoundIODevice.Float32FE,
            SoundIODevice.S32NE,
            SoundIODevice.S32FE,
            SoundIODevice.S24NE,
            SoundIODevice.S24FE,
            SoundIODevice.S16NE,
            SoundIODevice.S16FE,
            SoundIODevice.Float64NE,
            SoundIODevice.Float64FE,
            SoundIODevice.U32NE,
            SoundIODevice.U32FE,
            SoundIODevice.U24NE,
            SoundIODevice.U24FE,
            SoundIODevice.U16NE,
            SoundIODevice.U16FE,
            SoundIOFormat.S8,
            SoundIOFormat.U8,
            SoundIOFormat.Invalid,
        };

        static readonly int [] prioritized_sample_rates = {
            48000,
            44100,
            96000,
            24000,
            0,
        };

        public static int Main (string [] args)
        {
            string device_id = null;
            string backend_name = null;
            bool raw = false;
            string outfile = null;

            foreach (var arg in args) {
                switch (arg) {
                case "--raw":
                    raw = true;
                    continue;
                default:
                    if (arg.StartsWith ("--backend:"))
                        backend_name = arg.Substring (arg.IndexOf (':') + 1);
                    else if (arg.StartsWith ("--device:"))
                        device_id = arg.Substring (arg.IndexOf (':') + 1);
                    else
                        outfile = arg;
                    continue;
                }
            }

            var api = new SoundIO ();

            var backend = backend_name == null ? SoundIOBackend.None : (SoundIOBackend)Enum.Parse (typeof (SoundIOBackend), backend_name);
            if (backend == SoundIOBackend.None)
                api.Connect ();
            else
                api.ConnectBackend (backend);
            Console.WriteLine ("backend: " + api.CurrentBackend);

            api.FlushEvents ();

            var device = device_id == null ? api.GetInputDevice (api.DefaultInputDeviceIndex) :
                Enumerable.Range (0, api.InputDeviceCount)
                .Select (i => api.GetInputDevice (i))
                .FirstOrDefault (d => d.Id == device_id && d.IsRaw == raw);
            if (device == null) {
                Console.Error.WriteLine ("device " + device_id + " not found.");
                return 1;
            }
            Console.WriteLine ("device: " + device.Name);
            if (device.ProbeError != 0) {
                Console.Error.WriteLine ("Cannot probe device " + device_id + ".");
                return 1;
            }

            var sample_rate = prioritized_sample_rates.First (sr => device.SupportsSampleRate (sr));

            var fmt = prioritized_formats.First (f => device.SupportsFormat (f));

            var instream = device.CreateInStream ();
            instream.Format = fmt;
            instream.SampleRate = sample_rate;
            instream.ReadCallback = (fmin, fmax) => read_callback (instream, fmin, fmax);
            instream.OverflowCallback = () => overflow_callback (instream);

            instream.Open ();

            const int ring_buffer_duration_seconds = 30;
            int capacity = (int)(ring_buffer_duration_seconds * instream.SampleRate * instream.BytesPerFrame);
            ring_buffer = api.CreateRingBuffer (capacity);
            var buf = ring_buffer.WritePointer;

            instream.Start ();

            Console.WriteLine ("Type CTRL+C to quit by killing process...");
            using (var fs = File.OpenWrite (outfile)) {
                var arr = new byte [capacity];
                unsafe {
                    fixed (void* arrptr = arr) {
                        for (; ; ) {
                            api.FlushEvents ();
                            Thread.Sleep (1000);
                            int fill_bytes = ring_buffer.FillCount;
                            var read_buf = ring_buffer.ReadPointer;

                            Buffer.MemoryCopy ((void*)read_buf, arrptr, fill_bytes, fill_bytes);
                            fs.Write (arr, 0, fill_bytes);
                            ring_buffer.AdvanceReadPointer (fill_bytes);
                        }
                    }
                }
            }
            instream.Dispose ();
            device.RemoveReference ();
            api.Dispose ();
            return 0;
        }

        static void read_callback (SoundIOInStream instream, int frame_count_min, int frame_count_max)
        {
            var write_ptr = ring_buffer.WritePointer;
            int free_bytes = ring_buffer.FreeCount;
            int free_count = free_bytes / instream.BytesPerFrame;

            if (frame_count_min > free_count)
                throw new InvalidOperationException ("ring buffer overflow"); // panic()

            int write_frames = Math.Min (free_count, frame_count_max);
            int frames_left = write_frames;

            for (; ; ) {
                int frame_count = frames_left;

                var areas = instream.BeginRead (ref frame_count);

                if (frame_count == 0)
                    break;

                if (areas.IsEmpty) {
                    // Due to an overflow there is a hole. Fill the ring buffer with
                    // silence for the size of the hole.
                    for (int i = 0; i < frame_count * instream.BytesPerFrame; i++)
                        Marshal.WriteByte (write_ptr + i, 0);
                    Console.Error.WriteLine ("Dropped {0} frames due to internal overflow", frame_count);
                } else {
                    for (int frame = 0; frame < frame_count; frame += 1) {
                        int chCount = instream.Layout.ChannelCount;
                        int copySize = instream.BytesPerSample;
                        unsafe {
                            for (int ch = 0; ch < chCount; ch += 1) {
                                var area = areas.GetArea (ch);
                                Buffer.MemoryCopy ((void*)area.Pointer, (void*)write_ptr, copySize, copySize);
                                area.Pointer += area.Step;
                                write_ptr += copySize;
                            }
                        }
                    }
                }

                instream.EndRead ();

                frames_left -= frame_count;
                if (frames_left <= 0)
                    break;
            }

            int advance_bytes = write_frames * instream.BytesPerFrame;
            ring_buffer.AdvanceWritePointer (advance_bytes);
        }

        static int overflow_callback_count = 0;
        static void overflow_callback (SoundIOInStream instream)
        {
            Console.Error.WriteLine ("overflow {0}", overflow_callback_count++);
        }
    }

Adapted from sio-record/Program.cs

就.NET Core而言,您可以将其作为独立项目放在单例类中,或者如果您正在处理具有DI容器的项目,只需将其作为单例扔进去。

话虽如此,您需要libsoundio才能使工作正常,因为毕竟,它像音频的“ java”,而libsoundio-sharp是C#的包装。干杯!

答案 2 :(得分:-1)

OpenTK.NETCore是一个非常好的OpenTK跨平台端口。

使用AudioCapture类,我已经能够跨不同的OS平台捕获麦克风数据。

using (var audioCapture = new AudioCapture(_recorders.First(), _sampling_rate, ALFormat.Mono16, buffer_length_samples))
{
    audioCapture.Start();

    int available_samples = audioCapture.AvailableSamples;

    _buffer = _pool.Rent(MathHelper.NextPowerOfTwo((int)((available_samples * _sampleToByte / (double)BlittableValueType.StrideOf(_buffer)) + 0.5)));

    if (available_samples > 0)
    {
        audioCapture.ReadSamples(_buffer, available_samples);
    }
    else
    {
        _pool.Return(_buffer);
    }
}

编辑:基于OpenTK示例中的Parrot项目。