Songlib: reading notes

Song Li Buser

Revision Date: November 7, 2011

Printable Version

Reading a set of notes

In songlib, one can read in a single note or a set of notes with a single function call. To read in a set of notes, a call to readScale is made. The prototype of readScale is:

    int readScale(char *directory,char *prefix);

This reads in all the notes in the given directory with the given prefix. The notes are read as RRA objects and then stored in an array. The C note of ocatve 0 is stored at array slot 0, The B note of octave 2 is stored at array slot 25, and so on. For example, the following code reads in all the notes (as RRA files) in the "/home/songlib/samples/guitar/" directory that begin with the prefix "pick_".

    #define DIR "/usr/local/share/samples/guitar/"
    #define PREFIX "pick_"

    int guitar = readScale(DIR,PICK);


The function returns the instrument number assigned to notes that were specified. The first call to readScale returns 0, the second returns 1, and so on. The readScale command first looks for notes of the form:


If no notes are found, it then looks for notes of the form:


Notes of this form are assumed to correspond to midi note numbers. If the note number is x, then it is placed at octave x / 12 and note x % 12.

Note that at least one note need be present in the directory. If a request is made to play notes other than those found, songlib will resample the closest found note to make the desired note. However, the further away the found note, the more artificial the resampling will sound. A good rule of thumb is not to resample more than three or four semitones away. Also, resampling a note to a higher pitch will shorten the length of the resulting note.

Reading a single note

One can also read in a single note and then use the note playing functions that take an RRA object as an argument. To read in an RRA note, use the readRRA function:

    RRA *readRRA(FILE *in,void (*handler)(RRA *,char *,void *))

The first argument is a file pointer to the file containing the desired note. The second note is a handler for non-standard tags. For most songlib work, you do not need to supply a handler, so a null value (zero) is passed as the second argument. Here is an example:

    char *name = "stick.rra";
    RRA *note;
    FILE *fp;

    fp = readRRA(name,"r");
    if (fp == 0) Fatal("could not read file %s\n",name);
    note = readRRA(fp,0);


Notes read and played in this manner are not automatically filtered. They can be manually filtered by calling the built-in filters appropriately.

For other note playing functions, see notePlaying.