void Alg_smf_write::write_update(Alg_update_ptr update) { char *name = update->parameter.attr_name(); /****Non-Meta Events****/ if (!strcmp(name, "pressurer")) { write_delta(update->time); if (update->get_identifier() < 0) { // channel pressure message out_file->put(0xD0 + to_midi_channel(update->chan)); write_data((int)(update->parameter.r * 127)); } else { // just 1 key -- poly pressure out_file->put(0xA0 + to_midi_channel(update->chan)); write_data(update->get_identifier()); write_data((int)(update->parameter.r * 127)); } } else if (!strcmp(name, "programi")) { write_delta(update->time); out_file->put(0xC0 + to_midi_channel(update->chan)); write_data(update->parameter.i); } else if (!strcmp(name, "bendr")) { int temp = ROUND(0x2000 * (update->parameter.r + 1)); if (temp > 0x3fff) temp = 0x3fff; // 14 bits maximum if (temp < 0) temp = 0; int c1 = temp & 0x7F; // low 7 bits int c2 = temp >> 7; // high 7 bits write_delta(update->time); out_file->put(0xE0 + to_midi_channel(update->chan)); write_data(c1); write_data(c2); } else if (!strncmp(name, "control", 7) &&
void Alg_midifile_reader::update(int chan, int key, Alg_parameter_ptr param) { Alg_update_ptr update = new Alg_update; update->time = get_time(); update->chan = chan; if (chan != -1) { update->chan = chan + channel_offset + port * channel_offset_per_port; } update->set_identifier(key); update->parameter = *param; // prevent the destructor from destroying the string twice! // the new Update takes the string from param if (param->attr_type() == 's') param->s = NULL; track->append(update); }
void Alg_seq::write(ostream &file, bool in_secs) { int i, j; if (in_secs) convert_to_seconds(); else convert_to_beats(); Alg_event_ptr update_to_skip = write_track_name(file, 0, track_list[0]); Alg_beats &beats = time_map->beats; for (i = 0; i < beats.len - 1; i++) { Alg_beat_ptr b = &(beats[i]); if (in_secs) { file << "T" << TIMFMT << b->time; } else { file << "TW" << TIMFMT << b->beat / 4; } double tempo = (beats[i + 1].beat - b->beat) / (beats[i + 1].time - beats[i].time); file << " -tempor:" << GFMT << tempo * 60 << "\n"; } if (time_map->last_tempo_flag) { // we have final tempo: Alg_beat_ptr b = &(beats[beats.len - 1]); if (in_secs) { file << "T" << TIMFMT << b->time; } else { file << "TW" << TIMFMT << b->beat / 4; } file << " -tempor:" << GFMT << time_map->last_tempo * 60.0 << "\n"; } // write the time signatures for (i = 0; i < time_sig.length(); i++) { Alg_time_sig &ts = time_sig[i]; double time = ts.beat; if (in_secs) { file << "T" << TIMFMT << time << " V- -timesig_numr:" << GFMT << ts.num << "\n"; file << "T" << TIMFMT << time << " V- -timesig_denr:" << GFMT << ts.den << "\n"; } else { double wholes = ts.beat / 4; file << "TW" << TIMFMT << wholes << " V- -timesig_numr:" << GFMT << ts.num << "\n"; file << "TW" << TIMFMT << wholes << " V- -timesig_denr:" << GFMT << ts.den << "\n"; } } for (j = 0; j < track_list.length(); j++) { Alg_events ¬es = track_list[j]; if (j != 0) update_to_skip = write_track_name(file, j, notes); // now write the notes at beat positions for (i = 0; i < notes.length(); i++) { Alg_event_ptr e = notes[i]; // if we already wrote this event as a track or sequence name, // do not write it again if (e == update_to_skip) continue; double start = e->time; if (in_secs) { file << "T" << TIMFMT << start; } else { file << "TW" << TIMFMT << start / 4; } // write the channel as Vn or V- if (e->chan == -1) file << " V-"; else file << " V" << e->chan; // write the note or update data if (e->is_note()) { Alg_note_ptr n = (Alg_note_ptr) e; double dur = n->dur; file << " K" << n->get_identifier() << " P" << GFMT << n->pitch; if (in_secs) { file << " U" << TIMFMT << dur; } else { file << " Q" << TIMFMT << dur; } file << " L" << GFMT << n->loud; Alg_parameters_ptr p = n->parameters; while (p) { parameter_print(file, &(p->parm)); p = p->next; } } else { // an update assert(e->is_update()); Alg_update_ptr u = (Alg_update_ptr) e; if (u->get_identifier() != -1) { file << " K" << u->get_identifier(); } parameter_print(file, &(u->parameter)); } file << "\n"; } } }
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; }