Nuts and Bolts

Introduction:

The TR system runs on a Slax Linux server and is written completely in Perl.  This is a bit old-school, but Perl is easy to learn, the code is written as text files on almost any kind of editor – including VI remotely over Putty.  Perl is also extremely flexible and powerful.

This article will describe two of the Perl programs that make up the TR system – the controller and one of the weather sequencers.

Controller Program:

Once the files of a piece have been uploaded to TR and the configuration page filled out, the user presses the ‘Start Sequencing’ button on the configuration page.  This causes the configuration page to write a ‘1’ to a file named kyz.txt.  The controller program reads this file four times a minute looking for a value of 1, and once it is found the controller program initiates the appropriate sequencing program.

Here is the source code for the controller program, ctlr.pl

If you are unfamiliar with Perl, note that a Perl program file is a text file with the suffix .pl   Also, the program lines starting with # are comment lines and are not executed.  If you have any familiarity with Basic or C programming, Perl will be similar.

Timing and Re-Sequence Detection:

The first thing the controller program does when it is launched is to read a text file named reseq.txt and load in the number of hours to wait before a re-sequencing is run.  This is a number in hours as set by the configuration program.  The program then drops down into a section labeled STARTER: and this is a loop that reads the kyz.txt file four times a minute looking for a value of 1 to trigger a re-sequence if the user has changed the configuration.  If a 1 is detected in the kyz.txt file, the controller program writes a 0 to the program (to keep from re-triggering) and then the program jumps to the section of the program labeled READER: If no trigger is found, the controller loops for the duration of the re-sequence time limit, and then drops down into READER:

Re-sequencing:

Once the program is in the READER: section it reads a file named seq.txt which contains the number of the sequencing algorithm to be used for the re-sequence.  This number was deposited in seq.txt by the configuration program.  The reseq.txt file is re-read and the new time for the next re-sequence is obtained.

The controller program then determines which sequencing program to launch based on the code read from seq.txt.  When the re-sequencing program has been started, the controller program goes back to STARTER: and loops through the read and wait cycles described previously, ready to trigger a new re-sequence.

A Sample Sequencing Program:

The controller program is relatively simple – it just looks for a re-sequencing trigger in kyz.txt or waits until the time has elapsed for a new sequence.  The actual sequencing program is a bit more complicated.

The source code for wx3.pl, a weather sequencing program, is here.

The idea behind wx3.pl is to get weather data for 16 European cities and use this to determine the coordinates of the ogg files to be used in the final sequence.  The source files are then copied to /S0, /S1 and /S2 folders in order, and then concatenated into track files that are mixed and linked to the player in the listener web page that is created by wx3.

The source code is divided into sections and these are indicated by a long comment line.

 Section 1:

The first section of wx3.pl accomplishes several preliminary tasks.  Firstly, it deletes any files in /S0, /S1 and /S2 to clear space for the new sequence files.  The file AAAA.ogg is then copied from the cgi-bin folder to the /S1 folder.  AAAA.ogg is a .ogg file that is created to provide a fixed delay in the second track with respect to the other two tracks.  This displacement will be precisely in tempo as long as the dely is generated in the notation program using the same time signature and tempo, and the delay file has been edited to the correct duration for the number of beats.  12 to 24 measures usually provides enough delay so that there is sound even while the track files are changing during the course of the sequence.  Also, some offset tends to improve the texture of the piece.

Next all of the data from the configuration files is read and this includes the name and title of the piece, the number of staves uploaded, the number of segment files per staff, the total number of segments in the finished piece, the player status, the sequence type and the re-sequence timing.

Section 2:

In section 2 wx3 begins by calculating the number of coordinates needed to create the new sequence.  This is the number of staves times the number of segments called for in the configuration.  The program then jumps to a subroutine named  &getwx3.  This subroutine (described later) generates a file named scords.txt that contains coordinates calculated from weather data in 16 European cities. scords.txt is filled by the time the subroutine returns control to the part of section 2 that copies the source tracks to folders S0, S1 and S2.

The program loops for each segment as configured for the final piece and detects how many final tracks are needed.  If one or more staves are present, the first segment of the first track is created by copying the source track file indicated by the first coordinate in scords.txt.  The source file name is created by combining the string “S0_” + the coordinate string + “.ogg”.  The destination file name is created by the folder name “S0/” + “AD0” + “mp3”  The “A” in “AD0” is created by using the chr(I) command, where I starts at 65 and increments with each new segment. (ASCII 65 is “A”, ASCII 66 is “B”, etc).  In this way the file names are alphabetized as they are copied into the destination folders.  When so named, they concatenate in that same order to create the final track.

This process is repeated for each staff that is defined in the configuration and source files.  When the total number of segments specified for the piece is reached, the track files are created in Section 3 of the program.

Section 3:

Section 3 issues the sox concatenate command for the one, two or three folders containing the copied source segment files.  The program next writes a temporary index.html page alerting anyone who visits the TR site that a re-sequence is underway and to check back after the re-sequence is complete.

Next, the track1.ogg, track2.off and track3.ogg files are mixed using the sox -m command.  The result is that all the track files are mixed into a single trackallp .ogg file and this is  then modified by sox to add 15 seconds of fade-in and fade-out.  The result is trackall.ogg and this is what the player will be linked to.  Note that sox -m will maintain the beat throughout the final mix for all the tracks.

Section 4:

Section 4 writes the index.html web page for TR that contains the .ogg player and links to the trackall file created in section 3.

Section 5:

Section 5 is the heart of the sequencer – it contains the subroutine – &getwx3 –  that gets the real-time weather data and calculates coordinates from the temperature, wind velocity and humidity from 16 European cities.  The subroutine first opens a database containing the city names andWeather Underground codes for those cities.  WOEID codes are used to retrieve weather data from WeatherUnderground.com.

The subroutine parses the city name and code from each entry and forms a CURL command for WeatherUnderground.com.  CURL is part of the Slax Linux operating system and allows the command line to ask for a weather report from Yahoo.  The CURL command returns a file – wea.txt – containing a page of detailed weather data for the city whose code was specified.

The program next uses the Linux grep command to look through the wea.txt file and return the line that contains the wind speed, humidity or temperature data.  This line is then parsed to extract just the temperature numbers in degrees F, for example.

The next part of the subroutine takes the received temperature and scales it into a coordinate that is used to select the source track for a particular segment.  There may be 4 to 8 files for each stave, so the same temperature has to be scaled for a coordinate given the number of files present.

Eventually each parameter – wind velocity, humidity and temperature – is scaled into a coordinate value and stored in scords.txt.  The subroutine then returns control to the rest of wx3 and the selected files are copied, the player tracks built and the index.html web page created.

At this point the sequence has finished and the piece is ready to be heard from the TR index.html web page.

Going Further:

The source code for these Perl programs can be easily modified and you can use parts of this to create your own sequencer.  All that is necessary is to find an Internet source that returns data from a CURL request, and develop the code to parse for the data and scale it into a coordinate.

Advertisements