Newsgroup: it.comp.programmare.win32
Titolo: ancora delucidazioni su winmm.dll

Vorrei farvo alcune domando su come catturare l'audio da un microfono 
esterno in VB6 usando questo codice:
[vb]
Option Explicit
Private DevHandle As Long
Private InData(0 To 511) As Byte
Private Type WaveFormatEx
    FormatTag As Integer
    Channels As Integer
    SamplesPerSec As Long
    AvgBytesPerSec As Long
    BlockAlign As Integer
    BitsPerSample As Integer
    ExtraDataSize As Integer
End Type
Private Type WaveHdr
    lpData As Long
    dwBufferLength As Long
    dwBytesRecorded As Long
    dwUser As Long
    dwFlags As Long
    dwLoops As Long
    lpNext As Long
    Reserved As Long
End Type
Private Type WaveInCaps
    ManufacturerID As Integer
    ProductID As Integer
    DriverVersion As Long
    ProductName(1 To 32) As Byte
    Formats As Long
    Channels As Integer
    Reserved As Integer
End Type
Private Const WAVE_FORMAT_PCM = 1
Private Const WHDR_DONE = &H1&              '/* done bit */
Private Const WHDR_PREPARED = &H2&          '/* set if this header has 
been prepared */
Private Const WHDR_BEGINLOOP = &H4&         '/* loop start block */
Private Const WHDR_ENDLOOP = &H8&           '/* loop end block */
Private Const WHDR_INQUEUE = &H10&          '/* reserved for driver */
Private Const WIM_OPEN = &H3BE
Private Const WIM_CLOSE = &H3BF
Private Const WIM_DATA = &H3C0
Private Declare Function waveInAddBuffer Lib "winmm" (ByVal 
InputDeviceHandle As Long, ByVal WaveHdrPointer As Long, ByVal 
WaveHdrStructSize As Long) As Long
Private Declare Function waveInPrepareHeader Lib "winmm" (ByVal 
InputDeviceHandle As Long, ByVal WaveHdrPointer As Long, ByVal 
WaveHdrStructSize As Long) As Long
Private Declare Function waveInUnprepareHeader Lib "winmm" (ByVal 
InputDeviceHandle As Long, ByVal WaveHdrPointer As Long, ByVal 
WaveHdrStructSize As Long) As Long
Private Declare Function waveInGetNumDevs Lib "winmm" () As Long
Private Declare Function waveInGetDevCaps Lib "winmm" Alias 
"waveInGetDevCapsA" (ByVal uDeviceID As Long, ByVal WaveInCapsPointer As 
Long, ByVal WaveInCapsStructSize As Long) As Long
Private Declare Function waveInOpen Lib "winmm" (WaveDeviceInputHandle 
As Long, ByVal WhichDevice As Long, ByVal WaveFormatExPointer As Long, 
ByVal CallBack As Long, ByVal CallBackInstance As Long, ByVal Flags As 
Long) As Long
Private Declare Function waveInClose Lib "winmm" (ByVal 
WaveDeviceInputHandle As Long) As Long
Private Declare Function waveInStart Lib "winmm" (ByVal 
WaveDeviceInputHandle As Long) As Long
Private Declare Function waveInReset Lib "winmm" (ByVal 
WaveDeviceInputHandle As Long) As Long
Private Declare Function waveInStop Lib "winmm" (ByVal 
WaveDeviceInputHandle As Long) As Long
Private Sub StartButton_Click()
    Static WaveFormat As WaveFormatEx
    With WaveFormat
        .FormatTag = WAVE_FORMAT_PCM
        .Channels = 2 'Two channels -- left and right
        .SamplesPerSec = 11025 '11khz
        .BitsPerSample = 8
        .BlockAlign = (.Channels * .BitsPerSample) \ 8
        .AvgBytesPerSec = .BlockAlign * .SamplesPerSec
        .ExtraDataSize = 0
    End With
 
    Debug.Print "waveInOpen:"; waveInOpen(DevHandle, 0, 
VarPtr(WaveFormat), 0, 0, 0)
    Debug.Print " "; DevHandle
 
    Call waveInStart(DevHandle)
 
    Call capture_audio
 
End Sub
Private Sub StopButton_Click()
    Call DoStop
End Sub
Private Sub DoStop()
    Call waveInReset(DevHandle)
    Call waveInClose(DevHandle)
    DevHandle = 0
End Sub
Private Sub capture_audio()
    Static Wave As WaveHdr
 
    Wave.lpData = VarPtr(InData(0))
    Wave.dwBufferLength = 512 'This is now 512 so there's still 256 
samples per channel
    Wave.dwFlags = 0
 
    Do
 
        Call waveInPrepareHeader(DevHandle, VarPtr(Wave), Len(Wave))
        Call waveInAddBuffer(DevHandle, VarPtr(Wave), Len(Wave))
 
        Do
            'Nothing -- we're waiting for the audio driver to mark
            'this wave chunk as done.
        Loop Until ((Wave.dwFlags And WHDR_DONE) = WHDR_DONE) Or 
DevHandle = 0
 
        Call waveInUnprepareHeader(DevHandle, VarPtr(Wave), Len(Wave))
    Loop While DevHandle <> 0 'While the audio device is open
End Sub
[/vb]
Volevo sapere alcune cose riguardo a questo  codice:
1)
Come faccio a settarlo per registrare a 44.1Khz 16bit mono ?
andrebbe bene cosi':
    Static WaveFormat As WaveFormatEx
    With WaveFormat
        .FormatTag = WAVE_FORMAT_PCM
        .Channels = 1
        .SamplesPerSec = 44100
        .BitsPerSample = 16
        .BlockAlign = (.Channels * .BitsPerSample) \ 8
        .AvgBytesPerSec = .BlockAlign * .SamplesPerSec
        .ExtraDataSize = 0
    End With
 
Quale altra variabile dovrei cambiare per registrare a 44.1Khz 16bit 
mono ?
Io pensavo a queste ma non sono per niente sicuro:
Private InData(0 To 44100) As Byte
Wave.lpData = VarPtr(InData(0))
Wave.dwBufferLength = 44100 * 2
Wave.dwFlags = 0
2)
Potete spiegarmi questo pezzo di codice:
Do
'Nothing -- we're waiting for the audio driver to mark
'this wave chunk as done.
Loop Until ((Wave.dwFlags And WHDR_DONE) = WHDR_DONE) Or DevHandle = 0
equivale ad una specie di sleep? o serve per evitare la callback?
3)
L'audio č fisicamente contenuto in VarPtr(InData(0)) ??????
ovvero in InData????
Grazie mille a chi mi aiuterą!

 ovvero in InData????
lo  domando perchè penso che un secondo di audio campionato a 44.1khz 
duri più dei 44k stanziati con:
Private InData(0 To 44100) As Byte
o forse non ho capito una mazza!!!
AIUTO!!!!!!

 Grazie mille a chi mi aiuterà!
Spero di esser stato chiaro (anche perché ste funzioni non le ho mai 
usate :p )

 Private Const WIM_DATA = &H3C0
di queste variabili nel codice vedo che vengono usate solo:
Private Const WHDR_DONE = &H1&
Private Const WHDR_DONE = &H1&
Le posso togliere le altre? oppure vengono usate di nascoto da non so 
cosa?
e poi:
Wave.lpData = VarPtr(InData(0))
perchč viene passata cosi' e non cosi:
Wave.lpData = VarPtr(InData)
Non capisco quello zero nella parentesi
e poi:
posso chiamare la WaveInOpen con questi parametri?
waveInOpen(DevHandle, 0, VarPtr(WaveFormat), 0, 0, 0)
ovvero il primo 0 come parametro della device da aprire?
potrei usare questa variabile:
WaveDevice& = -1 'Wave Mapper
grazie

 o forse non ho capito una mazza!!!
Perchč innanzitutto il campionamento č a 16 bit, quindi devi occupare
il doppio...
-- 
:-) :-> ;-) :)  "Smilies everyone, Smilies"  Mr.Rourke
News 2000 [v 2.06] / StopDialer / PopDuster - 
http://www.socket2000.com
Akapulce portal: 
http://www.akapulce.net

Molto gentile, avrei un altra domanda... mi č stato detto che sarebbe 
meglio implementare due buffer anziche uno...e fare cosi' una specie di 
swap... ma non capito molto come fare ne perchč.
Potresti spiegarmi TU qualcosa?!?
Grazie mille!!!