Avatar billede larzy Nybegynder
12. maj 2003 - 17:22 Der er 3 kommentarer

Lave DirectX7-kode om til at bruge DirectX8

Jeg skal lave et program, som kan mixe forskellige lydfiler, og som kan lägge forskellige effekter på som reverb, chorus osv.

Jeg har fundet noget kode på nettet, som jeg kan bruge til at mixe forskellige lydfiler, men i denne kode bruges DirectX7. Jeg vil gerne bruge DirectX8. Er der en nem måde at lave min eksisterende kode om til at bruge DirectX8 i stedet.


DX7Sound (modul)
-------------------------------
Option Explicit

'This module was created by D.R Hall
'For more Information and latest version
'E-mail me, derek.hall@virgin.net


Private m_dx As New DirectX7
Private m_dxs As DirectSound 'Then there is the sub object, DirectSound:

Type dxBuffers
  isLoaded As Boolean
  Buffer As DirectSoundBuffer
End Type
Private SoundFolder As String 'Holds Path to Sound folder
Private SB() As dxBuffers 'An Array of BUFFERS,
Private CurrentBuffer As Integer 'Holds last assign Random Buffer Number

Public Sub SoundDir(FolderPath As String)
  SoundFolder = FolderPath & "\"
End Sub

Public Sub CreateBuffers(AmountOfBuffer As Integer, DefaultFile As String)
  ReDim SB(AmountOfBuffer)
  For AmountOfBuffer = 0 To AmountOfBuffer
    DX7LoadSound AmountOfBuffer, DefaultFile 'must assign a defualt sound
    VolumeLevel AmountOfBuffer, 50 ' set volume to 50% for default
  Next AmountOfBuffer
End Sub

Public Sub SetupDX7Sound(CurrentForm As Form)
  Set m_dxs = m_dx.DirectSoundCreate("") 'create a DSound object
'Next you check for any errors, if there are no errors the user has got DX7 and a functional sound card

  If Err.Number <> 0 Then
    MsgBox "Unable to start DirectSound. Check to see that your sound card is properly installed"
    End
  End If
  m_dxs.SetCooperativeLevel CurrentForm.hWnd, DSSCL_PRIORITY 'THIS MUST BE SET BEFORE WE CREATE ANY BUFFERS
 
  'associating our DS object with our window is important. This tells windows to stop
  'other sounds from interfering with ours, and ours not to interfere with other apps.
  'The sounds will only be played when the from has got focus.
  'DSSCL_PRIORITY=no cooperation, exclusive access to the sound card, Needed for games
  'DSSCL_NORMAL=cooperates with other apps, shares resources, Good for general windows multimedia apps.
 
End Sub

Public Sub DX7LoadSound(Buffer As Integer, sfile As String)
  Dim Filename As String
  Dim bufferDesc As DSBUFFERDESC  'a new object that when filled in is passed to the DS object to describe
  Dim waveFormat As WAVEFORMATEX 'what sort of buffer to create
 
  bufferDesc.lFlags = DSBCAPS_CTRLFREQUENCY Or DSBCAPS_CTRLPAN _
  Or DSBCAPS_CTRLVOLUME Or DSBCAPS_STATIC 'These settings should do for almost any app....
 
  waveFormat.nFormatTag = WAVE_FORMAT_PCM
  waveFormat.nChannels = 2    '2 channels
  waveFormat.lSamplesPerSec = 22050
  waveFormat.nBitsPerSample = 16  '16 bit rather than 8 bit
  waveFormat.nBlockAlign = waveFormat.nBitsPerSample / 8 * waveFormat.nChannels
  waveFormat.lAvgBytesPerSec = waveFormat.lSamplesPerSec * waveFormat.nBlockAlign

  Filename = SoundFolder & sfile
  On Error GoTo Continue
  Set SB(Buffer).Buffer = m_dxs.CreateSoundBufferFromFile(Filename, bufferDesc, waveFormat)
  SB(Buffer).isLoaded = True
  Exit Sub
Continue:
  MsgBox "Error can't find file: " & Filename
End Sub

Public Function PlaySoundAnyBuffer(Filename As String, Optional Volume As Byte, Optional PanValue As Byte, Optional LoopIt As Byte) As Integer
 
  Do While SB(CurrentBuffer).Buffer.GetStatus = DSBSTATUS_PLAYING 'Find an empty buffer
    CurrentBuffer = CurrentBuffer + 1
    If CurrentBuffer > UBound(SB) Then CurrentBuffer = 0
  Loop

  DX7LoadSound CurrentBuffer, Filename
  If PanValue <> 50 Then PanSound CurrentBuffer, PanValue
  If Volume < 100 Then VolumeLevel CurrentBuffer, Volume
  If SB(CurrentBuffer).isLoaded Then SB(CurrentBuffer).Buffer.Play LoopIt 'dsb_looping=1, dsb_default=0
End Function

Public Sub PlaySoundWithPan(Buffer As Integer, Filename As String, Optional Volume As Byte, Optional PanValue As Byte, Optional LoopIt As Byte)
  DX7LoadSound Buffer, Filename
  If PanValue <> 50 And PanValue < 100 Then PanSound Buffer, PanValue
  If Volume < 100 Then VolumeLevel Buffer, Volume
  If SB(Buffer).isLoaded Then SB(Buffer).Buffer.Play LoopIt 'dsb_looping=1, dsb_default=0
End Sub

Public Sub PanSound(Buffer As Integer, PanValue As Byte)
  Select Case PanValue
    Case 0
      SB(Buffer).Buffer.SetPan -10000
    Case 100
      SB(Buffer).Buffer.SetPan 10000
    Case Else
      SB(Buffer).Buffer.SetPan (100 * PanValue) - 5000
  End Select
End Sub

Public Sub VolumeLevel(Buffer As Integer, Volume As Byte)
  If Volume > 0 Then ' stop division by 0
    SB(Buffer).Buffer.SetVolume (60 * Volume) - 6000
  Else
    SB(Buffer).Buffer.SetVolume -6000
  End If
End Sub

Public Function IsPlaying(Buffer As Integer) As Long
  IsPlaying = SB(Buffer).Buffer.GetStatus
End Function




Form1 (form)
----------------------------
'This Code and Module was created by D.R Hall
'For more Information and latest version
'E-mail me, derek.hall@virgin.net

'This is a demo of how to use my module
'to make playing DirectX7 Sound easier

Option Explicit
Const cSoundBuffers = 10

Private Sub cmdAnyBuffer_Click(Index As Integer)
  PlaySoundAnyBuffer List1(Index), hsbVolume(Index).Value, hsbPan(Index).Value
End Sub

Private Sub cmdBuffer_Click(Index As Integer)
  PlaySoundWithPan Index, List1(Index), hsbVolume(Index).Value, hsbPan(Index).Value ' Play the buffer for this index
End Sub

Private Sub cmdPlayRndSound_Click()
  List1(1).ListIndex = (5 * Rnd)
  PlaySoundAnyBuffer List1(1), hsbVolume(1).Value, hsbPan(1).Value
End Sub

Private Sub cmdRndPan_Click()
  hsbPan(2).Value = (Rnd * 100) '+ 25
  PlaySoundAnyBuffer List1(2), hsbVolume(2).Value, hsbPan(2).Value
End Sub

Private Sub cmdRndPanRndSnd_Click()
  List1(3).ListIndex = (5 * Rnd)
  hsbPan(3).Value = (Rnd * 100) '+ 25
  PlaySoundAnyBuffer List1(3), hsbVolume(3).Value, hsbPan(3).Value
End Sub

Private Sub Form_Load()
'This Code was created by D.R Hall
'For more Information and latest version
'E-mail me, derek.hall@virgin.net
'To set up the DX7 sound module, just call these routines 3 routines
 
  SetupDX7Sound Me              ' Assign DX7 to this Application
  SoundDir App.Path & "\Sound"  'Where is the applications sound stored

  CreateBuffers cSoundBuffers, "Default.wav" ' How many Channels/buffers (I used 10)
                                              'and assign a default sound,
                                              'change sound later.
                                              'To stop errors you must supply
                                              'a default wave to set up a buffer
                                              'select your smallest wave file
'**** Make a selection for each listbox on this form
  List1(0).ListIndex = 4
  List1(1).ListIndex = 5
  List1(2).ListIndex = 0
  List1(3).ListIndex = 2
'**************************************
End Sub

Private Sub hsbPan_Change(Index As Integer)
  PanSound Index, hsbPan(Index).Value 'value must be 0 to 100, 50 is centered
End Sub

Private Sub hsbVolume_Change(Index As Integer)
  VolumeLevel Index, hsbVolume(Index).Value 'value must be 0 to 100, 0 no sound,
End Sub
Avatar billede martin_moth Mester
13. maj 2003 - 08:43 #1
Prøv at udskift
  Private m_dx As New DirectX7
med
  Private m_dx As New DirectX8
:o)
Avatar billede larzy Nybegynder
13. maj 2003 - 13:27 #2
har jeg prøvet - funger desværre ikke :-(
Avatar billede martin_moth Mester
13. maj 2003 - 13:43 #3
Øv - det var ellers oplagt. Har nul forstand på hvad du laver, så det er det eneste bud jeg har
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester