Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.54 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using System.Windows.Threading;
  15. using CSCore;
  16. using CSCore.Codecs;
  17. using CSCore.SoundOut;
  18. using CSCore.SoundOut.DirectSound;
  19. using CSCore.Streams.SampleConverter;
  20. using CSCore.Codecs.MP3;
  21. using CSCore.DSP;
  22. using CSCore.Streams;
  23. using System.Threading.Tasks;
  24.  
  25. namespace WPFVisualisation
  26. {
  27.     public partial class MainWindow : Window
  28.     {
  29.         ISoundOut _soundOut;
  30.         DispatcherTimer _timer;
  31.  
  32.         IWaveSource _audioSource;
  33.         GainSource _gainSource;
  34.  
  35.         double _gain = 1;
  36.         public double Gain
  37.         {
  38.             get { return _gain; }
  39.             set
  40.             {
  41.                 _gain = value;
  42.                 if (_gainSource != null)
  43.                     _gainSource.Gain = (float)value;
  44.             }
  45.         }
  46.  
  47.         public MainWindow()
  48.         {
  49.             InitializeComponent();
  50.             DataContext = this;
  51.             CSCore.Context.Current.CreateDefaultLogger();
  52.         }
  53.  
  54.         private void OnOpenFile(object sender, RoutedEventArgs e)
  55.         {
  56.             var ofn = new Microsoft.Win32.OpenFileDialog()
  57.             {
  58.                 Filter = CodecFactory.SupportedFilesFilterDE,
  59.                 Title = "Datei auswählen"
  60.             };
  61.  
  62.             if (ofn.ShowDialog().GetValueOrDefault())
  63.             {
  64.                 ShowBufferedIndicator(false);
  65.                 OpenSource(CodecFactory.Instance.GetCodec(ofn.FileName));
  66.             }
  67.         }
  68.  
  69.         private void OnOpenStream(object sender, RoutedEventArgs e)
  70.         {
  71.             StreamURLSelector streamSelector = new StreamURLSelector();
  72.             if (streamSelector.ShowDialog().GetValueOrDefault())
  73.             {
  74.                 Stop();
  75.                 var stream = new Mp3WebStream(streamSelector.Value, true);
  76.                 stream.ConnectionCreated += (s, args) =>
  77.                 {
  78.                     if (args.Success)
  79.                     {
  80.                         Dispatcher.Invoke(new Action(() =>
  81.                         {
  82.                             ShowBufferedIndicator(true);
  83.                             OpenSource(stream);
  84.                         }));
  85.                     }
  86.                     else MessageBox.Show("Es konnte keine Verbindung zum Server hergestellt werden.", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
  87.                 };
  88.             }
  89.         }
  90.  
  91.         private void OpenSource(IWaveSource source)
  92.         {
  93.             try
  94.             {
  95.                 Stop(); //if playing -> stop playback
  96.                 _audioSource = source;
  97.  
  98.                 _gainSource = new GainSource(source) { Gain = (float)this.Gain };
  99.  
  100.                 source = SetupVisualization(_gainSource.ToWaveSource(32));
  101.  
  102.                 if (WasapiOut.IsSupportedOnCurrentPlatform) // > Vista
  103.                 {
  104.                     _soundOut = new WasapiOut(false, CSCore.CoreAudioAPI.AudioClientShareMode.Shared, 100);
  105.                 }
  106.                 else // < Vista
  107.                 {
  108.                     _soundOut = new DirectSoundOut() { Latency = 100 };
  109.                 }
  110.  
  111.                 _soundOut.Initialize(source);
  112.                 _soundOut.Stopped += OnPlaybackStopped;
  113.                 _soundOut.Play();
  114.             }
  115.             catch (CSCore.CoreAudioAPI.CoreAudioAPIException ex)
  116.             {
  117.                 MessageBox.Show("Unbekannter Fehler beim Abspielen: 0x" + ex.ErrorCode.ToString("x"), "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
  118.                 Stop();
  119.             }
  120.             catch (Exception ex)
  121.             {
  122.                 MessageBox.Show("Unbekannter Fehler beim Abspielen: " + ex.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
  123.                 Stop();
  124.             }
  125.         }
  126.  
  127.         private void OnPlaybackStopped(object sender, EventArgs e)
  128.         {
  129.             Context.Current.Logger.Debug("Playback stopped");
  130.         }
  131.  
  132.         private void Stop()
  133.         {
  134.             if (_soundOut != null)
  135.             {
  136.                 var soundOut = _soundOut;
  137.                 _soundOut = null;
  138.                 soundOut.Dispose();
  139.                 if(soundOut.WaveSource != null)
  140.                     soundOut.WaveSource.Dispose();
  141.             }
  142.         }
  143.  
  144.         private void OnClosing(object sender, System.ComponentModel.CancelEventArgs e)
  145.         {
  146.             Stop();
  147.         }
  148.  
  149.         private void ShowBufferedIndicator(bool show)
  150.         {
  151.             //display the amount of buffered data
  152.             if (show)
  153.             {
  154.                 if (_timer == null || !_timer.IsEnabled)
  155.                 {
  156.                     _timer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(500) };
  157.                     _timer.Tick += OnTimerTick;
  158.                 }
  159.  
  160.                 _timer.Start();
  161.                 bufferedDataIndicator.Visibility = System.Windows.Visibility.Visible;
  162.             }
  163.             else
  164.             {
  165.                 if(_timer != null)
  166.                     _timer.Stop();
  167.                 bufferedDataIndicator.Visibility = System.Windows.Visibility.Collapsed;
  168.             }
  169.         }
  170.  
  171.         private void OnTimerTick(object sender, EventArgs e)
  172.         {
  173.             //update the amount of buffered data
  174.             var stream = _audioSource as Mp3WebStream;
  175.             if (stream != null)
  176.             {
  177.                 bufferedDataIndicator.Maximum = stream.BufferSize;
  178.                 bufferedDataIndicator.Value = stream.BufferedBytes;
  179.             }
  180.         }
  181.  
  182.         private IWaveSource SetupVisualization(IWaveSource source)
  183.         {
  184.             //setup for FFTVisualization
  185.             source = new CSCore.Visualization.FFTDataProvider(source) { Bands = 512 }; //using 512 bands by default
  186.             //apply data provider
  187.             spectrum.DataProvider = source as CSCore.Visualization.FFTDataProvider;
  188.             peak.DataProvider = source as CSCore.Visualization.FFTDataProvider;
  189.  
  190.             //setup for SampleVisalization
  191.             var sdp = new CSCore.Visualization.SampleDataProvider(source);
  192.             //apply data provider
  193.             waveform.DataProvider = sdp;
  194.  
  195.             //convert back to raw data
  196.             source = sdp.ToWaveSource(16);
  197.             return source;
  198.         }
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement