Converting Audio With Visual Basic 6

Convert MP3, WAV, ACM, WMA, OGG, APE and more audio files with VB6.


Requirements:

This demonstrate will show you how to convert MP3, PCM WAV, ACM WAV, WMA, OGG and APE from one format to another using VB6 and the Audio Converter ActiveX:

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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
Option Explicit
 
Private Sub SetFramesDisable()
 
    'Hide all audio formats:
    froToWAV.Visible = False
    froToACM.Visible = False
    froToMP3.Visible = False
    froToWMA.Visible = False
    froToOGG.Visible = False
    froToAPE.Visible = False
 
End Sub
 
Private Function SetConvertTo() As Long
 
    Dim sExt As String
 
    'First, hide them all:
    SetFramesDisable
 
    sExt = UCase(Left(cboConvertTo.Text, 3))
 
    'Then, display the selected format:
    Select Case sExt
 
        Case "WAV"
            froToWAV.Visible = True
            txtDest.Text = txtSource & "." & sExt
        Case "ACM"
            froToACM.Visible = True
            txtDest.Text = txtSource & ".wav"
        Case "MP3"
            froToMP3.Visible = True
            txtDest.Text = txtSource & "." & sExt
        Case "WMA"
            froToWMA.Visible = True
            txtDest.Text = txtSource & "." & sExt
        Case "OGG"
            froToOGG.Visible = True
            txtDest.Text = txtSource & "." & sExt
        Case "APE"
            froToAPE.Visible = True
            txtDest.Text = txtSource & "." & sExt
 
    End Select
 
End Function
 
Private Sub cboConvertTo_Click()
 
    'Set the format to convert to:
    
    SetConvertTo
 
End Sub
 
Private Sub cboWMACodecs_Click()
 
    'Display the WMA formats by the codec index:
    
    GetWMAFormatsX
 
End Sub
 
Private Sub chkBestAccordToSource_Click()
 
    'Get recommended codecs according to source file format:
    
    cboTags.Clear
    cboFormats.Clear
 
    If chkBestAccordToSource.Value = vbChecked Then
 
        'Get the tags according to the samples/bits/channeles
        'of the source file:
        cAudioConverter1.GetTags 1, cAudioConverter1.Info_Samplerate, _
        cAudioConverter1.Info_Channels, _
        cAudioConverter1.Info_BitsPerSample, _
        ACM_CONVERT
 
    Else
 
        'The tags list will raise under the cACMConverter1_TagCodecs event:
        Call cAudioConverter1.GetTags(1, 44100, 2, 16, ACM_CONVERT)
 
    End If
 
    If cboTags.ListCount > 0 Then
 
        'Select the first tag:
        cboTags.ListIndex = 0
        cboFormats.ListIndex = 0
 
    End If
 
End Sub
 
Private Sub cmdAbout_Click()
 
    'Display About message box with the license details.
    
    cAudioConverter1.About
 
End Sub
 
Private Sub cmdConvert_Click()
 
    'Convert the source file to the selected format:
    
    Dim nMsgRet As Integer
 
    'Check if the file already existed:
    If Dir(txtDest.Text) <> "" Then
 
        nMsgRet = MsgBox(txtDest.Text & " already exists, replace it?", vbOKCancel Or vbInformation, Me.Caption)
 
        If nMsgRet = 2 Then
            Exit Sub
        End If
    End If
 
    ConvertFile txtSource.Text, txtDest.Text
 
End Sub
 
Private Sub cmdRegistration_Click()
 
    'Display registration:
    
    cAudioConverter1.DisplayRegistration
 
End Sub
 
 
Private Sub cmdSource_Click()
 
        'Supported audio formats are:
        '*.acm;*.wav;*.aiff;*.aif;*.aifc;*.ape;*.cda;*.mp3;*.ogg;*.au;*.snd;*.vox;*.voc;*.wav;*.wma
        
        'Display the dialog box:
        
        CommonDialog1.Filter = "All supported audio files (*.acm;*.wav;*.aiff;*.aif;*.aifc;*.ape;*.cda;;*.mp3;*.ogg;*.au;*.snd;*.vox;*.voc;*.wav;*.wma;)" & _
        "|" & "*.acm;*.wav;*.aiff;*.aif;*.aifc;*.ape;*.cda;*.mp3;*.ogg;*.au;*.snd;*.vox;*.voc;*.wav;*.wma;" _
        & "|" & "Windows legacy audio components (*.acm;*.wav)" & "|" & "*.acm;*.wav" _
        & "|" & "Audio Interchange File Format (*.aiff;*.aif;*.aifc)" & "|" & "*.aiff;*.aif;*.aifc" _
        & "|" & "Monkey's Audio files (*.ape)" & "|" & "*.ape" _
        & "|" & "Compact Disc Audio track (*.cda)" & "|" & "*.cda" _
        & "|" & "MPEG-1 Audio Layer 3 (MP3) (*.mp3)" & "|" & "*.mp3" _
        & "|" & "Ogg Media format (*.ogg)" & "|" & "*.ogg" _
        & "|" & "Au/SND file format. (*.au;*.snd)" & "|" & "*.au;*.snd" _
        & "|" & "Dialogic ADPCM (VOX) (*.vox;*.voc)" & "|" & "*.vox;*.voc" _
        & "|" & "Waveform audio format (*.wav)" & "|" & "*.wav" _
        & "|" & "Windows Media Audio files (*.wma)" & "|" & "*.wma" & "|"
 
        CommonDialog1.DialogTitle = "Add Audio File"
 
        CommonDialog1.CancelError = True
 
        On Error GoTo ErrcmdSource_Click
 
        Call CommonDialog1.ShowOpen
 
        'Set the source and destination file names:
        txtSource.Text = CommonDialog1.FileName
        txtDest.Text = CommonDialog1.FileName & "." & Left(cboConvertTo.Text, 3)
 
        'Get the source file information and present
        'it in the text box:
        
        'Open the audio file
        cAudioConverter1.OpenFile txtSource.Text
 
        'Get the information about the audio file
        Dim sSrcInfo As String
 
        'General info:
        sSrcInfo = sSrcInfo & "General Information:" & vbCrLf
        sSrcInfo = sSrcInfo & "Time:" & cAudioConverter1.SecondsAsTimeString((cAudioConverter1.Info_Duration / 1000)) & vbCrLf
        sSrcInfo = sSrcInfo & "Bitrate:" & cAudioConverter1.Info_Bitrate & vbCrLf
        sSrcInfo = sSrcInfo & "Bit per sample:" & cAudioConverter1.Info_BitsPerSample & vbCrLf
        sSrcInfo = sSrcInfo & "Cahnnels:" & cAudioConverter1.Info_Channels & vbCrLf
        sSrcInfo = sSrcInfo & "Samplerate:" & cAudioConverter1.Info_Samplerate & vbCrLf & vbCrLf
 
        'Get the ID3 Tags:
        cAudioConverter1.GetID3Tags txtSource.Text
 
        'ID3 tags:
        txtTagAlbum.Text = cAudioConverter1.Tag_Album
        txtTagArtist.Text = cAudioConverter1.Tag_Artist
        txtTagComment.Text = cAudioConverter1.Tag_Comment
        txtTagGenre.Text = cAudioConverter1.Tag_Genre
        txtTagSong.Text = cAudioConverter1.Tag_Title
        txtTagTrack.Text = cAudioConverter1.Tag_TrackNumber
        txtTagYear.Text = cAudioConverter1.Tag_SongYear
        txtTagComment.Text = cAudioConverter1.Tag_Comment
 
        'Disaply the file name:
        txtSrcInfo.Text = sSrcInfo
 
        'Get the ACM wav tags and the formats according
        'to the source file properties:
        
        cboFormats.Clear
        cboTags.Clear
 
        'Get the best tags according to source file:
        'cAudioConverter1.GetTags 1, cAudioConverter1.Info_Samplerate, _
        cAudioConverter1.Info_Channels, _
        cAudioConverter1.Info_BitsPerSample, _
        ACM_CONVERT
    
        'Get default ACM codecs can be:
        cAudioConverter1.GetTags 1, 44100, 2, 16, ACM_CONVERT
 
        'Select the PCM tag
        cboTags.Text = "PCM"
 
        'Relaod the formats
        cboTags_Click
 
        'Set convert to
        SetConvertTo
 
ErrcmdSource_Click:
 
End Sub
 
Private Sub cmdAbort_Click()
 
    'Abort the operation
    cAudioConverter1.Cancel
 
End Sub
 
Private Sub SetDefaultValues()
 
 
    'Just init the default audio formats:
    
    'WAV:
    cboBitrateWAV.AddItem 8
    cboBitrateWAV.AddItem 16
    cboBitrateWAV.ListIndex = 1
    cboSamplesWAV.Text = "44100"
    cboChannelsWAV.ListIndex = 1
 
    'WMA:
    GetWMACodecsX
 
    'APE:
    cboAPELevel.ListIndex = 0
 
    'OGG:
    cboOggBitNom.ListIndex = 0
    sldOGG.Value = 50
 
    'MP3
    cboMP3Bitrate.Text = "128"
    cboMP3Mode.ListIndex = 0    'Stereo
    cboMP3Samplerate.Text = "44100"
    '0 (low quality) to 9 (high quality)
    cboMP3Quality.ListIndex = 5
    chkMP3Copyright.Value = vbChecked
    chkMP3Original.Value = vbChecked
 
    'VBR
    chkMP3VBR.Value = vbChecked
    '0 (low quality) to 9 (high quality)
    cboMP3VBRQuality.ListIndex = 5
    cboMP3MaxBitrate.Text = "128"
    cboMP3MinBitrate.Text = "96"
    chkMP3VBRTag.Value = vbChecked
 
    'ABR
    cboMP3ABRBitrate.Text = 96
 
End Sub
 
Private Sub Form_Load()
 
    ' 1)    Init the component with your license key.
    '
    ' 2)    This function must be call before using
    '       any functinality of the component.
    '
    ' 3)    For trial mode use "Trial Mode."
    
    cAudioConverter1.Init "Trial Mode."
 
    'Set the default audio formats:
    SetDefaultValues
 
    'Set the default convert to MP3:
    cboConvertTo.ListIndex = 2
 
    'List the ACM Wav tags.
    'Get the tags under the cAudioConverter1_TagCodecs event:
    
    Call cAudioConverter1.GetTags(1, 44100, 2, 16, ACM_CONVERT)
 
    'Set the default format as PCM
    If cboTags.ListCount > 0 Then
        cboTags.Text = "PCM"
        cboTags_Click
    End If
 
End Sub
 
Private Sub cboTags_Click()
 
    'List the formats to the combo box by the tag name
    'ListFormats cboFormats, cboTags.Text
    
    'Clear the existed formats:
    cboFormats.Clear
 
    'Get the formats to the cACMConverter1_TagFormats event
    'according to the tag name:
    Call cAudioConverter1.GetFormatsByTag(cboTags.Text)
 
    'Select the first format
    If cboFormats.ListCount > 0 Then
        cboFormats.ListIndex = 0
    End If
 
End Sub
 
 
Public Function SetDestination(sSourceFile As String)
 
    'Set the properties of the destination audio file:
    
    Dim sExt As String
 
    sExt = UCase(Left(cboConvertTo.Text, 3))
 
 
    'Set the destination file name:
    cAudioConverter1.Destination = txtDest.Text
 
    Select Case sExt
 
 
        Case "WAV"
 
            'Convert to standard PCM WAV
            cAudioConverter1.ConvertTo = CONVERT_TO_WAV
 
            'Declare and set the wav properties
            Dim WAVProperties As WAV_Properties
 
            WAVProperties.nBitsPerSample = CLng(cboBitrateWAV.Text)
            WAVProperties.lSampleRate = CLng(cboSamplesWAV.Text)
            WAVProperties.nChannels = cboChannelsWAV.ListIndex + 1
 
            'If you do not want to save the wav header
            'cAudioConverter1.WriteWavHeader = False
            
            'Set the wav PCM encoding properties
            cAudioConverter1.SetWAV WAVProperties
 
 
        Case "ACM"
 
            'Convert to ACM WAV file (wav with codecs)
            cAudioConverter1.ConvertTo = CONVERT_TO_WAV
 
            'If you do not want to save the wav header
            'cAudioConverter1.WriteWavHeader = False
            
            'Set the wav ACM encoding properties
            cAudioConverter1.SetACM cboTags.Text, cboFormats.Text
 
        Case "MP3"
 
            'Convert to MP3
            cAudioConverter1.ConvertTo = CONVERT_TO_MP3
 
            'Declare and set the mp3 properties
            Dim MP3Properties As MP3_Properties
 
            MP3Properties.Bitrate = CLng(cboMP3Bitrate.Text)
            MP3Properties.Quality = CLng(cboMP3Quality.Text)
            MP3Properties.samplerate = CLng(cboMP3Samplerate.Text)
            MP3Properties.mode = cboMP3Mode.ListIndex
            MP3Properties.ErrorProtection = CBool(chkMP3ErrorProtect.Value)
            MP3Properties.OriginalBit = CBool(chkMP3Original.Value)
            MP3Properties.CopyrightBit = CBool(chkMP3Copyright.Value)
            MP3Properties.StrictISO = CBool(chkMP3StrictISO.Value)
 
            'VBR
            If chkMP3VBR.Value = vbChecked Then
                MP3Properties.VBR = MTRH_VBR
 
                MP3Properties.WriteVBRTag = CBool(chkMP3VBRTag.Value)
                MP3Properties.VBRForceMinBitrate = CBool(chkMP3VBRForceMinBitrate.Value)
                MP3Properties.VBRQuality = CLng(cboMP3VBRQuality.Text)
                MP3Properties.VBRMinBitrate = CLng(cboMP3MinBitrate.Text)
                MP3Properties.VBRMaxBitrate = CLng(cboMP3MaxBitrate.Text)
            End If
 
 
            'ABR
            If chkMP3ABR.Value = vbChecked Then
                MP3Properties.VBR = ABR_VBR
            End If
 
            MP3Properties.ABRBitrate = CLng(cboMP3ABRBitrate.Text)
 
            'Set the MP3 encoding properties
            cAudioConverter1.SetMP3 MP3Properties
 
 
        Case "WMA"
 
            'Convert to WMA
            cAudioConverter1.ConvertTo = CONVERT_TO_WMA
 
            'Declare and set the wma properties
            Dim WMAProperties As WMA_Properties
 
            WMAProperties.lCodec = cboWMACodecs.ListIndex
            WMAProperties.lFormat = cboWMAFormat.ListIndex
 
            'Set the wma encoding properties
            cAudioConverter1.SetWMA WMAProperties
 
        Case "OGG"
 
            'Convert to OGG
            cAudioConverter1.ConvertTo = CONVERT_TO_OGG
 
            'Declare and set the ogg properties
            Dim OGGProperties As OGG_Properties
 
            If optOggBit.Value Then
                OGGProperties.eMode = Bitrate_Mode
                OGGProperties.lBitrateNominal = CLng(cboOggBitNom.Text)
            Else
                OGGProperties.eMode = Quality_Mode
                OGGProperties.lQuality = sldOGG.Value
            End If
 
            'Set the ogg encoding properties
            cAudioConverter1.SetOGG OGGProperties
 
 
        Case "APE"
 
            'Convert to APE
            cAudioConverter1.ConvertTo = CONVERT_TO_APE
 
            'Set the ape encoding properties. No type is needed.
            cAudioConverter1.SetAPE cboAPELevel.ListIndex
 
    End Select
 
End Function
 
Public Sub SetID3Tags()
 
    'Set the ID3 tags:
                
    'To get the ID3 tags, set CopySourceTags to true
    'before using the OpenFile function.

    'Set the ID3 tags:
    cAudioConverter1.Tag_Album = txtTagAlbum.Text
    cAudioConverter1.Tag_Artist = txtTagArtist.Text
    cAudioConverter1.Tag_Comment = txtTagComment.Text
    cAudioConverter1.Tag_Genre = txtTagGenre.Text
    cAudioConverter1.Tag_Title = txtTagSong.Text
    cAudioConverter1.Tag_TrackNumber = txtTagTrack.Text
    cAudioConverter1.Tag_SongYear = txtTagYear.Text
 
 
End Sub
 
Public Function ConvertFile(sSourceFile As String, sDestFile As String)
 
    'Convert the file:
    
    Dim sExt As String
 
    SetID3Tags
 
    'Set the source file to convert:
    cAudioConverter1.SourceFile = sSourceFile
 
    'Set the position to start convert from:
    cAudioConverter1.ConvertStartFromSec = CLng(txtStartFromSec.Text)
 
    'Set the length to convert in seconds:
    cAudioConverter1.ConvertLengthSec = CLng(txtConvertLengthSec.Text)
 
    'Set the format properties:
    SetDestination sSourceFile
 
    'GUI:
    cmdConvert.Enabled = False
    lblProgress.FontBold = False
    lblProgress.Caption = "Converting..."
 
    'Convert it!:
    cAudioConverter1.convert
 
    'Log:
    txtSrcInfo.Text = txtSrcInfo.Text & Time & ": Converting..." & vbCrLf
 
 
End Function
 
Private Sub GetWMACodecsX()
 
    'Get the WMA Codecs list without collection,
    'then add them to the cboWMACodecs combo-box.
    
    Dim cntr As Long
 
    'Display the collection in the cboWMACodecs combo-box:
    For cntr = 0 To cAudioConverter1.WMACodecsX - 1
 
        cboWMACodecs.AddItem cAudioConverter1.WMACodecXVal(cntr)
 
    Next cntr
 
    'Select the first codec:
    cboWMACodecs.ListIndex = 0
 
End Sub
 
Private Sub GetWMAFormatsX()
 
    'Get the WMA formats list without collection.
    
    Dim cntr As Long
 
    'Clear the combo wma formats:
    cboWMAFormat.Clear
 
    'Add the formats to the combo:
    For cntr = 0 To cAudioConverter1.WMACodecsFormatsX(cboWMACodecs.ListIndex) - 1
 
        cboWMAFormat.AddItem cAudioConverter1.WMACodecFormatXVal(cboWMACodecs.ListIndex, cntr)
 
    Next cntr
 
    'Select the default format:
    If cboWMAFormat.ListCount > 0 Then
        cboWMAFormat.ListIndex = 0
    End If
 
End Sub
 
Private Sub Form_Unload(Cancel As Integer)
 
    'Release the controls:
    cAudioConverter1.Cancel
    cAudioConverter1.DeInit
 
End Sub
 
'********************************************************************
'EVENTS
'********************************************************************

Private Sub cAudioConverter1_Done()
 
    'Raise when the converting proccess has done.
    
    lblProgress.Caption = "Done."
    cmdConvert.Enabled = True
 
    txtSrcInfo.Text = txtSrcInfo.Text & Time & ": DoneConvert Event." & vbCrLf
 
End Sub
 
Private Sub cAudioConverter1_Error(ByVal lErrNum As Long, ByVal sErrDescription As String)
 
    'Error handling: ErrNum - error number, ErrDescription - error description:
    
    lblProgress.FontBold = True
    lblProgress.Caption = sErrDescription & " (" & lErrNum & ")"
    cmdConvert.Enabled = True
 
    txtSrcInfo.Text = txtSrcInfo.Text & Time & ": Error " & sErrDescription & " (" & lErrNum & ")" & vbCrLf
 
End Sub
 
Private Sub cAudioConverter1_Progress(ByVal lPercent As Long)
 
    'Display the percent of ripping in the progress bar:
    
    PrgCurrent.Value = lPercent
 
End Sub
 
Private Sub cAudioConverter1_TagCodecs(ByVal sTagName As String)
 
    'Raise after calling the function GetTagsX:
    cboTags.AddItem sTagName
 
End Sub
 
Private Sub cAudioConverter1_TagFormats(ByVal sTagFormat As String)
 
    'Raise after calling the function GetFormatsByTagX:
    cboFormats.AddItem sTagFormat
 
End Sub
 
Private Sub cAudioConverter1_Aborted()
 
    'Raise when the converting proccess has aborted.
    
    lblProgress.Caption = "Aborted."
    cmdConvert.Enabled = True
 
    txtSrcInfo.Text = txtSrcInfo.Text & Time & ": Aborted Convert Event." & vbCrLf
 
End Sub

Please note, the Audio Converter ActiveX can also convert AIF, VOX, VOC and more audio files to MP3, PCM WAV, ACM WAV, WMA, OGG and APE.


Source:





read the original story ...

Replies are closed.