File size: 4,082 Bytes
557a17a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
:-include('../listprologinterpreter/la_files.pl').
:-include('../listprologinterpreter/la_maths.pl').
:-include('../listprologinterpreter/la_strings.pl').
:-include('../listprologinterpreter/la_strings_string.pl').

data_to_stems :-

	directory_files("data_files/",F),
	delete_invisibles_etc(F,G),

	findall([Filex1,Header,Footer,Instruments],(member(Filex1,G),
	string_concat("data_files/",Filex1,Filex),
		phrase_from_file_s(string(File_codes), Filex),
		%string_codes(File_string,File_codes),
		%atom_to_term(String02b,Data_file,[])

	%A=`b i `,
%trace,
	ba(File_codes, Header,Footer,Middle),
	%string_codes(Middle2,Middle),
	%trace,writeln1(Middle2),
		% retrieve data to make stem files
%length(B,6),
%foldr(B,Middle,_)
%trace,
	%writeln1(split_on_instrument(Middle,[],Instruments)),
	%trace,
	split_on_instrument(Middle,[],Instruments)		%,
		%writeln(	split_on_instrument(Middle,[],Instruments))
		%
		),String00),

%writeln(String00),
	findall(_,(member([File,Header,Footer,Instruments],String00),

	length(Instruments,Length),
	numbers(Length,1,[],Ns),
		
	findall(_,(member(N,Ns),get_item_n(Instruments,N,Instrument),
	foldr(string_concat,[Header,Instrument,Footer],"",File1),
	%trace,
	%File=File2,
	string_concat(File2,".txt",File),
	foldr(string_concat,["stems/",File2,"-","stem",N,".txt"],"",File_name),
	save_file_s(File_name,File1)),_)),_),!.

ba(File_codes, Header,Footer,Middle) :-
%trace,

	A=`BA    1   CR         0   TR  1   CH  1   Instrument `,

	append(Header2,D2,File_codes),
	string_codes(Header,Header2),
	append(A,D,D2),
%trace,	
	foldr(append,[A,D],[],D4),
	reverse(D4,File_codes_reversed),
	append(Footer1,D3,File_codes_reversed),
	append(`\n`,_D1,D3),
	reverse(Footer1,Footer2),
	string_codes(Footer,Footer2),

	foldr(append,[%`\n`,
	D3],[],E),
	reverse(E,Middle),
	!.

split_on_instrument(Middle,Instruments1,Instruments2) :-
	A=`Instrument`,
	%writeln(Middle),
	%trace,
	
	%A=[i],
	append(B,C22,Middle),
	append(A,C12,C22),
	%trace,
	%writeln(here1),
		
	(not((append(_B11,C11,C12),append(A,_C31,C11)))->
	(string_codes(Middle1,Middle),
	append(Instruments1,[Middle1],Instruments2));
%split_on_instrument(Middle,Instruments1,Instruments2) :-
	(%A=`Instrument `,
	%trace,
	%append(_B10,C2,Middle),
	%append(A,C,C2),
	%trace,
	append(B1,C3,C12),
	append(A,C1,C3),
	
	
	%writeln(here2),
	%trace,
	%reverse(B1,B_reversed),
	%append(Start_of_instrument1,D3,B_reversed),
	append(D33,Start_of_instrument1,B1),
	reverse(D33,D3),
	
	reverse(D3,D31),
	
	
	%writeln(here3),
	
	append(`\n`,_D1,D3),
	append(End_of_instrument1,D22,C1),
	
	foldr(append,[B,A,D31],[],D32),
	
	append(`\n`,D21,D22),
	%trace,
	End_of_instrument1=End_of_instrument,
	%reverse(End_of_instrument1,End_of_instrument),
	%reverse(D21,D2),
	D21=D2,

	%writeln(here4),
	%trace,
	reverse(Start_of_instrument1,Start_of_instrument2),
	append(B41,C4,Start_of_instrument2),
	append(`\n`,C41,C4),
	reverse(B41,B412),
	reverse(C4,C42),
	reverse(C41,C412),


	reverse(Start_of_instrument1,Start_of_instrument),
	

	foldr(append,[B412,A,C1%Start_of_instrument1,A,C1
	],[],Instrument1),
	Instrument1=Instrument2,
	%*/
	D34=D32,

	foldr(append,[D34,C42],[],D441),

	string_codes(D44,D441),

	append(Instruments1,[D44],Instruments31),
	%flatten(Instruments31,Instruments32),
	/*
	writeln([b412,B412]),
	writeln([c42,C42]),
	writeln([c412,C412]),
	writeln([b41,B41]),
	writeln([c4,C4]),
	writeln([c41,C41]),
	writeln([d3,D3]),
	writeln([b,B]),
	writeln([c22,C22]),
	writeln([c12,C12]),
	writeln(C12),
	writeln([c1,C1]),
	writeln([b1,B1]),
	writeln([d32,D32]),
	writeln([start_of_instrument1,Start_of_instrument1]),
	writeln([end_of_instrument1,End_of_instrument1]),
	%trace,
	%append()
	writeln(split_on_instrument(Instrument1,Instruments31,Instruments2)),
	*/
	split_on_instrument(Instrument1,Instruments31,Instruments2))),!.

stems_to_mid :-

	check_asc2mid,
	
	directory_files("stems/",F),
	delete_invisibles_etc(F,G),

	findall(_,(
	
	member(File,G),
	
	string_concat(File1,".txt",File),
	
	foldr(string_concat,["./asc2mid stems/",File," > mid/",File1,".mid"],Command),
	
	shell1_s(Command)

	),_),!.