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.





Sunday 20 July 2014

ChatBot 2 - Sioni at Pandorabots

Llanllwch - Sion's home town
AIML is a common approach for ChatBots. I take a look at Pandorabots and and see if it meets my needs.

Sioni the Person

I set out to build a ChatBot that would capture facts about a person such as place of birth, parents and stories from their life etc. A ChatBot that could provide much more than a simple family tree and some photos.
I need subject to test with so I've decided to pick myself.


To include all my personal history and stories is impractical, so I'll restrict the test to a short period of my life in a small village called Llanllwch in Carmarthenshire, Wales. I was about 3 years old when we moved there and left when I was about 7 years. That is, a short, contained period of my life in a single location, with a very limited number of friends, at an age where I only remember key events and short stories - perfect.

When we moved to Llanllwch, I was a Welsh language speaker. My mother used to call me Sioni (Johnny) or Sion (John). So I've picked this name for my ChatBot. Sioni Llanllwch. However, I'll assume for the time being that Sioni speaks English. Actually he does, so that's ok. However, there will (necessarily) be a smattering of Welsh terms, names etc that featured in Sioni's life. This will make things more interesting.

Sioni at Pandorabots

There is a Standard created to capture a ChatBot's knowledge base, it's AIML. The Artificial Intelligence Markup Language. A term (I now realise) that implies a broader range of capabilities than it actually covers.

There are a number of websites where you can create your own ChatBots based on AIML.

I've picked Pandorabots - it's free and easy to use.

Note: If you want to replicate my activities, then sign up and create a ChatBot.

The first thing I did was to remove all the default AIML files. I'm not sure if this is wise, but there are a lot of default files that contain information I simply would not have known as a kid!

I started using the "Train" option but this is rather laborious. Not only that but it's not a great way to capture a lot of information in a way that can be edited later.

You could edit the AIML files directly, but this requires a very good understanding of the XML syntax and the result is sure to be error prone. Also, the XML structure will simply get in the way of capturing a simple dialog.

To being with, I've opted for uploading my own text file using the Dialog to AIML Parser. This simply takes a relatively freeform question and answer structure and converts it into AIML which you can then load into your ChatBot.

The free form text I've used is here.
When converted to AIML, it looks like this.

Upload to your Pandorabots by going to the AIML Tab for the ChatBot properties.
Remember to publish your ChatBot after uploading new files.

You can talk to Sioni here.

Here's an example chat I had with Sioni when I knew exactly what to ask:

Me: Did you ride a bike?
Sioni: Yes, after a while we had bikes and we used to ride up and down the road to the railway bridge and back. Sometimes we went up Allt y Crap hill and rode down at high speed.

Me: Where did you live in Llanllwch?
Sioni: We lived in Leat House. It was a modern house designed by my father.

Me: Do you have a brother?
Sioni: I have a brother called Simon.

Me: What's your mother's name?
Sioni: Ileana Jones or Ileana Maesydd to some who knew here when she was younger. 


So far so good! However, if you don't match the questions exactly, the answers are repetitive and less than engaging:

Me: Did you have a cat?
Sioni: I have no answer for that.

Me: Did you have a dog?
Sioni: I have no answer for that. 


You can include wild cards which carry the matched part of the sentence through to the ChatBot answer but this isn't that useful when representing a knowledge base.

For example,
Me: "Do you like *"
Sioni: "Yes I like *"

More advanced AIML

To take advantage of AIML's power, you need to leave the simple text format and jump in to direct XML editing of the AIML.
In simple terms it has patterns to match on and templates for text output.  It supports recursive text matching through a key-word <srai>. This one construct is used to create numerous synonyms for words or phrases and provide a seemingly more intelligent agent.
A more elaborate AIML file can be found here.

Dialog example:
Me: did you have a bike?
Sioni2: After a while we had bikes and we used to ride up and down the road to the railway bridge and back. Sometimes we went up Allt y Crap hill and rode down at high speed. 

Me: Tell me about Allt y cnap hill
Sioni2: We used to watch the hunt from the top of the hill. I remember watch the fox get away. That was good to see. 

Me: and where else did you ride your bike?
Sioni2: I had a bike and when I learnt to ride I rode straight in to Any May! 

Me: what was your father's name?
Sioni2: Donald Jones, or Jones the Architect. 

Me: did you have a brother?
Sioni2: 1 I have a brother called Simon. He's a year younger than me. 

Me: did you know a tramp?
Sioni2: There was a tramp who lived in Allt y cnap woods sometimes. 

Me: tell me more about the tramp
Sioni2: When I was a kid there were lots of them arround. They used to go from one parish to another and get food from the vicar. 


Conclusion

This is not what I'm after...

To be fair, AIML does allow a whole host of additional functionality which amounts to more flexibility in pattern matching for pseudonyms etc. I could have made Sioni a lot more convincing with time and effort. Maybe that's exactly what's required.

There's no doubt that Pandorabots have also done exceedingly well at the Loebner prize over the years  - see their blog for an overview. AIML based ChatBots can be good - very good!

So what's my problem?

To begin with, AIML based systems follow a pattern matching approach and do not track context or represent knowledge in a way that can be queried in an ad-hoc manner. It's a bit like teaching a horse to count - see clever Hans.

The other problem with this approach is the input data needs to be highly structured. Ideally some means of inputing free form text would be good!

There's plenty of information on the web on the pros and cons of AIML. Take a look here.

So for now, I'll leave AIML based ChatBots and take a look at what other approaches have been tried and how they differ from (completely) AIML based systems.


Tuesday 14 January 2014

ChatBot 1 - My Grandmother's a ChatBot



Mechanical Turk

The Idea

It stuck me some time ago that the amount of knowledge we have of our grandparents and great grandparents is very limited. If you're lucky, you might know where they were born, where they lived and went to school, what job they had, who they married and who their children were. That's not a lot of information! You might get the odd story or two, anecdotes about their childhood etc.

You could record this information in a document, or... you could bring it to life through a conversational agent of some sort!

So... rather than aim for a complete biography of someone's life, would it be possible to capture a reasonable set of facts, stories and events that could be used to host a conversation with that "person"? A conversation that would not be too much different from one you might have had with your grandmother anyway. Light on detail, possibly contradictory, and often confusing - but informative, possibly funny and hopefully interesting.

Would this be an useful way to capture someone's life history? Would it help future generations understand a bit about their ancestors in a more engaging manner than reading a document? Is it an useful way to teach history? Is the current technology up to the task and freely available for the masses? Is it also possible to create a fictitious interviewer to gather this information in the first place? Does the information need to be codified by experts before making it available to a computer program? Is it possible to automate this activity and provide a system that was fit for commercial use? etc. etc.

This is not a new topic and much has been done to address these questions over the last few decades. I'm aware of some of this work but would like to find out more. I'm also after feedback and guidance from others who have explored this trail before.

I'l set off to explore these ideas with the aim of constructing various implementations from the freely available software and see if I can answer some of the questions I've raised above. This is meant to be a fun project and not an authoritative treatise on artificial intelligence!

Background

The idea of a computer or machine that embodies a person's knowledge and abilities has been around for quite a while. The term ChatBot is commonly used today.

The Turk (or Mechanical Turk) was an eighteenth century creation that used a real person inside a cabinet to provide the allusion of an intelligent machine that played chess. The appeal of such a contraption has remained with us ever since.

In the 1950s Alan Turing devised the Turing Test to determine if a machine could be regarded as intelligent. Actually, what he proposed was something slightly more subtle, a test to tell if a human could tell the difference between a human and a computer through a dialog with both via a screen and keyboard. This gets around the problem of defining "intelligence" but is rather subjective.

A competition is held each year along the same lines: it's the Loebner Prize.

There are other examples of computers beating humans at their own game. Watson, the IBM computer program that won the quiz show Jeopardy is one such impressive machine along with Deep Blue, the chess computer - a formidable challenge to the most accomplished chess masters.

Interestingly, Watson's creators were concerned that the Jeopardy staff would turn the game in to a Turing Test by exploring the  deficiencies of Watson. More on this problem later, but a clear indication that their aim was not to mimic a human Jeopardy player, but simply to create a machine to win Jeopardy.

But now we're digressing - while Watson and Deep Blue are examples of very smart computers, the capability we're after relates specifically to programs that can act as a convincing proxy for a real person. Unless all your grandparents did was quiz-shows and play chess, we'll need to look elsewhere.

So... have people thought about capturing an individual's knowledge before? Yes, many as it turns out...

Bruce Wilcox, a winner of the Loebner Prize, wrote a short paper titled: Speaker for the Dead. While a slightly macabre twist on the topic at hand, the paper explores the issues directly and provides insights into the problems and limitations of commonly used ChatBot technologies.

There are also many examples of specific individual ChatBots on the web:
John Lennon is a good example to start with and a whole page of others here.

There's lots more that can be said about ChatBots on the web, but I'll move on to looking at the most popular ChatBot technology next and see how they stack up.