Phillip Trelford's Array

POKE 36879,255

Next Generation Video Gaming

There’s a fair amount of excitement with the recent announcements of the new XBox One and PS4 consoles with exciting new harder specs and TV features. There has never been a better time to own an PS3 or XBox 360. On the “old” consoles there’s a massive back catalogue of quality games to choose from, many at knock down prices or available pre-owned. If you want a console for TV the PS3 has a blu-ray player, an optional remote control and supports services like NetFlix and LoveFilm, it’s also quite quiet. If you’re interested in gaming, developers really know these “old” consoles now and there’s a healthy indie games scene too.

If you’re a game developer on royalties you may want to think twice about jumping straight onto the latest and greatest consoles. Sure they’re new and shiny, but they’ll have a much smaller user base for quite some time yet, likely require a much larger team and longer to develop. This winter holiday season XBox 360 and PS3 will be at their peak. I remember being on a royalty share at Ocean on Jurrassic Park, I worked on the PC and Amiga versions, which were a huge success. But the big royalties came from the GameBoy version which had a team of 2. I guess this might be one of the reasons you see so many band members pursuing solo careers.

Further reading: The benefits of developing for PS3, not PS4

Same Same

After a while websites start to look the same:

every-damn-website Af

And most websites use the same server-side programming language:

Server side programming languages

And the other programming languages look the same.

PHP

PHP supports classes these days:

class Person {
 public $firstName;
 public $lastName;
 
 public function __construct($firstName, $lastName = '') { //Optional param
  $this->firstName = $firstName;
  $this->lastName = $lastName;
 }
 
 public function greet() {
  return "Hello, I’m " . $this->firstName . " " . $this->lastName . ".";
 }
 
 public static function staticGreet($firstName, $lastName) {
  return "Hello, I’m " . $firstName . " " . $lastName . ".";
 }
}
 
$he = new Person('John', 'Smith');
$she = new Person('Sally', 'Davis');
$other = new Person('iAmine');

C#

At a cursory glance besides the dollars C# classes are the same:

class Person
{
    // Field 
    public string name;

    // Constructor that takes no arguments. 
    public Person()
    {
        name = "unknown";
    }

    // Constructor that takes one argument. 
    public Person(string nm)
    {
        name = nm;
    }

    // Method 
    public void SetName(string newName)
    {
        name = newName;
    }
}
class TestPerson
{
    static void Main()
    {
        // Call the constructor that has no parameters.
        Person person1 = new Person();
        Console.WriteLine(person1.name);

        person1.SetName("John Smith");
        Console.WriteLine(person1.name);

        // Call the constructor that has one parameter.
        Person person2 = new Person("Sarah Jones");
        Console.WriteLine(person2.name);

        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}

The differences between Microsoft’s C# and Sun’s Oracle’s Java are even smaller.

TypeScript

Microsoft’s TypeScript is yet another language to JavaScript compiler, this time from Anders Hejlsberg inventor of Java C#.

class Person {
    private name: string;
    private age: number;
 
    constructor(name: string, age: number) {
        this.name = name;
        this.age = age;
    }
 
    toString(): string {
        return this.name + " (" + this.age + ")";
    }
}

 

TypeScript code looks like C# and PHP.

ActionScript

TypeScript also looks a lot like ActionScript 3:

package com.example
{
    import flash.text.TextField;
    import flash.display.Sprite;
 
    public class Greeter extends Sprite
    {
        public function Greeter()
        {
            var txtHello:TextField = new TextField();
            txtHello.text = "Hello World";
            addChild(txtHello);
        }
    }
}

Class declarations and type annotations in TypeScript and ActionScript look almost identical. ActionScript can be compiled to JavaScript or native code for iOS and Android using PlayScript.

Language wars

Looks like web developers using PHP, ASP.Net or Flash have quite a lot in common.

Monokeys

Press a key to hear a note:


GAFBB should sound familiar to fans of 70s sci-fi.

I wanted to programmatically generate sound effects for a retro game I’m working on. Generating wave files using F# turned out to be less than 30 lines of code:

open System.IO

/// Write WAVE PCM soundfile (8KHz Mono 8-bit)
let write stream (data:byte[]) =
    use writer = new BinaryWriter(stream)
    // RIFF
    writer.Write("RIFF"B)
    let size = 36 + data.Length in writer.Write(size)
    writer.Write("WAVE"B)
    // fmt
    writer.Write("fmt "B)
    let headerSize = 16 in writer.Write(headerSize)
    let pcmFormat = 1s in writer.Write(pcmFormat)
    let mono = 1s in writer.Write(mono)
    let sampleRate = 8000 in writer.Write(sampleRate)
    let byteRate = sampleRate in writer.Write(byteRate)
    let blockAlign = 1s in writer.Write(blockAlign)
    let bitsPerSample = 8s in writer.Write(bitsPerSample)
    // data
    writer.Write("data"B)
    writer.Write(data.Length)
    writer.Write(data)

let sample x = (x + 1.)/2. * 255. |> byte 

let data = Array.init 16000 (fun i -> sin (float i/float 8) |> sample)
let stream = File.Create(@"C:\tone.wav")
write stream data

F#’s ASCII string support and light syntax, combined with .Net’s convenient BinaryWriter class made this ridiculously easy.

Then just for fun I created the simple keyboard in 70 lines of code:

namespace MonoKeys

open System
open System.Windows
open System.Windows.Controls
open Microsoft.Xna.Framework.Audio

type App() as this = 
    inherit Application()

    let sampleRate = 8000

    let sample x = x * 32767. |> int16

    let toBytes (xs:int16[]) =
        let bytes = Array.CreateInstance(typeof<byte>, 2 * xs.Length)
        Buffer.BlockCopy(xs, 0, bytes, 0, 2 * xs.Length)
        bytes :?> byte[]

    let create freq =
        let samples = 12000
        let sine i = sin (Math.PI * 2. * float i / float sampleRate * freq)
        let fadeOut i = float (samples-i) / float samples
        Array.init samples (fun i -> sine i * fadeOut i |> sample)
        |> toBytes

    let play freq =        
        let bytes = create freq
        let effect = new SoundEffect(bytes, sampleRate, AudioChannels.Mono)
        effect.Play() |> ignore
    
    let notes = [
        "A", 220.00
        "A#", 233.08
        "B", 246.94
        "C", 261.63
        "C#", 277.18
        "D", 293.66
        "D#", 311.13
        "E", 329.63
        "F", 349.23
        "F#", 369.99
        "G", 392.00
        "G#", 415.30
        "A", 440.00
        "A#", 466.16
        "B", 493.88
        "C", 523.25
        "C#", 554.37
        "D", 587.33]

    let keys =
        notes |> Seq.map (fun (text,freq) ->
            let key = Button(Content=text)
            key.Click.Add(fun _ -> play freq)
            key
        )

    let grid = Grid()
    do  keys |> Seq.iteri (fun i key ->
            grid.ColumnDefinitions.Add(ColumnDefinition())
            Grid.SetColumn(key,i)
            grid.Children.Add key
        )

    do  this.Startup.AddHandler(fun o e -> this.RootVisual <- grid)


This time Silverlight 5’s SoundEffect class makes generating sounds easy accepting an array of bytes. I simply copied the notes from the web, formatted the text as tuples and mapped them to buttons that showed the text and played a note of the specified frequency. The keys were then laid out on a grid. Look no XAML ;)

Next I’m planning to add some more features to generate sounds like a synthesizer maybe one day like my monotribe

If you’re interested in programming and sound I’d recommend taking a look at Sam Aaron’s Overtone for Clojure and Rob Pickering’s Undertone for F#.

Rob will be running a session on Undertone at this year’s Progressive F# Tutorials in London:

progressivefsharplondon2013