/* Functions for writing a Midi file. Copyright (C) 1994 by R.Harmsen Original date: 26 December 1994. Slight corrections, for portability to Linux, under standard C (C89, C90 or C99): 7 March 2025. */ #include "ritemidi.h" #include #include #include static void ritemidi_delta_time (FILE *midi_fp, long delta_time); static long midi_bytes_written = 0; static long where_length = 0; /* Number of duration units not yet written out as delta-time of message. Will be accounted for by the next rite_midi_note_on or rite_midi_note_on. */ static long left_dura = 0; /* Last channel used. For using the "running status method. */ static int last_channel = -1; /* Last status used. For using the "running status method. */ static int last_status = -1; static int g_ticks_per_quarter; static fpos_t where_midi_head = -1; /******************************************************************** Writes a midi-file type-1 header. Returns 0 for success, negative if failure. *******************************************************************/ int ritemidi_head (FILE *midi_fp, int number_of_tracks, int ticks_per_sixteenth) { unsigned char fhead[] = "MThd" "\0\0\0\6" /* Length of header data */ "\0\1" /* Type of midi file */ "\0\0" /* Number of tracks in the file */ "\0\0" /* Number of ticks in a quarter-note */ ; fgetpos(midi_fp, &where_midi_head); /* Fill some data */ int2twochars(fhead + 10, number_of_tracks); int2twochars(fhead + 12, g_ticks_per_quarter = ticks_per_sixteenth * 4); fwrite(fhead, 1, sizeof fhead - 1, midi_fp); return 0; } /******************************************************************** Rewrites a midi-file type-1 header, at the file-position previously written to by rite_midi_head. This can be useful when the number of tracks is not definitively known in advance. *******************************************************************/ int reritemidi_head (FILE *midi_fp, int number_of_tracks, int ticks_per_sixteenth) { if (fsetpos(midi_fp, &where_midi_head) != -1) { return ritemidi_head( midi_fp, number_of_tracks, g_ticks_per_quarter = ticks_per_sixteenth); } return 0; } /******************************************************************** Writes a track header to the midi-file. Returns 0 for success, negative if failure. *******************************************************************/ int ritemidi_opentrack ( FILE *midi_fp, int sixteenth_per_minute, int channel, int program_change ) { unsigned char thead[] = "MTrk" "\0\0\0\0" /* Those last 4 bytes should contain the length of the midi data, but we don't know that yet, have to fill later */ ; unsigned char buf[4]; unsigned char tempo_event[] = "\x00" /* delta-time zero */ "\xff" /* meta-event */ "\x51" /* tempo event */ "\x03" /* three data bytes to follow */ "\0\0\0" /* microseconds per quarternote */ ; fwrite(thead, 1, sizeof thead - 1, midi_fp); /* Remember position of length-counter in the file, to be able to update it later */ where_length = ftell(midi_fp) - 4; midi_bytes_written = 0; long2fourchars(buf, /* Calculate microseconds per quarternote */ 1000000L * 4L * 60L / (long)sixteenth_per_minute); memcpy(tempo_event + 4, buf + 1, 3); midi_bytes_written += fwrite(tempo_event, 1, sizeof tempo_event - 1, midi_fp); /* Set guitar instrument and level. */ midi_bytes_written += fprintf(midi_fp, "%c%c%c", 0, /* Delta-time */ 0xc0 + channel, /* Program change */ program_change ); midi_bytes_written += fprintf(midi_fp, "%c%c%c%c", 0, /* Delta-time */ 0xb0 + channel, /* Control change */ 7, /* Main volume */ 80 /* 0x50 is intermediate level */ ); return 0; } /******************************************************************** Updates the length count in the track written so far. Returns 0 for success, negative if failure. *******************************************************************/ int ritemidi_clostrack (FILE *midi_fp) { unsigned char buf[4]; long was_pos; unsigned char endtrack[] = "\x00" /* delta-time zero */ "\xff" /* meta-event */ "\x2f" /* end of track */ "\x00" /* length of data */ ; midi_bytes_written += fwrite(endtrack, 1, sizeof endtrack - 1, midi_fp); was_pos = ftell(midi_fp); fseek(midi_fp, where_length, SEEK_SET); long2fourchars(buf, midi_bytes_written); fwrite(buf, 1, sizeof buf, midi_fp); /* Reset for next track */ midi_bytes_written = 0; fseek(midi_fp, was_pos, SEEK_SET); /* Track timing is done independently of other tracks, so initialise for a fresh start */ left_dura = 0; return 0; } /******************************************************************** Writes a note-on or note_off message for the specified channel and note. On_off is 1 for note_on, 0 for note_off. *******************************************************************/ void ritemidi_note_on_off ( enum RITEMIDI_ONOFF onoff, FILE *midi_fp, int channel, int note) { /* If note_on: Left_dura is nearly always zero here, because a previous note_off message has carried it. But the exception is when a track starts with rest(s), and there are no note-offs before the first note-on. If note_off: Here left_dura is often non-zero, because after the note-on for one or more notes, some time must pass, before they are switched of again. After the first note-off in a chord, left_dura is zeroed immediately, because other note-offs in the same chord must follow simultaneously, and so have a delta-time value of zero. */ ritemidi_delta_time(midi_fp, left_dura); left_dura = 0; /* Running status method: to save space, a repeating status and channel combination need not be repeated */ if (last_status != 0x90 || last_channel != channel) { midi_bytes_written++; putc(0x90 + channel, midi_fp); } last_status = 0x90; last_channel = channel; midi_bytes_written += fprintf(midi_fp, "%c%c", note, onoff == RITEMIDI_NOTE_ON ? 0x40 : 0x00); } /******************************************************************** Pass some time. Typically used between a note_on and note_off, to specify its durations, or in case of a chord, used between a group of "note_on"s and a group of corresponding "note_off"s. *******************************************************************/ void ritemidi_pass_time (long delta_time) { left_dura += delta_time; } /******************************************************************** Write a key signature. Negative sharps are flats. Minor = 1 for minor keys, 1 for major keys. *******************************************************************/ void ritemidi_key_signature (FILE *midi_fp, int sharps, int minor) { unsigned char key_sig[] = "\x00" /* delta-time zero */ "\xff" /* meta-event */ "\x59" /* key signature */ "\x02" /* length of data */ "\x00" /* no. of sharps */ "\x00" /* minor/major */ ; key_sig[4] = sharps; key_sig[5] = !!minor; if (sharps < 0) key_sig[5] *= -1; midi_bytes_written += fwrite(key_sig, 1, sizeof key_sig - 1, midi_fp); } /******************************************************************** Write a time signature. *******************************************************************/ void ritemidi_time_signature (FILE *midi_fp, int numerator, int denominator) { unsigned char time_sig[] = "\x00" /* delta-time zero */ "\xff" /* meta-event */ "\x58" /* time signature */ "\x04" /* length of data */ "\x00" /* numerator */ "\x00" /* denominator */ "\x00" /* ticks per quarter */ "\x08" /* 32ths per quarter */ ; time_sig[6] = g_ticks_per_quarter / (denominator / 4.0); int i; for (i = 0; denominator; denominator >>= 1, i++) /* */ ; time_sig[4] = numerator; time_sig[5] = i - 1; midi_bytes_written += fwrite(time_sig, 1, sizeof time_sig - 1, midi_fp); } /******************************************************************** Write a delta time code, needed before any other message. *******************************************************************/ static void ritemidi_delta_time (FILE *midi_fp, long delta_time) { if (delta_time) { unsigned char varl[5]; int varll; varll = var_length(delta_time, varl); midi_bytes_written += fwrite(varl, 1, varll, midi_fp); } else { /* This is an optimization, the other if-branch does the same, but with more overhead */ putc(0, midi_fp); midi_bytes_written++; } }