Click here to Skip to main content
15,885,309 members
Articles / Desktop Programming / WPF

Building FM Radio with RDS Support

Rate me:
Please Sign up or sign in to vote.
4.89/5 (18 votes)
8 Jan 2009Ms-PL4 min read 97.1K   3.7K   75  
This article explains how to build a simple FM radio player with RDS support by using WPF and USBFM library
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using USBFM;
using System.Windows.Threading;
using System.ComponentModel;

namespace RadioPlayer {
   /// <summary>
   /// Interaction logic for Window1.xaml
   /// </summary>
   public partial class Window1 : Window {
      public Window1() {
         InitializeComponent();
      }

      #region methods
      private void Window_Loaded(object sender, RoutedEventArgs e) {
         try {
            _device = USBRadioDevice.FindDevice(RadioPlayer.Properties.Settings.Default.PID, RadioPlayer.Properties.Settings.Default.VID);

            _device.PropertyChanged += (s, ed) => {
               if (ed.PropertyName == "RDS" && _device.RDS != null) { 
                  //set bindings
                  this.Dispatch(() => {
                     Presets.SetBinding(ListBox.ItemsSourceProperty, _device, "Presets");
                     Freq.SetBinding(TextBlock.TextProperty, _device, "CurrentFrequency", new ValueConverter<double, double>(d => { return d == 0 ? _device.CurrentStation : d; }));
                     PS.SetBinding(TextBlock.TextProperty, _device.RDS, "PS");
                     PTY.SetBinding(TextBlock.TextProperty, _device.RDS, "PTYString");
                     MonoStereo.SetBinding(Path.DataProperty, _device.RDS, "IsStereo", new ValueConverter<bool, Geometry>(b => { return (Geometry)(b ? this.Resources["StereoGeometry"] : this.Resources["MonoGeometry"]); }));
                     SignalTransform.SetBinding(ScaleTransform.ScaleYProperty, _device.RDS,"SignalStrength", new ValueConverter<byte, double>(b => { return 1-(b / 36d); }));
                     MS.SetBinding(TextBlock.IsEnabledProperty, _device.RDS, "HasMS");
                     TA.SetBinding(TextBlock.IsEnabledProperty, _device.RDS, "HasTA");
                     TP.SetBinding(TextBlock.IsEnabledProperty, _device.RDS, "HasTP");
                  });
               }
            };
            _device.InitAudio();
            _device.InitRDSReports();
            
            //we're intercepting properties changes
            Volume.AddValueChanged(RotaryControl.RotaryControl.AngleProperty, (s, ex) => {
               DirectSoundMethods.Volume = (int)Volume.Angle.ToRange(Volume.CounterClockwiseMostAngle, Volume.ClockwiseMostAngle, -4000, 0);
            });

            Tune.AddValueChanged(RotaryControl.RotaryControl.AngleProperty, (s, ex) => {
               _device.Tune(Tune.Angle > _prevTune);
               _prevTune = Tune.Angle;
            });
            
         } catch (ApplicationException ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); }
      }

      private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
         _device.StopRDSReports();
         _device.StopAudio();
      }

      private void Window_Unloaded(object sender, RoutedEventArgs e) {
         _device.Close();
         _device.Dispose();
         _device = null;
      }

      private void Window_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
         if (e.Source.GetType().IsSubclassOf(typeof(Window)) || e.Source.GetType().Equals(typeof(TextBlock))) DragMove();
      }

      private void Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
         var button = sender as ContentPresenter;
         if (button != null) {
            var frq = double.Parse(button.Content.ToString());
            _device.Tune(frq);
         }
      }

      #endregion

      #region properties
      private USBRadioDevice _device;
      private double _prevTune;
      #endregion
   }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Architect Better Place
Israel Israel
Hello! My name is Tamir Khason, and I am software architect, project manager, system analyst and [of course] programmer. In addition to writing big amount of documentation, I also write code, a lot of code. I used to work as a freelance architect, project manager, trainer, and consultant here, in Israel, but recently join the company with extremely persuasive idea - to make a world better place. I have very pretty wife and 3 charming kids, but unfortunately almost no time for them.

To be updated within articles, I publishing, visit my blog or subscribe RSS feed. Also you can follow me on Twitter to be up to date about my everyday life.

Comments and Discussions