Saturday, June 19, 2010

How to play raw PCM audio data in .NET using C#?

There is a Windows API for that. You can use the .NET Wrapper in RageLib. RageLib can be found in http://code.google.com/p/gtaivtools/source/browse/#svn/trunk/RageLib

class Program
{
    static byte[] _queue = new byte[0];
    static void Filler(IntPtr data, int size)
    {
        var length = Math.Min(size, _queue.Length);
        Marshal.Copy(_queue, 0, data, length);
        _queue = _queue.Skip(size).ToArray();
    }
    static void Main(string[] args)
    {
        var encoded = args[0];
        var decoded = Convert.FromBase64String(encoded);
        var samplingFrequency = 8000;
        var bits = 8;
        var channels = 1;
        var bufferSize = 256;
        var waveFormat = new WaveFormat(samplingFrequency, bits, channels);
        using (var waveOutPlayer = new WaveOutPlayer(-1, waveFormat, bufferSize, 1, Filler))
            while (_queue.Length > 0)
            {
            }
    }
}

How to play raw PCM audio data in Matlab?

function record()
    Fs = 8000;
    nBits = 16;
    nChannels = 1;
    recorder = audiorecorder(Fs, nBits, nChannels);
    disp('Start speaking for 2 seconds');
    recordblocking(recorder, 2);
    disp('End of Recording.');
    audiodata = getaudiodata(recorder, 'uint8');
    player = audioplayer(audiodata, Fs);
    playblocking(player);

Sunday, June 6, 2010

How to prevent Serial-to-USB devices from messing with your mouse on Windows

Since I am using one of my USB ports to receive RS232 data, it began to scramble my mouse pointer. It move randomly across the screen. It happens because Windows recognizes it as a serial mouse.

The solution for this is simple. Your USB device will be listed twice in the Windows Device Manager. All you have to do is disable the mouse entry.

Friday, June 4, 2010

Examples of Memcached telnet commands

I was looking for a example of a memcached telnet session, here is what I created. It shows the behavior of the set, get, add, gets, cas and replace commands.

I introduced the '>' sign before each input line to differentiate it from the output.

>add mykey 0 60 11
>hello world
STORED

>get mykey
hello world
END

>add mykey 0 60 11
>hello world
NOT_STORED

>replace mykey 0 60 7
>hello w
STORED

>get mykey
hello w
STORED

>replace newkey 0 60 7
>hello w
NOT_STORED

>get newkey
END

>set mykey 0 60 5
>hello
STORED

>set newkey 0 60 5
>hello
STORED

>gets newkey
VALUE newkey 0 5 999999
hello
END

>cas newkey 0 60 4 111111
>hola
EXISTS

>cas newkey 0 60 4 999999
>hola
STORED

Tuesday, June 1, 2010

How to send Memcached stats to a monitoring tool using bash?

Zabbix is my network monitoring tool of choice. You can send Memcached stats to it using the simple code bash script below:


#!/bin/bash
ZABBIX_SENDER="/usr/local/sbin/zabbix_sender"
ZABBIX_TRAPPER="localhost"
ZABBIX_TRAPPER_PORT=10051
ZABBIX_ITEM_KEY_PREFIX="memcached_"
ZABBIX_HOST="Memcached"
MEMCACHED_SERVER="localhost"
MEMCACHED_SERVER_PORT=11211
STATS=`(sleep 1 ; echo "stats"; sleep 1; echo "quit") | telnet $MEMCACHED_SERVER $MEMCACHED_SERVER_PORT`
STAT //' | sed -e 's/ /:/g' | sed -e 's/:STAT:/\n/g')
do
OLD_IFS="$IFS"
IFS=":"
KEY_VALUE=($i)
IFS="$OLD_IFS"
ITEM_KEY=$ZABBIX_ITEM_KEY_PREFIX${KEY_VALUE[0]}
echo $ITEM_KEY:${KEY_VALUE[1]}
$ZABBIX_SENDER --zabbix-server $ZABBIX_TRAPPER --port $ZABBIX_TRAPPER_PORT --host $ZABBIX_HOST --key $ITEM_KEY --value ${KEY_VALUE[1]}
done

Save it to a file and schedule it with CronD. You will also have to add the Memcached template to all your memcached hosts.

Never uninstall the Team Foundation Server component that comes with Visual Studio 2010

This component is essential to open solutions that were originally binded to a TFS server (ie. Codeplex projects)