forked from zebproj/xm2sco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorc2xi.c
executable file
·328 lines (290 loc) · 6.35 KB
/
orc2xi.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <unistd.h>
char zero_out(char *input, int num);
char get_size(FILE *fp); double pch2num(double pch);
double num2pch(int number);
int main(int argc, char **argv){
/* user-input variables */
float version = 1.01;
char *orc = NULL;
int samples = 1;
char note[7];
int keypos = 48;
int increment = 12;
char *dur = ".5";
int c;
opterr = 0;
while ((c = getopt (argc, argv, "i:s:k:n:d:")) != -1)
switch(c){
case 'i':
orc = optarg;
break;
case 's':
samples = atoi(optarg);
break;
case 'k':
keypos = atoi(optarg);
break;
case 'n':
increment = atoi(optarg);
break;
case 'd':
dur = optarg;
break;
case '?':
if (optopt == 'i')
fprintf (stderr, "Option -%c requires an argument. \n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'. \n", optopt);
else
fprintf (stderr, "Unknown option character `\\x%x' .\n", optopt);
return 1;
default:
abort();
}
if(orc == NULL){
printf("ORC2XI VERSION %g\nInvalid syntax. Example:\norc2xi -i ORC -s SAMPS -k KEYPOS -n INCR -d DUR\n", version);
return 0;
}
/**************/
/* RENDER ORC */
/**************/
int j;
int i;
double pch;
char x;
/* automatically add headerfile with proper sample rate and number of channels */
FILE *copy = fopen("orc2xi.orc", "w");
FILE *orc_file = fopen(orc, "r");
fprintf(copy, "sr = 44100\nksmps = 1\nnchnls = 1\n0dbfs = 1\n");
while(1){
if(feof(orc_file)){ break;}
x = fgetc(orc_file);
fputc(x, copy);
}
fclose(copy);
fclose(orc_file);
for (i = 0; i < samples; i++){
char out[20];
pch = num2pch(keypos + increment*(i));
sprintf(note, "%g", pch);
sprintf(out, "%i", i);
FILE *sco = fopen("out.sco", "w");
fprintf(sco, "i 1 0 %s %s 0.6", dur, note);
fclose(sco);
char *score = "out.sco";
//char *wav;
//sprintf(wav, "%i ", out);
char cmd[100];
//sprintf(cmd, "csound -h -o %s %s %s", out, orc, score);
strcpy(cmd, "csound -d -h -o ");
strcat(cmd, out);
strcat(cmd, " ");
strcat(cmd, "orc2xi.orc");
strcat(cmd, " ");
strcat(cmd, score);
system(cmd);
//pch = num2pch(pch2num(atof(note)) + increment);
}
/******************/
/* HEADER SECTION */
/******************/
char name[50];
strcpy(name, orc);
char xiname[54];
strcpy(xiname, name);
strcat(xiname, ".xi");
FILE *fp = fopen(xiname, "w");
char *extended = "Extended Instrument: ";
char *tracker = "FastTracker V2.00 ";
fprintf(fp, "%s%-22s%c%s%c%c",
extended,
name,
0x1a,
tracker,
0x02,
0x01);
/*XI information*/
char keymappings[96];
char volenvs[48];
char panenvs[48];
char panpoints = 2;
char volpoints = 2;
char volsuspoint = 0;
char vollpstart = 0;
char vollpend = 1;
char pansuspnt = 0;
char panlpstrt = 0;
char panlpend = 1;
char voltype = 0;
char pantype = 0;
char vibtype = 0;
char vibsweep = 0;
char vibdepth = 0;
char vibrate = 0;
char volfade[2] = {255, 127};
char reserve[22];
char numsamps = 1;
/*Zero out the arrays*/
zero_out(keymappings, 96);
/*Map out samples*/
int key_start = keypos;
for(i = 0; i < samples; i++){
for(j = 0; j < increment; j++){
keymappings[key_start+j] = i;
}
key_start += increment;
}
/*fill out upper register with highest sample*/
for(i = key_start; i < 96; i++){
keymappings[i] = samples - 1;
}
zero_out(volenvs, 48);
zero_out(panenvs, 48);
zero_out(reserve, 22);
/*Write to XI*/
printf("Writing orc header...");
for(i = 0; i < 96; i++){
fputc(keymappings[i], fp);
}
for(i = 0; i < 48; i++){
fputc(volenvs[i], fp);
}
for(i = 0; i < 48; i++){
fputc(panenvs[i], fp);
}
fputc(panpoints, fp);
fputc(volpoints, fp);
fputc(volsuspoint, fp);
fputc(vollpstart, fp);
fputc(vollpend, fp);
fputc(pansuspnt, fp);
fputc(panlpstrt, fp);
fputc(panlpend, fp);
fputc(voltype, fp);
fputc(pantype, fp);
fputc(vibtype, fp);
fputc(vibsweep, fp);
fputc(vibdepth, fp);
fputc(vibrate, fp);
fputc(volfade[0], fp);
fputc(volfade[1], fp);
for(i = 0; i < 22; i ++){
fputc(reserve[i], fp);
}
//fputc(numsamps, fp);
fprintf(fp, "%c", samples);
fputc(0, fp);
printf("Done!\n");
/****************/
/*SAMPLE SECTION*/
/****************/
printf("Writing sample headers....");
FILE *sample;
char fname[2];
int size[samples];
for(i = 0; i < samples; i++){
sprintf(fname, "%i", i);
sample = fopen(fname, "r");
/*Sample Information*/
/*find sample size */
fseek(sample, 0L, SEEK_END);
size[i] = ftell(sample);
/*Size conversion */
int byte1 = size[i] % 0x100;
int byte2 = ((size[i]-byte1) % 0x10000)/0x100;
int byte3 = ((((size[i]-byte1)-byte2*0x100)) % 0x1000000)/0x10000;
int byte4 = (((((size[i]-byte1)-byte2*0x100)-byte3*0x10000)) % 0x100000000)/0x100000;
char smp_vol = 0x40;
char smp_ft = 0;
char smp_type = 0x10;
char smp_pan = 0x80;
//char smp_note = 0x11;
int smp_note = 77 - keypos - increment*i;
char smp_name[22];
sprintf(smp_name,"Sample %i", i+1);
/* Write Sample Header */
fprintf(fp,"%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%-22s",
byte1,byte2,byte3,byte4,
0x0,0x0,0x0,0x0,
byte1,byte2,byte3,byte4,
smp_vol,
smp_ft,
smp_type,
smp_pan,
smp_note,
0x0,
smp_name
);
fclose(sample);
}
printf("Done!\n");
/*Encode sample data using sigma delta and write xi*/
printf("Encoding samples and writing to xi...");
for(i = 0; i < samples; i++){
sprintf(fname, "%i", i);
sample = fopen(fname, "r");
fseek(sample, 0L, SEEK_END);
size[i] = ftell(sample);
fseek(sample, 0, SEEK_SET);
int c1;
int c2;
int value;
int delta = 0;
int original;
for(j = 0; j < (size[i]/2); j++){
c1 = fgetc(sample);
c2 = fgetc(sample);
value = c2*0x100 + c1;
original = value;
value -= delta;
if(value < 0){
value += 0x10000;
}
delta = original;
c1 = value % 0x100;
c2 = (value-c1)/0x100;
fputc(c1, fp);
fputc(c2, fp);
}
fclose(sample);
}
fclose(fp);
printf("Done!\n");
printf("Cleaning up...");
for(i = 0; i < samples; i++){
char cmd[7];
sprintf(cmd, "rm %i", i);
system(cmd);
}
system("rm out.sco");
system("rm orc2xi.orc");
printf("Done!\n");
printf("%s created\n", xiname);
return 0;
}
char zero_out(char *input, int num){
int i;
for(i = 0; i < num; i++){
input[i] = 0;
}
return 0;
}
double pch2num(double pch){
double octave = floor(pch);
double note = (pch - octave)*100;
int number = (note)+((octave-4)*12);
//int number = note;
return number;
}
double num2pch(int number){
int octave = (number/12)+4;
double note = (number % 12)*0.01;
double pch = octave + note;
return pch;
}