Click here to Skip to main content
15,896,474 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.5K   3.7K   76  
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.Data;
using System.Diagnostics;
using System.Windows;
using System.Windows.Threading;
using System.ComponentModel;
using System.Globalization;

namespace RadioPlayer {
   public static class WPFExtensions {
      [DebuggerStepThrough]
      public static BindingExpressionBase SetBinding(this DependencyObject target, DependencyProperty dp, object source, string path) {
         Binding b = new Binding(path);
         b.Source = source;
         return BindingOperations.SetBinding(target, dp, b);
      }

      [DebuggerStepThrough]
      public static BindingExpressionBase SetBinding(this DependencyObject target, DependencyProperty dp, object source, string path, IValueConverter converter) {
         Binding b = new Binding(path);
         b.Source = source;
         b.Converter = converter;
         return BindingOperations.SetBinding(target, dp, b);
      }

      public static DispatcherOperation Dispatch(this DispatcherObject sender, Action callback) { return sender.Dispatch(DispatcherPriority.Normal, callback); }

      public static DispatcherOperation Dispatch(this DispatcherObject sender,  DispatcherPriority priority, Action callback) {
         if (sender.Dispatcher == null) return null;
         if (sender.Dispatcher.CheckAccess()) {
            callback();
            return null;
         } else {
            return sender.Dispatcher.BeginInvoke(priority, callback);
         }
      }

      public static void AddValueChanged(this DependencyObject sender, DependencyProperty property, EventHandler handler) {
         DependencyPropertyDescriptor.FromProperty(property, sender.GetType()).AddValueChanged(sender, handler);
      }

      public static void RemoveValueChanged(this DependencyObject sender, DependencyProperty property, EventHandler handler) {
         DependencyPropertyDescriptor.FromProperty(property, sender.GetType()).RemoveValueChanged(sender, handler);
      }

      public static double ToRange(this double value, double minSource, double maxSource, double minTarget, double maxTarget) {
         var sr = maxSource - minSource;
         var tr = maxTarget - minTarget;
         var ratio = sr / tr;
         return minTarget+(value / ratio);
      }
   }

   public class ValueConverter<TIN, TOUT> : IValueConverter {

      public ValueConverter(Func<TIN, TOUT> forwardConversion) {
         ForwardConversion = forwardConversion;
      }

      public ValueConverter(Func<TIN, TOUT> forwardConversion, Func<TOUT, TIN> reverseConversion) {
         ForwardConversion = forwardConversion;
         ReverseConversion = reverseConversion;
      }

      public Func<TIN, TOUT> ForwardConversion { get; set; }

      public Func<TOUT, TIN> ReverseConversion { get; set; }
      
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
         try {
            var in1 = Object.ReferenceEquals(value, DependencyProperty.UnsetValue) ? default(TIN) : (TIN)value;
            return ForwardConversion(in1);
         } catch {
            return Binding.DoNothing;
         }
      }

      /// <summary>Implements <see cref="M:IValueConverter.ConvertBack"/>.</summary>
      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
         try {
            var out1 = Object.ReferenceEquals(value, DependencyProperty.UnsetValue) ? default(TOUT) : (TOUT)value;
            return ReverseConversion(out1);
         } catch {
            return Binding.DoNothing;
         }
      }

   }
}

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