Fast algorithm for generating white noise

I was looking for ways to generate white noise in a C program running on STM32F4 (this family of MCU-s has a RNG, but it’s way too slow to use for realtime audio).
Didn’t really find much on the subject. A lot of stuff on PRNG, but all the articles I found seemed to focus on the cryptographic use and the algorithms for that are bit too slow to use on a puny MCU (in my project I wanted to generate 24000 noise samples every second, so I needed an algorithm that would be as fast and simple as possible – the randomness is a nonissue in audio world).

The following snippet is based on code found here:
http://www.bobwheeler.com/statistics/Password/MarsagliaPost.txt

 uint32_t noise_m_z=362436069;
 uint32_t noise_m_w=521288629;

 uint32_t getNoiseSample(void) {
     noise_m_z=36969*(noise_m_z&65535)+(noise_m_z>>16);
     noise_m_w=18000*(noise_m_w&65535)+(noise_m_w>>16);
     return (noise_m_z<<16)+noise_m_w;
 }

I personally treat the resulting uint as Q31 type. This way it is a proper audio sample – a value between -1 and +1 and usable with DSP functions.

Calculating note frequencies

The formula for calculating a frequency of a note is something like this:

freq=440*(2^((nIdx-57)/12))

Where the nIdx variable is the index of a note starting from 0.
Here are the frequencies of the zeroth octave:

nIdx Note Freq Freq as int
0 C 16.3516 1071618
1 C# 17.3239 1135339
2 D 18.3540 1202847
3 D# 19.4454 1274373
4 E 20.6017 1350153
5 F 21.8268 1430441
6 F# 23.1247 1515500
7 G 24.4997 1605612
8 G# 25.9565 1701085
9 A 27.5000 1802240
10 A# 29.1352 1909404
11 B 30.8677 2022945

If like me you prefer not to deal with floats and doubles, then you can multiply these numbers by a large constant… 65536 is a popular choiche for instance.

To save memory (my MCU for instance has only 4KB RAM) you can just precalculate the first (zeroth) octave frequencies and derive the frequencies of other octaves with a formula like:

freq=(1<<octave)*freqLookup[midiNoteID%12]

Where octave=midiNoteID/12 and midiNoteID is one from the following table…

 Octave C C# D D# E F F# G G# A A# B
0 0 1 2 3 4 5 6 7 8 9 10 11
1 12 13 14 15 16 17 18 19 20 21 22 23
2 24 25 26 27 28 29 30 31 32 33 34 35
3 36 37 38 39 40 41 42 43 44 45 46 47
4 48 49 50 51 52 53 54 55 56 57 58 59
5 60 61 62 63 64 65 66 67 68 69 70 71
6 72 73 74 75 76 77 78 79 80 81 82 83
7 84 85 86 87 88 89 90 91 92 93 94 95
8 96 97 98 99 100 101 102 103 104 105 106 107
9 108 109 110 111 112 113 114 115 116 117 118 119
10 120 121 122 123 124 125 126 127

Thats really it.

Calling FMS admin API methods with PHP

There is a rather simple way of getting status info from a running Flash Media Server. This can be used to draw graphs with Cacti for instance.

First make sure the admin API over HTTP is enabled in /opt/adobe/fms/conf/fms.ini

USERS.HTTPCOMMAND_ALLOW = true

Then enable the methods you need or use a keyword “All” in /opt/adobe/fms/conf/Users.xml

<AdminServer>
    <HTTPCommands>
        ...
        <Allow>All</Allow>
        <Deny></Deny>
        <Order>Deny,Allow</Order>
    </HTTPCommands>
</AdminServer>

Here is a working example of how to get the number of active connections with PHP (you need php-XML module to run this).

<?php
$adminUser='admin';
$adminPassw='SuperSecretPassword';

$xml=new DomDocument();
$url="http://localhost:1111/admin/getServerStats?auser=$adminUser&apswd=$adminPassw";
$xml->load($url);
$connected=getTagContents('connected',$xml);

printf("There are currently %d active connections on the server\n",$connected);

function getTagContents($tagName,$dom) {
    $node=$dom->getElementsByTagName($tagName)->item(0);
    return $node->nodeValue;
}
?>

In the example above I just extract a single value out of a returned XML that looks something like this:

<result>
    <level>status</level>
    <code>NetConnection.Call.Success</code>
    <timestamp>Thu 28 Apr 2011 11:59:24 AM EEST</timestamp>
    <data>
        <launchTime>Tue 26 Apr 2011 07:29:32 PM EEST</launchTime>
        <uptime>145792</uptime>
        <cpus>2</cpus>
        <cpu_Usage>0</cpu_Usage>
        <num_cores>1</num_cores>
        <memory_Usage>4</memory_Usage>
        <physical_Mem>98799616</physical_Mem>
        <io>
            <msg_in>1008000</msg_in>
            <msg_out>190805</msg_out>
            <msg_dropped>0</msg_dropped>
            <bytes_in>423676178</bytes_in>
            <bytes_out>3904886327</bytes_out>
            <reads>434063</reads>
            <writes>168869</writes>
            <bw_in>0</bw_in>
            <bw_out>0</bw_out>
            <total_connects>72</total_connects>
            <total_disconnects>69</total_disconnects>
            <connected>3</connected>
            <rtmp_connects>4</rtmp_connects>
            <rtmfp_connects>0</rtmfp_connects>
            <normal_connects>0</normal_connects>
            <virtual_connects>1</virtual_connects>
            <group_connects>3</group_connects>
            <service_connects>0</service_connects>
            <service_requests>0</service_requests>
            <admin_connects>0</admin_connects>
            <debug_connects>0</debug_connects>
            <total_threads>168</total_threads>
            <working_threads>2</working_threads>
            <swf_verification_attempts>0</swf_verification_attempts>
            <swf_verification_exceptions>0</swf_verification_exceptions>
            <swf_verification_failures>0</swf_verification_failures>
            <swf_verification_unsupported_rejects>0</swf_verification_unsupported_rejects>
            <swf_verification_matches>0</swf_verification_matches>
            <swf_verification_remote_misses>0</swf_verification_remote_misses>
            <server_bytes_in>0</server_bytes_in>
            <server_bytes_out>0</server_bytes_out>
            <rtmfp_lookups>0</rtmfp_lookups>
            <rtmfp_remote_lookups>0</rtmfp_remote_lookups>
            <rtmfp_remote_lookup_requests>0</rtmfp_remote_lookup_requests>
            <rtmfp_redirects>0</rtmfp_redirects>
            <rtmfp_remote_redirects>0</rtmfp_remote_redirects>
            <rtmfp_remote_redirect_requests>0</rtmfp_remote_redirect_requests>
            <rtmfp_forwards>0</rtmfp_forwards>
            <rtmfp_remote_forwards>0</rtmfp_remote_forwards>
            <rtmfp_remote_forward_requests>0</rtmfp_remote_forward_requests>
        </io>
    </data>
</result>

If you decide to pull status info from remote servers then keep in mind that it is not the best idea to make the admin API port world-accessible. Also be aware that the password is passed in plain text on the URL!

A better alternative might be to limit admin API calls to localhost and pass the needed values to your monitoring server over SNMP.

Compiling Strobe Media Playback using command line

The developers of Strobe Media Playback are rather vague about how to compile the source code. This article tries to list all the steps needed to get a working SWF on any modern OS.

Here is a list of things you will need to make the magic happen:

  • Java JDK 6
  • Apache Ant
  • Adobe Flex SDK 4
  • FlexUnit 4
  • Ant Contrib

First make sure you have Java (JDK 6) installed on your OS.
Next get Apache Ant from http://ant.apache.org/bindownload.cgi (I used v1.8.2 for this article) or if you are running some OS with central software repositories (Linux, FreeBSD, etc), then install using the tools provided with it (yum, pkg_add, apt-get, etc).
When setting up Apache Ant on a Windows machine make sure you add the bin directory to PATH variable of the system.

Then go and download the Strobe Media Playback source from http://sourceforge.net/projects/smp.adobe/files/ (for this article I used v1.5.1). Uncompress the downloaded file and at the root of the uncompressed source directory create the following subdirectories:

./buildtools/
./buildtools/libs/
./buildtools/sdks/
./buildtools/sdks/4.1.0/

Now lets populate the ./buildtools folder with some… umm build tools obviously… duh…

Go and download Flex 4 SDK from http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4 (for this article I used v4.1.0.16076). Uncompress the files into the ./buildtools/sdks/4.1.0/ subdirectory.

Now we’ll need something called ANT Contrib. Version 1.0b3 to be precise. Uncompress the archive and move the ant-contrib-1.0b3.jar file to ./buildtools/libs/

Finally something called FlexUnit… from the download page choose the stable release FlexUnit (4.0 SDK). Uncompress the the archive and move flexUnitTasks-4.0.0.jar to ./buildtools/libs/

If using Windows, then now you can execute the “src\build.bat compile.strobe”.

For unix-like OS-es you need to create a shell script ./src/compile.strobe, like:

#!/bin/bash
BUILD_TOOLS=”..\buildtools”
/usr/bin/ant \
-lib ${BUILD_TOOLS}\sdks\4.1.0\lib \
-lib ${BUILD_TOOLS}\sdks\4.1.0\ant\lib \
-lib ${BUILD_TOOLS}\libs \
compile.strobe

If you don’t have bash, then just tweak the script a bit to match the conventions of your favourite shell.

If you look inside ./src/build.xml, you’ll notice that there are many other build targets besides “compile.strobe”. Haven’t yet worked out how to make them all work… will post an update when I do.

Debug build for Flash Player 10.1