Second Stanza

November 12, 2008

Windows WorkFlow Activity Data

Filed under: .NET Development, WorkFlow — Tags: — dfbaskin @ 12:54 am

One of the first questions I had about Windows Workflow (in .NET 3.0) was how data was moved around between workflow activities. Activities, of course, are the meat of a workflow process in that they define the actual work a workflow will address. So if one activity picks up a useful data value that some other activity will later need, what technique should be used to pass this data between these activities?

Windows Workflow Foundation provides several features that aid in accomplishing this task:

  • Activity Properties – each activity may publish it’s own bindable set of properties that can be used as input and output parameters for the activity.
  • Workflow Properties – the workflow itself can publish it’s own set of properties.
  • Declarative Binding – using dependency properties, properties from one activity can be automatically bound to properties on other activities.
  • Workflow Navigation – From within an activity, you can navigate the hierarchy of a workflow process in order to find specific instances of other activities that may have information you need.

These features give you multiple options for moving activity data around within a workflow. However, you must be careful in your implementation since, for example, workflows allow parallel processing and you must be sure to get to the activity instance that you really want. MSDN published a very useful article about these and other related issues here.

To me, the best option is to use declarative binding, for two reasons. The first reason is that declarative binding eliminates any ambiguity about which activity instance you are binding to.

The second reason is to abstract your activity objects from your workflow. When navigating the hierarchy of the workflow, you are making assumptions about how the workflow is structured. Using bindings on properties allows the activity to be more stand-alone and more likely to be reusable. And of course, it allows easier unit testing of the activity.

Advertisement

November 2, 2008

Powershell Script for Shrinking MP3 Files

Filed under: Powershell — Tags: — dfbaskin @ 10:45 pm

I needed to shrink a bunch of lectures in MP3 format so that they would fit on a DVD. I compiled SoX to include MP3 support and then used the following Powershell script to compress the files:


$srcPaths = 'E:\MP3s\Year One', 'E:\MP3s\Year Two';
$dstPath = 'E:\MP3s\DVD';
$pathPrefixLen = 8;

Write-Host;
Write-Host '--------------------------------------------------------------------';
Write-Host 'MP3 Audio Update Utility';
Write-Host '--------------------------------------------------------------------';

foreach( $srcPath in $srcPaths )
{
    Write-Host 'Source: ' $srcPath;
}
Write-Host '  Dest: ' $dstPath;
Write-Host;

foreach( $srcPath in $srcPaths )
{
    Write-Host 'Reading ' $srcPath;
    $fileList = Get-ChildItem -path $srcPath -Recurse *.mp3

    foreach( $srcFile in $fileList )
    {
        $srcFullName = $srcFile.FullName;
        $dstFullName = [System.IO.Path]::Combine( $dstPath, $srcFile.FullName.Substring( $pathPrefixLen ) );
        $tmpFullName = [Regex]::Replace( $dstFullName, '\.mp3$', '.__temp__.mp3', [System.Text.RegularExpressions.RegexOptions]::IgnoreCase );

        if( ! [System.IO.File]::Exists( $dstFullName ) )
        {
            Write-Host $dstFullName;
    
            & 'sox.exe' $srcFullName '-r' '12k' '-c' '1' '-S' $tmpFullName 'dither' | out-null
            if( $lastExitCode -ne 0 )
            {
                Write-Host 'Stopped on error.'
                exit;
            }
    
            [System.IO.File]::Move( $tmpFullName, $dstFullName );
        }
    }
}

A file included in the SoX source code (‘INSTALL’) has instructions for building SoX with MP3 support under Windows. Here are the requirements (excerpted from this file):

Appendix: How To Compile SoX with MP3 Support on Win32
------------------------------------------------------

The following text, untested by the SoX team, was originally written by `Enter
AG' and released to the public domain.

= How To Compile Sox with MP3 Support =

== Requirements ==

The following requirements have been tested. Nevertheless, other versions may
work as well.

o Microsoft Windows (Win32, Win2k, Win2003).

o CMake (Cross Platform Make) version 2.4
  available on [http://www.cmake.org] 

o Microsoft Visual Stuio 2008 (also earlier Versions)

o SoX source distribution version 14.0.1
  available on [http://sox.sourceforge.net] 

o LAME source distribution version 3.97
  available on [http://lame.sourceforge.net] 

o MAD source distribution version 0.15.1b
  available on [http://www.underbit.com/products/mad/] 

Blog at WordPress.com.