Thursday, August 17, 2006

 

C# - Recording audio to a Wave file using DirectSound

In my work recently I used Microsoft's DirectShow API to create a video frame grabber from any Windows video input. in C++, DirectShow is a bit of a pain, but with the Managed wrapper classes from http://directshownet.sourceforge.net/, it's actually very pleasant to work with, and I've rather got the hang of it. So I was a little disappointed to find out it's not the best method of messing about with audio files - you can do it, if you find and install the WavDest filter, but naturally this hasn't been wrapped by the DirectShowNet chaps yet, and I couldn't be arsed to wrap it up myself. Anyway, it turns out that the recommended way of saving audio is to use another MS API, DirectSound.

All this meant that I was going to have to learn another API, doh, still I guess it keeps my brains working.

Anyway to get DirectSound you have to download the DirectX SDK, which fortunately contains a sample for capturing audio from any audio device recognised by Windows, to a file. Unfortunately, it's pretty complex
(grumble it was bloody simple in DirectShow grumble, if only I had the filter grumble). There some more info on what the sample is doing here, right at the bottom.

I'll be honest, I started going through the sample line by line and copying the relevant bits but, in the end, I realised I couldn't be arsed. So I ripped the GUI out, and with a bit of tweaking I was left with a fairly functional wave file recorder object. The only real thinking required is to hard-code which audio input device is required (just a matter of setting an int), and also code the details of DirectShow WaveFormat structure to 'default' wave information. This replaces the two 'intro' forms in the sample and lets you use the code as an object in your own program.

Why do I need to record wave files, I hear you cry? (tumbleweed...) Watch this space for information of my fully fledged chat bot - that actually lets you chat, rather than type!

Thursday, August 03, 2006

 

C# - Formatting text in a RichTextBox by parsing the Rich Text Format (RTF)

I've written a short article on this subject for The Code Project. Read it here!

This page is powered by Blogger. Isn't yours?