-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiscale.c
149 lines (129 loc) · 3.13 KB
/
iscale.c
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// iscale.c from listing 1.6.10
// generate E.T tables for N-notes to the octave (N <= 24)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/** usage [-m][-i] iscale N startval [outfile.txt]
-m: sets format of startval as MIDI note (default: freq, Hz)
-i: prints the calculated interval as well as the absolute frequency
outfile: write data to textfile
*/
/**
* usage [-m][-i] iscale N startval [outfile.txt]
* -m: sets format of startval as MIDI note (default: freq, Hz)
* -i: prints the calculated interval as well as the absolute frequency
* outfile: write data to textfile
*/
int main(int argc, char* argv[])
{
FILE *fp;
double startval, basefreq, ratio;
double intervals[25];
int notes;
int ismidi = 0;
int write_interval = 0;
int i, err = 0;
// check first arg for flag option: argc has to be at least 2
while (argc > 1) {
if (argv[1][0] == '-') {
if (argv[1][1] == 'm') {
ismidi = 1;
} else if (argv[1][1] == 'i') {
write_interval = 1;
} else {
printf("error: unrecognised flag option %s\n", argv[1]);
return 1;
}
/* step up to next arg */
argc--;
argv++;
} else {
break;
}
}
if (argc < 3) {
printf("insufficient arguments\n");
printf("Usage: iscale [-m] [-i] N startval [outfile.txt]\n");
return 1;
}
// read and check all arguments
// argv[1] now holds N, argv[2] holds startval
notes = atoi(argv[1]);
if (notes < 1 || notes > 24) {
printf("error: N out of range. Must be between 1 and 24.\n");
return 1;
}
startval = atof(argv[2]);
if (ismidi) {
if (startval > 127.0) {
printf("error: MIDI startval must be <= 127.\n");
return 1;
}
// startval starts at 0 for MIDI
if (startval < 0.0) {
printf("error: MIDI startval must be >= 0.\n");
return 1;
}
} else {
// freq must be positive number
if (startval <= 0.0) {
printf("error: frequency startval must be positive.\n");
return 1;
}
}
// check for optional filename
fp = NULL;
if (argc == 4) {
fp = fopen(argv[3], "w");
if (fp == NULL) {
printf("WARNING: unable to create file %s\n", argv[3]);
perror("");
}
}
// fill array and write to file if created
// find basefreq, if val is MIDI
if (ismidi) {
double c0,c5;
// find base MIDI note
ratio = pow(2.0, 1.0 / 12.0);
c5 = 220.0 * pow(ratio, 3);
c0 = c5 * pow(0.5, 5);
basefreq = c0 * pow(ratio, startval);
} else {
basefreq = startval;
}
// calc ratio from notes, fill array
ratio = pow(2.0, 1.0/notes);
for (i=0; i <= notes; i++) {
intervals[i] = basefreq;
basefreq *= ratio;
}
// read array, write to screen; optionally to file
for (i=0; i <= notes; i++) {
double nextfreq = intervals[i];
double noteratio = pow(ratio, (double) i);
if (write_interval) {
printf("%d:\t%f\t%f\n", i, noteratio, nextfreq);
} else {
printf("%d:\t%f\n", i, nextfreq);
}
if (fp) {
if (write_interval) {
err = fprintf(fp, "%d:\t%f\t%f\n", i, noteratio, nextfreq);
}
else {
err = fprintf(fp, "%d:\t%f\n", i, nextfreq);
}
if (err < 0) {
break;
}
}
}
if (err < 0) {
perror("There was an error writing the file.\n");
}
if (fp) {
fclose(fp);
}
return 0;
}