Visual Basic Programming Code Examples
Visual Basic > Multimedia Animation and Sound Code Examples
Functions for CDAudio (requires MCI-control)
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
Functions for CDAudio (requires MCI-control)
'save file and rename them to [name].BAS
Attribute VB_Name = "CD_Serial_Number"
Option Explicit
Private Declare Function GetPrivateProfileString Lib "Kernel32" Alias "GetPrivateProfileStringA" _
(ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "Kernel32" Alias "WritePrivateProfileStringA" _
(ByVal AppName As String, ByVal KeyName As String, ByVal keydefault As String, ByVal Filename As String) As Long
Global CDMin As Integer
Global CDSec As Integer
Global TMin As Integer
Global TSec As Integer
Global RMin As Integer
Global RSec As Integer
Global TimeTrack As String
Global TimeElapsed As String
Global TimeRemaining As String
Global Artist1 As String
Global Title1 As String
Global Artist2 As String
Global Title2 As String
Sub CDAudioProperties()
Dim T As Double
On Error Resume Next
T = Shell("rundll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,3", 5)
End Sub
Function GetRawRemainingTime(MMCOntrol1 As Object) As String
Dim Z As String, min As String, sec As String, _
Temp As String
Z = GetRunningTime(MMCOntrol1)
Z = GetTrackTime(MMCOntrol1)
Min = LTrim$(Str$(CDMin - TMin))
Sec = LTrim$(Str$(CDSec - TSec))
RMin = Val(Min)
RSec = Val(Sec)
If RSec < 0 Then
RSec = 60 + Val(Sec)
RMin = RMin - 1
End If
Min = LTrim$(Str$(RMin))
Temp = Trim$(Str$(RSec))
If Len(Temp) = 1 Then
sec = "0" + Temp
Else
sec = Temp
End If
GetRawRemainingTime = min + Sec
End Function
Function GetRemainingTime(MMCOntrol1 As Object) As String
Dim Z As String, min As String, sec As String, _
Temp As String
Z = GetRunningTime(MMCOntrol1)
Z = GetTrackTime(MMCOntrol1)
Min = LTrim$(Str$(CDMin - TMin))
Sec = LTrim$(Str$(CDSec - TSec))
RMin = Val(Min)
RSec = Val(Sec)
If RSec < 0 Then
RSec = 60 + Val(Sec)
RMin = RMin - 1
End If
Min = LTrim$(Str$(RMin))
Temp = Trim$(Str$(RSec))
If Len(Temp) = 1 Then
sec = "0" + Temp
Else
sec = Temp
End If
GetRemainingTime = min + ":" + Sec
End Function
Sub GetTime(MMCOntrol1 As Object)
Dim Z$
Z$ = GetRemainingTime(MMCOntrol1)
End Sub
Function GetTrackTime(MMCOntrol1 As Object) As String
Dim Length&, Entry2$, Min$, Sec$, D$, Entry$
MMCOntrol1.TimeFormat = 2
Length& = MMCOntrol1.TrackLength
Min$ = Str$(Length& And &HFF)
Sec$ = LTrim$(Str$((Length& And 65280) / 256))
Entry2$ = Min$ & ":" & Sec$
If Len(Sec$) = 1 Then Entry2$ = Min$ + ":0" + Sec$
Entry$ = Min$ + ":" + Sec$
If Len(Entry2$) = 4 Then
D$ = "0" + Entry2$
Else
D$ = Entry2$
End If
If Len(Entry2$) = 3 Then
D$ = "00" + Entry2$
Else
D$ = Entry2$
End If
D$ = Entry2$
GetTrackTime = Trim$(D$)
MMCOntrol1.TimeFormat = 10
CDMin = Val(Min$)
CDSec = Val(Sec$)
End Function
Function GetRunningTime(MMCOntrol1 As Object) As String
Dim E As Long, M As String, S As String, Length&, Min$, Sec$, D As Long, Entry2$
MMCOntrol1.TimeFormat = 2
Length& = MMCOntrol1.Position - MMCOntrol1.TrackPosition
Min$ = Str$(Length& And &HFF)
Sec$ = LTrim$(Str$((Length& And 65280) / 256))
If Len(Sec$) = 3 Then
D = Val(Min$) - 1
Min$ = LTrim$(Str$(D))
E = Val(Right$(Sec$, 2)) + 4
Sec$ = LTrim$(Str$(E))
End If
M = Min$
'If Len(Min$) = 1 Then M = "0" + Min$ Else M = Min$
'If Val(M) = 0 Then M = "00"
'If Val(M) = 1 Then M = "01"
'If Val(M) = 2 Then M = "02"
'If Val(M) = 3 Then M = "03"
'If Val(M) = 4 Then M = "04"
'If Val(M) = 5 Then M = "05"
'If Val(M) = 6 Then M = "06"
'If Val(M) = 7 Then M = "07"
'If Val(M) = 8 Then M = "08"
'If Val(M) = 9 Then M = "09"
If Len(Sec$) = 1 Then
S = "0" + Sec$
Else
If Len(Sec$) = 3 Then
S = Mid$(Sec$, 2)
Else
S = Sec$
end If
End If
TMin = Val(M)
TSec = Val(S)
Entry2$ = LTrim$(M) + ":" + LTrim$(S)
MMCOntrol1.TimeFormat = 10
GetRunningTime = Entry2$
End Function
Function GetRawRunningTime(MMCOntrol1 As Object) As String
Dim E As Long, M As String, S As String, Length&, Min$, Sec$, D As Long, Entry2$
MMCOntrol1.TimeFormat = 2
Length& = MMCOntrol1.Position - MMCOntrol1.TrackPosition
Min$ = Str$(Length& And &HFF)
Sec$ = LTrim$(Str$((Length& And 65280) / 256))
If Len(Sec$) = 3 Then
D = Val(Min$) - 1
Min$ = LTrim$(Str$(D))
E = Val(Right$(Sec$, 2)) + 4
Sec$ = LTrim$(Str$(E))
End If
M = Min$
'If Len(Min$) = 1 Then M = "0" + Min$ Else M = Min$
'If Val(M) = 0 Then M = "0"
'If Val(M) = 1 Then M = "1"
'If Val(M) = 2 Then M = "2"
'If Val(M) = 3 Then M = "3"
'If Val(M) = 4 Then M = "4"
'If Val(M) = 5 Then M = "5"
'If Val(M) = 6 Then M = "6"
'If Val(M) = 7 Then M = "7"
'If Val(M) = 8 Then M = "8"
'If Val(M) = 9 Then M = "9"
If Len(Sec$) = 1 Then
S = "0" + Sec$
Else
If Len(Sec$) = 3 Then
S = Mid$(Sec$, 2)
Else
S = Sec$
end If
End If
Entry2$ = M + S
MMCOntrol1.TimeFormat = 10
GetRawRunningTime = Entry2$
End Function
Function GetRawTrackTime(MMCOntrol1 As Object) As String
Dim Length&, Entry2$, Min$, Sec$, D$
MMCOntrol1.TimeFormat = 2
Length& = MMCOntrol1.TrackLength
Min$ = Str$(Length& And &HFF)
Sec$ = LTrim$(Str$((Length& And 65280) / 256))
Entry2$ = Min$ + Sec$
If Len(Sec$) = 1 Then Entry2$ = Min$ + "0" + Sec$
GetRawTrackTime = Entry2$
MMCOntrol1.TimeFormat = 10
End Function
Public Function myReadINI(inifile, inisection, inikey, iniDefault)
'Fail fracefully if no file / wrong file is specified.
'If no section (appname), default is first appname
'if no key, default is first key
Dim lpApplicationName As String
Dim lpKeyName As String
Dim lpDefault As String
Dim lpReturnedString As String
Dim nSize As Long
Dim lpFileName As String
Dim retval As Long
Dim Filename As String
lpDefault = Space$(254)
lpDefault = iniDefault
lpReturnedString = Space$(254)
nSize = 254
lpFileName = inifile
lpApplicationName = inisection
lpKeyName = inikey
Filename = lpFileName
retval = GetPrivateProfileString _
(lpApplicationName, lpKeyName, lpDefault, lpReturnedString, nSize, lpFileName)
myReadINI = lpReturnedString
End Function
Public Function myWriteINI(inifile As String, inisection As String, inikey As String, Info As String) As String
Dim retval As Long
retval = WritePrivateProfileString(inisection, inikey, Info, inifile)
myWriteINI = LTrim$(Str$(retval))
End Function
Public Function GenCDSerial(MMCOntrol1 As Object) As Long
Const MCI_FORMAT_MILLISECONDS = 0
Const MCI_FORMAT_MSF = 2
Const MCI_FORMAT_TMSF = 10
'MCI_Format :0,2,10 are the only supported formats for CD
Dim Compat As Integer
Dim i As Integer
Dim dwtotal&, dwtemp&
Dim byte0%, byte1%, byte2%, byte3%
'compat = 0 for EXISTING code
'Compat = 1 for use with CDPLAYER.EXE
Compat = 1
MMCOntrol1.Notify = False
MMCOntrol1.Wait = True
MMCOntrol1.Shareable = True
If MMCOntrol1.Error <> 0 Then
MsgBox MMCOntrol1.ErrorMessage
Exit Function
End If
MMCOntrol1.TimeFormat = MCI_FORMAT_MSF
dwtotal& = 0
For i = 1 To MMCOntrol1.Tracks
DoEvents
MMCOntrol1.Track = i
dwtemp& = MMCOntrol1.TrackPosition
byte0% = dwtemp& And &HFF&
byte1% = (dwtemp& And &HFF00&) \ &H100
byte2% = (dwtemp& And &HFF0000) \ &H10000
byte3% = (dwtemp& And &H7F000000) \ &H1000000
If (dwtemp& And &H80000000) <> 0 Then
' put sign bit back into byte4
byte3 = byte3 + &H80
end If
dwtemp& = byte0% * &H10000 + byte1% * &H100 + byte2%
dwtotal& = dwtotal& + dwtemp&
Next i
If MMCOntrol1.Tracks < 3 Then
dwtotal& = dwtotal& + msf2frames(MMCOntrol1.Length) + Compat
End If
GenCDSerial = dwtotal&
End Function
Function msf2frames(msf As Long) As Long
Rem From the KnowledgeBase
Rem byte1 = MMControl1.Position And &HFF&
Rem byte2 = (MMControl1.Position And &HFF00&) \ &H100
Rem byte3 = (MMControl1.Position And &HFF0000) \ &H10000
Rem byte4 = (MMControl1.Position And &H7F000000) \ &H1000000
Rem If (MMControl1.Position And &H80000000) <> 0 Then
Rem ' put sign bit back into byte4
Rem byte4 = byte4 + &H80
Rem end If
dim byte0, byte1, byte2, byte3 As Integer
dim Min, Sec, fra As Integer
byte0 = msf And &HFF&
byte1 = (msf And &HFF00&) \ &H100
byte2 = (msf And &HFF0000) \ &H10000
byte3 = (msf And &H7F000000) \ &H1000000
If (msf And &H80000000) <> 0 Then
' put sign bit back into byte4
byte3 = byte3 + &H80
end If
min = byte0
sec = byte1
fra = byte2
msf2frames = (Min * 60 + Sec) * 75 + fra
End Function
Function Z_Trim(String1 As String) As String
Dim A As Integer
For A = 1 To Len(String1)
If Mid$(String1, A, 1) = Chr$(0) Then Exit For
Next A
Z_Trim = RTrim$(Left$(String1, A - 1))
End Function
Return