|
| 1 | +// |
| 2 | +// Programmer: Ziemowit Laski <zlaski@ziemas.net> |
| 3 | +// Creation Date: Sun, Jun 12, 2016 11:09:34 AM |
| 4 | +// Last Modified: Sun, Jun 12, 2016 11:09:34 AM |
| 5 | +// Filename: stretch.cpp |
| 6 | +// Syntax: C++ |
| 7 | +// |
| 8 | +// Description: Stretches (or shrinks): |
| 9 | +// 1. The position of bars (measures) in tracks, |
| 10 | +// without affecting tempo, and/or |
| 11 | +// 2. The tempo (BPM) itself. |
| 12 | +// |
| 13 | +// This is useful when working with MIDI files |
| 14 | +// that were automatically generated from audio |
| 15 | +// (MP3, WAV) files. Conversion software often |
| 16 | +// has difficulty in correct placement of |
| 17 | +// bars, and also sometimes does not preserve |
| 18 | +// the tempo of the original. |
| 19 | +// |
| 20 | +#include "MidiFile.h" |
| 21 | +#include "Options.h" |
| 22 | +#include <iostream> |
| 23 | +#include <cmath> |
| 24 | +#include <algorithm> |
| 25 | + |
| 26 | +using namespace std; |
| 27 | + |
| 28 | +void doStretch (MidiFile& midifile, double bars, double duration); |
| 29 | + |
| 30 | +int main(int argc, char** argv) { |
| 31 | + Options options; |
| 32 | + options.define("b|bars|m|measures=d:1.0", |
| 33 | + "Stretch width of bars (measures) by factor specified (WITHOUT affecting tempo)"); |
| 34 | + options.define("d|duration|t|tempo=d:1.0", |
| 35 | + "Stretch duration of track(s) by factor specified"); |
| 36 | + options.process(argc, argv); |
| 37 | + if (options.getArgCount() != 2) { |
| 38 | + cerr << "two MIDI filenames are required.\n"; |
| 39 | + exit(1); |
| 40 | + } |
| 41 | + |
| 42 | + MidiFile midifile; |
| 43 | + midifile.read(options.getArg(1)); |
| 44 | + if (!midifile.status()) { |
| 45 | + cerr << "Error reading MIDI file " << options.getArg(1) << endl; |
| 46 | + exit(1); |
| 47 | + } |
| 48 | + |
| 49 | + if (options.getBoolean("bars") || options.getBoolean("duration")) { |
| 50 | + double bars = options.getDouble("bars"); |
| 51 | + double duration = options.getDouble("duration"); |
| 52 | + if (bars < 0.1 || bars > 10.0 || duration < 0.1 || duration > 10.0) { |
| 53 | + cerr << "stretch parameters must be between 0.1 and 10.\n"; |
| 54 | + exit(1); |
| 55 | + } |
| 56 | + doStretch(midifile, bars, duration); |
| 57 | + } |
| 58 | + |
| 59 | + midifile.write(options.getArg(2)); |
| 60 | + return 0; |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +////////////////////////////// |
| 65 | +// |
| 66 | +// doStretch -- Stretch size of all bars (measures) in the file |
| 67 | +// by a specified 'bars' factor. (Values lower than 1.0 will result |
| 68 | +// in shrinking). Note that bars (measures) are not encoded in MIDI |
| 69 | +// directly as an event type. Instead, they are computed from the |
| 70 | +// Pulse Per Quarter Note (PPQN) value in the file header; PPQN |
| 71 | +// multiplied by the number of quarter notes in a measure (as |
| 72 | +// determined by the time signature contained in the FF 58 |
| 73 | +// message) gives the number of ticks in each measure. The |
| 74 | +// 'duration' parameter specifies the factor by which the |
| 75 | +// duration of the track(s) should be stretched (or shrunk, |
| 76 | +// if less than 1.0). |
| 77 | + |
| 78 | +void doStretch(MidiFile& midifile, double bars, double duration) { |
| 79 | + int ppqn = midifile.getTicksPerQuarterNote(); |
| 80 | + int new_ppqn = max( 24576, ppqn ); |
| 81 | + midifile.setTicksPerQuarterNote( new_ppqn ); |
| 82 | + double tick_mult = (double)new_ppqn / (double)ppqn; |
| 83 | + tick_mult /= bars; |
| 84 | + |
| 85 | + for (int t = 0; t < midifile.size(); ++t) { |
| 86 | + MidiEventList &track = midifile[t]; |
| 87 | + for (int e = 0; e < track.size(); ++e) { |
| 88 | + MidiEvent &event = track[e]; |
| 89 | + event.tick *= tick_mult; |
| 90 | + if (event.getMetaType() == 0x51) { |
| 91 | + int tempo = event.getTempoMicroseconds(); |
| 92 | + tempo *= bars; |
| 93 | + tempo *= duration; |
| 94 | + event.setTempoMicroseconds( tempo ); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments