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
| Dim startText As Integer
Dim SongTxt As String
Public Division As Long
Public dmt(100, 100)
'
'Прочитать текст из караоке-файла, вычислить задержки, создать массив строк и задержек
Public Function ReadText(ByVal KarFile As String, ByVal delta As Double) As String
Dim ch As Long, i As Long
Dim mt As String '*4
Dim FormatType As Integer
Dim NmbTrack As Integer
Dim NumBytes As Double
Dim Status As Byte
Dim pos As Long, pPos As Long, P As Long
Dim b1 As Byte, b2 As Byte, b3 As Byte, b4 As Byte, B5 As Byte, b As Byte
Dim EndOfTrack As Boolean
Dim vLng As Long
Dim Track As Integer
Dim lenFile As Double
'
Dim Stat As String
Dim VR As Byte
Dim SR As Byte
Dim adr0 As Long
Dim adr1 As Long
Dim uI As Integer
Dim tStr As String
Dim lnStr As String
Dim tm
Dim mti As Integer
'
mti = 0
Erase dmt
ReDim dmt(2, 1)
startText = 0
SongTxt = ""
delta = 0
ch = FreeFile
Open KarFile For Binary As ch
Get #ch, 1, mt
If mt <> "MThd" Then GoTo ReadKarFileEND
Get #ch, 5, b1
Get #ch, 6, b2
Get #ch, 7, b3
Get #ch, 8, b4
If Not (b1 = 0 And b2 = 0 And b3 = 0 And b4 = 6) Then GoTo ReadKarFileEND
'==============
Get #ch, 9, b1
Get #ch, 10, b2
FormatType = b1 * 256 + b2
If FormatType <> 1 Then GoTo ReadKarFileEND
'========================================
Get #ch, 11, b1
Get #ch, 12, b2
NmbTrack = b1 * 256 + b2
If NmbTrack < 3 Then GoTo ReadKarFileEND
'=========================================
Get #ch, 13, b1
Get #ch, 14, b2
'
'======================================
Division = (b1 * CLng(256) + b2) * 0.25
EndOfTrack = False
pos = 15
'Первый трек
Get #ch, pos, mt
If mt <> "MTrk" Then GoTo ReadKarFileEND
Get #ch, , b1
Get #ch, , b2
Get #ch, , b3
Get #ch, , b4
'Длина первого трека
NumBytes = CDbl(CLng(b1) * 256 * 256 * 256 + CLng(b2) * 256 * 256 + CLng(b3) * 256 + CLng(b4))
pos = pos + 8 + NumBytes
'===============================================
'Второй трек
Get #ch, pos, mt
If mt <> "MTrk" Then GoTo ReadKarFileEND
Get #ch, , b1
Get #ch, , b2
Get #ch, , b3
Get #ch, , b4
'Длина второго трека
NumBytes = CDbl(CLng(b1) * 256 * 256 * 256 + CLng(b2) * 256 * 256 + CLng(b3) * 256 + CLng(b4))
pos = pos + 8 + NumBytes
'===============================================
'Третий трек
Get #ch, pos, mt
If mt <> "MTrk" Then GoTo ReadKarFileEND
Get #ch, , b1
Get #ch, , b2
Get #ch, , b3
Get #ch, , b4
'Длина третьего трека
NumBytes = CDbl(CLng(b1) * 256 * 256 * 256 + CLng(b2) * 256 * 256 + CLng(b3) * 256 + CLng(b4))
'===============================================
pos = pos + 8
pPos = pos
Dim pp As Long
pp = pPos
Status = 0
While pos - pp < NumBytes
Get #ch, pos, b1
If b1 = &HFF Then
pos = pos + 1
Status = b1
SongTxt = SongTxt & readTextFF(ch, pos, EndOfTrack, 3, Len(SongTxt), pPos, NumBytes)
Else
delta = delta + rdVL(ch, pos)
'===============
Get #ch, pos, b1
SR = b1 And &H80
If SR = &H80 Then
Status = b1
pos = pos + 1
End If
'
VR = Status And &HF0
If VR = &H80 Then
pos = pos + 2
ElseIf VR = &H90 Then
pos = pos + 2
ElseIf VR = &HB0 Then
pos = pos + 2
ElseIf VR = &HC0 Then
pos = pos + 1
ElseIf VR = &HD0 Then
pos = pos + 1
ElseIf VR = &HE0 Then
pos = pos + 2
ElseIf VR = &HF0 Then
If Status = &HFE Then
ElseIf Status = &HFF Then
tStr = readTextFF(ch, pos, EndOfTrack, 3, Len(SongTxt), pPos, NumBytes)
SongTxt = SongTxt & tStr
uI = UBound(dmt, 2)
If tStr <> "" Then
If Left(tStr, 2) = Chr(13) & Chr(10) Then
uI = uI + 1
ReDim Preserve dmt(2, uI)
lnStr = Len(tStr) - 2
tStr = Right(tStr, lnStr)
dmt(0, uI - 1) = tStr
dmt(1, uI - 1) = delta '/ Division
Else
'и формируем строку
dmt(0, uI - 1) = dmt(0, uI - 1) & tStr
Debug.Print(tStr)
If uI = 1 And mti = 0 Then
dmt(1, uI - 1) = delta '/ Division
mti = 1
End If
End If
End If
If EndOfTrack Then
If lenFile < NumBytes Then GoTo endWend
End If
ElseIf Status = &HF0 Then
P = pos
vLng = rdVL(ch, pos)
pos = pos + vLng
ElseIf Status = &HF7 Then
Else
End If
Else
VR = VR
SR = SR
End If
End If
End While
endWend:
'''''''''''''''''''''''''''''''''''''''
'
ReadKarFileEND:
Close(ch)
ReadText = SongTxt
End Function
'
'
Function readTextFF(ByVal ch As Long, ByVal pos As Long, ByVal EndOfTrack As Boolean, ByVal Track As Integer, ByVal ub As Integer, ByVal pPos, ByVal NumBytes)
Dim i As Long, Bytes As Long
Dim b As Byte, b1 As Byte, b2 As Byte, b3 As Byte, b4 As Byte, B5 As Byte
Dim txt As String
Dim mt As Integer
Dim bk As Integer
txt = ""
If pos - pPos > NumBytes Then
'Если встретился конец трека
GoTo exitIF
End If
Get #ch, pos, b2: pos = pos + 1
If b2 = 0 Then
Get #ch, pos, b2: pos = pos + 1
If b2 <> 2 Then GoTo exitIF
pos = pos + 2
ElseIf b2 >= 1 And b2 <= 7 Then
bk = -1
Bytes = rdVL(ch, pos)
mt = 1
If b2 = 1 Then
For i = 1 To Bytes
Get #ch, pos, b: pos = pos + 1
If b = 0 Then GoTo ext1
If Chr(b) = "@" Then
mt = 0
End If
If startText <> 1 And mt = 1 Then
'If b = 92 Or b = 47 Then
startText = 1
bk = -1
'End If
End If
If Track = 3 And b2 = 1 And startText = 1 And mt = 1 Then
If ub <> 0 Then
If b = 92 Or b = 47 Then
txt = txt & Chr(13)
b = 10
End If
If b = 32 And bk = 32 Then GoTo eXt
txt = txt & Chr(b)
Else
If b <> 92 And b <> 47 Then
If b = 32 And bk = 32 Then GoTo eXt
txt = txt & Chr(b)
End If
End If
eXt:
bk = b
End If
ext1:
Next i
Else
If b2 = 7 Or b2 = 6 Then
' Call rdVL(ch, pos)
GoTo exitIF
End If
pos = pos + Bytes
End If
pos = pos
ElseIf b2 = &H20 Then
pos = pos + 2
ElseIf b2 = &H21 Then
pos = pos + 2
ElseIf b2 = &H2F Then
pos = pos + 1
EndOfTrack = True
ElseIf b2 = &H51 Then
pos = pos + 4
ElseIf b2 = &H54 Then
Get #ch, pos, b3
pos = pos + b3
ElseIf b2 = &H58 Then
pos = pos + 5
ElseIf b2 = &H59 Then
Get #ch, pos, b3
pos = pos + b3
ElseIf b2 = &H7F Then
End If
exitIF:
readTextFF = txt
End Function
Public Function rdVL(ByVal ch As Long, ByVal pos As Long) As Long
Dim l(4) As Long
Dim b(4) As Byte
Dim k As Integer, i As Integer, n As Integer
Dim tm As Long
Dim pPos As Long
'
l(0) = 1
l(1) = 128
l(2) = 16384
l(3) = 2097152
pPos = pos
For i = 0 To 3
k = i
Get #ch, pPos + i, b(3 - i): pos = pos + 1
If b(3 - i) < 128 Then
Exit For
Else
b(3 - i) = b(3 - i) - 128
End If
n = n
Next i
For i = k To 0 Step -1
tm = tm + b(3 - i) * l(k - i)
Next i
rdVL = tm
End Function |