home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Planet Source Code Jumbo …e CD Visual Basic 1 to 7
/
5_2007-2008.ISO
/
data
/
Zips
/
Audio_Reco210496362008.psc
/
modWaveIn.bas
< prev
Wrap
BASIC Source File
|
2008-03-06
|
16KB
|
556 lines
Attribute VB_Name = "modWaveIn"
Option Explicit
' DEBUGING
Public Const Debuging As Boolean = False
Enum OutputMode
WAVE = 0
MP3 = 1
End Enum
Enum eMP3_TYPE
CBR = 0
ABR = 1
vbr = 2
End Enum
Enum eVBR_Routine
New_Routine = 0
Old_Routine = 1
End Enum
Public Type tMP3
MP3_Type As eMP3_TYPE
VBR_MinBitrate As Integer
VBR_MaxBitrate As Integer
VBR_Quality As Integer
VBR_Routine As eVBR_Routine
ABR_AvgBitrate As Integer
CBR_Bitrate As Integer
LAME As String
End Type
Public INI_FILE As String
Private Const RECORD As Long = 100
Private Const MONITOR As Long = 10
Private Const GMEM_FIXED As Long = &H0
Private Const CALLBACK_WINDOW As Long = &H10000
Private Const STATUS_PENDING As Long = &H103
Private Const STILL_ACTIVE As Long = STATUS_PENDING
Private Const WAVE_FORMAT_PCM As Long = 1
Private Const MM_WIM_CLOSE As Long = &H3BF
Private Const MM_WIM_DATA As Long = &H3C0
Private Const MM_WIM_OPEN As Long = &H3BE
Private Const WIM_CLOSE As Long = MM_WIM_CLOSE
Private Const WIM_DATA As Long = MM_WIM_DATA
Private Const WIM_OPEN As Long = MM_WIM_OPEN
Private Const WHDR_DONE As Long = &H1
Private Const PROCESS_TERMINATE As Long = (&H1)
Private Type WAVEFORMATEX
wFormatTag As Integer
nChannels As Integer
nSamplesPerSec As Long
nAvgBytesPerSec As Long
nBlockAlign As Integer
wBitsPerSample 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 Declare Function TerminateProcess Lib "kernel32.dll" ( _
ByVal hProcess As Long, _
ByVal uExitCode As Long) As Long
Private Declare Function GetTickCount Lib "kernel32.dll" () As Long
Private Declare Sub ZeroMemory Lib "kernel32.dll" Alias "RtlZeroMemory" ( _
ByRef Destination As Any, _
ByVal Length As Long)
Private Declare Function CloseHandle Lib "kernel32.dll" ( _
ByVal hObject As Long) As Long
Private Const MB_ICONHAND As Long = &H10&
Private Const MB_ICONERROR As Long = MB_ICONHAND
Private Declare Function MessageBox Lib "user32.dll" Alias "MessageBoxA" ( _
ByVal hwnd As Long, _
ByVal lpText As String, _
ByVal lpCaption As String, _
ByVal wType As Long) As Long
Private Const SW_HIDE As Long = 0
Private Const SW_SHOW As Long = 5
Private Const STARTF_USESTDHANDLES As Long = &H100
Private Const STARTF_USESHOWWINDOW As Long = &H1
Private Const STD_INPUT_HANDLE As Long = -10&
Private Const STD_OUTPUT_HANDLE As Long = -11&
Private Declare Function AllocConsole Lib "kernel32.dll" () As Long
Private Declare Function FreeConsole Lib "kernel32.dll" () As Long
Private Declare Function GetStdHandle Lib "kernel32.dll" ( _
ByVal nStdHandle As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32.dll" ( _
ByVal hProcess As Long, _
ByRef lpExitCode As Long) As Long
Private Declare Function CreatePipe Lib "kernel32" ( _
ByRef phReadPipe As Long, _
ByRef phWritePipe As Long, _
ByRef lpPipeAttributes As SECURITY_ATTRIBUTES, _
ByVal nSize As Long) As Long
Private Declare Sub GetStartupInfoA Lib "kernel32" ( _
ByRef lpInfo As STARTUPINFO)
Private Declare Function CreateProcessA Lib "kernel32" ( _
ByVal lpApplicationName As String, _
ByVal lpCommandLine As String, _
ByRef lpProcessAttributes As Any, _
ByRef lpThreadAttributes As Any, _
ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, _
ByRef lpEnvironment As Any, _
ByVal lpCurrentDriectory As String, _
ByRef lpStartupInfo As STARTUPINFO, _
ByRef lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function WriteFile Lib "kernel32.dll" ( _
ByVal hFile As Long, _
ByVal lpBuffer As String, _
ByVal nNumberOfBytesToWrite As Long, _
ByRef lpNumberOfBytesWritten As Long, _
ByRef lpOverlapped As Any) As Long
Private Declare Function ReadFile Lib "kernel32" ( _
ByVal hFile As Long, _
ByVal lpBuffer As String, _
ByVal nNumberOfBytesToRead As Long, _
ByRef lpNumberOfBytesRead As Long, _
ByRef lpOverlapped As Any) As Long
Private Declare Function PeekNamedPipe Lib "kernel32" ( _
ByVal hReadPipe As Long, _
ByRef lpBuffer As Any, _
ByVal nBufferSize As Long, _
ByRef lpBytesRead As Long, _
ByRef lpTotalBytesAvail As Long, _
ByRef lpBytesLeftThisMessage As Long) As Long
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadId As Long
End Type
Private Type STARTUPINFO
cb As Long
lpReserved As Long
lpDesktop As Long
lpTitle As Long
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Byte
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Declare Function GetLastError Lib "kernel32.dll" () As Long
Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMiliseconds As Long)
Private Declare Function mmioStringToFOURCC Lib "winmm.dll" Alias "mmioStringToFOURCCA" ( _
ByVal sz As String, _
ByVal uFlags As Long) As Long
Private Type tHeader
RIFF As Long ' "RIFF"
LenR As Long ' size of following segment
WAVE As Long ' "WAVE"
fmt As Long ' "fmt
FormatSize As Long ' chunksize
format As WAVEFORMATEX ' audio format
data As Long ' "data"
DataLength As Long ' length of datastream
End Type
Public Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" ( _
ByRef Destination As Any, _
ByRef Source As Any, _
ByVal Length As Long)
Public Declare Sub CopyAudioMemory Lib "kernel32.dll" Alias "RtlMoveMemory" ( _
ByRef Destination As Any, _
ByVal Source As Long, _
ByVal Length As Long)
Private Declare Function GlobalAlloc Lib "kernel32.dll" ( _
ByVal wFlags As Long, _
ByVal dwBytes As Long) As Long
Private Declare Function GlobalLock Lib "kernel32.dll" ( _
ByVal hMem As Long) As Long
Private Declare Function GlobalFree Lib "kernel32.dll" ( _
ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" ( _
ByVal hMem As Long) As Long
Private Const GWL_WNDPROC As Long = -4
Private Declare Function CallWindowProc Lib "user32.dll" Alias "CallWindowProcA" ( _
ByVal lpPrevWndFunc As Long, _
ByVal hwnd As Long, _
ByVal msg As Long, _
ByVal wParam As Long, _
ByRef lParam As Any) As Long
Private Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" ( _
ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Private Declare Function waveInGetErrorText Lib "winmm.dll" Alias "waveInGetErrorTextA" ( _
ByVal err As Long, _
ByVal lpText As String, _
ByVal uSize As Long) As Long
Private Declare Function waveInReset Lib "winmm.dll" ( _
ByVal hWaveIn As Long) As Long
Private Declare Function waveInAddBuffer Lib "winmm.dll" ( _
ByVal hWaveIn As Long, _
ByRef lpWaveInHdr As WAVEHDR, _
ByVal uSize As Long) As Long
Private Declare Function waveInClose Lib "winmm.dll" ( _
ByVal hWaveIn As Long) As Long
Private Declare Function waveInOpen Lib "winmm.dll" ( _
ByRef lphWaveIn As Long, _
ByVal uDeviceID As Long, _
ByRef lpFormat As WAVEFORMATEX, _
ByVal dwCallback As Long, _
ByVal dwInstance As Long, _
ByVal dwFlags As Long) As Long
Private Declare Function waveInPrepareHeader Lib "winmm.dll" ( _
ByVal hWaveIn As Long, _
ByRef lpWaveInHdr As WAVEHDR, _
ByVal uSize As Long) As Long
Private Declare Function waveInStart Lib "winmm.dll" ( _
ByVal hWaveIn As Long) As Long
Private Declare Function waveInStop Lib "winmm.dll" ( _
ByVal hWaveIn As Long) As Long
Private Declare Function waveInUnprepareHeader Lib "winmm.dll" ( _
ByVal hWaveIn As Long, _
ByRef lpWaveInHdr As WAVEHDR, _
ByVal uSize As Long) As Long
Private hStdinWrite As Long
Private hStdoutWrite As Long
Private hStdinRead As Long
Private hStdoutRead As Long
Private pi As PROCESS_INFORMATION
Private Const BUFFERS As Integer = 4
Private Const BUFFERS_MONITOR As Integer = 2
Private Const BUFFERSIZE_MONITOR As Integer = 8192
Private BUFFERSIZE As Long
'Private Const BUFFERSIZE As Long = 8192
Dim hWaveIn As Long
Public hWaveIn_Monitor As Long
Dim ret As Long
Dim format As WAVEFORMATEX
Dim hMem(BUFFERS) As Long
Dim hmem_monitor(BUFFERS_MONITOR) As Long
Dim hdr(BUFFERS) As WAVEHDR
Dim hdr_monitor(BUFFERS_MONITOR) As WAVEHDR
Dim lpPrevWndFunc As Long
Dim hwnd As Long
Dim num As Integer
Public msg As String * 255
Dim pos As Long
Dim pos_mp3 As Long
Dim bHeaderWritten As Boolean
Public bRecording As Boolean
Public bMonitoring As Boolean
Dim bPaused As Boolean
Dim bPaused_Monitoring As Boolean
Dim OutputFile As String
Public OutMode As OutputMode
Public MP3_Settings As tMP3
Dim curBuffer As Long
' =============
' Subclassing function
' =============
Sub Hook(bHook As Boolean)
' Save hWnd of main form for waveOutOpen purposes => see PrepareRecording sub
hwnd = frmMain.hwnd
'Exit Sub
' Prevent double hooking
If lpPrevWndFunc <> 0 And bHook Then
MessageBox 0&, "Double-hooking not permited!", "ERROR!", MB_ICONERROR
Exit Sub
End If
Logging "[Hook] About to hook/unhook frmMain"
' bHook chooses whether we should subclass the form or restore previous Callback
' function
If bHook Then
lpPrevWndFunc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf CallbackProc)
Else
lpPrevWndFunc = 0
SetWindowLong hwnd, GWL_WNDPROC, lpPrevWndFunc
End If
End Sub
' ================
' Pausing function
' ----------------
' It's a toggling function
' ================
Sub Pause()
Dim i As Integer
bPaused = Not bPaused
If bPaused Then
' Order MM subsystem to stop sending recorded data
waveInStop hWaveIn
Else
' We have to add buffers to MM subsystem, otherwise it won't send any new data
For i = 0 To BUFFERS
waveInAddBuffer hWaveIn, hdr(i), Len(hdr(i))
Next
' Finally start again recording
waveInStart hWaveIn
End If
End Sub
' internal function: delete recording file (obviously, used after recording has stopped)
Private Sub DeleteFile()
Close #num
Kill OutputFile
End Sub
' Resets monitoring
' =================
' Sometimes does monitoring to 'stuck'
' In most cases this solved it
'
Public Sub ResetMonitoring()
waveInReset hWaveIn_Monitor
End Sub
' ==================
' Stop monitoring
' ------------------
' this stops secondary recording for monitoring
' It should be called at program quit. To pause use PauseMonitoring
' ==================
Public Sub StopMonitoring()
Dim i As Integer
' Signal that recording for monitoring has stopped (no longer available)
bMonitoring = False
' Stops monitoring
ret = waveInStop(hWaveIn_Monitor)
' Reset it
ret = waveInReset(hWaveIn_Monitor)
' Unprepare buffers
For i = 0 To BUFFERS_MONITOR
waveInUnprepareHeader hWaveIn_Monitor, hdr_monitor(i), Len(hdr_monitor(i))
GlobalUnlock hdr_monitor(i).lpData
GlobalFree hdr_monitor(i).lpData
Next i
' Close hWaveIn_Monitor
ret = waveInClose(hWaveIn_Monitor)
' Clear handle
hWaveIn_Monitor = 0
End Sub
' ===============
' Stops recording
' ---------------
' this stops main recording
'================
Sub StopRec()
Dim i As Integer
' if not recording, exit sub
If Not bRecording Then Exit Sub
' stop sending data
ret = waveInStop(hWaveIn)
' reset hWaveIn
ret = waveInReset(hWaveIn)
' Unprepare buffers
For i = 0 To BUFFERS
waveInUnprepareHeader hWaveIn, hdr(i), Len(hdr(i))
GlobalUnlock hdr(i).lpData
GlobalFree hdr(i).lpData
Next i
' Close device
ret = waveInClose(hWaveIn)
' Write header only if its not already written AND output _MUST_ be WAVE (obviously)
If (bHeaderWritten = False) And (OutMode = WAVE) Then WriteWAVHeader
' closes recorded file
Close #num
' Signal that recording has stopped
bRecording = False
' If recording to MP3 stop (kill) encoder
' killing lame.exe is because Lame just can't know _when_ we want to stop it
If OutMode = MP3 Then StopEncoder
' Clear handle
hWaveIn = 0
End Sub
' =====================
' Pause monitoring : toggling function
' =====================
Public Sub PauseMonitoring()
Dim i As Integer
' Toggle monitoring signal "Paused"
bPaused_Monitoring = Not bPaused_Monitoring
If bPaused_Monitoring Then
' stop sending data
waveInStop hWaveIn_Monitor
Else
' We have to add buffers or we won't get any data
For i = 0 To BUFFERS_MONITOR
waveInAddBuffer hWaveIn_Monitor, hdr_monitor(i), Len(hdr_monitor(i))
Next
' Reset it
waveInReset hWaveIn_Monitor
' Start monitoring (hWaveIn_Monitor)
waveInStart hWaveIn_Monitor
End If
End Sub
' =======
' Prepares monitoring
' =======
Public Sub PrepareMonitoring()
Dim formatMonitor As WAVEFORMATEX
Dim i As Integer
' Set constant parameters for monitoring
With formatMonitor
.wFormatTag = 1
.nChannels = 2
.wBitsPerSample = 16
.nSamplesPerSec = 44100
.nBlockAlign = .nChannels * .wBitsPerSample / 8
.nAvgBytesPerSec = .nSamplesPerSec * .nBlockAlign
End With
' open recording (hWaveIn_Monitor) with format (formatMonitor), send callbacks
' (CALLBACK_WINDOW) to main form (frmMain.hwnd)
ret = waveInOpen(hWaveIn_Monitor, 0, formatMonitor, hwnd, 0&, CALLBACK_WINDOW)
If ret <> 0 Then
' Was there an error? Catch it and display
waveInGetErrorText ret, msg, Len(msg)
MessageBox 0&, Trim(msg), App.Title, MB_ICONERROR
' quit
Exit Sub
End If
' Preparing buffers for monitoring
For i = 0 To BUFFERS_MONITOR
' Allocate space buffer (size BUFFERSIZE_MONITOR)
' Notice GMEM_FIXED, it's essential, because otherwise MM system won't be able
' to write into it.
hmem_monitor(i) = GlobalAlloc(GMEM_FIXED, BUFFERSIZE_MONITOR)
With hdr_monitor(i)
.lpData = GlobalLock(hmem_monitor(i)) ' Lock buffer (I don't know exactly why...)
.dwBufferLength = BUFFERSIZE_MONITOR ' Set buffer length
.dwFlags = 0 ' no flags
.dwLoops = 0 ' no loops
.dwUser = CLng(i) + MONITOR ' Here we put _ID_ of buffer
' Notice MONITOR constant (up). It's for callback function to recognize
' whether it's montoring or recording data
End With
Next
' Prepare buffers + Add them to MM systemback function to recognize
' wh hWaveIn_Monitor = 0
systembaOruSot r Next
sysA systemback function tltMonito ThWave
' Prepare buffers hR Om ' Sedata oops
.dwUser uSon waveInStop Lib "wt with format (formatM