Installing the Haskell PortAudio package on Windows

Version 1.1 written December 6, 2009
Anyone may copy and host this content without attribution.
This tutorial was compiled from help provided by the Haskell-Cafe mailing list December 4th and 5th.

download (.zip 102 kb)
This download will provide you with precompiled binaries and files that may be necessary to complete this tutorial.

These are links to resources you *might* need. Versions used in this tutorial are in ( ).

Content

Introduction
Part 1: Compile PortAudio
Part 2: Prepare the binaries for Haskell
Part 3: Install the Haskell PortAudio package
Part 4: Compiling and running Haskell apps using PortAudio

Introduction

This tutorial details how to install Haskell's PortAudio library on Windows.
Most people should just download the binaries provided above and do PART 3 + 4 ONLY!
If you wish to compile the binaries yourself, do PART 1 and 2 as well.


NOTE: This tutorial assumes the working directory is "C:\PortAudio".
This was not chosen arbitrarily - a path with spaces in it might not work!

NOTE: If you don't have it, you will need c2hs. Install it with "cabal install c2hs".
Make sure to add it to the system path. It is ordinarily in "C:\Program Files\Haskell\bin".

PART 1: Compile PortAudio

[1] Download and unpack the PortAudio source code.

The latest version is provided as a ".tar.gz" file instead of a more familiar ".zip" file.
7-Zip will unpack this file (WinZip might also?). Run "extract here" twice on the ".tar.gz"
file until it extracts a "portaudio" folder.

[2] Move the "portaudio" folder extracted from the download into the working directory "C:/PortAudio".

Its path should now be "C:\PortAudio\portaudio".
Note that in the folder "C:\PortAudio\portaudio\build\msvc" there is a file named "readme.txt".
It contains instructions for some of what follows.

[3] Download and unpack the ASIO SDK.

NOTE: Steinberg requires that you register to download the ASIO SDK.
There should now be a folder "ASIOSDK2" containing the folders "common" and
"host" (as well as other folders and files).

[4] Move the "ASIOSDK2" folder into "C:\PortAudio\portaudio\src\hostapi\asio".
[5] Rename "C:\PortAudio\portaudio\src\hostapi\asio\ASIOSDK2" to "C:\PortAudio\portaudio\src\hostapi\asio\ASIOSDK".

[6] Obtain "dsound.h" and copy it into "C:\PortAudio\directx".

The PortAudio project needs a single header file from the DirectX SDK.
NOTE: The DirectX SDK is 1.5 GB!
Instead of installing the SDK you may obtain "dsound.h" from the download above.
If you do install the SDK, the file is in "C:\Program Files\Microsoft DirectX SDK (August 2009)\Include".

[7] Double-click on "C:\PortAudio\portaudio\build\msvc\portaudio.dsp"
[8] Update the project.

When Visual C++ Express launches it should prompt you to update the project. Hit "Next" or "Finish" to update.

[9] Add "C:\PortAudio\directx" to the include directories.

In the menubar go to "Project > Properties".
Under "Configuration Properties" click on "C\C++".
Click on "Additional Include Directories" and add "C:\PortAudio\directx".

[10] Change the .lib output filename to "libportaudio.lib"

Go to "Project > Properties"
Then "Configuration Properties > Linker > Advanced"
Change the "Import Library" field so that it ends with "libportaudio.lib" instead of "portaudio_x86.lib".

[11] Change the .dll output filename to "libportaudio.dll"

Go to "Project > Properties"
Then "Configuration Properties > Linker > General"
Change the "Output File" field so that it ends with "libportaudio.dll" instead of "portaudio_x86.dll".

[12] Change the project type from "debug" to "release".

In the toolbar, change the dropdown menu from "debug" to "release".

[13] Build the project.

In the menubar go to "Build > Solution".
It should build successfully with output similar to:


portaudio - 0 error(s), 3 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========




PART 2: Prepare the binaries for Haskell

[1] Create a "bin" folder in "C:\PortAudio".
[2] Copy files.

Copy "libportaudio.lib" and "libportaudio.dll" from
"C:\PortAudio\portaudio\build\msvc\Win32\Release" into "C:\PortAudio\bin".

[3] Obtain "pexports.exe" and move it to "C:\PortAudio\pexports".

It is available in the download above.

NOTE: If you have MinGW installed and in your path, you may skip the rest of PART 2 by creating
and executing a .bat file with the commands:


cd c:\portaudio\bin
"..\pexports\pexports.exe" libportaudio.dll > libportaudio.def
dlltool --input-def libportaudio.def --output-lib libportaudio.a
pause


[4] Create "libportaudio.def".

Open a command window (start > run > "cmd") and type: cd c:\portaudio\bin (use a backslash!)
Type the command: "..\pexports\pexports.exe" libportaudio.dll > libportaudio.def

The command line should look like this:
C:\PortAudio\bin>"..\pexports\pexports.exe" libportaudio.dll > libportaudio.def

"C:\PortAudio\bin" will now contain "libportaudio.def".

[5] Create "libportaudio.a".

You will need MinGW.
Add "C:\MinGW\bin" to the system path (because "C:\MinGW\bin\dlltool.exe" and "C:\MinGW\bin\cpp.exe" are required).
Start > Control Panel > System > Advanced > Environment Variables > System Variables > Path
Hit "edit" and append ";C:\MinGW\bin". Note the ";" and NO SPACES! Hit "OK" all the way through.

At the command line enter the command: cd c:\portaudio\bin
Then enter the command: dlltool --input-def libportaudio.def --output-lib libportaudio.a

The command line should look like this:
C:\PortAudio\bin>dlltool --input-def libportaudio.def --output-lib libportaudio.a



PART 3: Install the Haskell PortAudio package

NOTE: Most people reading this tutorial will ONLY have to do this part.

If you did not compile the binaries yourself, they are provided in the download above.
Unzip the download and move the root "PortAudio" folder into the "C:" drive.

[1] Install

Open a command window.
Then type the command: cabal install portaudio --extra-include-dirs="C:\PortAudio\include" --extra-lib-dirs="C:\PortAudio\bin"

The command window should look like:


C:\>cabal install portaudio --extra-include-dirs="C:\PortAudio\include" --extra-lib-dirs="C:\PortAudio\bin"
Resolving dependencies...
Configuring portaudio-0.0.1...
Preprocessing library portaudio-0.0.1...
Building portaudio-0.0.1...
[1 of 4] Compiling C2HS             ( C2HS.hs, dist\build\C2HS.o )
[2 of 4] Compiling Sound.PortAudio.Helpers ( Sound\PortAudio\Helpers.hs, dist\build\Sound\PortAudio\Helpers.o )
[3 of 4] Compiling Sound.PortAudio.Base ( dist\build\Sound\PortAudio\Base.hs, dist\build\Sound\PortAudio\Base.o )
[4 of 4] Compiling Sound.PortAudio  ( Sound\PortAudio.hs, dist\build\Sound\PortAudio.o )
C:\Program Files\Haskell Platform\2009.2.0.2\bin\ar.exe: creating dist\build\libHSportaudio-0.0.1.a
Installing library in C:\Program Files\Haskell\portaudio-0.0.1\ghc-6.10.4
Registering portaudio-0.0.1...
Reading package info from "dist\\installed-pkg-config" ... done.
Writing new package config file... done.


That's it, you're done! Note that it has installed into "C:\Program Files\Haskell\portaudio-0.0.1".
If you ever need to uninstall, use the command: ghc-pkg unregister portaudio and then delete that directory.
Though you are done, do not delete the contents of "C:\PortAudio\bin".
Though the package is now installed, you still need these files to run apps using PortAudio.

[2] Test it out!

Run the GHC interpretter with the command "ghci".
Type "import Sound.PortAudio".

The command window should look like:


C:\>ghci
GHCi, version 6.10.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
Prelude> import Sound.PortAudio
Prelude Sound.PortAudio>



PART 4: Compiling and running Haskell apps using PortAudio

[1] Create the folder "test" in "C:\PortAudio\haskell"
[2] Create the file "test.hs"

Open a text editor and type the 2 lines:

   import Sound.PortAudio
   main = do getHostApiCount

Save the file as "test.hs" in "C:\PortAudio\test"

[3] Compile

Open a command window and type the command: cd C:\PortAudio\haskell\test
Type the command: ghc -package portaudio test.hs

[4] Run

Notice that running the compiled program "main.exe" from the command line displays the error:
"This application has failed to start because libportaudio.dll was not found. Re-installing the application may fix this problem."

This is because the program requires the portaudio .dll file.
When deploying your applications, simply copy "libportaudio.dll" from "C:\PortAudio\bin"
into your application's folder.

[5] ghci

When running ghci, "C:\PortAudio\bin\libportaudio.dll" must be in the path for Sound.PortAudio function calls to work.

There are at least 3 ways to do this:
1. Put "C:\PortAudio\bin" in your System path (detailed above).
2. At the command line, run the ghci command from inside "C:\PortAudio\bin"
3. At the command line, append it to the path for your session with the command: path=%path%;C:\portaudio\bin

Test it out:


C:\>path=%path%;c:\portaudio\bin

C:\>ghci
GHCi, version 6.10.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
Prelude> import Sound.PortAudio
Prelude Sound.PortAudio> getVersion
...
Loading package portaudio-0.0.1 ... linking ... done.
1899
Prelude Sound.PortAudio>




The End.