示例#1
0
bool NoteTrack::ExportMIDI(wxString f)
{
   Alg_seq_ptr seq = MakeExportableSeq();
   bool rslt = seq->smf_write(f.mb_str());
   if (seq != mSeq) delete seq;
   return rslt;
}
示例#2
0
double Alg_reader::parse_dur(string &field, double base)
{
    char *msg = "Duration expected";
    char *durs = "SIQHW";
    char *p;
    int last;
    double dur;
    if (field.length() < 2) {
        // fall through to error message
        return -1;
    } else if (isdigit(field[1])) {
        last = find_real_in(field, 1);
        string real_string = field.substr(1, last - 1);
        dur = atof(real_string.c_str());
        // convert dur from seconds to beats
        dur = seq->get_time_map()->time_to_beat(base + dur) - 
              seq->get_time_map()->time_to_beat(base);
    } else if (p = strchr(durs, toupper(field[1]))) {
        dur = duration_lookup[p - durs];
        last = 2;
    } else {
        parse_error(field, 1, msg);
        return 0;
    }
    dur = parse_after_dur(dur, field, last, base);
    dur = seq->get_time_map()->beat_to_time(
              seq->get_time_map()->time_to_beat(base) + dur) - base;
    return dur;
}
示例#3
0
double Alg_reader::parse_after_dur(double dur, string &field, 
                                   int n, double base)
{
    if ((int) field.length() == n) {
        return dur;
    }
    if (toupper(field[n]) == 'T') {
        return parse_after_dur(dur * 2/3, field, n + 1, base);
    }
    if (field[n] == '.') {
        return parse_after_dur(dur * 1.5, field, n + 1, base);
    }
    if (isdigit(field[n])) {
        int last = find_real_in(field, n);
        string a_string = field.substr(n, last - n);
        double f = atof(a_string.c_str());
        return parse_after_dur(dur * f, field, last, base);
    }
    if (field[n] == '+') {
        string a_string = field.substr(n + 1);
        return dur + parse_dur(
                a_string, seq->get_time_map()->beat_to_time(
                        seq->get_time_map()->time_to_beat(base) + dur));
    }
    parse_error(field, n, "Unexpected character in duration");
    return dur;
}
示例#4
0
#include "assert.h"
#include "stdlib.h"
#include "stdio.h"
#include "allegro.h"
#include "string.h"
#include "ctype.h"
//#include "memory.h"
#include "trace.h"
#include "strparse.h"
#ifndef EXPERIMENTAL_NOTE_TRACK
#include "allegrord.h"
#endif /* EXPERIMENTAL_NOTE_TRACK */
#define streql(s1, s2) (strcmp(s1, s2) == 0)
#define field_max 80


//Note that this is an #ifdef, not an #ifndef
#ifdef EXPERIMENTAL_NOTE_TRACK

class Alg_reader {
public:
    FILE *file;
    int line_no;
    String_parse line_parser;
    bool line_parser_flag;
    char field[field_max];
    bool error_flag;
    Alg_seq_ptr seq;
    double tsnum;
    double tsden;

    Alg_reader(FILE *a_file, Alg_seq_ptr new_seq);
    void readline();
    Alg_parameters_ptr process_attributes(Alg_parameters_ptr attributes, double time);
    bool parse();
    long parse_chan(char *field);
    long parse_int(char *field);
    int find_real_in(char *field, int n);
    double parse_real(char *field);
    void parse_error(char *field, long offset, char *message);
    double parse_dur(char *field, double base);
    double parse_after_dur(double dur, char *field, int n, double base);
    double parse_loud(char *field);
    long parse_key(char *field);
    double parse_pitch(char *field);
    long parse_after_key(int key, char *field, int n);
    long find_int_in(char *field, int n);
    bool parse_attribute(char *field, Alg_parameter_ptr parm);
    bool parse_val(Alg_parameter_ptr param, char *s, int i);
    bool check_type(char type_char, Alg_parameter_ptr param);
};

#endif /* EXPERIMENTAL_NOTE_TRACK */

void subseq(char *result, char *source, int from, int to)
{
    memcpy(result, source + from, to - from);
    result[to - from] = 0;
}

#ifndef EXPERIMENTAL_NOTE_TRACK
double Allegro_reader::parse_pitch(char *field)
#else /* EXPERIMENTAL_NOTE_TRACK */
double Alg_reader::parse_pitch(char *field)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    if (isdigit(field[1])) {
        char real_string[80];
        int last = find_real_in(field, 1);
        subseq(real_string, field, 1, last);
        return atof(real_string);
    } else {
        return (double) parse_key(field);
    }
}


#ifndef EXPERIMENTAL_NOTE_TRACK
Allegro_reader::Allegro_reader(FILE *a_file)
{
    file = a_file; // save the file
    line_parser_flag = false;
    line_no = 0;
    seq = Seq();
    tsnum = 4; // default time signature
    tsden = 4;
}
#else /* EXPERIMENTAL_NOTE_TRACK */
// it is the responsibility of the caller to delete
// the seq
Alg_reader::Alg_reader(FILE *a_file, Alg_seq_ptr new_seq)
{
    file = a_file; // save the file
    line_parser_flag = false;
    line_no = 0;
    tsnum = 4; // default time signature
    tsden = 4;
    seq = new_seq;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


//Note that this is an #ifdef, not an #ifndef
#ifdef EXPERIMENTAL_NOTE_TRACK
Alg_seq_ptr alg_read(FILE *file, Alg_seq_ptr new_seq)
    // read a sequence from allegro file
{
    if (!new_seq) new_seq = new Alg_seq();
    Alg_reader alg_reader(file, new_seq);
    alg_reader.parse();
    return alg_reader.seq;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


#ifndef EXPERIMENTAL_NOTE_TRACK
void Allegro_reader::readline()
#else /* EXPERIMENTAL_NOTE_TRACK */
void Alg_reader::readline()
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    char line[256];
    char *line_flag = fgets(line, 256, file);
    line_parser_flag = false;
    if (line_flag) {
        line_parser.init(line);
        line_parser_flag = true;
        error_flag = false;
    }
}


#ifndef EXPERIMENTAL_NOTE_TRACK
void Allegro_reader::process_attributes(Parameters_ptr attributes, double time)
{
    // print "process_attributes:", attributes
    bool ts_flag;
    if (attributes) {
        Parameters_ptr a;
        if (a = Parameters::remove_key(&attributes, "tempor")) {
            double tempo = a->parm.r;
            seq.insert_tempo(tempo, seq.map.time_to_beat(time));
        }
        if (a = Parameters::remove_key(&attributes, "beatr")) {
            double beat = a->parm.r;
            seq.insert_beat(time, beat);
        }
        if (a = Parameters::remove_key(&attributes, "tsnumr")) {
            tsnum = a->parm.r;
            ts_flag = true;
        }
        if (a = Parameters::remove_key(&attributes, "tsdenr")) {
            tsden = a->parm.r;
            ts_flag = true;
        }
        if (ts_flag) {
            seq.set_time_sig(seq.map.time_to_beat(time), tsnum, tsden);
        }
    }
}
#else /* EXPERIMENTAL_NOTE_TRACK */
Alg_parameters_ptr Alg_reader::process_attributes(
        Alg_parameters_ptr attributes, double time)
{
    // print "process_attributes:", attributes
    bool ts_flag = false;
    if (attributes) {
        Alg_parameters_ptr a;
        bool in_seconds = seq->get_units_are_seconds();
        if (a = Alg_parameters::remove_key(&attributes, "tempor")) {
            double tempo = a->parm.r;
            seq->insert_tempo(tempo, seq->get_time_map()->time_to_beat(time));
        }
        if (a = Alg_parameters::remove_key(&attributes, "beatr")) {
            double beat = a->parm.r;
            seq->insert_beat(time, beat);
        }
        if (a = Alg_parameters::remove_key(&attributes, "timesig_numr")) {
            tsnum = a->parm.r;
            ts_flag = true;
        }
        if (a = Alg_parameters::remove_key(&attributes, "timesig_denr")) {
            tsden = a->parm.r;
            ts_flag = true;
        }
        if (ts_flag) {
            seq->set_time_sig(seq->get_time_map()->time_to_beat(time),
            tsnum, tsden);
        }
        if (in_seconds) seq->convert_to_seconds();
    }
    return attributes; // in case it was modified
}
#endif /* EXPERIMENTAL_NOTE_TRACK */

#ifndef EXPERIMENTAL_NOTE_TRACK
bool Allegro_reader::parse()
{
    int voice = 0;
    int key = 60;
    double loud = 100.0;
    double pitch = 60.0;
    double dur = 1.0;
    double time = 0.0;
    readline();
    bool valid = false; // ignore blank lines
    while (line_parser_flag) {
        bool time_flag = false;
        bool next_flag = false;
        double next;
        bool voice_flag = false;
        bool loud_flag = false;
        bool dur_flag = false;
        bool new_pitch_flag = false; // "P" syntax
        double new_pitch = 0.0;
        bool new_key_flag = false;   // "K" syntax
        int new_key = 0;
        bool new_note_flag = false;  // "A"-"G" syntax
        int new_note = 0;
        Parameters_ptr attributes = NULL;
        line_parser.get_nonspace_quoted(field);
        char pk = line_parser.peek();
        if (pk && !isspace(pk)) {
            line_parser.get_nonspace_quoted(field + strlen(field));
        }
        while (field[0]) {
            // print "field", "|";field;"|", "|";line_parser.string;"|", line_parser.pos
            char first = toupper(field[0]);
            if (strchr("ABCDEFGKLPUSIQHW-", first)) {
                valid = true; // it's a note or event
            }
            if (first == 'V') {
                if (voice_flag) {
                    parse_error(field, 0, "Voice specified twice");
                } else {
                    voice = parse_int(field);
                }
                voice_flag = true;
            } else if (first == 'T') {
                if (time_flag) {
                    parse_error(field, 0, "Time specified twice");
                } else {
                    time = parse_dur(field, 0.0);
                }
                time_flag = true;
            } else if (first == 'N') {
                if (next_flag) {
                    parse_error(field, 0, "Next specified twice");
                } else {
                    next = parse_dur(field, time);
                }
                next_flag = true;
            } else if (first == 'K') {
                if (new_key_flag) {
                    parse_error(field, 0, "Key specified twice");
                } else {
                    new_key = parse_key(field);
                    new_key_flag = true;
                }
            } else if (first == 'L') {
                if (loud_flag) {
                    parse_error(field, 0, "Loudness specified twice");
                } else {
                    loud = parse_loud(field);
                }
                loud_flag = true;
            } else if (first == 'P') {
                if (new_note_flag || new_pitch_flag) {
                    parse_error(field, 0, "Pitch specified twice");
                } else {
                    new_pitch = parse_pitch(field);
                    new_pitch_flag = true;
                }
            } else if (first == 'U') {
                if (dur_flag) {
                    parse_error(field, 0, "Dur specified twice");
                } else {
                    dur = parse_dur(field, time);
                    dur_flag = true;
                }
            } else if (strchr("SIQHW", first)) {
                if (dur_flag) {
                    parse_error(field, 0, "Dur specified twice");
                } else {
                    // prepend 'U' to field, copy EOS too
                    memmove(field + 1, field, strlen(field) + 1);
                    field[0] = 'U';
                    dur = parse_dur(field, time);
                    dur_flag = true;
                }
            } else if (strchr("ABCDEFG", first)) {
                if (new_note_flag || new_pitch_flag) {
                    parse_error(field, 0, "Pitch specified twice");
                } else {
                    // prepend 'K' to field, copy EOS too
                    memmove(field + 1, field, strlen(field) + 1);
                    field[0] = 'K';
                    new_note = parse_key(field);
                    new_note_flag = true;
                }
            } else if (first == '-') {
                Parameter parm;
                if (parse_attribute(field, &parm)) { // enter attribute-value pair
                    attributes = new Parameters(attributes);
                    attributes->parm = parm;
                    parm.s = NULL; // protect string from deletion by destructor
                }
            } else {
                parse_error(field, 0, "Unknown field");
            }

            if (error_flag) {
                field[0] = 0; // exit the loop
            } else {
                line_parser.get_nonspace_quoted(field);
                pk = line_parser.peek();
                if (pk && !isspace(pk)) {
                    line_parser.get_nonspace_quoted(field + strlen(field));
                }
            }
        }
        // a case analysis:
        // Key < 128 counts as both key and pitch
        // A-G implies pitch AND key unless key given too
        //   K60 P60 -- both are specified, use 'em
        //   K60 P60 C4 -- overconstrained, an error
        //   K60 C4 -- overconstrained
        //   K60 -- OK, pitch is 60
        //   C4 P60 -- over constrained
        //   P60 -- OK, key is from before, pitch is 60
        //   C4 -- OK, key is 60, pitch is 60
        //   <nothing> -- OK, key and pitch from before
        //   K200 with P60 ok, pitch is 60
        //   K200 with neither P60 nor C4 uses 
        //       pitch from before
        // figure out what the key/instance is:
        if (new_key_flag) { // it was directly specified
            key = new_key;
            if (key < 128 && new_note_flag) {
                parse_error("", 0, "Pitch specified twice");
            }
        } else if (new_note_flag) { // "A"-"G" used
            key = new_note;
        }
        if (new_pitch_flag) {
            pitch = new_pitch;
        } else if (key < 128) {
            pitch = key;
        }
        // now we've acquired new parameters
        // if (it is a note, then enter the note
        if (valid) {
            // change tempo or beat
            process_attributes(attributes, time);
            // if there's a duration or pitch, make a note:
            if (new_pitch_flag || dur_flag || new_note_flag) {
                new_key_flag = false;
                new_pitch_flag = false;
                Allegro_note_ptr note_ptr = new Allegro_note;
                note_ptr->chan = voice;
                note_ptr->time = time;
                note_ptr->dur = dur;
                note_ptr->key = key;
                note_ptr->pitch = pitch;
                note_ptr->loud = loud;
                note_ptr->parameters = attributes;
                seq.add_event(note_ptr); // sort later
            } else {
                int update_key = -1;
                // key or pitch must appear explicitly; otherwise
                //    update applies to channel
                if (new_key_flag || new_pitch_flag) {
                    update_key = key;
                }
                if (loud_flag) {
                    Allegro_update_ptr new_upd = new Allegro_update;
                    new_upd->chan = voice;
                    new_upd->time = time;
                    new_upd->key = update_key;
                    new_upd->parameter.set_attr(symbol_table.insert_string("loudr"));
                    new_upd->parameter.r = pitch;
                    seq.add_event(new_upd);
                }
                if (attributes) {
                    while (attributes) {
                        Allegro_update_ptr new_upd = new Allegro_update;
                        new_upd->chan = voice;
                        new_upd->time = time;
                        new_upd->key = update_key;
                        new_upd->parameter = attributes->parm;
                        seq.add_event(new_upd);
                        Parameters_ptr p = attributes;
                        attributes = attributes->next;
                        delete p;
                    }
                }
            }
            if (next_flag) {
                time = time + next;
            } else if (dur_flag) {
                time = time + dur;
            }
        }
        readline();
    }
    //print "Finished reading score"
    if (!error_flag) {
        seq.convert_to_seconds(); // make sure format is correct
        // seq.notes.sort('event_greater_than');
    }
    // print "parse returns error_flag", error_flag
    return error_flag;
}
#else /* EXPERIMENTAL_NOTE_TRACK */
bool Alg_reader::parse()
{
    int voice = 0;
    int key = 60;
    double loud = 100.0;
    double pitch = 60.0;
    double dur = 1.0;
    double time = 0.0;
    int track_num = 0;
    seq->convert_to_seconds();
    //seq->set_real_dur(0.0); // just in case it's not initialized already
    readline();
    bool valid = false; // ignore blank lines
    while (line_parser_flag) {
        bool time_flag = false;
        bool next_flag = false;
        double next;
        bool voice_flag = false;
        bool loud_flag = false;
        bool dur_flag = false;
        bool new_pitch_flag = false; // "P" syntax
        double new_pitch = 0.0;
        bool new_key_flag = false;   // "K" syntax
        int new_key = 0;
        bool new_note_flag = false;  // "A"-"G" syntax
        int new_note = 0;
        Alg_parameters_ptr attributes = NULL;
        if (line_parser.peek() == '#') {
            // look for #track
            line_parser.get_nonspace_quoted(field);
            if (streql(field, "#track")) {
                line_parser.get_nonspace_quoted(field); // number
                track_num = parse_int(field - 1);
                seq->add_track(track_num);
            }
            // maybe we have a comment
        } else {
            // we must have a track to insert into
            if (seq->tracks() == 0) seq->add_track(0);
            line_parser.get_nonspace_quoted(field);
            char pk = line_parser.peek();
            // attributes are parsed as two adjacent nonspace_quoted tokens
            // so we have to conditionally call get_nonspace_quoted() again
            if (pk && !isspace(pk)) {
                line_parser.get_nonspace_quoted(field + strlen(field));
            }
            while (field[0]) {
                char first = toupper(field[0]);
                if (strchr("ABCDEFGKLPUSIQHW-", first)) {
                    valid = true; // it's a note or event
                }
                if (first == 'V') {
                    if (voice_flag) {
                        parse_error(field, 0, "Voice specified twice");
                    } else {
                        voice = parse_chan(field);
                    }
                    voice_flag = true;
                } else if (first == 'T') {
                    if (time_flag) {
                        parse_error(field, 0, "Time specified twice");
                    } else {
                        time = parse_dur(field, 0.0);
                    }
                    time_flag = true;
                } else if (first == 'N') {
                    if (next_flag) {
                        parse_error(field, 0, "Next specified twice");
                    } else {
                        next = parse_dur(field, time);
                    }
                    next_flag = true;
                } else if (first == 'K') {
                    if (new_key_flag) {
                        parse_error(field, 0, "Key specified twice");
                    } else {
                        new_key = parse_key(field);
                        new_key_flag = true;
                    }
                } else if (first == 'L') {
                    if (loud_flag) {
                        parse_error(field, 0, "Loudness specified twice");
                    } else {
                        loud = parse_loud(field);
                    }
                    loud_flag = true;
                } else if (first == 'P') {
                    if (new_note_flag || new_pitch_flag) {
                        parse_error(field, 0, "Pitch specified twice");
                    } else {
                        new_pitch = parse_pitch(field);
                        new_pitch_flag = true;
                    }
                } else if (first == 'U') {
                    if (dur_flag) {
                        parse_error(field, 0, "Dur specified twice");
                    } else {
                        dur = parse_dur(field, time);
                        dur_flag = true;
                    }
                } else if (strchr("SIQHW", first)) {
                    if (dur_flag) {
                        parse_error(field, 0, "Dur specified twice");
                    } else {
                        // prepend 'U' to field, copy EOS too
                        memmove(field + 1, field, strlen(field) + 1);
                        field[0] = 'U';
                        dur = parse_dur(field, time);
                        dur_flag = true;
                    }
                } else if (strchr("ABCDEFG", first)) {
                    if (new_note_flag || new_pitch_flag) {
                        parse_error(field, 0, "Pitch specified twice");
                    } else {
                        // prepend 'K' to field, copy EOS too
                        memmove(field + 1, field, strlen(field) + 1);
                        field[0] = 'K';
                        new_note = parse_key(field);
                        new_note_flag = true;
                    }
                } else if (first == '-') {
                    Alg_parameter parm;
                    if (parse_attribute(field, &parm)) { // enter attribute-value pair
                        attributes = new Alg_parameters(attributes);
                        attributes->parm = parm;
                        parm.s = NULL; // protect string from deletion by destructor
                    }
                } else {
                    parse_error(field, 0, "Unknown field");
                }

                if (error_flag) {
                    field[0] = 0; // exit the loop
                } else {
                    line_parser.get_nonspace_quoted(field);
                    pk = line_parser.peek();
                    // attributes are parsed as two adjacent nonspace_quoted 
                    // tokens so we have to conditionally call 
                    // get_nonspace_quoted() again
                    if (pk && !isspace(pk)) {
                        line_parser.get_nonspace_quoted(field + strlen(field));
                    }
                }
            }
            // a case analysis:
            // Key < 128 counts as both key and pitch
            // A-G implies pitch AND key unless key given too
            //   K60 P60 -- both are specified, use 'em
            //   K60 P60 C4 -- overconstrained, an error
            //   K60 C4 -- overconstrained
            //   K60 -- OK, pitch is 60
            //   C4 P60 -- over constrained
            //   P60 -- OK, key is from before, pitch is 60
            //   C4 -- OK, key is 60, pitch is 60
            //   <nothing> -- OK, key and pitch from before
            //   K200 with P60 ok, pitch is 60
            //   K200 with neither P60 nor C4 uses 
            //       pitch from before
            // figure out what the key/instance is:
            if (new_key_flag) { // it was directly specified
                key = new_key;
                if (key < 128 && new_note_flag) {
                    parse_error("", 0, "Pitch specified twice");
                }
            } else if (new_note_flag) { // "A"-"G" used
                key = new_note;
            }
            if (new_pitch_flag) {
                pitch = new_pitch;
            } else if (key < 128) {
                pitch = key;
            }
            // now we've acquired new parameters
            // if (it is a note, then enter the note
            if (valid) {
                // change tempo or beat
                attributes = process_attributes(attributes, time);
                // if there's a duration or pitch, make a note:
                if (new_pitch_flag || dur_flag || new_note_flag) {
                    new_key_flag = false;
                    new_pitch_flag = false;
                    Alg_note_ptr note_ptr = new Alg_note;
                    note_ptr->chan = voice;
                    note_ptr->time = time;
                    note_ptr->dur = dur;
                    note_ptr->set_identifier(key);
                    note_ptr->pitch = pitch;
                    note_ptr->loud = loud;
                    note_ptr->parameters = attributes;
                    seq->add_event(note_ptr, track_num); // sort later
                    if (seq->get_real_dur() < (time + dur)) seq->set_real_dur(time + dur);
                } else {
                    int update_key = -1;
                    // key or pitch must appear explicitly; otherwise
                    //    update applies to channel
                    if (new_key_flag || new_pitch_flag) {
                        update_key = key;
                    }
                    if (loud_flag) {
                        Alg_update_ptr new_upd = new Alg_update;
                        new_upd->chan = voice;
                        new_upd->time = time;
                        new_upd->set_identifier(update_key);
                        new_upd->parameter.set_attr(symbol_table.insert_string("loudr"));
                        new_upd->parameter.r = pitch;
                        seq->add_event(new_upd, track_num);
                        if (seq->get_real_dur() < time) seq->set_real_dur(time);
                    }
                    if (attributes) {
                        while (attributes) {
                            Alg_update_ptr new_upd = new Alg_update;
                            new_upd->chan = voice;
                            new_upd->time = time;
                            new_upd->set_identifier(update_key);
                            new_upd->parameter = attributes->parm;
                            seq->add_event(new_upd, track_num);
                            Alg_parameters_ptr p = attributes;
                            attributes = attributes->next;
                            p->parm.s = NULL; // so we don't delete the string
                            delete p;
                        }
                    }
                }
                if (next_flag) {
                    time = time + next;
                } else if (dur_flag) {
                    time = time + dur;
                }
            }
        }
        readline();
    }
    //print "Finished reading score"
    if (!error_flag) {
        seq->convert_to_seconds(); // make sure format is correct
        // seq->notes.sort('event_greater_than');
    }
    // real_dur is valid, translate to beat_dur
    seq->set_beat_dur((seq->get_time_map())->time_to_beat(seq->get_real_dur()));
    // print "parse returns error_flag", error_flag
    return error_flag;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


//Note that this is an #ifdef, not an #ifndef
#ifdef EXPERIMENTAL_NOTE_TRACK
long Alg_reader::parse_chan(char *field)
{
    char *int_string = field + 1;
    char *msg = "Integer or - expected";
    char *p = int_string;
    char c;
    // check that all chars in int_string are digits or '-':
    while (c = *p++) {
        if (!isdigit(c) && c != '-') {
            parse_error(field, p - field - 1, msg);
            return 0;
        }
    }
    p--; // p now points to end-of-string character
    if (p - int_string == 0) {
        // bad: string length is zero
        parse_error(field, 1, msg);
        return 0;
    }
    if (p - int_string == 1 && int_string[0] == '-') {
        // special case: entire string is "-", interpret as -1
        return -1;
    }
    return atoi(int_string);
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


#ifndef EXPERIMENTAL_NOTE_TRACK
long Allegro_reader::parse_int(char *field)
{
    char int_string[field_max];
    strcpy(int_string, field + 1);
    char *msg = "Integer expected";
    char *p = int_string;
    char c;
    while (c = *p++) {
        if (!isdigit(c)) {
            parse_error(field, 1, msg);
            return 0;
        }
    }
    if (strlen(int_string) < 1) {
        parse_error(field, 1, msg);
        return 0;
    }
    return atoi(int_string);
}
#else /* EXPERIMENTAL_NOTE_TRACK */
long Alg_reader::parse_int(char *field)
{
    char *int_string = field + 1;
    char *msg = "Integer expected";
    char *p = int_string;
    char c;
    // check that all chars in int_string are digits:
    while (c = *p++) {
        if (!isdigit(c)) {
            parse_error(field, p - field - 1, msg);
            return 0;
        }
    }
    p--; // p now points to end-of-string character
    if (p - int_string == 0) {
        // bad: string length is zero
        parse_error(field, 1, msg);
        return 0;
    }
    return atoi(int_string);
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


#ifndef EXPERIMENTAL_NOTE_TRACK
int Allegro_reader::find_real_in(char *field, int n)
#else /* EXPERIMENTAL_NOTE_TRACK */
int Alg_reader::find_real_in(char *field, int n)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    // scans from offset n to the end of a real constant
    bool decimal = false;
    int len = strlen(field);
    for (int i = n; i < len; i++) {
        char c = field[i];
        if (!isdigit(c)) {
            if (c == '.' && !decimal) {
                decimal = true;
            } else {
                return i;
            }
        }
    }
    return strlen(field);
}


#ifndef EXPERIMENTAL_NOTE_TRACK
double Allegro_reader::parse_real(char *field)
#else /* EXPERIMENTAL_NOTE_TRACK */
double Alg_reader::parse_real(char *field)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    char real_string[80];
    char *msg = "Real expected";
    bool decimal = false;
    int last = find_real_in(field, 1);
    subseq(real_string, field, 1, last);
    if (last <= 1 || last < (int) strlen(field)) {
       parse_error(field, 1, msg);
       return 0;
    }
    return atof(real_string);
}


#ifndef EXPERIMENTAL_NOTE_TRACK
void Allegro_reader::parse_error(char *field, long offset, char *message)
#else /* EXPERIMENTAL_NOTE_TRACK */
void Alg_reader::parse_error(char *field, long offset, char *message)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    int position = line_parser.pos - strlen(field) + offset;
    error_flag = true;
    puts(line_parser.string);
    for (int i = 0; i < position; i++) {
        putc(' ', stdout);
    }
    putc('^', stdout);
    printf("    %s\n", message);
}


double duration_lookup[] = { 0.25, 0.5, 1.0, 2.0, 4.0 };


#ifndef EXPERIMENTAL_NOTE_TRACK
double Allegro_reader::parse_dur(char *field, double base)
{
    char *msg = "Duration expected";
    char real_string[80];
    char *durs = "SIQHW";
    char *p;
    int last;
    double dur;
    if (strlen(field) < 2) {
        // fall through to error message
        return -1;
    } else if (isdigit(field[1])) {
        last = find_real_in(field, 1);
        subseq(real_string, field, 1, last);
        dur = atof(real_string);
        // convert dur from seconds to beats
        dur = seq.map.time_to_beat(base + dur) - seq.map.time_to_beat(base);
    } else if (p = strchr(durs, field[1])) {
        dur = duration_lookup[p - durs];
        last = 2;
    } else {
        parse_error(field, 1, msg);
        return 0;
    }
    dur = parse_after_dur(dur, field, last, base);
    dur = seq.map.beat_to_time(seq.map.time_to_beat(base) + dur) - base;
    return dur;
}
#else /* EXPERIMENTAL_NOTE_TRACK */
double Alg_reader::parse_dur(char *field, double base)
{
    char *msg = "Duration expected";
    char real_string[80];
    char *durs = "SIQHW";
    char *p;
    int last;
    double dur;
    if (strlen(field) < 2) {
        // fall through to error message
        return -1;
    } else if (isdigit(field[1])) {
        last = find_real_in(field, 1);
        subseq(real_string, field, 1, last);
        dur = atof(real_string);
        // convert dur from seconds to beats
        dur = seq->get_time_map()->time_to_beat(base + dur) - 
              seq->get_time_map()->time_to_beat(base);
    } else if (p = strchr(durs, field[1])) {
        dur = duration_lookup[p - durs];
        last = 2;
    } else {
        parse_error(field, 1, msg);
        return 0;
    }
    dur = parse_after_dur(dur, field, last, base);
    dur = seq->get_time_map()->beat_to_time(
              seq->get_time_map()->time_to_beat(base) + dur) - base;
    return dur;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


#ifndef EXPERIMENTAL_NOTE_TRACK
double Allegro_reader::parse_after_dur(double dur, char *field, int n, double base)
{
    char a_string[80];
    if ((int) strlen(field) == n) {
        return dur;
    }
    if (field[n] == 'T') {
        return parse_after_dur(dur * 2/3, field, n + 1, base);
    }
    if (field[n] == '.') {
        return parse_after_dur(dur * 1.5, field, n + 1, base);
    }
    if (isdigit(field[n])) {
        int last = find_real_in(field, n);
        subseq(a_string, field, n, last);
        double f = atof(a_string);
        return parse_after_dur(dur * f, field, last, base);
    }
    if (field[n] == '+') {
        subseq(a_string, field, n + 1, -1);
        return dur + parse_dur(a_string, 
                               seq.map.beat_to_time(
                                   seq.map.time_to_beat(base) + dur));
    }
    parse_error(field, n, "Unexpected character in duration");
    return dur;
}
#else /* EXPERIMENTAL_NOTE_TRACK */
double Alg_reader::parse_after_dur(double dur, char *field, int n, double base)
{
    char a_string[80];
    if ((int) strlen(field) == n) {
        return dur;
    }
    if (field[n] == 'T') {
        return parse_after_dur(dur * 2/3, field, n + 1, base);
    }
    if (field[n] == '.') {
        return parse_after_dur(dur * 1.5, field, n + 1, base);
    }
    if (isdigit(field[n])) {
        int last = find_real_in(field, n);
        subseq(a_string, field, n, last);
        double f = atof(a_string);
        return parse_after_dur(dur * f, field, last, base);
    }
    if (field[n] == '+') {
        subseq(a_string, field, n + 1, -1);
        return dur + parse_dur(
                a_string, seq->get_time_map()->beat_to_time(
                              seq->get_time_map()->time_to_beat(base) + dur));
    }
    parse_error(field, n, "Unexpected character in duration");
    return dur;
}
示例#5
0
bool Alg_midifile_reader::parse()
{
    channel_offset = 0;
    seq->convert_to_beats();
    midifile();
    seq->set_real_dur(seq->get_time_map()->beat_to_time(seq->get_beat_dur()));
    return midifile_error != 0;
}
示例#6
0
void Alg_midifile_reader::Mf_endtrack()
{
    // note: track is already part of seq, so do not add it here
    // printf("finished track, length %d number %d\n", track->len, track_num / 100);
    channel_offset += seq->channel_offset_per_track;
    track = NULL;
    double now = get_time();
    if (seq->get_beat_dur() < now) seq->set_beat_dur(now);
    meta_channel = -1;
    port = 0;
}
示例#7
0
void Alg_midifile_reader::Mf_starttrack()
{
    // printf("starting new track\n");
    // create a new track that will share the sequence time map
    // since time is in beats, the seconds parameter is false
    track_number++;
    seq->add_track(track_number); // make sure track exists
    track = seq->track(track_number); // keep pointer to current track
    meta_channel = -1;
    port = 0;
}
示例#8
0
void Alg_midifile_reader::Mf_tempo(int tempo)
{
    double beat = get_currtime();
    beat = beat / divisions; // convert to quarters
    // 6000000 us/min / n us/beat => beat / min
    double bpm = 60000000.0 / tempo;
    seq->insert_tempo(bpm, beat);
}
示例#9
0
void NoteTrack::SetSequence(Alg_seq_ptr seq)
{
    if (mSeq)
        delete mSeq;

    mSeq = seq;
    mLen = (seq ? seq->get_real_dur() : 0.0);
}
示例#10
0
Alg_parameters_ptr Alg_reader::process_attributes(
        Alg_parameters_ptr attributes, double time)
{
    // print "process_attributes:", attributes
    bool ts_flag = false;
    if (attributes) {
        Alg_parameters_ptr a;
        bool in_seconds = seq->get_units_are_seconds();
        if (a = Alg_parameters::remove_key(&attributes, "tempor")) {
            double tempo = a->parm.r;
            seq->insert_tempo(tempo, seq->get_time_map()->time_to_beat(time));
        }
        if (a = Alg_parameters::remove_key(&attributes, "beatr")) {
            double beat = a->parm.r;
            seq->insert_beat(time, beat);
        }
        if (a = Alg_parameters::remove_key(&attributes, "timesig_numr")) {
            tsnum = a->parm.r;
            ts_flag = true;
        }
        if (a = Alg_parameters::remove_key(&attributes, "timesig_denr")) {
            tsden = a->parm.r;
            ts_flag = true;
        }
        if (ts_flag) {
            seq->set_time_sig(seq->get_time_map()->time_to_beat(time),
            tsnum, tsden);
        }
        if (in_seconds) seq->convert_to_seconds();
    }
    return attributes; // in case it was modified
}
示例#11
0
void Alg_midifile_reader::Mf_on(int chan, int key, int vel)
{
    assert(!seq->get_units_are_seconds());
    if (vel == 0) {
        Mf_off(chan, key, vel);
        return;
    }
    Alg_note_ptr note = new Alg_note();
    note_list = new Alg_note_list(note, note_list);
    /*    trace("on: %d at %g\n", key, get_time()); */
    note->time = get_time();
    note->chan = chan + channel_offset + port * channel_offset_per_port;
    note->dur = 0;
    note->set_identifier(key);
    note->pitch = (float) key;
    note->loud = (float) vel;
    track->append(note);
    meta_channel = -1;
}
示例#12
0
Alg_seq_ptr NoteTrack::MakeExportableSeq()
{
   double offset = GetOffset();
   if (offset == 0)
      return mSeq;
   // make a copy, deleting events that are shifted before time 0
   double start = -offset;
   if (start < 0) start = 0;
   // notes that begin before "start" are not included even if they
   // extend past "start" (because "all" parameter is set to false)
   Alg_seq_ptr seq = mSeq->copy(start, mSeq->get_dur() - start, false);
   if (offset > 0) {
      // swap seq and mSeq so that Shift operates on the new copy
      Alg_seq_ptr old_seq = mSeq;
      mSeq = seq;
      Shift(offset);
      seq = mSeq;  // undo the swap
      mSeq = old_seq;
#ifdef OLD_CODE
      // now shift events by offset. This must be done with an integer
      // number of measures, so first, find the beats-per-measure
      double beats_per_measure = 4.0;
      Alg_time_sig_ptr tsp = NULL;
      if (seq->time_sig.length() > 0 && seq->time_sig[0].beat < ALG_EPS) {
         // there is an initial time signature
         tsp = &(seq->time_sig[0]);
         beats_per_measure = (tsp->num * 4) / tsp->den;
      }
      // also need the initial tempo
      double bps = ALG_DEFAULT_BPM / 60;
      Alg_time_map_ptr map = seq->get_time_map();
      Alg_beat_ptr bp = &(map->beats[0]);
      if (bp->time < ALG_EPS) { // tempo change at time 0
         if (map->beats.len > 1) { // compute slope to get tempo
            bps = (map->beats[1].beat - map->beats[0].beat) /
                  (map->beats[1].time - map->beats[0].time);
         } else if (seq->get_time_map()->last_tempo_flag) {
            bps = seq->get_time_map()->last_tempo;
         }
      }
      // find closest number of measures to fit in the gap
      // number of measures is offset / measure_time
      double measure_time = beats_per_measure / bps; // seconds per measure
      int n = ROUND(offset / measure_time);
      if (n == 0) n = 1;
      // we will insert n measures. Compute the desired duration of each.
      measure_time = offset / n;
      bps = beats_per_measure / measure_time;
      // insert integer multiple of measures at beginning
      seq->convert_to_beats();
      seq->insert_silence(0, beats_per_measure * n);
      // make sure time signature at 0 is correct
      if (tsp) {
         seq->set_time_sig(0, tsp->num, tsp->den);
      }
      // adjust tempo to match offset
      seq->set_tempo(bps * 60.0, 0, beats_per_measure * n);
#endif
   } else {
      // if offset is negative, it might not be a multiple of beats, but
      // we want to preserve the relative positions of measures. I.e. we
      // should shift barlines and time signatures as well as notes.
      // Insert a time signature at the first bar-line if necessary.

      // Translate start from seconds to beats and call it beat:
      double beat = mSeq->get_time_map()->time_to_beat(start);
      // Find the time signature in mSeq in effect at start (beat):
      int i = mSeq->time_sig.find_beat(beat);
      // i is where you would insert a new time sig at beat,
      // Case 1: beat coincides with a time sig at i. Time signature
      // at beat means that there is a barline at beat, so when beat
      // is shifted to 0, the relative barline positions are preserved
      if (mSeq->time_sig.length() > 0 &&
          within(beat, mSeq->time_sig[i].beat, ALG_EPS)) {
         // beat coincides with time signature change, so offset must
         // be a multiple of beats
         /* do nothing */ ;
      // Case 2: there is no time signature before beat.
      } else if (i == 0 && (mSeq->time_sig.length() == 0 ||
                            mSeq->time_sig[i].beat > beat)) {
         // If beat does not fall on an implied barline, we need to
         // insert a time signature.
         double measures = beat / 4.0;
         double imeasures = ROUND(measures);
         if (!within(measures, imeasures, ALG_EPS)) {
            double bar_offset = (int(measures) + 1) * 4.0 - beat;
            seq->set_time_sig(bar_offset, 4, 4);
         }
      // This case should never be true because if i == 0, either there
      // are no time signatures before beat (Case 2),
      // or there is one time signature at beat (Case 1)
      } else if (i == 0) {
         /* do nothing (might be good to assert(false)) */ ;
      // Case 3: i-1 must be the effective time sig position
      } else {
         i -= 1; // index the time signature in effect at beat
         Alg_time_sig_ptr tsp = &(mSeq->time_sig[i]);
         double beats_per_measure = (tsp->num * 4) / tsp->den;
         double measures = (beat - tsp->beat) / beats_per_measure;
         int imeasures = ROUND(measures);
         if (!within(measures, imeasures, ALG_EPS)) {
            // beat is not on a measure, so we need to insert a time sig
            // to force a bar line at the first measure location after
            // beat
            double bar = tsp->beat + beats_per_measure * (int(measures) + 1);
            double bar_offset = bar - beat;
            // insert new time signature at bar_offset in new sequence
            // It will have the same time signature, but the position will
            // force a barline to match the barlines in mSeq
            seq->set_time_sig(bar_offset, tsp->num, tsp->den);
         }
         // else beat coincides with a barline, so no need for an extra
         // time signature to force barline alignment
      }
   }
   return seq;
}
示例#13
0
void Alg_midifile_reader::Mf_timesig(int i1, int i2, int i3, int i4)
{
    seq->set_time_sig(double(get_currtime()) / divisions, i1, 1 << i2);
}
示例#14
0
bool MidiImport::readSMF( TrackContainer* tc )
{
	QString filename = file().fileName();
	closeFile();

	const int preTrackSteps = 2;
	QProgressDialog pd( TrackContainer::tr( "Importing MIDI-file..." ),
	TrackContainer::tr( "Cancel" ), 0, preTrackSteps, gui->mainWindow() );
	pd.setWindowTitle( TrackContainer::tr( "Please wait..." ) );
	pd.setWindowModality(Qt::WindowModal);
	pd.setMinimumDuration( 0 );

	pd.setValue( 0 );

	Alg_seq_ptr seq = new Alg_seq(filename.toLocal8Bit(), true);
	seq->convert_to_beats();

	pd.setMaximum( seq->tracks()  + preTrackSteps );
	pd.setValue( 1 );
	
	// 128 CC + Pitch Bend
	smfMidiCC ccs[129];
	smfMidiChannel chs[256];

	MeterModel & timeSigMM = Engine::getSong()->getTimeSigModel();
	AutomationPattern * timeSigNumeratorPat = 
		AutomationPattern::globalAutomationPattern( &timeSigMM.numeratorModel() );
	AutomationPattern * timeSigDenominatorPat = 
		AutomationPattern::globalAutomationPattern( &timeSigMM.denominatorModel() );
	
	// TODO: adjust these to Time.Sig changes
	double beatsPerTact = 4; 
	double ticksPerBeat = DefaultTicksPerTact / beatsPerTact;

	// Time-sig changes
	Alg_time_sigs * timeSigs = &seq->time_sig;
	for( int s = 0; s < timeSigs->length(); ++s )
	{
		Alg_time_sig timeSig = (*timeSigs)[s];
		// Initial timeSig, set song-default value
		if(/* timeSig.beat == 0*/ true )
		{
			// TODO set song-global default value
			printf("Another timesig at %f\n", timeSig.beat);
			timeSigNumeratorPat->putValue( timeSig.beat*ticksPerBeat, timeSig.num );
			timeSigDenominatorPat->putValue( timeSig.beat*ticksPerBeat, timeSig.den );
		}
		else
		{
		}

	}

	pd.setValue( 2 );

	// Tempo stuff
	AutomationPattern * tap = tc->tempoAutomationPattern();
	if( tap )
	{
		tap->clear();
		Alg_time_map * timeMap = seq->get_time_map();
		Alg_beats & beats = timeMap->beats;
		for( int i = 0; i < beats.len - 1; i++ )
		{
			Alg_beat_ptr b = &(beats[i]);
			double tempo = ( beats[i + 1].beat - b->beat ) /
						   ( beats[i + 1].time - beats[i].time );
			tap->putValue( b->beat * ticksPerBeat, tempo * 60.0 );
		}
		if( timeMap->last_tempo_flag )
		{
			Alg_beat_ptr b = &( beats[beats.len - 1] );
			tap->putValue( b->beat * ticksPerBeat, timeMap->last_tempo * 60.0 );
		}
	}

	// Song events
	for( int e = 0; e < seq->length(); ++e )
	{
		Alg_event_ptr evt = (*seq)[e];

		if( evt->is_update() )
		{
			printf("Unhandled SONG update: %d %f %s\n", 
					evt->get_type_code(), evt->time, evt->get_attribute() );
		}
	}

	// Tracks
	for( int t = 0; t < seq->tracks(); ++t )
	{
		QString trackName = QString( tr( "Track" ) + " %1" ).arg( t );
		Alg_track_ptr trk = seq->track( t );
		pd.setValue( t + preTrackSteps );

		for( int c = 0; c < 129; c++ )
		{
			ccs[c].clear();
		}

		// Now look at events
		for( int e = 0; e < trk->length(); ++e )
		{
			Alg_event_ptr evt = (*trk)[e];

			if( evt->chan == -1 )
			{
				bool handled = false;
                if( evt->is_update() )
				{
					QString attr = evt->get_attribute();
                    if( attr == "tracknames" && evt->get_update_type() == 's' ) {
						trackName = evt->get_string_value();
						handled = true;
					}
				}
                if( !handled ) {
                    // Write debug output
                    printf("MISSING GLOBAL HANDLER\n");
                    printf("     Chn: %d, Type Code: %d, Time: %f", (int) evt->chan,
                           evt->get_type_code(), evt->time );
                    if ( evt->is_update() )
                    {
                        printf( ", Update Type: %s", evt->get_attribute() );
                        if ( evt->get_update_type() == 'a' )
                        {
                            printf( ", Atom: %s", evt->get_atom_value() );
                        }
                    }
                    printf( "\n" );
				}
			}
			else if( evt->is_note() && evt->chan < 256 )
			{
				smfMidiChannel * ch = chs[evt->chan].create( tc, trackName );
				Alg_note_ptr noteEvt = dynamic_cast<Alg_note_ptr>( evt );
				int ticks = noteEvt->get_duration() * ticksPerBeat;
				Note n( (ticks < 1 ? 1 : ticks ),
						noteEvt->get_start_time() * ticksPerBeat,
						noteEvt->get_identifier() - 12,
						noteEvt->get_loud());
				ch->addNote( n );
				
			}
			
			else if( evt->is_update() )
			{
				smfMidiChannel * ch = chs[evt->chan].create( tc, trackName );

				double time = evt->time*ticksPerBeat;
				QString update( evt->get_attribute() );

				if( update == "programi" )
				{
					long prog = evt->get_integer_value();
					if( ch->isSF2 )
					{
						ch->it_inst->childModel( "bank" )->setValue( 0 );
						ch->it_inst->childModel( "patch" )->setValue( prog );
					}
					else {
						const QString num = QString::number( prog );
						const QString filter = QString().fill( '0', 3 - num.length() ) + num + "*.pat";
						const QString dir = "/usr/share/midi/"
								"freepats/Tone_000/";
						const QStringList files = QDir( dir ).
						entryList( QStringList( filter ) );
						if( ch->it_inst && !files.empty() )
						{
							ch->it_inst->loadFile( dir+files.front() );
						}
					}
				}

				else if( update.startsWith( "control" ) || update == "bendr" )
				{
					int ccid = update.mid( 7, update.length()-8 ).toInt();
					if( update == "bendr" )
					{
						ccid = 128;
					}
					if( ccid <= 128 )
					{
						double cc = evt->get_real_value();
						AutomatableModel * objModel = NULL;

						switch( ccid ) 
						{
							case 0:
								if( ch->isSF2 && ch->it_inst )
								{
									objModel = ch->it_inst->childModel( "bank" );
									printf("BANK SELECT %f %d\n", cc, (int)(cc*127.0));
									cc *= 127.0f;
								}
								break;

							case 7:
								objModel = ch->it->volumeModel();
								cc *= 100.0f;
								break;

							case 10:
								objModel = ch->it->panningModel();
								cc = cc * 200.f - 100.0f;
								break;

							case 128:
								objModel = ch->it->pitchModel();
								cc = cc * 100.0f;
								break;
							default:
								//TODO: something useful for other CCs
								break;
						}

						if( objModel )
						{
							if( time == 0 && objModel )
							{
								objModel->setInitValue( cc );
							}
							else
							{
								if( ccs[ccid].at == NULL ) {
									ccs[ccid].create( tc, trackName + " > " + (
										  objModel != NULL ? 
										  objModel->displayName() : 
										  QString("CC %1").arg(ccid) ) );
								}
								ccs[ccid].putValue( time, objModel, cc );
							}
						}
					}
				}
				else {
					printf("Unhandled update: %d %d %f %s\n", (int) evt->chan, 
							evt->get_type_code(), evt->time, evt->get_attribute() );
				}
			}
		}
	}

	delete seq;
	
	
	for( int c=0; c < 256; ++c )
	{
		if( !chs[c].hasNotes && chs[c].it )
		{
			printf(" Should remove empty track\n");
			// must delete trackView first - but where is it?
			//tc->removeTrack( chs[c].it );
			//it->deleteLater();
		}
	}

	// Set channel 10 to drums as per General MIDI's orders
	if( chs[9].hasNotes && chs[9].it_inst && chs[9].isSF2 )
	{
		// AFAIK, 128 should be the standard bank for drums in SF2.
		// If not, this has to be made configurable.
		chs[9].it_inst->childModel( "bank" )->setValue( 128 );
		chs[9].it_inst->childModel( "patch" )->setValue( 0 );
	}

	return true;
}
示例#15
0
bool Alg_reader::parse()
{
    int voice = 0;
    int key = 60;
    double loud = 100.0;
    double pitch = 60.0;
    double dur = 1.0;
    double time = 0.0;
    int track_num = 0;
    seq->convert_to_seconds();
    //seq->set_real_dur(0.0); // just in case it's not initialized already
    readline();
    bool valid = false; // ignore blank lines
    while (line_parser_flag) {
        bool time_flag = false;
        bool next_flag = false;
        double next;
        bool voice_flag = false;
        bool loud_flag = false;
        bool dur_flag = false;
        bool new_pitch_flag = false; // "P" syntax or "A"-"G" syntax
        double new_pitch = 0.0;
        bool new_key_flag = false;   // "K" syntax
        int new_key = 0;
        Alg_parameters_ptr attributes = NULL;
        if (line_parser.peek() == '#') {
            // look for #track
            line_parser.get_nonspace_quoted(field);
            if (streql(field.c_str(), "#track")) {
                line_parser.get_nonspace_quoted(field); // number
                field.insert(0, " "); // need char at beginning because
                // parse_int ignores the first character of the argument
                track_num = parse_int(field);
                seq->add_track(track_num);

                // maybe we have a sequence or track name
                line_parser.get_remainder(field);
                // if there is a non-space character after #track n then
                // use it as sequence or track name. Note that because we
                // skip over spaces, a sequence or track name cannot begin
                // with leading blanks. Another decision is that the name
                // must be at time zero
                if (field.length() > 0) {
                    // insert the field as sequence name or track name
                    Alg_update_ptr update = new Alg_update;
                    update->chan = -1;
                    update->time = 0;
                    update->set_identifier(-1);
                    // sequence name is whatever is on track 0
                    // other tracks have track names
                    const char *attr =
                            (track_num == 0 ? "seqnames" : "tracknames");
                    update->parameter.set_attr(
                            symbol_table.insert_string(attr));
                    update->parameter.s = heapify(field.c_str());
                    seq->add_event(update, track_num);
                }
            } else if (streql(field.c_str(), "#offset")) {
                if (offset_found) {
                    parse_error(field, 0, "#offset specified twice");
                }
                offset_found = true;
                line_parser.get_nonspace_quoted(field); // number
                field.insert(0, " "); // need char at beginning because
                // parse_real ignores first character in the argument
                offset = parse_real(field);
            }
        } else {
            // we must have a track to insert into
            if (seq->tracks() == 0) seq->add_track(0);
            line_parser.get_nonspace_quoted(field);
            char pk = line_parser.peek();
            // attributes are parsed as two adjacent nonspace_quoted tokens
            // so we have to conditionally call get_nonspace_quoted() again
            if (pk && !isspace(pk)) {
                string field2;
                line_parser.get_nonspace_quoted(field2);
                field.append(field2);
            }
            while (field[0]) {
                char first = toupper(field[0]);
                if (strchr("ABCDEFGKLPUSIQHW-", first)) {
                    valid = true; // it's a note or event
                }
                if (first == 'V') {
                    if (voice_flag) {
                        parse_error(field, 0, "Voice specified twice");
                    } else {
                        voice = parse_chan(field);
                    }
                    voice_flag = true;
                } else if (first == 'T') {
                    if (time_flag) {
                        parse_error(field, 0, "Time specified twice");
                    } else {
                        time = parse_dur(field, 0.0);
                    }
                    time_flag = true;
                } else if (first == 'N') {
                    if (next_flag) {
                        parse_error(field, 0, "Next specified twice");
                    } else {
                        next = parse_dur(field, time);
                    }
                    next_flag = true;
                } else if (first == 'K') {
                    if (new_key_flag) {
                        parse_error(field, 0, "Key specified twice");
                    } else {
                        new_key = parse_key(field);
                        new_key_flag = true;
                    }
                } else if (first == 'L') {
                    if (loud_flag) {
                        parse_error(field, 0, "Loudness specified twice");
                    } else {
                        loud = parse_loud(field);
                    }
                    loud_flag = true;
                } else if (first == 'P') {
                    if (new_pitch_flag) {
                        parse_error(field, 0, "Pitch specified twice");
                    } else {
                        new_pitch = parse_pitch(field);
                        new_pitch_flag = true;
                    }
                } else if (first == 'U') {
                    if (dur_flag) {
                        parse_error(field, 0, "Dur specified twice");
                    } else {
                        dur = parse_dur(field, time);
                        dur_flag = true;
                    }
                } else if (strchr("SIQHW", first)) {
                    if (dur_flag) {
                        parse_error(field, 0, "Dur specified twice");
                    } else {
                        // prepend 'U' to field, copy EOS too
                        field.insert((unsigned int) 0, 1, 'U');
                        dur = parse_dur(field, time);
                        dur_flag = true;
                    }
                } else if (strchr("ABCDEFG", first)) {
                    if (new_pitch_flag) {
                        parse_error(field, 0, "Pitch specified twice");
                    } else {
                        // prepend 'P' to field
                        field.insert((unsigned int) 0, 1, 'P');
                        new_pitch = parse_pitch(field);
                        new_pitch_flag = true;
                    }
                } else if (first == '-') {
                    Alg_parameter parm;
                    if (parse_attribute(field, &parm)) { // enter attribute-value pair
                        attributes = new Alg_parameters(attributes);
                        attributes->parm = parm;
                        parm.s = NULL; // protect string from deletion by destructor
                    }
                } else {
                    parse_error(field, 0, "Unknown field");
                }

                if (error_flag) {
                    field[0] = 0; // exit the loop
                } else {
                    line_parser.get_nonspace_quoted(field);
                    pk = line_parser.peek();
                    // attributes are parsed as two adjacent nonspace_quoted 
                    // tokens so we have to conditionally call 
                    // get_nonspace_quoted() again
                    if (pk && !isspace(pk)) {
                        string field2;
                        line_parser.get_nonspace_quoted(field2);
                        field.append(field2);
                    }
                }
            }
            // a case analysis:
            // Key < 128 implies pitch unless pitch is explicitly given
            // Pitch implies Key unless key is explicitly given,
            // Pitch is rounded to nearest integer to determine the Key
            //    if necessary, so MIDI files will lose the pitch fraction
            // A-G is a Pitch specification (therefore it implies Key)
            //   K60 P60 -- both are specified, use 'em
            //   K60 P60 C4 -- overconstrained, an error
            //   K60 C4 -- OK, but K60 is already implied by C4
            //   K60 -- OK, pitch is 60
            //   C4 P60 -- over constrained
            //   P60 -- OK, key is 60
            //   P60.1 -- OK, key is 60
            //   C4 -- OK, key is 60, pitch is 60
            //   <nothing> -- OK, key and pitch from before
            //   K200 P60 -- ok, pitch is 60
            //   K200 with neither P60 nor C4 uses 
            //       pitch from before

            // figure out what the key/instance is:
            if (new_key_flag) { // it was directly specified
                key = new_key;
            } else if (new_pitch_flag) {
                // pitch was specified, but key was not; get key from pitch
                key = (int) (new_pitch + 0.5); // round to integer key number
            }
            if (new_pitch_flag) {
                pitch = new_pitch;
            } else if (key < 128 && new_key_flag) {
                // no explicit pitch, but key < 128, so it implies pitch
                pitch = key;
                new_pitch_flag = true;
            }
            // now we've acquired new parameters
            // if (it is a note, then enter the note
            if (valid) {
                // change tempo or beat
                attributes = process_attributes(attributes, time);
                // if there's a duration or pitch, make a note:
                if (new_pitch_flag || dur_flag) {
                    Alg_note_ptr note_ptr = new Alg_note;
                    note_ptr->chan = voice;
                    note_ptr->time = time;
                    note_ptr->dur = dur;
                    note_ptr->set_identifier(key);
                    note_ptr->pitch = (float) pitch;
                    note_ptr->loud = (float) loud;
                    note_ptr->parameters = attributes;
                    seq->add_event(note_ptr, track_num); // sort later
                    if (seq->get_real_dur() < (time + dur)) seq->set_real_dur(time + dur);
                } else {
                    int update_key = -1;
                    // key must appear explicitly; otherwise
                    //    update applies to channel
                    if (new_key_flag) {
                        update_key = key;
                    }
                    if (loud_flag) {
                        Alg_update_ptr new_upd = new Alg_update;
                        new_upd->chan = voice;
                        new_upd->time = time;
                        new_upd->set_identifier(update_key);
                        new_upd->parameter.set_attr(symbol_table.insert_string("loudr"));
                        new_upd->parameter.r = pitch;
                        seq->add_event(new_upd, track_num);
                        if (seq->get_real_dur() < time) seq->set_real_dur(time);
                    }
                    if (attributes) {
                        while (attributes) {
                            Alg_update_ptr new_upd = new Alg_update;
                            new_upd->chan = voice;
                            new_upd->time = time;
                            new_upd->set_identifier(update_key);
                            new_upd->parameter = attributes->parm;
                            seq->add_event(new_upd, track_num);
                            Alg_parameters_ptr p = attributes;
                            attributes = attributes->next;
                            p->parm.s = NULL; // so we don't delete the string
                            delete p;
                        }
                    }
                }
                if (next_flag) {
                    time = time + next;
                } else if (dur_flag || new_pitch_flag) { // a note: incr by dur
                    time = time + dur;
                }
            }
        }
        readline();
    }
    if (!error_flag) { // why not convert even if there was an error? -RBD
        seq->convert_to_seconds(); // make sure format is correct
    }
    // real_dur is valid, translate to beat_dur
    seq->set_beat_dur((seq->get_time_map())->time_to_beat(seq->get_real_dur()));
    return error_flag;
}
示例#16
0
#include "assert.h"
#include "stdlib.h"
#include "stdio.h"
#include "allegro.h"
#include "string.h"
#include "ctype.h"
//#include "memory.h"
#include "trace.h"
#include "strparse.h"
#ifndef EXPERIMENTAL_NOTE_TRACK
#include "allegrord.h"
#endif /* EXPERIMENTAL_NOTE_TRACK */
#define streql(s1, s2) (strcmp(s1, s2) == 0)
#define field_max 80


//Note that this is an #ifdef, not an #ifndef
#ifdef EXPERIMENTAL_NOTE_TRACK

class Alg_reader {
public:
    FILE *file;
    int line_no;
    String_parse line_parser;
    bool line_parser_flag;
    char field[field_max];
    bool error_flag;
    Alg_seq_ptr seq;
    double tsnum;
    double tsden;

    Alg_reader(FILE *a_file, Alg_seq_ptr new_seq);
    void readline();
    Alg_parameters_ptr process_attributes(Alg_parameters_ptr attributes, double time);
    bool parse();
    long parse_chan(char *field);
    long parse_int(char *field);
    int find_real_in(char *field, int n);
    double parse_real(char *field);
    void parse_error(char *field, long offset, char *message);
    double parse_dur(char *field, double base);
    double parse_after_dur(double dur, char *field, int n, double base);
    double parse_loud(char *field);
    long parse_key(char *field);
    double parse_pitch(char *field);
    long parse_after_key(int key, char *field, int n);
    long find_int_in(char *field, int n);
    bool parse_attribute(char *field, Alg_parameter_ptr parm);
    bool parse_val(Alg_parameter_ptr param, char *s, int i);
    bool check_type(char type_char, Alg_parameter_ptr param);
};

#endif /* EXPERIMENTAL_NOTE_TRACK */

void subseq(char *result, char *source, int from, int to)
{
    memcpy(result, source + from, to - from);
    result[to - from] = 0;
}

#ifndef EXPERIMENTAL_NOTE_TRACK
double Allegro_reader::parse_pitch(char *field)
#else /* EXPERIMENTAL_NOTE_TRACK */
double Alg_reader::parse_pitch(char *field)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    if (isdigit(field[1])) {
        char real_string[80];
        int last = find_real_in(field, 1);
        subseq(real_string, field, 1, last);
        return atof(real_string);
    } else {
        return (double) parse_key(field);
    }
}


#ifndef EXPERIMENTAL_NOTE_TRACK
Allegro_reader::Allegro_reader(FILE *a_file)
{
    file = a_file; // save the file
    line_parser_flag = false;
    line_no = 0;
    seq = Seq();
    tsnum = 4; // default time signature
    tsden = 4;
}
#else /* EXPERIMENTAL_NOTE_TRACK */
// it is the responsibility of the caller to delete
// the seq
Alg_reader::Alg_reader(FILE *a_file, Alg_seq_ptr new_seq)
{
    file = a_file; // save the file
    line_parser_flag = false;
    line_no = 0;
    tsnum = 4; // default time signature
    tsden = 4;
    seq = new_seq;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


//Note that this is an #ifdef, not an #ifndef
#ifdef EXPERIMENTAL_NOTE_TRACK
Alg_seq_ptr alg_read(FILE *file, Alg_seq_ptr new_seq)
    // read a sequence from allegro file
{
    if (!new_seq) new_seq = new Alg_seq();
    Alg_reader alg_reader(file, new_seq);
    alg_reader.parse();
    return alg_reader.seq;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


#ifndef EXPERIMENTAL_NOTE_TRACK
void Allegro_reader::readline()
#else /* EXPERIMENTAL_NOTE_TRACK */
void Alg_reader::readline()
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    char line[256];
    char *line_flag = fgets(line, 256, file);
    line_parser_flag = false;
    if (line_flag) {
        line_parser.init(line);
        line_parser_flag = true;
        error_flag = false;
    }
}


#ifndef EXPERIMENTAL_NOTE_TRACK
void Allegro_reader::process_attributes(Parameters_ptr attributes, double time)
{
    // print "process_attributes:", attributes
    bool ts_flag;
    if (attributes) {
        Parameters_ptr a;
        if (a = Parameters::remove_key(&attributes, "tempor")) {
            double tempo = a->parm.r;
            seq.insert_tempo(tempo, seq.map.time_to_beat(time));
        }
        if (a = Parameters::remove_key(&attributes, "beatr")) {
            double beat = a->parm.r;
            seq.insert_beat(time, beat);
        }
        if (a = Parameters::remove_key(&attributes, "tsnumr")) {
            tsnum = a->parm.r;
            ts_flag = true;
        }
        if (a = Parameters::remove_key(&attributes, "tsdenr")) {
            tsden = a->parm.r;
            ts_flag = true;
        }
        if (ts_flag) {
            seq.set_time_sig(seq.map.time_to_beat(time), tsnum, tsden);
        }
    }
}
#else /* EXPERIMENTAL_NOTE_TRACK */
Alg_parameters_ptr Alg_reader::process_attributes(
        Alg_parameters_ptr attributes, double time)
{
    // print "process_attributes:", attributes
    bool ts_flag = false;
    if (attributes) {
        Alg_parameters_ptr a;
        bool in_seconds = seq->get_units_are_seconds();
        if (a = Alg_parameters::remove_key(&attributes, "tempor")) {
            double tempo = a->parm.r;
            seq->insert_tempo(tempo, seq->get_time_map()->time_to_beat(time));
        }
        if (a = Alg_parameters::remove_key(&attributes, "beatr")) {
            double beat = a->parm.r;
            seq->insert_beat(time, beat);
        }
        if (a = Alg_parameters::remove_key(&attributes, "timesig_numr")) {
            tsnum = a->parm.r;
            ts_flag = true;
        }
        if (a = Alg_parameters::remove_key(&attributes, "timesig_denr")) {
            tsden = a->parm.r;
            ts_flag = true;
        }
        if (ts_flag) {
            seq->set_time_sig(seq->get_time_map()->time_to_beat(time),
            tsnum, tsden);
        }
        if (in_seconds) seq->convert_to_seconds();
    }
    return attributes; // in case it was modified
}
#endif /* EXPERIMENTAL_NOTE_TRACK */

#ifndef EXPERIMENTAL_NOTE_TRACK
bool Allegro_reader::parse()
{
    int voice = 0;
    int key = 60;
    double loud = 100.0;
    double pitch = 60.0;
    double dur = 1.0;
    double time = 0.0;
    readline();
    bool valid = false; // ignore blank lines
    while (line_parser_flag) {
        bool time_flag = false;
        bool next_flag = false;
        double next;
        bool voice_flag = false;
        bool loud_flag = false;
        bool dur_flag = false;
        bool new_pitch_flag = false; // "P" syntax
        double new_pitch = 0.0;
        bool new_key_flag = false;   // "K" syntax
        int new_key = 0;
        bool new_note_flag = false;  // "A"-"G" syntax
        int new_note = 0;
        Parameters_ptr attributes = NULL;
        line_parser.get_nonspace_quoted(field);
        char pk = line_parser.peek();
        if (pk && !isspace(pk)) {
            line_parser.get_nonspace_quoted(field + strlen(field));
        }
        while (field[0]) {
            // print "field", "|";field;"|", "|";line_parser.string;"|", line_parser.pos
            char first = toupper(field[0]);
            if (strchr("ABCDEFGKLPUSIQHW-", first)) {
                valid = true; // it's a note or event
            }
            if (first == 'V') {
                if (voice_flag) {
                    parse_error(field, 0, "Voice specified twice");
                } else {
                    voice = parse_int(field);
                }
                voice_flag = true;
            } else if (first == 'T') {
                if (time_flag) {
                    parse_error(field, 0, "Time specified twice");
                } else {
                    time = parse_dur(field, 0.0);
                }
                time_flag = true;
            } else if (first == 'N') {
                if (next_flag) {
                    parse_error(field, 0, "Next specified twice");
                } else {
                    next = parse_dur(field, time);
                }
                next_flag = true;
            } else if (first == 'K') {
                if (new_key_flag) {
                    parse_error(field, 0, "Key specified twice");
                } else {
                    new_key = parse_key(field);
                    new_key_flag = true;
                }
            } else if (first == 'L') {
                if (loud_flag) {
                    parse_error(field, 0, "Loudness specified twice");
                } else {
                    loud = parse_loud(field);
                }
                loud_flag = true;
            } else if (first == 'P') {
                if (new_note_flag || new_pitch_flag) {
                    parse_error(field, 0, "Pitch specified twice");
                } else {
                    new_pitch = parse_pitch(field);
                    new_pitch_flag = true;
                }
            } else if (first == 'U') {
                if (dur_flag) {
                    parse_error(field, 0, "Dur specified twice");
                } else {
                    dur = parse_dur(field, time);
                    dur_flag = true;
                }
            } else if (strchr("SIQHW", first)) {
                if (dur_flag) {
                    parse_error(field, 0, "Dur specified twice");
                } else {
                    // prepend 'U' to field, copy EOS too
                    memmove(field + 1, field, strlen(field) + 1);
                    field[0] = 'U';
                    dur = parse_dur(field, time);
                    dur_flag = true;
                }
            } else if (strchr("ABCDEFG", first)) {
                if (new_note_flag || new_pitch_flag) {
                    parse_error(field, 0, "Pitch specified twice");
                } else {
                    // prepend 'K' to field, copy EOS too
                    memmove(field + 1, field, strlen(field) + 1);
                    field[0] = 'K';
                    new_note = parse_key(field);
                    new_note_flag = true;
                }
            } else if (first == '-') {
                Parameter parm;
                if (parse_attribute(field, &parm)) { // enter attribute-value pair
                    attributes = new Parameters(attributes);
                    attributes->parm = parm;
                    parm.s = NULL; // protect string from deletion by destructor
                }
            } else {
                parse_error(field, 0, "Unknown field");
            }

            if (error_flag) {
                field[0] = 0; // exit the loop
            } else {
                line_parser.get_nonspace_quoted(field);
                pk = line_parser.peek();
                if (pk && !isspace(pk)) {
                    line_parser.get_nonspace_quoted(field + strlen(field));
                }
            }
        }
        // a case analysis:
        // Key < 128 counts as both key and pitch
        // A-G implies pitch AND key unless key given too
        //   K60 P60 -- both are specified, use 'em
        //   K60 P60 C4 -- overconstrained, an error
        //   K60 C4 -- overconstrained
        //   K60 -- OK, pitch is 60
        //   C4 P60 -- over constrained
        //   P60 -- OK, key is from before, pitch is 60
        //   C4 -- OK, key is 60, pitch is 60
        //   <nothing> -- OK, key and pitch from before
        //   K200 with P60 ok, pitch is 60
        //   K200 with neither P60 nor C4 uses 
        //       pitch from before
        // figure out what the key/instance is:
        if (new_key_flag) { // it was directly specified
            key = new_key;
            if (key < 128 && new_note_flag) {
                parse_error("", 0, "Pitch specified twice");
            }
        } else if (new_note_flag) { // "A"-"G" used
            key = new_note;
        }
        if (new_pitch_flag) {
            pitch = new_pitch;
        } else if (key < 128) {
            pitch = key;
        }
        // now we've acquired new parameters
        // if (it is a note, then enter the note
        if (valid) {
            // change tempo or beat
            process_attributes(attributes, time);
            // if there's a duration or pitch, make a note:
            if (new_pitch_flag || dur_flag || new_note_flag) {
                new_key_flag = false;
                new_pitch_flag = false;
                Allegro_note_ptr note_ptr = new Allegro_note;
                note_ptr->chan = voice;
                note_ptr->time = time;
                note_ptr->dur = dur;
                note_ptr->key = key;
                note_ptr->pitch = pitch;
                note_ptr->loud = loud;
                note_ptr->parameters = attributes;
                seq.add_event(note_ptr); // sort later
            } else {
                int update_key = -1;
                // key or pitch must appear explicitly; otherwise
                //    update applies to channel
                if (new_key_flag || new_pitch_flag) {
                    update_key = key;
                }
                if (loud_flag) {
                    Allegro_update_ptr new_upd = new Allegro_update;
                    new_upd->chan = voice;
                    new_upd->time = time;
                    new_upd->key = update_key;
                    new_upd->parameter.set_attr(symbol_table.insert_string("loudr"));
                    new_upd->parameter.r = pitch;
                    seq.add_event(new_upd);
                }
                if (attributes) {
                    while (attributes) {
                        Allegro_update_ptr new_upd = new Allegro_update;
                        new_upd->chan = voice;
                        new_upd->time = time;
                        new_upd->key = update_key;
                        new_upd->parameter = attributes->parm;
                        seq.add_event(new_upd);
                        Parameters_ptr p = attributes;
                        attributes = attributes->next;
                        delete p;
                    }
                }
            }
            if (next_flag) {
                time = time + next;
            } else if (dur_flag) {
                time = time + dur;
            }
        }
        readline();
    }
    //print "Finished reading score"
    if (!error_flag) {
        seq.convert_to_seconds(); // make sure format is correct
        // seq.notes.sort('event_greater_than');
    }
    // print "parse returns error_flag", error_flag
    return error_flag;
}
#else /* EXPERIMENTAL_NOTE_TRACK */
bool Alg_reader::parse()
{
    int voice = 0;
    int key = 60;
    double loud = 100.0;
    double pitch = 60.0;
    double dur = 1.0;
    double time = 0.0;
    int track_num = 0;
    seq->convert_to_seconds();
    //seq->set_real_dur(0.0); // just in case it's not initialized already
    readline();
    bool valid = false; // ignore blank lines
    while (line_parser_flag) {
        bool time_flag = false;
        bool next_flag = false;
        double next;
        bool voice_flag = false;
        bool loud_flag = false;
        bool dur_flag = false;
        bool new_pitch_flag = false; // "P" syntax
        double new_pitch = 0.0;
        bool new_key_flag = false;   // "K" syntax
        int new_key = 0;
        bool new_note_flag = false;  // "A"-"G" syntax
        int new_note = 0;
        Alg_parameters_ptr attributes = NULL;
        if (line_parser.peek() == '#') {
            // look for #track
            line_parser.get_nonspace_quoted(field);
            if (streql(field, "#track")) {
                line_parser.get_nonspace_quoted(field); // number
                track_num = parse_int(field - 1);
                seq->add_track(track_num);
            }
            // maybe we have a comment
        } else {
            // we must have a track to insert into
            if (seq->tracks() == 0) seq->add_track(0);
            line_parser.get_nonspace_quoted(field);
            char pk = line_parser.peek();
            // attributes are parsed as two adjacent nonspace_quoted tokens
            // so we have to conditionally call get_nonspace_quoted() again
            if (pk && !isspace(pk)) {
                line_parser.get_nonspace_quoted(field + strlen(field));
            }
            while (field[0]) {
                char first = toupper(field[0]);
                if (strchr("ABCDEFGKLPUSIQHW-", first)) {
                    valid = true; // it's a note or event
                }
                if (first == 'V') {
                    if (voice_flag) {
                        parse_error(field, 0, "Voice specified twice");
                    } else {
                        voice = parse_chan(field);
                    }
                    voice_flag = true;
                } else if (first == 'T') {
                    if (time_flag) {
                        parse_error(field, 0, "Time specified twice");
                    } else {
                        time = parse_dur(field, 0.0);
                    }
                    time_flag = true;
                } else if (first == 'N') {
                    if (next_flag) {
                        parse_error(field, 0, "Next specified twice");
                    } else {
                        next = parse_dur(field, time);
                    }
                    next_flag = true;
                } else if (first == 'K') {
                    if (new_key_flag) {
                        parse_error(field, 0, "Key specified twice");
                    } else {
                        new_key = parse_key(field);
                        new_key_flag = true;
                    }
                } else if (first == 'L') {
                    if (loud_flag) {
                        parse_error(field, 0, "Loudness specified twice");
                    } else {
                        loud = parse_loud(field);
                    }
                    loud_flag = true;
                } else if (first == 'P') {
                    if (new_note_flag || new_pitch_flag) {
                        parse_error(field, 0, "Pitch specified twice");
                    } else {
                        new_pitch = parse_pitch(field);
                        new_pitch_flag = true;
                    }
                } else if (first == 'U') {
                    if (dur_flag) {
                        parse_error(field, 0, "Dur specified twice");
                    } else {
                        dur = parse_dur(field, time);
                        dur_flag = true;
                    }
                } else if (strchr("SIQHW", first)) {
                    if (dur_flag) {
                        parse_error(field, 0, "Dur specified twice");
                    } else {
                        // prepend 'U' to field, copy EOS too
                        memmove(field + 1, field, strlen(field) + 1);
                        field[0] = 'U';
                        dur = parse_dur(field, time);
                        dur_flag = true;
                    }
                } else if (strchr("ABCDEFG", first)) {
                    if (new_note_flag || new_pitch_flag) {
                        parse_error(field, 0, "Pitch specified twice");
                    } else {
                        // prepend 'K' to field, copy EOS too
                        memmove(field + 1, field, strlen(field) + 1);
                        field[0] = 'K';
                        new_note = parse_key(field);
                        new_note_flag = true;
                    }
                } else if (first == '-') {
                    Alg_parameter parm;
                    if (parse_attribute(field, &parm)) { // enter attribute-value pair
                        attributes = new Alg_parameters(attributes);
                        attributes->parm = parm;
                        parm.s = NULL; // protect string from deletion by destructor
                    }
                } else {
                    parse_error(field, 0, "Unknown field");
                }

                if (error_flag) {
                    field[0] = 0; // exit the loop
                } else {
                    line_parser.get_nonspace_quoted(field);
                    pk = line_parser.peek();
                    // attributes are parsed as two adjacent nonspace_quoted 
                    // tokens so we have to conditionally call 
                    // get_nonspace_quoted() again
                    if (pk && !isspace(pk)) {
                        line_parser.get_nonspace_quoted(field + strlen(field));
                    }
                }
            }
            // a case analysis:
            // Key < 128 counts as both key and pitch
            // A-G implies pitch AND key unless key given too
            //   K60 P60 -- both are specified, use 'em
            //   K60 P60 C4 -- overconstrained, an error
            //   K60 C4 -- overconstrained
            //   K60 -- OK, pitch is 60
            //   C4 P60 -- over constrained
            //   P60 -- OK, key is from before, pitch is 60
            //   C4 -- OK, key is 60, pitch is 60
            //   <nothing> -- OK, key and pitch from before
            //   K200 with P60 ok, pitch is 60
            //   K200 with neither P60 nor C4 uses 
            //       pitch from before
            // figure out what the key/instance is:
            if (new_key_flag) { // it was directly specified
                key = new_key;
                if (key < 128 && new_note_flag) {
                    parse_error("", 0, "Pitch specified twice");
                }
            } else if (new_note_flag) { // "A"-"G" used
                key = new_note;
            }
            if (new_pitch_flag) {
                pitch = new_pitch;
            } else if (key < 128) {
                pitch = key;
            }
            // now we've acquired new parameters
            // if (it is a note, then enter the note
            if (valid) {
                // change tempo or beat
                attributes = process_attributes(attributes, time);
                // if there's a duration or pitch, make a note:
                if (new_pitch_flag || dur_flag || new_note_flag) {
                    new_key_flag = false;
                    new_pitch_flag = false;
                    Alg_note_ptr note_ptr = new Alg_note;
                    note_ptr->chan = voice;
                    note_ptr->time = time;
                    note_ptr->dur = dur;
                    note_ptr->set_identifier(key);
                    note_ptr->pitch = pitch;
                    note_ptr->loud = loud;
                    note_ptr->parameters = attributes;
                    seq->add_event(note_ptr, track_num); // sort later
                    if (seq->get_real_dur() < (time + dur)) seq->set_real_dur(time + dur);
                } else {
                    int update_key = -1;
                    // key or pitch must appear explicitly; otherwise
                    //    update applies to channel
                    if (new_key_flag || new_pitch_flag) {
                        update_key = key;
                    }
                    if (loud_flag) {
                        Alg_update_ptr new_upd = new Alg_update;
                        new_upd->chan = voice;
                        new_upd->time = time;
                        new_upd->set_identifier(update_key);
                        new_upd->parameter.set_attr(symbol_table.insert_string("loudr"));
                        new_upd->parameter.r = pitch;
                        seq->add_event(new_upd, track_num);
                        if (seq->get_real_dur() < time) seq->set_real_dur(time);
                    }
                    if (attributes) {
                        while (attributes) {
                            Alg_update_ptr new_upd = new Alg_update;
                            new_upd->chan = voice;
                            new_upd->time = time;
                            new_upd->set_identifier(update_key);
                            new_upd->parameter = attributes->parm;
                            seq->add_event(new_upd, track_num);
                            Alg_parameters_ptr p = attributes;
                            attributes = attributes->next;
                            p->parm.s = NULL; // so we don't delete the string
                            delete p;
                        }
                    }
                }
                if (next_flag) {
                    time = time + next;
                } else if (dur_flag) {
                    time = time + dur;
                }
            }
        }
        readline();
    }
    //print "Finished reading score"
    if (!error_flag) {
        seq->convert_to_seconds(); // make sure format is correct
        // seq->notes.sort('event_greater_than');
    }
    // real_dur is valid, translate to beat_dur
    seq->set_beat_dur((seq->get_time_map())->time_to_beat(seq->get_real_dur()));
    // print "parse returns error_flag", error_flag
    return error_flag;
}