Giter VIP home page Giter VIP logo

Comments (1)

craigsapp avatar craigsapp commented on August 15, 2024 1

I try to set different TPQ with method setTPQ, but the actual ticks do not change accordingly.

Yes, setting the TPQ/PPQN value just changes that value in the MIDI header and does not change the timestamps within the file.

I added an example program to do what you want:

https://github.com/craigsapp/midifile/blob/master/src-programs/retick.cpp

The main contents of the program that do all of the work:

int tpq = midifile.getTicksPerQuarterNote();
int newtpq = options.getInteger("tpq");
double factor = (double)newtpq / (double)tpq;
for (int track=0; track<midifile.getTrackCount(); track++) {
for (int event=0; event < midifile.getEventCount(track); event++) {
MidiEvent& ev = midifile.getEvent(track, event);
ev.tick = int(ev.tick * factor + 0.5);
}
}
midifile.setTPQ(newtpq);

factor rescales the absolute tick timestamps by the given factor to convert from the old TPQ to the new TPQ.

Demonstration (in linux/unix):

make
cd bin
./createmidifile2           # this creates twinkle.mid with TPQ set to 120
./retick twinkle.mid -t 100 twinkle100.mid    # convert timestamps to quarter note = 100 ticks
./retick twinkle.mid -t 100           # display output in ASCII format

Here is the result of ./retick twinkle.mid -t 100:

"MThd"			; MIDI header chunk marker
4'6			; bytes to follow in header chunk
2'1			; file format: Type-1 (multitrack)
2'3			; number of tracks
2'100			; ticks per quarter note

;;; TRACK 0 ----------------------------------
"MTrk"			; MIDI track chunk marker
4'43			; bytes to follow in track chunk
v0	ff 3 v28 "Twinkle, Twinkle Little Star"	; track name
v0	ff 51 v3 t104	; tempo
v0	ff 2f v0	; end-of-track

;;; TRACK 1 ----------------------------------
"MTrk"			; MIDI track chunk marker
4'131			; bytes to follow in track chunk
v0	ff 3 v6 "Melody"	; track name
v0	c0 '40		; patch-change
v0	90 '72 '64	; note-on C5
v100	80 '72 '64	; note-off C5
v0	90 '72 '64	; note-on C5
v100	80 '72 '64	; note-off C5
v0	90 '79 '64	; note-on G5
v100	80 '79 '64	; note-off G5
v0	90 '79 '64	; note-on G5
v100	80 '79 '64	; note-off G5
v0	90 '81 '64	; note-on A5
v100	80 '81 '64	; note-off A5
v0	90 '81 '64	; note-on A5
v100	80 '81 '64	; note-off A5
v0	90 '79 '64	; note-on G5
v200	80 '79 '64	; note-off G5
v0	90 '77 '64	; note-on F5
v100	80 '77 '64	; note-off F5
v0	90 '77 '64	; note-on F5
v100	80 '77 '64	; note-off F5
v0	90 '76 '64	; note-on E5
v100	80 '76 '64	; note-off E5
v0	90 '76 '64	; note-on E5
v100	80 '76 '64	; note-off E5
v0	90 '74 '64	; note-on D5
v100	80 '74 '64	; note-off D5
v0	90 '74 '64	; note-on D5
v100	80 '74 '64	; note-off D5
v0	90 '72 '64	; note-on C5
v200	80 '72 '64	; note-off C5
v0	ff 2f v0	; end-of-track

;;; TRACK 2 ----------------------------------
"MTrk"			; MIDI track chunk marker
4'141			; bytes to follow in track chunk
v0	ff 3 v9 "Bass line"	; track name
v0	c1 '58		; patch-change
v0	91 '48 '90	; note-on C3
v100	81 '48 '90	; note-off C3
v0	91 '60 '90	; note-on C4
v100	81 '60 '90	; note-off C4
v0	91 '64 '90	; note-on E4
v100	81 '64 '90	; note-off E4
v0	91 '60 '90	; note-on C4
v100	81 '60 '90	; note-off C4
v0	91 '65 '90	; note-on F4
v100	81 '65 '90	; note-off F4
v0	91 '60 '90	; note-on C4
v100	81 '60 '90	; note-off C4
v0	91 '64 '90	; note-on E4
v100	81 '64 '90	; note-off E4
v0	91 '60 '90	; note-on C4
v100	81 '60 '90	; note-off C4
v0	91 '62 '90	; note-on D4
v100	81 '62 '90	; note-off D4
v0	91 '59 '90	; note-on B3
v100	81 '59 '90	; note-off B3
v0	91 '60 '90	; note-on C4
v100	81 '60 '90	; note-off C4
v0	91 '57 '90	; note-on A3
v100	81 '57 '90	; note-off A3
v0	91 '53 '90	; note-on F3
v100	81 '53 '90	; note-off F3
v0	91 '55 '90	; note-on G3
v100	81 '55 '90	; note-off G3
v0	91 '48 '90	; note-on C3
v200	81 '48 '90	; note-off C3
v0	ff 2f v0	; end-of-track

And here is the output of the command

./retick twinkle.mid -t 960
"MThd"			; MIDI header chunk marker
4'6			; bytes to follow in header chunk
2'1			; file format: Type-1 (multitrack)
2'3			; number of tracks
2'960			; ticks per quarter note

;;; TRACK 0 ----------------------------------
"MTrk"			; MIDI track chunk marker
4'43			; bytes to follow in track chunk
v0	ff 3 v28 "Twinkle, Twinkle Little Star"	; track name
v0	ff 51 v3 t104	; tempo
v0	ff 2f v0	; end-of-track

;;; TRACK 1 ----------------------------------
"MTrk"			; MIDI track chunk marker
4'143			; bytes to follow in track chunk
v0	ff 3 v6 "Melody"	; track name
v0	c0 '40		; patch-change
v0	90 '72 '64	; note-on C5
v960	80 '72 '64	; note-off C5
v0	90 '72 '64	; note-on C5
v960	80 '72 '64	; note-off C5
v0	90 '79 '64	; note-on G5
v960	80 '79 '64	; note-off G5
v0	90 '79 '64	; note-on G5
v960	80 '79 '64	; note-off G5
v0	90 '81 '64	; note-on A5
v960	80 '81 '64	; note-off A5
v0	90 '81 '64	; note-on A5
v960	80 '81 '64	; note-off A5
v0	90 '79 '64	; note-on G5
v1920	80 '79 '64	; note-off G5
v0	90 '77 '64	; note-on F5
v960	80 '77 '64	; note-off F5
v0	90 '77 '64	; note-on F5
v960	80 '77 '64	; note-off F5
v0	90 '76 '64	; note-on E5
v960	80 '76 '64	; note-off E5
v0	90 '76 '64	; note-on E5
v960	80 '76 '64	; note-off E5
v0	90 '74 '64	; note-on D5
v960	80 '74 '64	; note-off D5
v0	90 '74 '64	; note-on D5
v960	80 '74 '64	; note-off D5
v0	90 '72 '64	; note-on C5
v1920	80 '72 '64	; note-off C5
v0	ff 2f v0	; end-of-track

;;; TRACK 2 ----------------------------------
"MTrk"			; MIDI track chunk marker
4'155			; bytes to follow in track chunk
v0	ff 3 v9 "Bass line"	; track name
v0	c1 '58		; patch-change
v0	91 '48 '90	; note-on C3
v960	81 '48 '90	; note-off C3
v0	91 '60 '90	; note-on C4
v960	81 '60 '90	; note-off C4
v0	91 '64 '90	; note-on E4
v960	81 '64 '90	; note-off E4
v0	91 '60 '90	; note-on C4
v960	81 '60 '90	; note-off C4
v0	91 '65 '90	; note-on F4
v960	81 '65 '90	; note-off F4
v0	91 '60 '90	; note-on C4
v960	81 '60 '90	; note-off C4
v0	91 '64 '90	; note-on E4
v960	81 '64 '90	; note-off E4
v0	91 '60 '90	; note-on C4
v960	81 '60 '90	; note-off C4
v0	91 '62 '90	; note-on D4
v960	81 '62 '90	; note-off D4
v0	91 '59 '90	; note-on B3
v960	81 '59 '90	; note-off B3
v0	91 '60 '90	; note-on C4
v960	81 '60 '90	; note-off C4
v0	91 '57 '90	; note-on A3
v960	81 '57 '90	; note-off A3
v0	91 '53 '90	; note-on F3
v960	81 '53 '90	; note-off F3
v0	91 '55 '90	; note-on G3
v960	81 '55 '90	; note-off G3
v0	91 '48 '90	; note-on C3
v1920	81 '48 '90	; note-off C3
v0	ff 2f v0	; end-of-track

from midifile.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.