Kamis, 20 Oktober 2011

Membuat Frekuensi Suara (Visual Basic)


Buatlah Tampilan Form seperti berikut :
















Kemudian ketikan program berikut ini :
Option Explicit
Dim OneSample  As Double
Dim DX As DirectX8 ' menggunakan directx 8
Dim DS As DirectSound8
Dim BufferSuara As DirectSoundSecondaryBuffer8
Dim desc As DSBUFFERDESC

Const PI = 3.14159265358979 '
Const SRATE = 44100         ' Kecepatan Sample / sampling rate
Const DUR = 1                    ' Durasi suara
Const FREQ = 20000               ' referensi frekuensisuara
Const CHAN = 2                   ' stereo
Const BITDEPTH = 16              ' 16 bit
Const BLOCK = 4

Dim sbuf(0 To DUR * SRATE * CHAN) As Integer
Dim SedangBersuara As Boolean


Private Sub Form_Load()

Set DX = New DirectX8   ' menggunkan directx 8
Set DS = DX.DirectSoundCreate("")
DS.SetCooperativeLevel Me.hWnd, DSSCL_NORMAL
desc.fxFormat.nFormatTag = WAVE_FORMAT_PCM  ' header audio wav
desc.fxFormat.nSize = 0
'desc.fxFormat.lExtra = 0
desc.fxFormat.nChannels = CHAN
desc.fxFormat.lSamplesPerSec = SRATE
desc.fxFormat.nBitsPerSample = BITDEPTH
desc.fxFormat.nBlockAlign = BLOCK
desc.fxFormat.lAvgBytesPerSec = BLOCK * SRATE
desc.lFlags = DSBCAPS_STATIC Or DSBCAPS_CTRLVOLUME Or DSBCAPS_CTRLFREQUENCY Or DSBCAPS_CTRLPAN
desc.lBufferBytes = BLOCK * DUR * SRATE
Set BufferSuara = DS.CreateSoundBuffer(desc)  '  membuat buffer
Dim i    ' pembuatan frekuensi suara
Dim n
For i = 0 To DUR * SRATE - 1
n = 2 * PI * (FREQ * i / SRATE)

    OneSample = Sin(n)
    sbuf(2 * i) = OneSample * 32767   ' chanel kiri
    sbuf(2 * i + 1) = sbuf(2 * i)     ' chanel kanan
 
Next i
BufferSuara.WriteBuffer 0, BLOCK * DUR * SRATE, sbuf(0), DSBLOCK_DEFAULT 'mengisi buffer
End Sub


Private Sub Command1_Click()

If SedangBersuara = False Then
BufferSuara.SetFrequency SRATE * Val(Text1.Text) / FREQ '----mengubah frekuensi
BufferSuara.Play 0  '----bunyikan frekuensi
BufferSuara.SetVolume -200
Command1.Caption = "Stop"
End If

If SedangBersuara = True Then
BufferSuara.Stop  ' ----- matikan suara
BufferSuara.SetVolume -10000
BufferSuara.SetCurrentPosition 0
Command1.Caption = "Bunyikan"
End If

SedangBersuara = Not SedangBersuara
End Sub


Sebelum dijalankan pilih menu Project ->References
 Kemudian carilah DirectX 8 for Visual Basic Type Library,  jangan lupa beri tanda cheklist.
Setelah di-cheklist kemudian pilih tombol OK

Program Audio siap dijalankan ..

Selamat Berkreasi...!

Klik DISINI untuk mendownload DirectX 8 


0 komentar:

Posting Komentar

 
Design by Wordpress Theme Template Blog Free | Bloggerized by Free Blogger Templates | coupon codes