Madwin74

VirtualLine-in

May 7th, 2023 (edited)
785
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.05 KB | Source Code | 0 0
  1. /***********************************************************
  2.  **                                                       **
  3.  **   Codesnippets written by Madwin for MMP 2023         **
  4.  **                                                       **
  5.  **   Not to be used freely. the code still has errors    **
  6.  **   It's intended to be used as starting point.         **
  7.  **   Code has not been checked and might still need      **
  8.  **   (a lot of) work.                                    **
  9.  **                                                       **
  10.  **   © MadMedia Productions 2023                         **
  11.  **                                                       **
  12.  ***********************************************************/
  13.  
  14. //  C# class that creates an additional virtual line in audio input:
  15.  
  16.  
  17.  
  18. using System;
  19. using System.Runtime.InteropServices;
  20. using NAudio.Wave;
  21.  
  22. public class AudioInputWithVirtualLine : IDisposable
  23. {
  24.     private WaveInEvent waveIn;
  25.     private BufferedWaveProvider bufferedWaveProvider;
  26.     private WaveOut waveOut;
  27.     private bool disposed;
  28.  
  29.     public event EventHandler<WaveInEventArgs> DataAvailable;
  30.  
  31.     public AudioInputWithVirtualLine()
  32.     {
  33.         int inputDeviceNumber = 0; // Change this to the index of your desired input device
  34.  
  35.         waveIn = new WaveInEvent();
  36.         waveIn.DeviceNumber = inputDeviceNumber;
  37.         waveIn.WaveFormat = new WaveFormat(44100, 16, 1);
  38.         waveIn.DataAvailable += OnDataAvailable;
  39.  
  40.         bufferedWaveProvider = new BufferedWaveProvider(waveIn.WaveFormat);
  41.         bufferedWaveProvider.DiscardOnBufferOverflow = true;
  42.  
  43.         waveOut = new WaveOut();
  44.         waveOut.Init(bufferedWaveProvider);
  45.         waveOut.Play();
  46.     }
  47.  
  48.     private void OnDataAvailable(object sender, WaveInEventArgs e)
  49.     {
  50.         bufferedWaveProvider.AddSamples(e.Buffer, 0, e.BytesRecorded);
  51.         DataAvailable?.Invoke(this, e);
  52.     }
  53.  
  54.     public void Dispose()
  55.     {
  56.         if (!disposed)
  57.         {
  58.             disposed = true;
  59.             waveIn.StopRecording();
  60.             waveIn.Dispose();
  61.             bufferedWaveProvider.ClearBuffer();
  62.             waveOut.Stop();
  63.             waveOut.Dispose();
  64.         }
  65.     }
  66. }
  67.  
  68.  
  69. /*
  70. This class uses the NAudio library to capture audio input from
  71. the specified input device and route it to a virtual output device.
  72. The virtual output device is created using a BufferedWaveProvider
  73. and played back using a WaveOut object. The DataAvailable event is
  74. raised whenever new audio data is available from the input device.
  75.  
  76. You can use this class like this:
  77. */
  78.  
  79.  
  80.  
  81. var audioInput = new AudioInputWithVirtualLine();
  82. audioInput.DataAvailable += OnDataAvailable;
  83.  
  84. private void OnDataAvailable(object sender, WaveInEventArgs e)
  85. {
  86.     // Do something with the audio data
  87. }
  88.  
  89. // ...
  90.  
  91. audioInput.Dispose();
  92.  
  93.  
  94. /*
  95. Be sure to change the inputDeviceNumber field to the index of
  96. your desired input device. You can find the index of your input
  97. device by calling WaveInEvent.DeviceCount and iterating over
  98. the available devices.
  99. */
Advertisement
Add Comment
Please, Sign In to add comment