|
|
|
|
@@ -18,7 +18,7 @@
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#define VERSION "0.67 March 14 2023 midistats"
|
|
|
|
|
#define VERSION "0.74 September 11 2023 midistats"
|
|
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
/* Microsoft Visual C++ Version 6.0 or higher */
|
|
|
|
|
@@ -52,7 +52,8 @@ void stats_finish();
|
|
|
|
|
float histogram_entropy (int *histogram, int size);
|
|
|
|
|
void stats_noteoff(int chan,int pitch,int vol);
|
|
|
|
|
void stats_eot ();
|
|
|
|
|
|
|
|
|
|
#define max(a,b) (( a > b ? a : b))
|
|
|
|
|
#define min(a,b) (( a < b ? a : b))
|
|
|
|
|
|
|
|
|
|
/* Global variables and structures */
|
|
|
|
|
|
|
|
|
|
@@ -77,6 +78,7 @@ int percanalysis;
|
|
|
|
|
int percpattern;
|
|
|
|
|
int percpatternfor;
|
|
|
|
|
int percpatternhist;
|
|
|
|
|
int pitchclassanalysis;
|
|
|
|
|
int corestats;
|
|
|
|
|
int chordthreshold; /* number of maximum number of pulses separating note */
|
|
|
|
|
int beatsPerBar = 4; /* 4/4 time */
|
|
|
|
|
@@ -104,6 +106,7 @@ int percanalysis = 0;
|
|
|
|
|
int percpattern = 0;
|
|
|
|
|
int percpatternfor = 0;
|
|
|
|
|
int percpatternhist = 0;
|
|
|
|
|
int pitchclassanalysis = 0;
|
|
|
|
|
int corestats = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -112,8 +115,10 @@ int trackcount = 0;
|
|
|
|
|
|
|
|
|
|
int notechan[2048],notechanvol[2048]; /*for linking on and off midi
|
|
|
|
|
channel commands */
|
|
|
|
|
int last_tick[17]; /* for getting last pulse number in MIDI file */
|
|
|
|
|
int lastTick[2048]; /* for getting last pulse number for chan (0-15) and pitch (0-127) in MIDI file */
|
|
|
|
|
int last_on_tick[17]; /* for detecting chords [SS] 2019-08-02 */
|
|
|
|
|
int channel_active[17]; /* for dealing with chords [SS] 2023-08-30 */
|
|
|
|
|
int channel_used_in_track[17]; /* for dealing with quietTime [SS] 2023-09-06 */
|
|
|
|
|
|
|
|
|
|
int histogram[256];
|
|
|
|
|
unsigned char drumpat[8000];
|
|
|
|
|
@@ -129,7 +134,11 @@ struct trkstat {
|
|
|
|
|
int notecount[17];
|
|
|
|
|
int chordcount[17];
|
|
|
|
|
int notemeanpitch[17];
|
|
|
|
|
int notepitchmin[17];
|
|
|
|
|
int notepitchmax[17];
|
|
|
|
|
int notelength[17];
|
|
|
|
|
int notelengthmin[17];
|
|
|
|
|
int notelengthmax[17];
|
|
|
|
|
int pitchbend[17];
|
|
|
|
|
int pressure[17];
|
|
|
|
|
int cntlparam[17];
|
|
|
|
|
@@ -139,6 +148,7 @@ struct trkstat {
|
|
|
|
|
int lastNoteOff[17];
|
|
|
|
|
int quietTime[17];
|
|
|
|
|
int rhythmpatterns[17];
|
|
|
|
|
int numberOfGaps[17];
|
|
|
|
|
} trkdata;
|
|
|
|
|
|
|
|
|
|
/* The trkstat references the individual channels in the midi file.
|
|
|
|
|
@@ -417,8 +427,9 @@ void stats_header (int format, int ntrks, int ldivision)
|
|
|
|
|
trkdata.cntlparam[i] = 0; /* [SS] 2022-03-04 */
|
|
|
|
|
trkdata.pressure[i] = 0; /* [SS] 2022-03-04 */
|
|
|
|
|
trkdata.quietTime[i] = 0; /* [SS] 2022-08-22 */
|
|
|
|
|
trkdata.numberOfGaps[i] = 0; /* [SS] 2023-09-07 */
|
|
|
|
|
progcolor[i] = 0;
|
|
|
|
|
channel2prog[i] = -1;
|
|
|
|
|
channel2prog[i] = 0; /* [SS] 2023-06-25-8/
|
|
|
|
|
channel2nnotes[i] = 0;
|
|
|
|
|
chnactivity[i] = 0; /* [SS] 2018-02-02 */
|
|
|
|
|
}
|
|
|
|
|
@@ -481,7 +492,7 @@ if (nprogs > 0) output_progs_data();
|
|
|
|
|
else {
|
|
|
|
|
for (i=0;i<17;i++)
|
|
|
|
|
if(chnactivity[i] > 0)
|
|
|
|
|
progactivity[channel2prog[i]] = chnactivity[i];
|
|
|
|
|
progactivity[channel2prog[i]] += chnactivity[i]; /* [SS] 2023-06-25 */
|
|
|
|
|
output_progs_data();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -514,15 +525,6 @@ if (npulses > 0)
|
|
|
|
|
for (i=1;i<17;i++) printf("%5.2f ",chnactivity[i]/(double) trkdata.npulses[0]);
|
|
|
|
|
else
|
|
|
|
|
for (i=0;i<17;i++) printf("%5.2f ",(double) chnactivity[i]);
|
|
|
|
|
printf("\nquietTime ");
|
|
|
|
|
for (i=1;i<17;i++) {
|
|
|
|
|
delta = trkdata.npulses[0] - trkdata.quietTime[i];
|
|
|
|
|
if (trkdata.quietTime[i] < quietLimit) delta = 0;
|
|
|
|
|
delta = delta / (double) trkdata.npulses[0];
|
|
|
|
|
/* printf (" %5.3f ", delta); */
|
|
|
|
|
printf (" %d ", trkdata.quietTime[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("\npitchentropy %f\n",histogram_entropy(pitchclass_activity,12));
|
|
|
|
|
printf("totalrhythmpatterns =%d\n",nrpatterns);
|
|
|
|
|
printf("collisions = %d\n",ncollisions);
|
|
|
|
|
@@ -582,10 +584,17 @@ for (i=1;i<17;i++) {
|
|
|
|
|
printf("trkinfo ");
|
|
|
|
|
printf("%d %d ",i,trkdata.program[i]); /* channel number and program*/
|
|
|
|
|
printf("%d %d ",trkdata.notecount[i],trkdata.chordcount[i]);
|
|
|
|
|
printf("%d %d ",trkdata.notemeanpitch[i], trkdata.notelength[i]);
|
|
|
|
|
/* [SS] 2023-08-22 */
|
|
|
|
|
if (i != 10) printf("%d %d ",trkdata.notemeanpitch[i], trkdata.notelength[i]);
|
|
|
|
|
else
|
|
|
|
|
printf("-1 0 ");
|
|
|
|
|
printf("%d %d ",trkdata.cntlparam[i],trkdata.pressure[i]); /* [SS] 2022-03-04 */
|
|
|
|
|
printf("%d %d",trkdata.quietTime[i],trkdata.rhythmpatterns[i]);
|
|
|
|
|
trkdata.quietTime[i] = 0;
|
|
|
|
|
printf("%d %d ",trkdata.quietTime[i],trkdata.rhythmpatterns[i]);
|
|
|
|
|
if (i != 10) printf("%d %d %d %d %d",trkdata.notepitchmin[i], trkdata.notepitchmax[i] ,trkdata.notelengthmin[i], trkdata.notelengthmax[i], trkdata.numberOfGaps[i]);
|
|
|
|
|
else
|
|
|
|
|
printf("-1 0");
|
|
|
|
|
trkdata.quietTime[i] = 0; /* in case channel i is used in another track */
|
|
|
|
|
trkdata.numberOfGaps[i] = 0;
|
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
|
|
channel2nnotes[i] += trkdata.notecount[i] + trkdata.chordcount[i];
|
|
|
|
|
@@ -602,19 +611,28 @@ void stats_trackstart()
|
|
|
|
|
for (i=0;i<17;i++) {
|
|
|
|
|
trkdata.notecount[i] = 0;
|
|
|
|
|
trkdata.notemeanpitch[i] = 0;
|
|
|
|
|
trkdata.notepitchmin[i] = 128;
|
|
|
|
|
trkdata.notepitchmax[i] = 0;
|
|
|
|
|
trkdata.notelength[i] = 0;
|
|
|
|
|
trkdata.notelengthmin[i] = 10000;
|
|
|
|
|
trkdata.notelengthmax[i] = 0;
|
|
|
|
|
trkdata.chordcount[i] = 0;
|
|
|
|
|
trkdata.cntlparam[i] = 0;
|
|
|
|
|
last_tick[i] = -1;
|
|
|
|
|
last_on_tick[i] = -1;
|
|
|
|
|
channel_active[i] = 0;
|
|
|
|
|
}
|
|
|
|
|
printf("trk %d \n",tracknum);
|
|
|
|
|
|
|
|
|
|
for (i=0;i<2048;i++) lastTick[i] = -1;
|
|
|
|
|
for (i=0;i<17;i++) channel_used_in_track[i] = 0; /* [SS] 2023-09-06 */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void stats_trackend()
|
|
|
|
|
{
|
|
|
|
|
trkdata.npulses[tracknum] = Mf_currtime;
|
|
|
|
|
int chan;
|
|
|
|
|
if (trkdata.npulses[0] < Mf_currtime) trkdata.npulses[0] = Mf_currtime;
|
|
|
|
|
for (chan = 1; chan < 17; chan++) /* [SS] 2023-09-06 */
|
|
|
|
|
if (channel_used_in_track[chan] > 0) trkdata.quietTime[chan] += (trkdata.npulses[0] - trkdata.lastNoteOff[chan]);
|
|
|
|
|
output_track_summary();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -626,7 +644,10 @@ int chan, pitch, vol;
|
|
|
|
|
int delta;
|
|
|
|
|
int barnum;
|
|
|
|
|
int unit;
|
|
|
|
|
int dithermargin; /* [SS] 2023-08-22 */
|
|
|
|
|
|
|
|
|
|
channel_used_in_track[chan+1]++; /* [SS] 2023-09-06 */
|
|
|
|
|
dithermargin = unitDivision/2 - 1;
|
|
|
|
|
if (vol == 0) {
|
|
|
|
|
/* treat as noteoff */
|
|
|
|
|
stats_noteoff(chan,pitch,vol);
|
|
|
|
|
@@ -634,17 +655,20 @@ int chan, pitch, vol;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
trkdata.notemeanpitch[chan+1] += pitch;
|
|
|
|
|
trkdata.notepitchmax[chan+1] = max(trkdata.notepitchmax[chan+1],pitch);
|
|
|
|
|
trkdata.notepitchmin[chan+1] = min(trkdata.notepitchmin[chan+1],pitch);
|
|
|
|
|
if (trkdata.lastNoteOff[chan+1] >= 0) {
|
|
|
|
|
delta = Mf_currtime - trkdata.lastNoteOff[chan+1];
|
|
|
|
|
trkdata.lastNoteOff[chan+1] = -1; /* in case of chord */
|
|
|
|
|
if (delta > quietLimit) {
|
|
|
|
|
trkdata.quietTime[chan+1] += delta;
|
|
|
|
|
trkdata.numberOfGaps[chan+1]++;
|
|
|
|
|
trkdata.lastNoteOff[chan+1] = -1; /* in case of chord */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (abs(Mf_currtime - last_on_tick[chan+1]) < chordthreshold) trkdata.chordcount[chan+1]++;
|
|
|
|
|
else trkdata.notecount[chan+1]++; /* [SS] 2019-08-02 */
|
|
|
|
|
last_tick[chan+1] = Mf_currtime;
|
|
|
|
|
lastTick[chan*128 + pitch] = Mf_currtime;
|
|
|
|
|
last_on_tick[chan+1] = Mf_currtime; /* [SS] 2019-08-02 */
|
|
|
|
|
/* last_on_tick not updated by stats_noteoff */
|
|
|
|
|
|
|
|
|
|
@@ -659,7 +683,7 @@ int chan, pitch, vol;
|
|
|
|
|
barChn[chan].rhythmPattern = 0;
|
|
|
|
|
barChn[chan].activeBarNumber = barnum;
|
|
|
|
|
}
|
|
|
|
|
unit = (Mf_currtime % divisionsPerBar)/unitDivision;
|
|
|
|
|
unit = ((Mf_currtime+dithermargin) % divisionsPerBar)/unitDivision;
|
|
|
|
|
//printf("unit = %d pattern = %d \n",unit,barChn[chan].rhythmPattern);
|
|
|
|
|
barChn[chan].rhythmPattern = barChn[chan].rhythmPattern |= (1UL << unit);
|
|
|
|
|
}
|
|
|
|
|
@@ -672,6 +696,8 @@ int chan, pitch, vol;
|
|
|
|
|
else drumhistogram[pitch]++;
|
|
|
|
|
}
|
|
|
|
|
else pitchhistogram[pitch % 12]++; /* [SS] 2017-11-01 */
|
|
|
|
|
|
|
|
|
|
channel_active[chan+1]++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -684,17 +710,27 @@ void stats_noteoff(int chan,int pitch,int vol)
|
|
|
|
|
int length;
|
|
|
|
|
int program;
|
|
|
|
|
/* ignore if there was no noteon */
|
|
|
|
|
if (last_tick[chan+1] == -1) return;
|
|
|
|
|
length = Mf_currtime - last_tick[chan+1];
|
|
|
|
|
if (lastTick[chan*128+pitch] == -1) return;
|
|
|
|
|
length = Mf_currtime - lastTick[chan*128+pitch];
|
|
|
|
|
trkdata.notelength[chan+1] += length;
|
|
|
|
|
trkdata.notelengthmax[chan+1] = max(trkdata.notelengthmax[chan+1],length);
|
|
|
|
|
trkdata.notelengthmin[chan+1] = min(trkdata.notelengthmin[chan+1],length);
|
|
|
|
|
//if (length < 3) printf("chan = %d lasttick = %d currtime = %ld\n",chan,lastTick[chan*128+pitch],Mf_currtime);
|
|
|
|
|
trkdata.lastNoteOff[chan+1] = Mf_currtime; /* [SS] 2022.08.22 */
|
|
|
|
|
chnactivity[chan+1] += length;
|
|
|
|
|
if (chan == 9) return; /* drum channel */
|
|
|
|
|
pitchclass_activity[pitch % 12] += length;
|
|
|
|
|
program = trkdata.program[chan+1];
|
|
|
|
|
progactivity[program] += length;
|
|
|
|
|
channel_active[chan+1]--;
|
|
|
|
|
/* [SS] 2018-04-18 */
|
|
|
|
|
if(Mf_currtime > last_tick[chan+1]) last_tick[chan+1] = Mf_currtime;
|
|
|
|
|
if(Mf_currtime > lastTick[chan*128+pitch] && channel_active[chan+1] == 0)
|
|
|
|
|
lastTick[chan*128+pitch] = Mf_currtime; /* [SS] 2023.08.30 handle chords */
|
|
|
|
|
|
|
|
|
|
if (length > 4800) {
|
|
|
|
|
lastTick[chan*128+pitch] = Mf_currtime; /* handle stuck note [SS] 2023.08.30 */
|
|
|
|
|
channel_active[chan+1] = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -728,7 +764,7 @@ if (trkdata.program[chan+1] != 0) {
|
|
|
|
|
printf("program %d %d\n",chan+1,program);
|
|
|
|
|
trkdata.program[chan+1] = program;
|
|
|
|
|
}
|
|
|
|
|
if (channel2prog[chan+1]== -1) channel2prog[chan+1] = program;
|
|
|
|
|
if (channel2prog[chan+1]== 0) channel2prog[chan+1] = program; /* [SS] 2023-06-25*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -982,6 +1018,7 @@ for (i = 0; i <lastEvent; i++) {
|
|
|
|
|
if (index >= 8000) {printf("index too large in drumpattern\n");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
part = 3 - part; /* order the bits going left to right */
|
|
|
|
|
drumpat[index] = drumpat[index] |= 1 << part;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -1009,11 +1046,25 @@ for (i = 0; i <lastEvent; i++) {
|
|
|
|
|
}
|
|
|
|
|
remainder = onset % division;
|
|
|
|
|
part = remainder/quarter;
|
|
|
|
|
part = 3 - part; /* order the bits from left to right */
|
|
|
|
|
if (pitch == perc1) drumpat[index] = drumpat[index] |= 1 << part;
|
|
|
|
|
else drumpat[index] = drumpat[index] |= 16 * (1 << part);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pitchClassAnalysis () {
|
|
|
|
|
int i;
|
|
|
|
|
int channel;
|
|
|
|
|
int pitch;
|
|
|
|
|
for (i=0;i<12;i++) pitchclass_activity[i] = 0;
|
|
|
|
|
for (i = 0; i < lastEvent; i++) {
|
|
|
|
|
channel = midievents[i].channel;
|
|
|
|
|
if (channel == 9) continue;
|
|
|
|
|
pitch = midievents[i].pitch;
|
|
|
|
|
pitchclass_activity[pitch % 12]++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void output_perc_pattern (int i) {
|
|
|
|
|
int left,right;
|
|
|
|
|
left = i/16;
|
|
|
|
|
@@ -1078,6 +1129,17 @@ if (bassmax && snaremax) {
|
|
|
|
|
dualDrumPattern(bassindex,snareindex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void outputPitchClassHistogram() {
|
|
|
|
|
int i;
|
|
|
|
|
float activity;
|
|
|
|
|
activity = 0;
|
|
|
|
|
for (i=0;i<12;i++) activity += (float) pitchclass_activity[i];
|
|
|
|
|
for (i=0;i<11;i++) printf("%5.3f,",pitchclass_activity[i]/activity);
|
|
|
|
|
printf("%5.3f",pitchclass_activity[11]/activity);
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void corestatsOutput() {
|
|
|
|
|
printf("%d\t%d\t%d\n", division,lastEvent,lastBeat);
|
|
|
|
|
@@ -1229,6 +1291,12 @@ int argc;
|
|
|
|
|
stats = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
arg = getarg("-pitchclass",argc,argv);
|
|
|
|
|
if (arg != -1) {
|
|
|
|
|
pitchclassanalysis = 1;
|
|
|
|
|
stats = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
arg = getarg("-o",argc,argv);
|
|
|
|
|
if ((arg != -1) && (arg < argc)) {
|
|
|
|
|
@@ -1256,6 +1324,7 @@ int argc;
|
|
|
|
|
printf(" -ppat\n");
|
|
|
|
|
printf(" -ppatfor\n");
|
|
|
|
|
printf(" -ppathist\n");
|
|
|
|
|
printf(" -pitchclass\n");
|
|
|
|
|
printf(" -ver version number\n");
|
|
|
|
|
printf(" -d <number> debug parameter\n");
|
|
|
|
|
printf(" The input filename is assumed to be any string not\n");
|
|
|
|
|
@@ -1308,6 +1377,10 @@ if (percpatternhist) {
|
|
|
|
|
drumPatternHistogram();
|
|
|
|
|
}
|
|
|
|
|
if (corestats) corestatsOutput();
|
|
|
|
|
if (pitchclassanalysis) {
|
|
|
|
|
pitchClassAnalysis();
|
|
|
|
|
outputPitchClassHistogram();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -1322,6 +1395,7 @@ int argc;
|
|
|
|
|
arg = process_command_line_arguments(argc,argv);
|
|
|
|
|
if(stats == 1) midistats(argc,argv);
|
|
|
|
|
if(pulseanalysis || corestats || percanalysis ||\
|
|
|
|
|
percpatternfor || percpattern || percpatternhist) loadEvents();
|
|
|
|
|
percpatternfor || percpattern || percpatternhist ||\
|
|
|
|
|
pitchclassanalysis) loadEvents();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|