Friday 8 August 2014

The League of Clandestine Guitarists

The options for clandestine guitarists today are endless. I'm talking about that large group of people who pick up the guitar once a year, people who strummed away in their bedrooms as kids and still cling on to a distant memory of how much fun it was. They are the people who refuse to play anything in public because a) they can't remember a single tune and b) are transfixed with fear if asked.

I understand - I'm one of them!

And then one day I passed a guitar shop, stared longingly through the window for a while and decided to revisit guitar-land.

If you're thinking "mmmm that could be me, but how embarrassing...", then I'd encourage you to put your embarrassment to one side and think again - the options are accessible, easy, fun and relatively cheap. This blog post (and some to follow) record my trail of guitar devastation back into guitar-land as a clandestine guitarist.

Note: I'm not a musician as will become clear shortly. I'm a tinkerer and fiddler, I've learnt possibly two pieces of music in my entire life but love playing with guitars, keyboards and computers. So I can pretty much guarantee that you'll not be intimidated by my playing but hopefully inspired by the possibilities!


Before I start, here's an example I "created" and published on the web within a couple of days of starting my exploration:



Forget the guitar playing for a moment (yes I know), what's important is that it's just me, a guitar and an iPhone having a huge amount of fun without annoying people. The "tune" is made up as I go along - it's unlikely that I could ever play it again. This is in the spirit of the clandestine guitarist and should be considered a virtue.

What you need to do...

Guitar




You'll need a guitar. No surprise there. I have an accoustic which I rarely use - partly because other people can hear me and partly because the action is to too high. I decided to go electric. With headphones this provides a way to make as many mistakes as I like without my family throwing things at me. It also provides a much easier action and can be recorded easily. Not to mention the large selection of interesting effects now easily available.
I chose a Fender Squier Strat as they're very cheap considering what you get, freely available in most music shops and... I always wanted a Fender Strat. It was up there on my Armchair Bucket List but I really couldn't justify the cost of a "proper Strat", so I opted for Fender's lower end range of guitars and have been very happy with the result. As an clandestine guitarist, you really can't go to far wrong with one of these.


Amp






You'll need something to plug it in to. As I've mentioned, I picked an electric so I could play quietly not loudly. 
I bought an iRig with my guitar and downloaded AmpliTube Fender for my iPhone. Plug it in and off you go! Amazing.






Band

Next you'll need something to play along with. As a clandestine guitarist you'll need a backing track not real musicians. Remember, you can't be seen playing your guitar in public.
Fortunately there are thousands of free backing tracks to choose from on the web. Simply search for "Jam backing tracks" or "Jam tracks" or "guitar backing tracks" and you'll see a whole range of sites. Many of these sites also provide TAB notation for the lead and rhythm.
The track I've used above is: http://www.jamtracks.ru/mpjam2/Blues/Texas_Blues_In_A.mp3
YouTube is also a great place to find backing tracks and tutorials,

Record

AmpliTube allows you to load backing tracks, change their speed for practising and when you're ready - you can record it as well. That's how the Oxtail Soup track (above) was recorded: A guitar, and iPhone and an iRig adaptor and red wine.

Publish

I've used SoundCloud, it's very easy to get going and free to upload quite a bit of content. You can make it publicly visible or you could keep it private if you like. Don't be worried about making it public - no one really cares about what you play and the chances of anyone stumbling across it are close to zero anyway.

Of course you don't have to publish. I wanted to have something to aim for so I could say "done!". But also, as I've mentioned, I tend to make all my "music" up on the spot. Sure I figure out what the scale is in the key that's being played, but apart from that, all the things I've ever played have long faded from memory. Some of the tunes I quite liked and wish I'd recorded them. Publishing on the web makes it real and reminds me of what some of the tunes were like - that's if I ever feel like playing them again.

No one in their right mind would take this stuff seriously - so there's no reason for you to.

So go buy a guitar, download some software and have fun!



Tuesday 5 August 2014

Music for Knob Twiddlers


Generative Music is fun, cheap and freely available. If you're a techie and interested in music you'd be mad not to play with this stuff. I'm not so I did.

As a kid, the Moog synthesiser was an object of desire - a cross between a church organ and a 1950s telephone exchange. What could be better?

I liked the sound, but if I'm honest, it was the technology that interested me the most.
So years later, my curiosity in electronics and music has resurfaced. Sadly my musical ability is still lacking and the keyboard is as elusive as ever.



Solution: Take a Generative Music (MIDI) app and a software synthesiser that accepts MIDI and away you go. In the video below I've used Nodal to create music in MIDI format and pump the resulting MIDI stream into a software synthesiser: MULAB.




Nodal & MU LABS


Nodal is a very simple, graph based music generator. Each note is represented by a node and the vertices represent the time between notes. Nodes may be connected to multiple nodes and you decide if the exit points are sequential, parallel, or random. You can build a number of separate graphs which output on separate MIDI channels. So you'll need a soft-synth that can handle multiple MIDI input channels. That excludes apps like Garage Band for example.

I've used MULAB as the synth as it can easily be configured to handle multiple MIDI input channels.

MULAB ships with a free soft-synth that supports a large number of synths and sound effects. It's easy to set up and use.
I've used sound flower to record the output and generate the video. Unfortunately I've used a relatively low resolution video - I'll change it next time.

Having rekindled my interest in music, I'm off on the next adventure:  Intermorphic's Noatikl. This is a much more complex environment than Nodal - more to follow.

If you've got a favourite Generative Music app then please let me know.



Saturday 2 August 2014

Reverse Engineering the Fretboard


Ever wondered how you'd work out the spacing of the frets on a guitar neck? No?

Well, I did and it led me through the maze of (western) music scales and on to an awful truth... there is no single universal harmonious musical scale!

First the Maths

I have no idea what the formal derivation of this is, but wanted to think it through to understand a bit more about the basic building blocks of music. That and, how would I make a guitar if I was stranded on a desert island?

So... the things I know as a starting point:
  • I know that a string with a frequency A (say) will have a frequency A*2 when the string length is halved. That's basic physics.
  • I also know that there are 12 frets between any given note and its double (octave above in music speak). That's twelve frets with the thirteenth being twice the frequency of where we started. Twelve is a western music convention. People have tried other combinations.
But there are many ways of dividing up a length of string into 12 parts. They could be equal, they could be a few percent closer for each fret etc. They could increase and decrease in distance apart. But we also know that the ratio between a string length and the next note up must be the same regardless of the string's length. To understand why this must be so, imagine a string that plays frequency A again. Now assume you play the next note up A' with a string length L'. Now detune the string so that L' now plays A again. To play A' on the L' string you have to move up to L''. Now we know that the frequency is relative to the length (physics again) and so L/L' = L'/L'' etc. So it doesn't matter how long the string is, to get to the next note you have to move up by a fixed ratio (R say) to the length of the string. The next note up on any string is a fixed ratio of  the length of the string regardless of its length. The 'scale' is length invariant as you might expect.


So we have:





That is, to work out how long the string is for the next note up, multiply its length by the inverse of the 12th root of 2. Nice and simple :)

Test Results

If you plug this formula into a spread-sheet and take the first note to be A=440 Hz (it's the standard) then you get the following:

LCalc HzEven tempered scaleMusical Note
0440.0440A
1466.2466.2Bb
2493.9493.9B
3523.3523.3C
4554.4554.4C#
5587.3587.3D
6622.3622.3Eb
7659.3659.3E
8698.5698.5F
9740.0740F#
10784.0784G
11830.6830.6G#
12880.0880A
R0.9438743127
12 root of 1/2

As you can see, the calculated values match those I've found on the internet. So they must be right.

More than one scale?

It turns out there is more than one scale in western music. In fact, people have created quite a number over the years. I've reverse engineered only one, the even tempered scale.

Why would you have more than one? Let's start with the scale we've created above. It turns out that the ratios of the notes to each other cannot be expressed exactly as simple fractions. Why does this matter? Because simple multiples of frequencies played together sound good - they beat together to form harmonious combinations. You could say that's a fundamental aspect of what we think of as music. Conversely, frequencies that are not simple multiples of each other often sound unpleasant when played together. 

It is possible to create a series of notes, similar to the ones above, but based on simple multiples of frequencies only. That is, they do not follow a pre-defined formula but rather they are a hand picked selection of notes that sound good together. Take a look at the just intonation.
The problem with this approach is that music played with these notes cannot be transposed (moved up and down) easily without breaking the simple ratios. But the even tempered scale does allow you to do this - it has that property but is a compromise between the harmonious benefit of simple ratios and the ability to play the same sequence of notes anywhere on the keyboard and sound much the same. Obviously higher or lower in frequency that is. 

So... as much as I'd like to think of the musical scale as something mathematically pure that's straight out of nature, the truth is that what's used in practice is often a compromise between creating harmonious combinations of notes, the physical implications of scale invariance for stringed instruments and man's desire to make the scale even, so that music can be played much the same in any key.

For an comprehensive overview of the history behind the various scales:

And for very good short description of the differences between the scales and their implications: 






Friday 1 August 2014

ChatBot 3 - Getting started with ChatScript

A random picture of Alan Turing
for inspiration
Having lost faith in AIML as a platform to capture life history, I've turned to ChatScript by Bruce Wilcox. Amazing.

Bruce explains why his approach (ChatScript) is an improvement on AIML based ChatBots in the Speaker for the Dead paper:

"AIML is based purely on pattern matching literal words. It compares scripted patterns against the input until it finds a match and then presents the prerecorded response. This is incredibly limiting, and AIML has excruciatingly over-precise pattern match capabilities. One can not easily author a lot of content, nor can it get anywhere close to finding the meaning of an input.

I've played with it for a while now and am very impressed!
There are a couple of major functional differences between ChatScript and AIML to note:

  • ChatScript is written in plain text and not XML. It's a computer scripting language and to my mind, much easier to write, test and debug than AIML.
  • It supports the notion of concepts. That is, you can test to see if a word fits the concept rather than simply a text match. It comes with built in set of concepts arranged in a taxonomy and you can simply add your own as well. 
  • The pattern matching is MUCH more flexible that AIML.
  • Groups of rules are arranged in topics. Topics can be selected through a list of keywords or concepts. ChatScript will then select rules in that context. For example, a concept such as my my_dog, may list keywords such as ( dog Sarah puppy ) as triggers to enter this topic.

But first, I'm going to list the basics of how to get started. This section is all covered in Bruce's documentation but is spread across a few documents. What follows is my "cheat sheet" for getting started.

How to get the source:

Go to the sourceforge project here:

Download and unzip the latest zip file, for example: ChatScript-4.5.zip

Running on a Mac?

If you're running on a Windows PC or Linux, then skip this section.

Sadly, if you're running on a Mac then you'll need to compile the code. It's not difficult - follow thee steps:

Download Xcode for Mac from here:

If you're not a Mac developer you'll have to register first.

Run Xcode and download the command line tools. You'll find this option in
Preferences>Downloads.

Browse to the base directory of your ChatScript files and in a command line window and run:
g++ -lpthread -funsigned-char src/*.cpp -O2 -oMacChatScript 2>err.txt

Then try and run the server as follows:
./MacChatScript local


You should see the server start and have a promp on the command line. Press enter and see if it’s working (talking). If it is, you'll be talking to Harry - the default bot.

Note: If you get stuck and just want a working Mac binary let me know and I'll send you mine. 
In the long run it's worthwhile being able to build your own as Bruce releases new versions quite frequently.


Running on a PC?

There is a EXE file provided with the download. Simply run that as follows:
ChatScript.exe local

Running on Linux?

32 bit and 64 bit binaries shipped with the zip file. You'll need to make them executable:
chmod +x ./LinuxChatScript32
./LinuxChatScript32 local

or 
chmod +x ./LinuxChatScript64
./LinuxChatScript64 local

Documents

Take a look under the DOCUMENTATION folder and you'll see a comprehensive collection. It's not easy to navigate at first, but you'll quickly learn where the relevant information is.

Create a new bot

The first think you'll want to do is create a new bot - your bot.

The document "Bot Harry - basic bot.pdf" covers this in some detail.
Essentially you have to make a copy of HARRY and change all the Harry text references to your own. Mine's Sioni so I'll stick to that below. 

So...
In the folder RAWDATA copy the HARRY folder and rename it. Use this as the base for your new bot.

Change the name at the bottom of introductions.top
Change Harry in simplecontrol.top to Sioni

In main folder, copy filesHarry.txt and rename filesSion.txt
Edit this file and change the path for Harry's files to Sioni's files.

I've reused childhood.top by removing the content and stating again. This is fine for Sioni as it is all about his childhood. If you wanted a file with a different name simply rename it.

Common Rules

The complete ChatScript language is quite extensive and complicated. However you can build a complete chatbot with just a subset of simple commands.

# This is a comment. We're going to have a topic about my dog Sarah.
# If the user is asking about dog or Sarah this topic will 
# be active, or if there's no input anything with a t: may be 
# spoken by Sioni.

topic: ~about_dog ( dog Sarah )

# Sion will speak this once as a conversation starter.
t: We have a dog called Sarah.
# if the answer includes 'do you' Sioni will say the following text.
    a: ( do you ) Yes. What do you want to know about her?

# Again, random question from Sioni
t: Do you know what Sarah likes to eat?
# If you type anything with 'what' or anything that's the concept 'no' then Sioni will tell you she likes rabbits.
    a: ( [ what ~no ] ) She likes to catch and eat rabbits the most but also quite likes squirrel.
# If you guessed rabbits.
    a: ( rabbit ) Yes! She likes rabbit!
# If no match, capture input and write it back in another sentence.
    a: ( _* ) Oh I don't think she likes that... '_0 really?

Simple Build and Test cycle

You'll be editing code and wanting to test it quite frequently. It's important to remember that when you exit ChatScript and login again, ChatScript will continue the session where you left off. Just to be clear, you don't really login to ChatScript, rather you tell it who you are and it uses that to maintain a session state. But when you're writing and testing code, starting where you left off can be very confusing. It's better to have a clean slate. Occasionally your code will cause a catastrophic error which messes up the ChatScript state so badly that you have to rebuild the knowledge base. This is addressed below.

Note: I'm using local mode, hence the 'local' for the startup argument. This simply allows you to interact with ChatScript at the command line. You can run it as a server and use a web front end but for the purposes of developing the scripts themselves, the command line interaction is much more effective. I'll talk about other front-ends in some later post - honest.

Typical Steps are:

Edit some code the run the server, provide a user name (anything) and then type:
:build sioni

Test your new rules, debug and fix bugs and try again... etc.


to reset the rules and start the conversation afresh do this when you're logged on:
:reset user

I do this pretty much every time I want to retest by code.

Complete Clean Build

In the event of a bad crash, rebuild the whole system as follows:
Delete the contents of the following two directories: USERS and TOPIC

Then start the ChatScript again and run:
:build: 0
:build sioni

This builds the ChatScript base again and adds your bot. You have a fresh new bot with all the old history cleaned out!

Unit Tests

If you're a developer you'll be familiar with the notion of unit test. ChatScript supports this through a special comment type before a pattern. The comment is used to test the pattern that follows.  For example:

#! Does Sarah like rabbits?
?: ( Sarah like rabbits ) Yes she does.

It's worth adding these unit tests as you go.


Next time: I'll show you a much more functional Sioni and let you know what I've learnt along the way about constructing a chatbot using ChatScript.