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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
/*
* lingot, a musical instrument tuner.
*
* Copyright (C) 2004-2011 Ibán Cereijo Graña, Jairo Chapela Martínez.
*
* This file is part of lingot.
*
* lingot is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* lingot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with lingot; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "lingot-config-scale.h"
LingotScale* lingot_config_scale_new() {
LingotScale* scale = malloc(sizeof(LingotScale));
scale->name = NULL;
scale->notes = 0;
scale->note_name = NULL;
scale->offset_cents = NULL;
scale->offset_ratios[0] = NULL;
scale->offset_ratios[1] = NULL;
scale->base_frequency = 0.0;
return scale;
}
void lingot_config_scale_allocate(LingotScale* scale, unsigned short int notes) {
scale->notes = notes;
scale->note_name = malloc(notes * sizeof(char*));
scale->offset_cents = malloc(notes * sizeof(FLT));
scale->offset_ratios[0] = malloc(notes * sizeof(short int));
scale->offset_ratios[1] = malloc(notes * sizeof(short int));
}
void lingot_config_scale_destroy(LingotScale* scale) {
unsigned short int i;
for (i = 0; i < scale->notes; i++) {
free(scale->note_name[i]);
}
if (scale->offset_cents != NULL)
free(scale->offset_cents);
if (scale->offset_ratios[0] != NULL)
free(scale->offset_ratios[0]);
if (scale->offset_ratios[1] != NULL)
free(scale->offset_ratios[1]);
if (scale->note_name != NULL)
free(scale->note_name);
if (scale->name != NULL)
free(scale->name);
scale->name = NULL;
scale->notes = 0;
scale->note_name = NULL;
scale->offset_cents = NULL;
scale->offset_ratios[0] = NULL;
scale->offset_ratios[1] = NULL;
scale->base_frequency = 0.0;
}
void lingot_config_scale_restore_default_values(LingotScale* scale) {
unsigned short int i;
static char* tone_string[] = { "C", "C#", "D", "D#", "E", "F", "F#", "G",
"G#", "A", "A#", "B", };
lingot_config_scale_destroy(scale);
// default 12 tones equal-tempered scale hard-coded
scale->name = strdup("Default equal-tempered scale");
lingot_config_scale_allocate(scale, 12);
scale->base_frequency = MID_C_FREQUENCY;
scale->note_name[0] = strdup(tone_string[0]);
scale->offset_cents[0] = 0.0;
scale->offset_ratios[0][0] = 1;
scale->offset_ratios[1][0] = 1; // 1/1
for (i = 1; i < scale->notes; i++) {
scale->note_name[i] = strdup(tone_string[i]);
scale->offset_cents[i] = 100.0 * i;
scale->offset_ratios[0][i] = -1; // not used
scale->offset_ratios[1][i] = -1; // not used
}
}
void lingot_config_scale_copy(LingotScale* dst, LingotScale* src) {
unsigned short int i;
lingot_config_scale_destroy(dst);
*dst = *src;
dst->name = strdup(src->name);
lingot_config_scale_allocate(dst, dst->notes);
for (i = 0; i < dst->notes; i++) {
dst->note_name[i] = strdup(src->note_name[i]);
dst->offset_cents[i] = src->offset_cents[i];
dst->offset_ratios[0][i] = src->offset_ratios[0][i];
dst->offset_ratios[1][i] = src->offset_ratios[1][i];
}
}
int lingot_config_scale_parse_shift(char* char_buffer, double* cents,
short int* numerator, short int* denominator) {
const static char* delim = "/";
char* char_buffer_pointer1 = strtok(char_buffer, delim);
char* char_buffer_pointer2 = strtok(NULL, delim);
short int num, den;
int result = 1;
if (numerator != NULL) {
*numerator = -1;
}
if (denominator != NULL) {
*denominator = -1;
}
int n = 0;
if (!char_buffer_pointer2) {
n = sscanf(char_buffer_pointer1, "%lf", cents);
if (!n) {
result = 0;
}
} else {
n = sscanf(char_buffer_pointer1, "%hd", &num);
if (!n) {
result = 0;
} else {
n = sscanf(char_buffer_pointer2, "%hd", &den);
if (!n) {
result = 0;
} else {
*cents = 1200.0 * log2(1.0 * num / den);
if (numerator != NULL) {
*numerator = num;
}
if (denominator != NULL) {
*denominator = den;
}
}
}
}
if (!result) {
*numerator = 1;
*denominator = 1;
*cents = 0.0;
}
return result;
}
void lingot_config_scale_format_shift(char* char_buffer, double cents,
short int numerator, short int denominator) {
if (numerator < 0) {
sprintf(char_buffer, "%0.4lf", cents);
} else {
sprintf(char_buffer, "%hd/%hd", numerator, denominator);
}
}
int lingot_config_scale_load_scl(LingotScale* scale, char* filename) {
FILE* fp;
int i;
char* char_buffer_pointer1;
char* nl;
const static char* delim = " \t\n";
int result = 1;
# define MAX_LINE_SIZE 1000
char char_buffer[MAX_LINE_SIZE];
if ((fp = fopen(filename, "r")) == NULL) {
sprintf(char_buffer, "error opening scale file %s", filename);
perror(char_buffer);
return 0;
}
scale->base_frequency = MID_C_FREQUENCY;
fgets(char_buffer, MAX_LINE_SIZE, fp);
if (strchr(char_buffer, '!') != char_buffer) {
fclose(fp);
return 0;
}
fgets(char_buffer, MAX_LINE_SIZE, fp);
fgets(char_buffer, MAX_LINE_SIZE, fp);
nl = strrchr(char_buffer, '\r');
if (nl)
*nl = '\0';
nl = strrchr(char_buffer, '\n');
if (nl)
*nl = '\0';
scale->name = strdup(char_buffer);
fgets(char_buffer, MAX_LINE_SIZE, fp);
sscanf(char_buffer, "%hu", &scale->notes);
fgets(char_buffer, MAX_LINE_SIZE, fp);
lingot_config_scale_allocate(scale, scale->notes);
scale->note_name[0] = strdup("1");
scale->offset_cents[0] = 0.0;
scale->offset_ratios[0][0] = 1;
scale->offset_ratios[1][0] = 1; // 1/1
for (i = 1; i < scale->notes; i++) {
fgets(char_buffer, MAX_LINE_SIZE, fp);
char_buffer_pointer1 = strtok(char_buffer, delim);
int r = lingot_config_scale_parse_shift(char_buffer_pointer1,
&scale->offset_cents[i], &scale->offset_ratios[0][i],
&scale->offset_ratios[1][i]);
if (!r) {
result = 0;
}
sprintf(char_buffer, "%d", i + 1);
scale->note_name[i] = strdup(char_buffer);
}
fclose(fp);
# undef MAX_LINE_SIZE
return result;
}
|