Click here to Skip to main content
15,894,630 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.4K   3.7K   75  
This article explains how to build a simple FM radio player with RDS support by using WPF and USBFM library
<Window x:Class="RadioPlayer.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="clr-namespace:RotaryControl;assembly=RotaryControl"
    Title="USB Radio" Height="200" Width="500"
        WindowStartupLocation="CenterScreen"
        WindowStyle="None"
        AllowsTransparency="True"
        Background="Black"
        Foreground="White"  
        Loaded="Window_Loaded"
        Unloaded="Window_Unloaded"
        Closing="Window_Closing"
        PreviewMouseLeftButtonDown="Window_PreviewMouseLeftButtonDown"
        >
    <Window.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="TextAlignment" Value="Center"/>
            <Setter Property="FontFamily" Value="{x:Static SystemFonts.SmallCaptionFontFamily}"/>
            <Setter Property="FontStyle" Value="{x:Static SystemFonts.SmallCaptionFontStyle}"/>
        </Style>
        <Style x:Key="LargeTextStyle" TargetType="TextBlock">
            <Setter Property="TextAlignment" Value="Center"/>
            <Setter Property="FontSize" Value="50"/>
        </Style>
        <Style x:Key="PTYTextStyle" TargetType="TextBlock">
            <Setter Property="TextAlignment" Value="Right"/>
            <Setter Property="FontSize" Value="10"/>
        </Style>
        <Style BasedOn="{StaticResource PTYTextStyle}" x:Key="IndiStyle" TargetType="TextBlock">
            <Setter Property="Margin" Value="5,0,5,0"/>
            <Setter Property="Foreground" Value="White"/>
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Foreground" Value="Gray"/>
                </Trigger>
            </Style.Triggers>
        </Style>
        <Style TargetType="Button">
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border Height="25" Width="35" BorderThickness=".5" Background="Black" Name="PART_Border" >
                            <Border.BorderBrush>
                                <LinearGradientBrush EndPoint="0.854,0.854" StartPoint="0.146,0.146">
                                    <GradientStop Color="#FF262626" Offset="0"/>
                                    <GradientStop Color="#FFD7D7D7" Offset="1"/>
                                </LinearGradientBrush>
                            </Border.BorderBrush>
                            <ContentPresenter HorizontalAlignment="Center" SnapsToDevicePixels="True" Margin="0" MouseLeftButtonDown="Button_MouseLeftButtonDown"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter Property="BorderBrush" TargetName="PART_Border">
                                    <Setter.Value>
                                        <LinearGradientBrush EndPoint="0.854,0.854" StartPoint="0.146,0.146">
                                            <GradientStop Color="#FF262626" Offset="1"/>
                                            <GradientStop Color="#FFD7D7D7" Offset="0"/>
                                        </LinearGradientBrush>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style TargetType="{x:Type c:RotaryControl}">
            <Setter Property="SnapToSnapAngle" Value="False"/>
        </Style>
        <DataTemplate x:Key="PresetTemplate">
            <Button Content="{Binding}" />
        </DataTemplate>
        <Geometry x:Key="MonoGeometry">M0,0L1,2 2,2 2,4 1,4 0,6z</Geometry>
        <Geometry x:Key="StereoGeometry">M0,0L1,2 2,2 3,0 3,6 2,4 1,4 0,6z</Geometry>
        <DrawingBrush x:Key="SignalBrush" TileMode="Tile" Viewport="0,0,.3,.1" Stretch="Uniform">
            <DrawingBrush.Drawing>
                <DrawingGroup>
                    <GeometryDrawing Brush="Black">
                        <GeometryDrawing.Geometry>
                            <RectangleGeometry Rect="0,0,20,20"/>
                        </GeometryDrawing.Geometry>
                    </GeometryDrawing>
                    <GeometryDrawing Brush="White">
                        <GeometryDrawing.Geometry>
                            <RectangleGeometry Rect="0,20,20,40"/>
                        </GeometryDrawing.Geometry>
                    </GeometryDrawing>
                </DrawingGroup>
            </DrawingBrush.Drawing>
        </DrawingBrush>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="35px"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <c:RotaryControl Name="Volume" RotationIsConstrained="True"  ClockwiseMostAngle="340" Angle="340"/>
        <c:RotaryControl Name="Tune" Grid.Column="2"/>

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

        <ListBox Name="Presets" ItemTemplate="{StaticResource PresetTemplate}" Grid.ColumnSpan="3" Grid.Row="2" Background="Transparent" HorizontalAlignment="Center" >
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <DockPanel Margin="0" IsItemsHost="True"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>
        
        <Grid Grid.Column="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="12px"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="20px"/>
                <RowDefinition Height="20px"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width=".2*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Name="Freq" Grid.Column="1" Grid.Row="0" Grid.RowSpan="2" Style="{StaticResource LargeTextStyle}"/>
            <TextBlock Name="PS" Grid.Column="1" Grid.Row="2"/>
            <TextBlock Name="PTY" Grid.Column="1" Grid.Row="0" Style="{StaticResource PTYTextStyle}"/>
            <Path Name="MonoStereo" Stroke="White" Fill="White" Stretch="Fill" Grid.Column="1" Grid.Row="0" Width="12" Height="12" HorizontalAlignment="Left"/>
            <Rectangle Grid.RowSpan="4" Fill="{StaticResource SignalBrush}" Margin="10"/>
            <Rectangle Grid.RowSpan="4" Fill="Black" Margin="9" RenderTransformOrigin="0.5,0">
                <Rectangle.RenderTransform>
                    <ScaleTransform x:Name="SignalTransform" ScaleX="1"/>
                </Rectangle.RenderTransform>
            </Rectangle>
            <StackPanel Grid.Column="1" Grid.Row="4" HorizontalAlignment="Right" Orientation="Horizontal">
                <TextBlock Style="{StaticResource IndiStyle}" Text="MS" Name="MS"/>
                <TextBlock Style="{StaticResource IndiStyle}" Text="TA" Name="TA"/>
                <TextBlock Style="{StaticResource IndiStyle}" Text="TP" Name="TP"/>
            </StackPanel>
        </Grid>
    </Grid>
</Window>

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