Building an FM Radio with RDS Support


This article explains how to use the open source USB FM library (written by me) and Windows Presentation Foundation to build a simple yet fully functional radio player with RDS and TMC support.


The USB FM library provides managed interfaces, developed with C# to USB FM receivers that support RDS. WPF (Windows Presentation Foundation) provides an easy-to-use framework to build rich user interfaces with zero time investment. "Blending" those together will bring you an ability to build fully functional applications without a heavy time investment.

Step 1: Building Wireframes

To build a WPF application, you should first build a wireframe. WPF provides you with a rich choice of layout controls. In your case, you'll use a Grid to mark up areas in the main (and only) application window.

      <RowDefinition Height="*"/>
      <RowDefinition Height="35px"/>
      <RowDefinition Height="Auto"/>
      <ColumnDefinition Width="Auto"/>
      <ColumnDefinition Width="*"/>
      <ColumnDefinition Width="Auto"/>

As you can see, you have three rows and three columns. Now, you can start putting your controls into it.

In any radio receiver, you have jogs to control volume level and tune to stations. There is a ready-made jog control, prepared by the Microsoft Expression Blend team, so you'll use it "as-is."

To do this, you have to reference the control library and define a namespace of the control within the XAML file of the application body.

   <c:RotaryControl Name="Volume" RotationIsConstrained="True"
                    ClockwiseMostAngle="340" Angle="340"/>
   <c:RotaryControl Name="Tune" Grid.Column="2"/>

Also, you'll add two labels and a list box of preset stations. These will be bound later to the FM device library.

<TextBlock Text="Volume" Grid.Row="1"/>
<TextBlock Text="Tune" Grid.Row="1" Grid.Column="2"/>

<ListBox Name="Presets"
         ItemTemplate="{StaticResource PresetTemplate}"
         HorizontalAlignment="Center" >
         <DockPanel Margin="0" IsItemsHost="True"/>

The only thing that remains in the XAML markup is to set the display for frequency and program text indicators, mono/stereo icon, and signal strength emitter. To set all those, you'll create another grid and put everything inside it.

<Grid Grid.Column="1">
      <RowDefinition Height="12px"/>
      <RowDefinition Height="*"/>
      <RowDefinition Height="20px"/>
      <RowDefinition Height="20px"/>
      <ColumnDefinition Width=".2*"/>
      <ColumnDefinition Width="*"/>
   <TextBlock Name="Freq"
              Style="{StaticResource LargeTextStyle}"/>
   <TextBlock Name="PS" Grid.Column="1" Grid.Row="2"/>
   <TextBlock Name="PTY"
              Style="{StaticResource PTYTextStyle}"/>
   <Path Name="MonoStereo"
   <Rectangle Grid.RowSpan="4"
              Fill="{StaticResource SignalBrush}"
   <Rectangle Grid.RowSpan="4"
         <ScaleTransform x:Name="SignalTransform"
   <StackPanel Grid.Column="1"
      <TextBlock Style="{StaticResource IndiStyle}"
                 Text="MS" Name="MS"/>
      <TextBlock Style="{StaticResource IndiStyle}"
                 Text="TA" Name="TA"/>
      <TextBlock Style="{StaticResource IndiStyle}"
                 Text="TP" Name="TP"/>

You are finished wireframing your application. Now, it's time to make it look better.

Building an FM Radio with RDS Support

Step 2: Styling the WPF Application

WPF is not just easy for building UI with markup. It also provides wide range of styling possibilities. Resources have a hierarchical structure; however, in your application you'll put all styles and templates in the Window.Resource level. First of all, set the application-wide style for all TextBlocks.

<Style TargetType="TextBlock">
   <Setter Property="TextAlignment" Value="Center"/>
   <Setter Property="FontFamily"
           Value="{x:Static SystemFonts.SmallCaptionFontFamily}"/>
   <Setter Property="FontStyle"
           Value="{x:Static SystemFonts.SmallCaptionFontStyle}"/>

As you can see, when you do not set the x:Key property, it is applied for all resources lower in the hierarchy. Also, you can inherit styles and set special keys to identify resources within the XAML markup and code.

<Style x:Key="LargeTextStyle" TargetType="TextBlock">
   <Setter Property="TextAlignment" Value="Center"/>
   <Setter Property="FontSize" Value="50"/>
<Style x:Key="PTYTextStyle" TargetType="TextBlock">
   <Setter Property="TextAlignment" Value="Right"/>
   <Setter Property="FontSize" Value="10"/>

Also, you can use triggers. Triggers are basic event handlers directly inside styles.

<Style BasedOn="{StaticResource PTYTextStyle}"
   <Setter Property="Margin" Value="5,0,5,0"/>
   <Setter Property="Foreground" Value="White"/>
      <Trigger Property="IsEnabled" Value="False">
         <Setter Property="Foreground" Value="Gray"/>

In additional to all this, you can completely redefine the look and feel of controls by overriding the Template property, like this:

<Style TargetType="Button">
   <Setter Property="Foreground" Value="White"/>
   <Setter Property="Template">
         <ControlTemplate TargetType="Button">
            <Border Height="25"
                    Name="PART_Border" >
                  <LinearGradientBrush EndPoint="0.854,0.854"
                     <GradientStop Color="#FF262626"
                     <GradientStop Color="#FFD7D7D7"
               <ContentPresenter HorizontalAlignment="Center"
                  <Trigger Property="IsPressed" Value="True">
                     <Setter Property="BorderBrush"
                              <GradientStop Color="#FF262626"
                              <GradientStop Color="#FFD7D7D7"

Styles aren't the only things that can be stored inside Resources. you also can share other objects, such as geometry (for mono/stereo indicator) or brushes.

<Geometry x:Key="MonoGeometry">
   M0,0L1,2 2,2 2,4 1,4 0,6z
<Geometry x:Key="StereoGeometry">
   M0,0L1,2 2,2 3,0 3,6 2,4 1,4 0,6z

<DrawingBrush x:Key="SignalBrush"
         <GeometryDrawing Brush="Black">
               <RectangleGeometry Rect="0,0,20,20"/>
         <GeometryDrawing Brush="White">
               <RectangleGeometry Rect="0,20,20,40"/>

Also, you can define templates for data classes used in the application. For example, I want to double the value of the radio preset to appear as a button. Here how to do this.

<DataTemplate x:Key="PresetTemplate">
   <Button Content="{Binding}" />

Now, you are completely done with the UI. It's time to go toward the "code-behind."

Building an FM Radio with RDS Support

Step 3: Wiring the Basic Business Logic

First of all, you have to initialize your USB FM device. This is very simple task. Just find:

_device =
   Default.PID, RadioPlayer.Properties.Settings.Default.VID);

Now, you need to subscribe to its events and wire data bindings for some members.

_device.PropertyChanged += (s, ed) => {
   if (ed.PropertyName == "RDS" && _device.RDS != null) { 
      //set bindings
      this.Dispatch(() => {
         Presets.SetBinding(ListBox.ItemsSourceProperty, _device,
         Freq.SetBinding(TextBlock.TextProperty, _device,
            new ValueConverter<double, double>(d =>
            { return d == 0 ? _device.CurrentStation : d; }));
         PS.SetBinding(TextBlock.TextProperty, _device.RDS, "PS");
         PTY.SetBinding(TextBlock.TextProperty, _device.RDS,
         MonoStereo.SetBinding(Path.DataProperty, _device.RDS,
            "IsStereo", new ValueConverter<bool, Geometry>(b =>
            { return (Geometry) (b ?
            this.Resources["StereoGeometry"] :
            this.Resources["MonoGeometry"]); }));
            new ValueConverter<byte, double>(b =>
            { return 1-(b / 36d); }));
         MS.SetBinding(TextBlock.IsEnabledProperty, _device.RDS,
         TA.SetBinding(TextBlock.IsEnabledProperty, _device.RDS,
         TP.SetBinding(TextBlock.IsEnabledProperty, _device.RDS,

In this code, I'm using some "time savers" that I developed to simplify some WPF aspects. If you want to learn more about those time savers, visit and subscribe via RSS to my blog.

Now is a good time to initialize the Audio and RDS reports:


An addition small trick is to be notified about when the volume and tune knobs' "angle" dependency property changed without setting binding explicitly.

   (s, ex) => {
   DirectSoundMethods.Volume =
      Volume.ClockwiseMostAngle, -4000, 0);

      (s, ex) => {
      _device.Tune(Tune.Angle > _prevTune);
   _prevTune = Tune.Angle;

Actually, you done. Now, your application is almost ready. The rest is clear and simple.

Building an FM Radio with RDS Support

Step 4: Finalizing the Application

First of all, you're using a platform to invoke calls in the USB FM library so that it implements the IDisposable interface. If you do not want to leave handlers in memory, it's a very good idea to stop the audio, RDS reports thread, and dispose of the USB handler.

private void Window_Closing(object sender,
   System.ComponentModel.CancelEventArgs e) {

private void Window_Unloaded(object sender, RoutedEventArgs e) {
   _device = null;

Also, your application running in a borderless window, so you have to drag and move it somehow. Why not to use the DragMove() Windows method? Note that the jog control is capturing move movement, so if you want it to continue working, it makes sense to learn where the mouse move came from.

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

The last thing is to get the preset button click and tune to the selected station.

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

You're done. Now, you're ready to compile and run your application. Wasn't it fun?


Revision History

  • January, 2009: Initial Publication.

This article was originally published on January 27th, 2009

About the Author

Tamir Khason

Hello! My name is Tamir Khason, and I am software architect, project manager, system analyst and [of couse] programmer. In addition to writing big amount of documentation, I also write code, a lot of code. I work as a freelance architect, project manager, trainer, and consultant here, in Israel. I have very pretty wife and 3 charming kids, but unfortunately almost no time for them. If you are in need of my services, please check out my experience, skillsets and rates. I can be reached at tamir[AT]khason.biz or at www.dotNet.us if you have any questions. To be updated within articles, I publishing, visit my blog or subscribe RSS feed.


Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date