Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***********************************************************
- ** **
- ** Codesnippets written by Madwin for MMP 2023 **
- ** **
- ** Not to be used freely. the code still has errors **
- ** It's intended to be used as starting point. **
- ** Code has not been checked and might still need **
- ** (a lot of) work. **
- ** **
- ** © MadMedia Productions 2023 **
- ** **
- ***********************************************************/
- // C# class that creates an additional virtual line in audio input:
- using System;
- using System.Runtime.InteropServices;
- using NAudio.Wave;
- public class AudioInputWithVirtualLine : IDisposable
- {
- private WaveInEvent waveIn;
- private BufferedWaveProvider bufferedWaveProvider;
- private WaveOut waveOut;
- private bool disposed;
- public event EventHandler<WaveInEventArgs> DataAvailable;
- public AudioInputWithVirtualLine()
- {
- int inputDeviceNumber = 0; // Change this to the index of your desired input device
- waveIn = new WaveInEvent();
- waveIn.DeviceNumber = inputDeviceNumber;
- waveIn.WaveFormat = new WaveFormat(44100, 16, 1);
- waveIn.DataAvailable += OnDataAvailable;
- bufferedWaveProvider = new BufferedWaveProvider(waveIn.WaveFormat);
- bufferedWaveProvider.DiscardOnBufferOverflow = true;
- waveOut = new WaveOut();
- waveOut.Init(bufferedWaveProvider);
- waveOut.Play();
- }
- private void OnDataAvailable(object sender, WaveInEventArgs e)
- {
- bufferedWaveProvider.AddSamples(e.Buffer, 0, e.BytesRecorded);
- DataAvailable?.Invoke(this, e);
- }
- public void Dispose()
- {
- if (!disposed)
- {
- disposed = true;
- waveIn.StopRecording();
- waveIn.Dispose();
- bufferedWaveProvider.ClearBuffer();
- waveOut.Stop();
- waveOut.Dispose();
- }
- }
- }
- /*
- This class uses the NAudio library to capture audio input from
- the specified input device and route it to a virtual output device.
- The virtual output device is created using a BufferedWaveProvider
- and played back using a WaveOut object. The DataAvailable event is
- raised whenever new audio data is available from the input device.
- You can use this class like this:
- */
- var audioInput = new AudioInputWithVirtualLine();
- audioInput.DataAvailable += OnDataAvailable;
- private void OnDataAvailable(object sender, WaveInEventArgs e)
- {
- // Do something with the audio data
- }
- // ...
- audioInput.Dispose();
- /*
- Be sure to change the inputDeviceNumber field to the index of
- your desired input device. You can find the index of your input
- device by calling WaveInEvent.DeviceCount and iterating over
- the available devices.
- */
Advertisement
Add Comment
Please, Sign In to add comment