SHARE
TWEET

Untitled

a guest Dec 19th, 2018 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public partial class MainWindow : Window
  2. {
  3.     private AudioLive MyLive;
  4.     public MainWindow()
  5.     {
  6.         InitializeComponent();
  7.  
  8.         MyLive = new AudioLive();
  9.         MyLive.Init();
  10.  
  11.  
  12.     }
  13.  
  14.     private void ButtonStart(object sender, RoutedEventArgs e)
  15.     {
  16.         MyLive.StartLive();
  17.     }
  18.  
  19.     private void ButtonEnd(object sender, RoutedEventArgs e)
  20.     {
  21.         MyLive.EndLive();
  22.     }
  23. }
  24.  
  25. class AudioLive
  26. {
  27.  
  28.     private WaveIn m_Recorder;
  29.     private BufferedWaveProvider m_BufferedWaveProvider;
  30.     private SavingWaveProvider m_SavingWaveProvider;
  31.     private WaveOut m_Player;
  32.  
  33.  
  34.     public void Dispose()
  35.     {
  36.         Dispose(true);
  37.  
  38.     }
  39.  
  40.  
  41.  
  42.     protected virtual void Dispose(bool disposing)
  43.     {
  44.         if (m_Recorder != null)
  45.         {
  46.             m_Recorder.Dispose();
  47.         }
  48.  
  49.  
  50.         m_Recorder = null;
  51.  
  52.         if (m_SavingWaveProvider != null)
  53.         {
  54.             m_SavingWaveProvider.Dispose();
  55.         }
  56.  
  57.  
  58.         m_SavingWaveProvider = null;
  59.  
  60.     }
  61.  
  62.     private void RecorderOnDataAvailable(object sender, WaveInEventArgs waveInEventArgs)
  63.     {            
  64.         m_BufferedWaveProvider.AddSamples(waveInEventArgs.Buffer, 0, waveInEventArgs.BytesRecorded);            
  65.     }
  66.  
  67.  
  68.  
  69.     public bool Init()
  70.     {
  71.         m_Recorder = new WaveIn();
  72.         m_Recorder.WaveFormat = new WaveFormat(44100, 2);
  73.  
  74.         m_Recorder.DataAvailable += RecorderOnDataAvailable;
  75.  
  76.         // set up our signal chain
  77.         m_BufferedWaveProvider = new BufferedWaveProvider(m_Recorder.WaveFormat);
  78.  
  79.         return true;
  80.     }
  81.  
  82.  
  83.  
  84.  
  85.     public void StartLive()
  86.     {
  87.  
  88.         m_SavingWaveProvider = new SavingWaveProvider(m_BufferedWaveProvider, "live.wav");
  89.  
  90.         // set up playback
  91.         m_Player = new WaveOut();
  92.         m_Player.Init(m_SavingWaveProvider);
  93.  
  94.         // begin playback & record
  95.         m_Player.Play();
  96.  
  97.         m_Recorder.StartRecording();
  98.  
  99.     }
  100.  
  101.  
  102.  
  103.     public void EndLive()
  104.     {
  105.  
  106.         // stop recording
  107.         m_Recorder.StopRecording();
  108.         // stop playback
  109.         m_Player.Stop();
  110.         m_Player.Dispose();
  111.  
  112.         // finalise the WAV file
  113.         m_SavingWaveProvider.Dispose();
  114.  
  115.     }
  116.  
  117.  
  118. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top