Archive for the ‘programming’ Category

MIDI notes and enharmonic equivalence – towards unequal temperaments in Clojure

21/04/14

“Positiv Division, Manila Cathedral Pipe Organ” by Cealwyn on flickr

One current ‘when-I-get-spare-time-in-the-evening’ project is to explore how different keys sounded before the advent of equal temperament. Partly out of interest and partly because whenever I hear/read discussions of how keys got their distinctive characteristics (for example in answers to this question on the Musical Practise and Performance Stack Exchange) temperament is raised as an issue or explanation.

Having recently enjoyed Karsten Schmidt‘s Clojure workshop at Resonate 2014 Clojure and Overtone seem a good place to start. My first steps are with the easiest non-equal temperament to get my head around, the Pythagorean Temperament. My (albeit limited) understanding of temperaments has been helped enormously by the amazing chapters on the subject in David Benson’s book Music, a mathematical offering.

The story, as I was told it, has Pythagoras walking down a street in ancient Greece when he hears the sound coming from blacksmiths hammering iron bars. He notes that some hammerings produce harmonious sounds and some do not. On further investigation into the pleasing sounds Pythagoras discovered the frequency ratios behind the octave (a doubling of the frequency) and the perfect fifth (adding a half to the bar length, a frequency ratio of 3:2). Thus the Pythagorean Temperament is formed solely of repeated perfect fifths and octaves.

So how might that look in Clojure? This is me working through my understanding so I’m going to keep it simple. But first up I need some helper functions

(defn keep-in-octave [ratio]
  "Shift ratio by octaves until it falls between 1 and 2 inclusive"
(cond
(> ratio 2) (keep-in-octave (/ ratio 2))
(< ratio 1) (keep-in-octave (* ratio 2))
:else ratio))

(defn apply-ratio
"Vector of ratio and pitch shifted by ratio"
([ratio] (apply-ratio ratio 440.0))
([ratio pitch] [ratio (* pitch ratio)]))

With them in hand I can define the ratios that take our root pitch up through the fifths in Pythagorean tuning:

(def pythagorean-ratios-ascending-fifths
"Lazy sequence of ascending fifth ratios in Pythagorean temperament starting from root."
(iterate (fn [x] (keep-in-octave (* x (/ 3 2)))) 1))

Inverting the ratio to take us up a fifth takes us down a fifth, which when we shift up an octave is the same as going up a fourth:

(def pythagorean-ratios-descending-fifths
"Lazy sequence of descending fifth ratios in Pythagorean temperament starting from root."
(iterate (fn [x] (keep-in-octave (* x (/ 2 3)))) 1))

I can now just write out the names of the fifths and fourths we are interested in. Starting from A the first few give us the notes we need for a scale of A major: A, B, C♯, D, E, F♯, and G♯, and if we take the first twelve of each we get to common notes before needing double sharps, double flats, etc. (N.B. We’ll come back to that “etc”!)

(def fifths-ascending-from-a
[:A :E :B :F♯ :C♯ :G♯ :D♯ :A♯ :E♯ :C :G :D])

(def fifths-descending-from-a
[:A :D :G :C :F :B♭ :E♭ :A♭ :D♭ :G♭ :C♭ :F♭])

Zipping these together gives us the ascending fifths and fourths:

(zipmap fifths-ascending-from-a
(map apply-ratio pythagorean-ratios-ascending-fifths))

=>{:A [1 440.0], 
:D♯ [729/512 626.484375], 
:C [19683/16384 528.59619140625], 
:B [9/8 495.0], 
:G [59049/32768 792.894287109375], 
:D [177147/131072 594.6707153320314], 
:E [3/2 660.0], 
:C♯ [81/64 556.875], 
:G♯ [243/128 835.3125], 
:F♯ [27/16 742.5], 
:E♯ [6561/4096 704.794921875], 
:A♯ [2187/2048 469.86328125]}

(zipmap fifths-descending-from-a
(map apply-ratio pythagorean-ratios-descending-fifths))

=>{:A [1 440.0], 
:C [32/27 521.4814814814814], 
:D♭ [8192/6561 549.3796677335771], 
:F [128/81 695.3086419753087], 
:G [16/9 782.2222222222223], 
:D [4/3 586.6666666666665], 
:C♭ [65536/59049 488.33748242984626], 
:F♭ [262144/177147 651.1166432397949], 
:B♭ [256/243 463.5390946502056], 
:G♭ [32768/19683 732.5062236447696], 
:A♭ [4096/2187 824.0695016003657], 
:E♭ [1024/729 618.0521262002745]}

That’s a fun start, but it has left me with a question. Overtone seems to use MIDI notes as the underlying note ‘name’, but MIDI notes give enharmonically equivalent notes the same name. So, for example, the MIDI note 73 is C♯ and D♭ but in Pythagorean Temperament rooted on A they have different pitches. Benson refers to this by changing the familiar “circle of fifths” to the “spiral of fifths” noting that the “Pythagorean spiral never joins up”, i.e. after double sharps there will be triple sharps etc. without ever reaching enharmonic equivalence. I’ll check on the Overtone mailing list to see how important/correct my sense of underlying MIDI notes is.

Advertisements

Looking an svn_dump file in the mouth

24/09/10

from the horse's mouth.. by Sarah McD on flickr
from the horse’s mouth.. by Sarah McD on flickr

Crispin, the web master at the orchestra I play in (the Cambridge Concert Orchestra) is leaving us (and the UK) and so I’m taking over as web master. Crispin managed the website versioning in Subversion and so handed over the SVN dump file. I’m more of a Team Foundation Server  person, so it’s been a bit of a journey to get Subversion working on my Win7 box, though now I come to write some notes just-in-case I need to do it again the steps look easy. Big thanks to Dinan and a cast of thousands on the interweb for their help!

STEP ONE

I started off trying Subversion via Linux, using either VMware + Ubuntu or Slax to get Linux up and running on my Win7 box. However that seemed overkill, so I looked around for Windows versions of Subversion (there’s a list on the Subversion site here: http://subversion.apache.org/packages.html#windows) and settled on SlikSVN

STEP TWO

Once SlikSVN was installed I made a suitable directory, put Crispin’s SVN dump file in it, and from the command typed

>svnadmin create repository
>svnadmin load repository < svn_dump

That gets me the SVN repository unpacked. Next I need a working copy and so this was the command:

>svn checkout file:///C:/Users/timregan/CCO/repository website

That’s it for the command line.

STEP THREE

To edit the files it was easier for me to use Visual Studio 2010 as it’s powerful and I’m familiar with it. I used a Subversion plugin for Visual Studio called AnkhSVN which I installed.

Under the view menu AnkhSVN includes a repository explorer and a working copy explorer. I’m yet to figure out how to get it so that double clicking an shtml file in the working copy explorer opens the file in that instance of visual studio, and marks it as changed if it’s edited, but I’ll note those tips down to when I’m done.

The SVN book online is also fantastically useful: http://svnbook.red-bean.com/en/1.5/svn-book.html

The genius that is Sara Ford

10/10/08

The video from ReMIX 08 is up. There were some great talks but I’m particularly excited to rewatch Sara Ford’s 20/20 session. The brief was twenty slides, twenty seconds a slide. Sara didn’t use slides but instead went through 20 tips for using Visual Studio, and spent 20 seconds on each tip. I learnt loads in under seven minutes! The instructions are here on Sara’s blog: http://blogs.msdn.com/saraford/archive/2008/10/07/remix-sessions-codeplex-visual-studio-tips-now-available-online.aspx

Getting Processing working with SQL Server 2008

09/07/08


"messy times"
from el frijole(?)

(N.B. If you read down to the comments you’ll notice that Michael Friis has written a shorter and far more readable version of this process with my erroneous steps removed at http://friism.com/processing-and-sql-server-in-windows )

I’ve just spent a day getting Processing (aka Proce55ing) working with a Microsoft SQL Server 2008 database. It wasn’t hard, just a bit fiddly, so I thought others might appreciate it if I documented my route. Partly because of the Processing language’s open source heritage most of the discussion about databases on the Processing site centres on MySQL, but with a few changes it can be made to work for SQL Server too.

STEP 1
I already had Microsoft SQL Server 2008 installed.

STEP 2
Next I downloaded the SQL Server 2005 JDBC Driver and unzipped it to the suggested default location. Ignore the 2005 in it’s name – it works for 2008 as well though does not confer the additional 2008 features. Following the set-up instructions I added "C:\Program Files\Microsoft SQL Server 2005 JDBC Driver\sqljdbc_1.2\enu\sqljdbc.jar" to the CLASSPATH environmental variable, though I think this might work without that step.

STEP 3
I used the code in Ben Fry’s book "Visualizing Data". It’s on page 291 in the section titled "Using MySQL with Processing". I adjusted it slightly – Ben hides the construction of the connection string inside the Database class’ constructor.

Here’s the helper class (added in a second tab)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

class Database
{
  Connection conn;
  String connectionURL;
  
  public Database (String connectionURL)
  {
    this.connectionURL = connectionURL;
        
    this.conn = connect();
  }
  
  public Connection connect()
  {
    try
    {
      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
    }
    catch (ClassNotFoundException e)
    {
      e.printStackTrace();
    }
    catch (InstantiationException e)
    {
      e.printStackTrace();
    }
    catch (IllegalAccessException e)
    {
      e.printStackTrace();
    }    
    
    try
    {
      return DriverManager.getConnection(this.connectionURL);
    }
    catch (SQLException e)
    {
      e.printStackTrace();
      return null;
    }    
  }
  
  public ResultSet query(String query)
  {
    try
    {
      Statement st = this.conn.createStatement();
      ResultSet rs = st.executeQuery(query);
      return rs;
    }
    catch (SQLException e)
    {
      e.printStackTrace();
      return null;
    }     
  }
}

and the main code is …

import java.sql.ResultSet;

void setup()
{
  Database db = new Database("jdbc:sqlserver://localhost:1433;databaseName=Book;integratedSecurity=true;");
  ResultSet rs = db.query("SELECT TOP 100 * FROM Words");
  
  try
  {
    while (rs.next())
    {
      String word = rs.getString(1);
      println(word);
    }
  }
  catch (SQLException e)
  {
    e.printStackTrace();
  }
}

But this threw an error: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect, and so I thought I’d try to get it working in the NetBeans IDE first.

STEP 4

At first I couldn’t get the code to build and needed to add a few import statements (e.g. this should have been a clue that I was using the wrong SQL library but there you go). Then I found Nipawit Luangaroon’s excellent post all about accessing SQL Server from NetBeans in which he gives clear simple instructions on adding the correct library to the project’s CLASSPATH:  http://www.linglom.com/2007/03/04/accessing-sql-server-on-netbeans-using-jdbc-part-i-create-a-connection/

STEP 5

Then I got a different error message: WARNING: Failed to load the sqljdbc_auth.dll com.microsoft.sqlserver.jdbc.SQLServerException: This driver is not configured for integrated authentication. This time the online documentation came to the rescue: http://msdn.microsoft.com/en-us/library/ms378428.aspx I needed to copy the file sqljdbc_auth.dll from "C:\Program Files\Microsoft SQL Server 2005 JDBC Driver\sqljdbc_1.2\enu\auth\x86" to "C:\Windows\System32".

STEP 6

Still not quite there. The error message I got this time was back to com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host  has failed. java.net.ConnectException: Connection refused: connect. So I needed to check if the SQL server was running on the port I expected. I tried "telnet localhost 1433" from the cmd prompt which fail with "Connecting To localhost…Could not open connection to the host, on port 1433: Connect failed". Actually I’m missing out a step.

STEP 7

Install telnet. Vista doesn’t seem to have telnet enabled by default but if you go to Control Panel -> Programs and Features -> Turn Windows features on or off -> [check] Telnet Client -> OK

STEP 8

To find out what port SQL Server 2008 is running on I went into the management studio and expanded "Management" in the Object Explorer and then opened SQL Server Logs -> Current. That had a line saying that the server is listening on port 1434 (I thought it was on 1433). So my line of code became

Database db = new Database("jdbc:sqlserver://localhost:1434;databaseName=Pullman;integratedSecurity=true;");

Done 🙂

It’s all working now and I’m a happy bunny. I don’t know how many other folk are working through the same path but I thought I’d post it just-in-case it’s useful.

———- Revision ———-

I’ve just been struggling to get this working on Linda’s laptop. I think I missed out documenting a step. You seem to need a library directory containing sqljdbc.jar in the Processing libraries folder. For me that means creating a directory named sqljdbc inside C:\Program Files\processing-0135-expert\libraries. Within that directory create another called library, and inside that copy sqljdbc.jar from C:\Program Files\processing-0135-expert\libraries. Without this I get the same exception I’m seeing on Linda’s laptop: java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver but with it the sketch works. I’ll check that fix works on Linda’s laptop too tomorrow.

Note to self: Western European Windows ASCII extensions come from code page 1252

06/07/08

I’ve just spent more hours than were available trying to work out how to get the StreamReader in my book-parsing C# code to recognise characters beyond ASCII. Here are some notes so I don’t spend so long next time!

  1. ASCII is seven bit so the characters covered have encodings 0 / 00 / 0000000 through to 127 / 7F / 1111111 (decimal / hexadecimal / binary). Wikipedia has a useful lookup table here: http://en.wikipedia.org/wiki/ASCII
  2. I’ve been reading in a text file that contains characters beyond this range. For example em dash (—), ellipses (…), quotation marks (“”, not the straight ones), etc
  3. Since computers usually use eight bits to store ASCII characters the additional space (0x80 through 0xFF) is used to encode such additional characters.
  4. But how does I tell which code page I am using? I could see the symbols when I opened the text files in word, notepad, or Visual studio. I could even tell the hex representation (from browsing ‘Insert Symbol’ in Word or opening the text file in Visual Studio’s binary editor), but which ASCII extension was it, and how could I get my C# code to use it?
  5. The library documentation for the .Net System.Text.Encoding class contains a long list of supported encodings and their associated code pages. Half-way down was a hopeful looking “Windows-1252 Western European (Windows)”
  6. Now things heated up. Wikipedia has a page showing the encodings in code page Windows-1252 and ‘Bingo!’, the character I was looking for (em dash) had the encoding I was seeing (0x97).
  7. Finally a quick search for “csharp StreamReader 1252” revealed the code I needed. I replaced
         using (this.bookStream = new StreamReader(fileName))
    with
         using (this.bookStream = new StreamReader(fileName, Encoding.GetEncoding(1252)))
    and all works fine!

Quick rant about calling a ‘language’ “Processing”

29/06/08


“437 scientific potatoes”
from yesyesnono(?)

I’m just deciding whether to use Processing or the Windows Presentation Foundation (WPF) for my book visualization (bookviz) project with Linda. The advantages of us using WPF are that:

  1. I’ve already got some familiarity;
  2. It’s a part of a fully fledged programming language so I can hone performance, robustness, etc;
  3. I know how to glue in data sources;
  4. There’s a good integration story with the design tool Expression Blend (which Gavin, Nic, and I have been using); and
  5. There’s the potential to webalize the resulting application via Silverlight.

But there’s one big disadvantage. WPF is part of C# and the .Net Framework and learning that is well beyond the scope of a twelve week internship for a designer (don’t ask Richard about forcing designers to use object oriented languages unless you have your asbestos suit on!) So if we go the WPF route then all the code will be written by me. That’ll be fun but it may prove a bottleneck, and in would be less fun than writing code together.

Enter Processing. Processing is a ‘language’ designed by Ben Fry and Casey Reas when they were both in the Aesthetics and Computation Group at the MIT Media Lab. I think that it wasn’t always called “Processing” but use to have the cutesy hacker spelling “Proce55ing”. More on that later. I’ve put “language” in single quotation marks (single inverted commas) because what is interesting about Processing isn’t the language syntax, it’s just a cut down version of Java, but the API and the environment. The advantages of us using Processing are that:

  1. Ben Fry and Casey Reas designed the language so that it would be easy (or easier) for designers to use as a “software sketchbook”, so we could potentially share coding tasks;
  2. There’s a buzz about Processing, especially amongst the information aesthetics community (the designy end of information visualization);
  3. Other people at work have been playing with it; and
  4. The visualizations produced in Processing, like the one that heads this post, are often beautiful and subtle.

But like many information visualization projects our bookviz work is information intensive, and so will make extensive use of SQL queries and a SQL Server 2008 database. Hence I need to get whatever we choose running against SQL. If we go the WPF route I have an embarrassment of riches (and acronyms 😉 ). I could use ADO.Net, OLE DB, Windows DAC, LINQ, … But if I choose Processing what should I use? The obvious way to find out is to check the Processing site: http://www.google.com/search?hl=en&as_q=SQL&as_sitesearch=processing.org Only that search reveals that most Procesing integration with SQL is to MySQL not Microsoft SQL Server. Never mind I can just search the SQL Server discussion groups.

Now I am stuck. Of course the search http://groups.google.co.uk/group/microsoft.public.sqlserver.programming/search?hl=en&group=microsoft.public.sqlserver.programming&q=processing returns over 10,000 results, but how do I distinguish those that use the word “processing” as a verb from those that use it as a noun? If it had a curious spelling, like “Proce55ing”, the job would be easy. As it is I cannot search outside the Processing site itself because (obviously) the word “processing” is already heavily used on programming discussion boards! Arghhhhh.

It reminds me of the early days of C# when Google searches were made difficult because Google would drop the “#” from the search term and return lots of results about the C language. In the unlikely event that I ever invent a language I’ll make darn sure that it is easy to search for. That does remind me of my favourite language name. Years ago we were musing about object oriented languages over lunch when Paul Sanders’ wife (sorry – senior moment on her name) suggested that the object oriented version of COBOL ought to be called “ADD 1 TO COBOL” 😉 (N.B. Wow – that joke has even make it to Wikipedia’s COBOL page!)

NB Before signing off this post I just want to record one other apprehension about Processing: the way it handles fonts. Rather than render fonts dynamically one first loads the fonts into the Processing environment so that each each letter of the alphabet is stored as an image. Is that right? I need to think about that. What happens to kerning? What about tiny font size? Can one use font of fractional size? What would 0.55 point font look like? What size font does Brad Paley use around the outer ellipse of TextArc? In one of the processing books Reas and Fry cite LettError‘s Beowolf font, which prints each letter differently every time the letter is printed. How does that square with preloading fonts as character images? Too many questions 😦

I’ll be getting my Thursdays from a banana

15/05/08


Camel” by bgiguere2

Like many programmers I have a long list of programming languages that I’ve been meaning to try, meaning to master, or just meaning to dust off and revisit. For example my boss wants me to check out F#. In fact lots of the people at work are using F# on an increasing variety of projects. For some folk this is clearly a great idea. Take Ralf for example. Ralf’s work often relies on applying clever Bayesian models to new problem domains. When Ralf codes the mathematical model and something doesn’t work he needs to deduce whether it’s something with the model or something in its translation into code that went wrong. So a language like F# that moves the implementation close to the maths without sacrificing the ability to call rich libraries is perfect for Ralf. But most of my code is user interface or (or data handling at present) so I doubt I’d see the same advantage. Still, F# is high up on my ‘to try’ list.

Another entry is Processing. I’m increasingly doing lots of data visualization work and some of the most beautiful interactive visualizations I encounter on the web are written in Processing. But I am a tad confused (not least by if it is called Processing or Proce55ing). As I understand it Processing is a cut down Java that lets designers approach programming in a sketchbook style. But I can code full Java so why would I do that? Wouldn’t it be like putting stabilisers or training wheels on my racing bike? Possibly not; it looks like the iterative nature of the Processing environment is its power. So that’s on my list too.

But the catalyst for this post is Perl. I did go through a brief Perl phase back in the late nineties, but I never got beyond the struggling phase. I remember once working for several hours on a VRML file manipulation script in Perl before I finally got so stuck that I didn’t mind revealing my lack of knowledge and asking for help. At the time we had John Dent (aka Denty) interning in my group so I popped my head up over the divide between our desks and explained what I was trying to do. Denty started typing at the command prompt, and one line of Perl later he hit return. The computer thought for a few seconds and out popped the answer I needed. One line of Perl may give the impression it was a brief script, but IIRC it was a few hundred characters long. Wow. So Perl has been on my ‘to try and master’ list for a decade now.

So what bumped it up? I’ve just finished Clay Shirky‘s book “Here Comes Everybody“. There’s lots to say about this excellent book, so I’ll weave it into more posts. Today it had me laughing on the train – and it is rare that a work book has you laugh out loud in public. It was the bit where Clay is recalling his days as a Perl programmer. Here are the two passages that had me LOLing.

>>> Where, [the AT&T engineers] asked, did we get our commercial support for Perl? We told them we didn’t have any, which brought on yet more shocked reactions: We didn’t have any support? “We didn’t say that,” we replied. “We just don’t have any commercial support. We get out support from the Perl community.”

It was as if we’d told them, “We get our Thursdays from a banana” <<< (p. 256)

>>> Perl is a viable programming language today because millions of people woke up today loving Perl and, more important, loving one another in the context of Perl. <<< (pp. 257-258 )

Note to self: fixing “cannot locate resource window1.xaml” exception in vanilla WPF project

05/02/08

Visual Studio 2008 Screenshot

I’ve had this problem come up a few times. If I start a new WPF project and delete the default window1.xaml and add in a different starting page I forget to fix App.xaml to reflect the change. That’s the file with the line startupUri=”Window1.xaml” in it.