File: midi_utils.c

package info (click to toggle)
gretl 2016d-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 48,620 kB
  • ctags: 22,779
  • sloc: ansic: 345,830; sh: 4,648; makefile: 2,712; xml: 570; perl: 364
file content (134 lines) | stat: -rw-r--r-- 2,271 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* MIDI utility functions for gretl audio plugin */

#include <stdio.h>

#include "miditypes.h"
#include "midi_utils.h"

int write_var_len (long val, FILE *fp)
{
    long buf = val & 0x7f;
    int wrote = 0;

    while ((val >>= 7) > 0) {
	buf <<= 8;
	buf |= 0x80;
	buf += (val & 0x7f);
    }

    while (1) {
	putc(buf, fp);
	wrote++;
	if (buf & 0x80) buf >>= 8;
	else break;
    }

    return wrote;
}

int delta_time (double beat, int nticks, FILE *fp)
{
    double dval = beat * nticks;
    long val = (long) dval;

    return write_var_len(val, fp); 
}

int write_be_long (unsigned long val, FILE *fp)
{
    unsigned char c[4];

    c[0] = (val >> 24) & 0xFFL; 
    c[1] = (val >> 16) & 0xFFL;
    c[2] = (val >> 8) & 0xFFL;
    c[3] = val & 0xFFL;
    
    return fwrite(c, 1, 4, fp);
}

int write_be_24 (int val, FILE *fp)
{
    unsigned char c[3];

    c[0] = (val >> 16) & 0xFFL;
    c[1] = (val >> 8) & 0xFFL;
    c[2] = val & 0xFFL;
    
    return fwrite(c, 1, 3, fp);
}

int write_be_short (unsigned short val, FILE *fp)
{
    unsigned char c[2];

    c[0] = (val >> 8) & 0xFF;
    c[1] = val & 0xFF;

    return fwrite(c, 1, 2, fp);
}

int write_midi_byte (unsigned char c, FILE *fp) 
{
    putc(c, fp);

    return 1;
}

int write_midi_meta (int val, FILE *fp)
{
    unsigned char c = val;

    putc(MIDI_META, fp);
    putc(c, fp);

    return 2;
}

int write_midi_eot (FILE *fp)
{
    unsigned char eot[] = { 0xff, 0x2f, 0x00 };

    return fwrite(eot, 1, 3, fp);
}

void write_midi_header (int format, int tracks, int ticks,
			FILE *fp)
{
    const char *hdr = "MThd";

    fwrite(hdr, 1, 4, fp);
    write_be_long(6, fp);
    write_be_short(format, fp);
    write_be_short(tracks, fp);
    write_be_short(ticks, fp);
}

const char *get_patch_name (int pnum)
{
    switch (pnum) {
    case PC_GRAND:
	return "Acoustic Grand Piano";
    case PC_ELEC_PIANO_2:
	return "Electric Piano 2";
    case PC_HARPSICHORD:
	return "Harpsichord";
    case PC_MUSIC_BOX:
	return "Music Box";
    case PC_HARP:
	return "Harp";
    case PC_TRUMPET:
	return "Trumpet";
    case PC_ALTO_SAX:
	return "Alto Sax";
    case PC_BASSOON:
	return "Bassoon";
    case PC_MARIMBA:
	return "Marimba";
    case PC_CELLO:
	return "Cello";
    default:
	return "Unknown";
    }
}