Click here to Skip to main content
13,050,237 members (131,223 online)
Click here to Skip to main content


76 bookmarked
Posted 8 Jan 2009

Building FM Radio with RDS Support

, 8 Jan 2009
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() {

      #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");
            //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) {

      private void Window_Unloaded(object sender, RoutedEventArgs e) {
         _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());


      #region properties
      private USBRadioDevice _device;
      private double _prevTune;

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.


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


About the Author

Tamir Khason
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.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 8 Jan 2009
Article Copyright 2009 by Tamir Khason
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid