Building Applications that Can Talk

هذه المقالة متوفرة أيضا باللغة العربية، اقرأها هنا.

Overview

Stephen Hawking is one of the most famous people using speech synthesis to communicate
Stephen Hawking is one of the most famous people using speech synthesis to communicate

In this article we are going to explore the Speech API library that’s part of the TTS SDK that helps you reading text and speaking it. We’re going to see how to do it programmatically using C# and VB.NET and how to make use of LINQ to make it more interesting. The last part of this article talks about…… won’t tell you more, let’s see!

Introduction

The Speech API library that we are going to use today is represented by the file sapi.dll which’s located in %windir%System32SpeechCommon. This library is not part of the .NET BCL and it’s not even a .NET library, so we’ll use Interoperability to communicate with it (don’t worry, using Visual Studio it’s just a matter of adding a reference to the application.)

Implementation

In this example, we are going to create a Console application that reads text from the user and speaks it. To complete this example, follow these steps:

As an example, we’ll create a simple application that reads user inputs and speaks it. Follow these steps:

  1. Create a new Console application.
  2. Add a reference to the Microsoft Speech Object Library (see figure 1.)

    Figure 1 - Adding Reference to SpeechLib Library
    Figure 1 - Adding Reference to SpeechLib Library
  3. Write the following code and run your application:
// C#

using SpeechLib;

static void Main()
{
    Console.WriteLine("Enter the text to read:");
    string txt = Console.ReadLine();
    Speak(txt);
}

static void Speak(string text)
{
    SpVoice voice = new SpVoiceClass();
    voice.Speak(text, SpeechVoiceSpeakFlags.SVSFDefault);
}
' VB.NET

Imports SpeechLib

Sub Main()
    Console.WriteLine("Enter the text to read:")
    Dim txt As String = Console.ReadLine()
    Speak(txt)
End Sub

Sub Speak(ByVal text As String)
    Dim voice As New SpVoiceClass()
    voice.Speak(text, SpeechVoiceSpeakFlags.SVSFDefault)
End Sub

If you are using Visual Studio 2010 and .NET 4.0 and the application failed to run because of Interop problems, try disabling Interop Type Embedding feature from the properties on the reference SpeechLib.dll.

Building Talking Strings

Next, we’ll make small modifications to the code above to provide an easy way to speak a given System.String. We’ll make use of the Extension Methods feature of LINQ to add the Speak() method created earlier to the System.String. Try the following code:

// C#

using SpeechLib;

static void Main()
{
    Console.WriteLine("Enter the text to read:");
    string txt = Console.ReadLine();
    txt.Speak();
}

static void Speak(this string text)
{
    SpVoice voice = new SpVoiceClass();
    voice.Speak(text, SpeechVoiceSpeakFlags.SVSFDefault);
}
' VB.NET

Imports SpeechLib
Imports System.Runtime.CompilerServices

Sub Main()
    Console.WriteLine("Enter the text to read:")
    Dim txt As String = Console.ReadLine()
    txt.Speak()
End Sub

<Extension()> _
Sub Speak(ByVal text As String)
    Dim voice As New SpVoiceClass()
    voice.Speak(text, SpeechVoiceSpeakFlags.SVSFDefault)
End Sub

I Love YOU ♥

Let’s make it more interesting. We are going to code a VBScript file that says “I Love YOU” when you call it. To complete this example, these steps:

  1. Open Notepad.
  2. Write the following code:
    CreateObject("SAPI.SpVoice").Speak "I love YOU!"

    Of course, CreateObject() is used to create a new instance of an object resides in a given library. SAPI is the name of the Speech API library stored in Windows Registry. SpVoice is the class name.

  3. Save the file as ‘love.vbs’ (you can use any name you like, just preserve the vbs extension.)
  4. Now open the file and listen, who is telling that he loves you!

Microsoft Speech API has many voices; two of them are Microsoft Sam (male), the default for Windows XP and Windows 2000, and Microsoft Ann (female), the default for Windows Vista and Windows 7. Read more about Microsoft TTS voices here.

Thanks to our friend, Mohamed Gafar, for providing the VBScript.

Changing Screen Resolution Programmatically via DirectX

هذه المقالة متوفرة أيضا باللغة العربية، اقرأها هنا.

Overview

This lesson focuses on how to change the screen resolution and color system programmatically via DirectX. It starts by an overview about how the Windows satisfies user’s need through the Display Settings window. Then, it digs into discussing how to retrieve these settings and to change these programmatically in the .NET environment.

Introduction

It is common to change the screen resolution when working with some applications. In addition, games automatically change the screen resolution (bounds) and color system (bit count) to accommodate performance issues.

Background

In Windows, you can change display settings from Display Settings window where you can change screen resolution and color system. Figure 1 shows the Display Settings window.

Display Settings
Figure 1. Display Settings Dialog

However, your screen might support more than these settings. For instance, it could support 8 bit color system which you cannot see in the colors list.

To list all modes that are supported by your screen, you can click Advanced Settings then List All Modes button to display all modes supported and change to the desired mode. Figure 2 shows the List All Modes dialog.

Display List All Modes
Figure 2. Listing All Display Modes Supported

What is a mode? A mode is a combination of four settings, resolution (width and height,) orientation (rotation,) bit count (color system,) and frequency (refresh rate.)

Accessing the DirectX Library

DirectX is the technology of choice when working with multimedia of any type. Here in this lesson, we will focus on how to use DirectX to retrieve screen settings and to change them in the .NET environment.

DirectX consists of many libraries of which every library is specialized in some processing. The library we are interested in is dx3j.dll (Direct 1.0 Type Library) which resides in the System32 folder. You can reference this library in your project by adding it from the Add Reference dialog from the COM tab. Because it is a COM component, you end up creating an interop assembly (DIRECTLIB.dll) for accessing the library. Figure 3 shows the Add Reference dialog.

Figure 3. Adding Direct 1.0 Type Library to References
Figure 3. Adding Direct 1.0 Type Library to References

Because there is no such compatibility between .NET and unmanaged code, you cannot call COM components directly. Instead, you may create a RCW (Runtime Callable Wrapper) assembly that acts as a proxy to the COM component. RCWs also called Interop Assemblies are created automatically when you try to reference a COM component in Visual Studio .NET. However, if you want to have more control over the creation process, you can create your RCW via the tool tlbimp.exe. This tool allows you to control the RCW at a granular level. For instance it allows you to sign the RCW, change its name and version, and to control the marshalling process of the unmanaged types. It is worth mentioning that ActiveX COM components are created with the tool aximp.exe not tlbimp.exe.

Retrieving Current Display Settings

After referencing the required DirectX library, you can now dig into the details of programmatically retrieving and changing display settings.

You can access display settings from the _dxj_DirectDrawClass that resides in our RCW assembly.

The getDisplayMode() function is used to retrieve current display mode information; it accepts a single output parameter that is of type DDSurfaceDesc which encapsulates the retrieved data.

Like other DirectX structures, DDSurfaceDesc is fairly big. However, here, we are interested in four members:

  • width and height:
    Represent the screen bounds (resolution.)
  • rgbBitCount:
    Represents the bit count (color system) of the screen.
  • refreshRate:
    Represents the screen refresh rate (monitor flicker.)

Now, it is the time for the code retrieves current display mode information:

// C# Code

static void Main()
{
    DIRECTLib._dxj_DirectDrawClass ddraw =
        new DIRECTLib._dxj_DirectDrawClass();

    DIRECTLib.DDSurfaceDesc desc;

    ddraw.getDisplayMode(out desc);

    Console.WriteLine("{0} by {1}, {2} bit, {3} Hertz",
        desc.width, desc.height,
        desc.rgbBitCount, desc.refreshRate);
}
' VB.NET Code

Sub Main()
    Dim ddraw As _
        New DIRECTLib._dxj_DirectDrawClass()

    Dim desc As DIRECTLib.DDSurfaceDesc

    ddraw.getDisplayMode(desc)

    Console.WriteLine("{0} by {1}, {2} bit, {3} Hertz", _
        desc.width, desc.height, _
        desc.rgbBitCount, desc.refreshRate)
End Sub

Changing Current Display Settings

Changing the current display settings is very easy. All you need is to provide the new settings to the setDisplayMode() function.

The setDisplayMode() function takes five parameters. However, we are interested in the first three parameters:

  • w:
    The screen width.
  • h:
    The screen height:
  • bpp:
    The bit count (color system.)

The following code sets the display bounds to 640 by 480, and sets the bit count to only 8. I think that feeling reminds you of the ancients Windows ME and its ascendants specially before installing the video driver.

    // C# Code

static void Main()
{
    DIRECTLib._dxj_DirectDrawClass ddraw =
        new DIRECTLib._dxj_DirectDrawClass();

    DIRECTLib.DDSurfaceDesc desc =
        new DIRECTLib.DDSurfaceDesc();

    ModeCallback callback = new ModeCallback();
    const uint DDEDM_REFRESHRATES = 3;
    string format = "{0} by {1}, {2} bit, {3} Hertz";
    ddraw.enumDisplayModes
        (DDEDM_REFRESHRATES, ref desc, format, callback);
}

class ModeCallback : DIRECTLib.IEnumModesCallback
{
    public void callbackEnumModes
        (ref DIRECTLib.DDSurfaceDesc surfDesc, object ctxt)
    {
        Console.WriteLine(ctxt.ToString(),
            surfDesc.width, surfDesc.height,
            surfDesc.rgbBitCount, surfDesc.refreshRate);
    }
}
    ' VB.NET Code

Sub Main()

    Dim ddraw As New DIRECTLib._dxj_DirectDrawClass()

    Dim desc As New DIRECTLib.DDSurfaceDesc()

    Dim callback As New ModeCallback()
    Const DDEDM_REFRESHRATES As UInt32 = 3
    Dim format As String = "{0} by {1}, {2} bit, {3} Hertz"
        ddraw.enumDisplayModes _
        (DDEDM_REFRESHRATES, desc, format, callback)
End Sub

Class ModeCallback
    Implements DIRECTLib.IEnumModesCallback

    Public Sub callbackEnumModes _
        (ByRef surfDesc As DIRECTLib.DDSurfaceDesc, _
        ByVal ctxt As Object) _
        Implements DIRECTLib.IEnumModesCallback.callbackEnumModes
        Console.WriteLine(ctxt.ToString(), _
        surfDesc.width, surfDesc.height, _
        surfDesc.rgbBitCount, surfDesc.refreshRate)
    End Sub
End Class

Notice that closing your application rolls everything back to its original state.
It is worth mentioning that, trying to change the display settings to a mode that is not supported by the display throws NotImplementedException. Despite this, you can enumerate all display modes by the enumDisplayModes() function.

The Last Word

You can use this technique to change the display settings at the beginning of the application and let the runtime returns it back for you when closing the application. For instance, you could add the code to the Main() function and everything will be returned back after the last line of Main() completes.

Download Vista Bridge Sample Library 1.4

The Vista Bridge Sample Library contains source code for assemblies that provide managed code developers access to Windows Vista features that are not available in the .NET Framework. Some of the features included in the Vista Bridge Sample Library are – Vista style Task and File Dialogs, Common Open and Save dialogs, Application Recovery and Restart, Known Folders, Network Lists, Power Management, User Account Control, CommandLink control, Aero Wizard Control, System provided icons etc.

The Vista Bridge Sample Library also includes sample applications that demonstrate many of the features included in the library. A help file for the library, (vistabridgedocumentation.chm), is included.

The latest version (1.4) of this library includes various bug fixes and new features like Custom Controls for Common File Dialogs, BreadCrumb bar control and Aero Glass.

To build the library in Visual Studio 2008, please extract the contents of the ‘VistaBridge1.4.zip’ file in a new folder, open the included ‘vistabridge.sln’ file and build it.

Download from HERE

Windows 7 .NET Interop Sample Libraries

We’ve got .NET sample libraries and example for managed code developers to target Windows 7 new APIs!

The Windows 7 Beta SDK is a great source of documentation and examples for using these new sets of APIs. However, most of the examples are written in native code similar to Windows 7 APIs which are all native C, C++, and COM APIs, which makes the life of managed code developers a bit hard. For that reason, Microsoft created the Windows Vista Bridge project that makes it easier for managed code developers to use Windows OS-specific APIs such as those described above.

The current version of the Window Vista Bridge, version 1.4 contains many useful “Windows Vista” features such as  Restart and Recovery, Search, Power Awareness and other Shell integrations. However the current version doesn’t include any Windows 7 features. It will support key Windows 7 APIs in the near future, but until then, we have developed an intermediate set of solutions for supporting managed code developers who wish to target Windows 7 Beta today and not wait for the time the Windows API Code Pack for the .NET Library (the new name of Windows Vista Bridge) will be available.

With that caveat in place, we can go ahead and introduce some new managed code wrappers that will allow managed code developers to use the Windows 7 Taskbar, manipulate Libraries, add Multi-Touch support for WinForms or WPF 3.5 SP1, and enable Sensors and Location in their applications.

Taskbar

The Taskbar Sample .NET Interop Library allows developers to:

  • Create and manipulate JumpLists including tasks and items
  • Display Dynamic Overlay Icons, Thumbnail Toolbars
  • Use the Taskbar progress bar
  • Control Custom Thumbnail Preview, and custom Preview also known as – AeroPeek

The Windows 7 Taskbar Sample .NET Interop Library is available for download and include 4 demos showcasing all the Taskbar features.

Libraries

Libraries are new in Windows 7 and provide a logical representation of the user’s data on  his local computer and on remote computers. With Libraries, the user can define which physical folders are mapped to which library and achieve better search quality and easier “maintenance” of his content. In Windows 7, it is important for developers to

enable their applications to become Library-aware by supporting Libraries. This will integrate the user’s application and Windows experiences and maintain the integrity of your applications in various scenarios

The Windows 7 Library Sample .NET Interop Library allows developers to:

  • Manipulate and control the libraries in Windows 7 including create a new library or delete an existing one
  • Add or remove physical locations to a Library
  • Set an icon for each library
  • Enumerate the contents of a given library’s physical location (the actual folders) to track down and map all the items in that library

Read more at the new Windows 7 Blog for Developers

Source- Tech Today

DirectX 9.26.1590 (March 2009), End-User Runtime and Developers SDK

The March 2009 DirectX SDK download contains the tools needed to build cutting-edge, media-rich, interactive applications. It includes runtimes, headers and libraries, samples, documentation, utilities, and support for C++ development.

The March SDK also includes an update to the Technical Preview for Direct3D 11, the latest version of Direct3D that provides support for tessellation and general purpose GPU processing, as well as the Technical Preview for Direct2D and DirectWrite, the new Windows APIs for accelerated 2D rendering and font and text rendering. Further changes in the March SDK include improvements to the audio tools and the new XNA Math library.

Source- Tech Today

Download SDK for Windows 7 and .NET Framework 3.5 SP1

The Windows SDK for Windows 7 and .NET Framework 3.5 SP1: BETA provides the documentation, samples, header files, libraries, and tools (including C++ compilers) that you need to develop applications to run on Windows 7 BETA and the .NET Framework 3.5 SP1.

To build and run .NET Framework applications, you must have the corresponding version of the .NET Framework installed. This SDK is compatible with Visual Studio® 2008, including Visual Studio Express Editions, which are available free of charge.

Download now…
http://www.microsoft.com/downloads/details.aspx?FamilyID=a91dc12a-fc94-4027-b67e-46bab7c5226c&DisplayLang=en