Speagram Home | tutorial | use me | live interface | documentation | developer's corner | links | contact

SIMPLE WEB MUSIC PROGRAM USING SPEAGRAM AND OPEN SOURCE

If you can already write in Speagram and know how to query it from PHP you might want to use it all together. On this page we show you how a simple music generation program can be built by combining Speagram with other Open Source tools.

Declaring XSLT within Speagram

To use the output of Speagram in other programs you normally have two choices. On one hand you might use the internal Speagram XML (discussed on web access page) directly and transform it yourself. The other option that we are going to use here is to enter the necessary XSLT transformations into Speagram and then let Speagram give you the XML you need. To see how this can be done please look at the music.php file, where we define basic Speagram datatypes for music and enter the necessary XSLT templates to transform these elements to Music XML. To understand what is going on there you must have basic knowledge of Speagram, understand XSLT and you should at least take a look at the basic Music XML example.

Once you know how to handle XSLT inside Speagram let us again use a PHP query function similar to the one on our web access page. Try to run the PHP code presented below and see the resulting XML output. Please note that we extract the xml tags we want from Speagram response in PHP. This is a very basic way of handling errors, but we will not discuss error handling in detail in this example.

  function query_music ($query, $tag) {
    // We query against the test file, music will just be loaded.
    $url = "http://www.speagram.org/useme/test.php";
    $name = music;

    // No XML option specified, as we let Speagram do the transformation.
    $prefix = "$url?cmd=run_get&name=$name&len=1&stop=3&n0=1";
    $speagram_query = "$prefix&t1=" . rawurlencode ($query);
    $pattern = '/^(.*?)(<' . $tag . '.*<\/' . $tag . '>)(.*?)$/s';
    return preg_replace ($pattern, '\2', file_get_contents ($speagram_query));
  }

  echo query_music ('%$Load state file:/music. C 4 whole.$%', "note");

The output you get should look like this:

<note>
  <pitch>
    <step>C</step>
    <octave>4</octave>
  </pitch>
  <voice>1</voice>
  <duration>16</duration>
  <type>whole</type>
</note>

Combining Speagram with Open Source Tools for Music

Once you know how to use XSLT within Speagram and know how to get the XML in your preferred form back, you can start using external programs to process the results. In this example we are going to use the Music XML output from Speagram (score-partwise tag) and process it using the following Open Source tools:

Combining all these tools with Speagram's parsing ability we can easily create a PHP web site that allows you to write music in the syntax defined in music file and get the results printed and played for you.

See for yourself how it works in our music test live file!

Valid XHTML 1.1