home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 4_2005-2006.ISO / data / Zips / WaveIn_Rec2001136152006.psc / EncoderWAV.cls < prev   
Text File  |  2006-06-14  |  13KB  |  439 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "EncoderWAV"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15.  
  16. ' WAV Encoder with ACM support for compressed formats
  17. ' (from my project "Monoton")
  18.  
  19. ' this class implemented "IEncoder".
  20. ' to hold the size of the project down, I removed it.
  21. ' now the members of IEncoder have the prefix "encoder".
  22.  
  23.  
  24. Private Const Extension         As String = "wav"
  25. Private Const Description       As String = "Wave ACM Encoder"
  26.  
  27.  
  28. Private Declare Sub CpyMem Lib "kernel32" _
  29. Alias "RtlMoveMemory" ( _
  30.     pDst As Any, _
  31.     pSrc As Any, _
  32.     ByVal dwLen As Long _
  33. )
  34.  
  35. Private Declare Sub ZeroMem Lib "kernel32" _
  36. Alias "RtlZeroMemory" ( _
  37.     pDst As Any, _
  38.     ByVal dwLen As Long _
  39. )
  40.  
  41. Private Declare Function IsBadReadPtr Lib "kernel32" ( _
  42.     ptr As Any, _
  43.     ByVal ucb As Long _
  44. ) As Long
  45.  
  46. Private Declare Function IsBadWritePtr Lib "kernel32" ( _
  47.     ptr As Any, _
  48.     ByVal ucb As Long _
  49. ) As Long
  50.  
  51. Private Declare Function CreateFile Lib "kernel32.dll" _
  52. Alias "CreateFileA" ( _
  53.     ByVal lpFileName As String, _
  54.     ByVal dwDesiredAccess As Long, _
  55.     ByVal dwShareMode As Long, _
  56.     lpSecurityAttributes As Any, _
  57.     ByVal dwCreationDisposition As Long, _
  58.     ByVal dwFlagsAndAttributes As Long, _
  59.     ByVal hTemplateFile As Long _
  60. ) As Long
  61.  
  62. Private Declare Function ReadFile Lib "kernel32" ( _
  63.     ByVal HFILE As Long, _
  64.     lpBuffer As Any, _
  65.     ByVal nNumberOfBytesToRead As Long, _
  66.     lpNumberOfBytesRead As Long, _
  67.     ByVal lpOverlapped As Any _
  68. ) As Long
  69.  
  70. Private Declare Function WriteFile Lib "kernel32" ( _
  71.     ByVal HFILE As Long, _
  72.     lpBuffer As Any, _
  73.     ByVal nNumberOfBytesToWrite As Long, _
  74.     lpNumberOfBytesWritten As Long, _
  75.     ByVal lpOverlapped As Any _
  76. ) As Long
  77.  
  78. Private Declare Function SetFilePointer Lib "kernel32" ( _
  79.     ByVal HFILE As Long, _
  80.     ByVal lDistanceToMove As Long, _
  81.     ByVal lpDistanceToMoveHigh As Long, _
  82.     ByVal dwMoveMethod As Long _
  83. ) As Long
  84.  
  85. Private Declare Function GetFileSize Lib "kernel32" ( _
  86.     ByVal HFILE As Long, _
  87.     ByVal lpFileSizeHigh As Long _
  88. ) As Long
  89.  
  90. Private Declare Function CloseHandle Lib "kernel32" ( _
  91.     ByVal hObject As Long _
  92. ) As Long
  93.  
  94. Private Declare Function acmStreamPrepareHeader Lib "msacm32" ( _
  95.     ByVal has As Long, _
  96.     pash As ACMSTREAMHEADER, _
  97.     ByVal fdwPrepare As Long _
  98. ) As Long
  99.  
  100. Private Declare Function acmStreamUnprepareHeader Lib "msacm32" ( _
  101.     ByVal has As Long, _
  102.     pash As ACMSTREAMHEADER, _
  103.     ByVal fdwUnprepare As Long _
  104. ) As Long
  105.  
  106. Private Declare Function acmStreamOpen Lib "msacm32" ( _
  107.     phas As Long, _
  108.     ByVal had As Long, _
  109.     pwfxSrc As Any, _
  110.     pwfxDst As Any, _
  111.     ByVal pwfltr As Long, _
  112.     ByVal dwCallback As Long, _
  113.     ByVal dwInstance As Long, _
  114.     ByVal fdwOpen As Long _
  115. ) As Long
  116.  
  117. Private Declare Function acmStreamSize Lib "msacm32" ( _
  118.     ByVal has As Long, _
  119.     ByVal cbInput As Long, _
  120.     pdwOutputBytes As Long, _
  121.     ByVal fdwSize As Long _
  122. ) As Long
  123.  
  124. Private Declare Function acmStreamConvert Lib "msacm32" ( _
  125.     ByVal has As Long, _
  126.     pash As ACMSTREAMHEADER, _
  127.     ByVal fdwConvert As Long _
  128. ) As Long
  129.  
  130. Private Declare Function acmStreamReset Lib "msacm32" ( _
  131.     ByVal has As Long, _
  132.     ByVal fdwReset As Long _
  133. ) As Long
  134.  
  135. Private Declare Function acmStreamClose Lib "msacm32" ( _
  136.     ByVal has As Long, _
  137.     ByVal fdwClose As Long _
  138. ) As Long
  139.  
  140. Private Declare Function acmFormatChoose Lib "msacm32" _
  141. Alias "acmFormatChooseA" ( _
  142.     pfmtc As ACMFORMATCHOOSEA _
  143. ) As Long
  144.  
  145. Public Enum SND_RESULT
  146.     SND_ERR_SUCCESS = 0
  147.     SND_ERR_INVALID_SOURCE
  148.     SND_ERR_INVALID_OUTPUT
  149.     SND_ERR_INTERNAL
  150.     SND_ERR_OUT_OF_RANGE
  151.     SND_ERR_END_OF_STREAM
  152.     SND_ERR_INVALID_TAG
  153.     SND_ERR_INVALID_PARAM
  154.     SND_ERR_TOO_BIG
  155.     SND_ERR_NEED_MORE
  156.     SND_ERR_WRITE_ERROR
  157.     SND_ERR_UNKNOWN
  158. End Enum
  159.  
  160. Public Enum ENCODER_METRICS
  161.     ENC_M_PCM_PER_DELIVERY_MIN = 0
  162.     ENC_M_PCM_PER_DELIVERY_MAX
  163.     ENC_M_SUPPORT_TAGS
  164. End Enum
  165.  
  166. Private Enum FILE_OPEN_METHOD
  167.     CREATE_NEW = 1
  168.     CREATE_ALWAYS = 2
  169.     OPEN_EXISTING = 3
  170.     OPEN_ALWAYS = 4
  171. End Enum
  172.  
  173. Private Enum FILE_SHARE_RIGHTS
  174.     FILE_SHARE_READ = &H1
  175.     FILE_SHARE_WRITE = &H2
  176. End Enum
  177.  
  178. Private Enum FILE_ACCESS_RIGHTS
  179.     GENERIC_READ = &H80000000
  180.     GENERIC_WRITE = &H40000000
  181. End Enum
  182.  
  183. Private Enum SEEK_METHOD
  184.     FILE_BEGIN = 0
  185.     FILE_CURRENT = 1
  186.     FILE_END = 2
  187. End Enum
  188.  
  189. Private Enum HACMSTREAM
  190.     INVALID_STREAM_HANDLE = 0
  191. End Enum
  192.  
  193. Private Enum ACM_STREAMSIZEF
  194.     ACM_STREAMSIZEF_DESTINATION = &H1
  195.     ACM_STREAMSIZEF_SOURCE = &H0
  196.     ACM_STREAMSIZEF_QUERYMASK = &HF
  197. End Enum
  198.  
  199. Private Enum ACM_STREAMCONVERTF
  200.     ACM_STREAMCONVERTF_BLOCKALIGN = &H4
  201.     ACM_STREAMCONVERTF_START = &H10
  202.     ACM_STREAMCONVERTF_END = &H20
  203. End Enum
  204.  
  205. Private Const INVALID_HANDLE                As Long = -1
  206.  
  207. Private Const ACMFMTDET_FORMAT_CHARS        As Long = 128
  208. Private Const ACMFMTTAGDET_FORMATTAG_CHARS  As Long = 48
  209.  
  210. Private Const ACM_FORMATENUMF_CONVERT       As Long = &H100000
  211.  
  212. Private Const WAVE_FORMAT_PCM               As Long = 1
  213.  
  214. ' some codecs (e.g. Vorbis ACM) have very big
  215. ' WFX structs. so make the WFX buffer extra large.
  216. Private Const WFXSize                       As Long = 128& * 1024&
  217.  
  218. Private Const WAV_RIFF                      As Long = 1179011410
  219. Private Const WAV_WAVE                      As Long = 1163280727
  220. Private Const WAV_DATA                      As Long = 1635017060
  221. Private Const WAV_FMT                       As Long = 544501094
  222.  
  223. Private Type HFILE
  224.     handle                                  As Long
  225.     path                                    As String
  226. End Type
  227.  
  228. Private Type ACMSTREAMHEADER
  229.     cbStruct                                As Long
  230.     fdwStatus                               As Long
  231.     dwUser                                  As Long
  232.     pbSrc                                   As Long
  233.     cbSrcLength                             As Long
  234.     cbSrcLengthUsed                         As Long
  235.     dwSrcUser                               As Long
  236.     pbDst                                   As Long
  237.     cbDstLength                             As Long
  238.     cbDstLengthUsed                         As Long
  239.     dwDstUser                               As Long
  240.     dwReservedDriver(9)                     As Long
  241. End Type
  242.  
  243. Private Type ACMFORMATCHOOSEA
  244.     cbStruct                                As Long
  245.     fdwStyle                                As Long
  246.     hwndOwner                               As Long
  247.     pwfx                                    As Long
  248.     cbwfx                                   As Long
  249.     pszTitle                                As Long
  250.     szFormatTag                             As String * ACMFMTTAGDET_FORMATTAG_CHARS
  251.     szFormat                                As String * ACMFMTDET_FORMAT_CHARS
  252.     pszName                                 As Long
  253.     cchName                                 As Long
  254.     fdwEnum                                 As Long
  255.     pwfxEnum                                As Long
  256.     hInstance                               As Long
  257.     pszTemplateName                         As Long
  258.     lCustData                               As Long
  259.     pfnHook                                 As Long
  260.     btSpace(1023)                           As Byte ' had some strange crashes...
  261. End Type
  262.  
  263. Private Type MMWAVEFORMATEX
  264.     wFormatTag                              As Integer
  265.     nChannels                               As Integer
  266.     nSamplesPerSec                          As Long
  267.     nAvgBytesPerSec                         As Long
  268.     nBlockAlign                             As Integer
  269.     wBitsPerSample                          As Integer
  270.     cbSize                                  As Integer
  271. End Type
  272.  
  273. Private Type WAVRIFF
  274.     RIFF                                    As Long         ' "RIFF"
  275.     hdrlen                                  As Long
  276.     WAVE                                    As Long         ' "WAVE"
  277.     fmt                                     As Long         ' "fmt "
  278.     chnksize                                As Long
  279. End Type
  280.  
  281. Private udtWFXIn                            As MMWAVEFORMATEX
  282. Private btWfxOut()                          As Byte
  283. Private lngWFXOutLen                        As Long
  284. Private strFormatID                         As String
  285. Private strFormatTag                        As String
  286.  
  287. Private btInp()                             As Byte
  288. Private btOut()                             As Byte
  289. Private lngInpLen                           As Long
  290. Private lngOutLen                           As Long
  291.  
  292. Private udtStreamHdr                        As ACMSTREAMHEADER
  293. Private hStream                             As HACMSTREAM
  294.  
  295. Private hFOut                               As HFILE
  296.  
  297. Private blnWriteHeader                      As Boolean
  298.  
  299. '''''''''''''''''''''''''''''''''''''''''''''''''
  300. '''''''''''''''''''''''''''''''''''''''''''''''''
  301. '''''''''''''''''''''''''''''''''''''''''''''''''
  302.  
  303. Private Sub Class_Initialize()
  304.     Dim wfx As MMWAVEFORMATEX
  305.  
  306.     ReDim btWfxOut(WFXSize - 1) As Byte
  307.     blnWriteHeader = True
  308.  
  309.     wfx = CreateWFX(44100, 2, 16)
  310.  
  311.     SetFormat VarPtr(wfx), _
  312.               Len(wfx), _
  313.               "44.1 kHz 16 Bit Stereo", "PCM"
  314.  
  315.     hFOut.handle = INVALID_HANDLE
  316. End Sub
  317.  
  318. Private Sub Class_Terminate()
  319.     Encoder_EncoderClose
  320. End Sub
  321.  
  322. '''''''''''''''''''''''''''''''''''''''''''''''''
  323. '''''''''''''''''''''''''''''''''''''''''''''''''
  324. '''''''''''''''''''''''''''''''''''''''''''''''''
  325.  
  326. ' write RIFF/WAVE/FMT /DATA chunks
  327. Public Property Get WriteHeader( _
  328. ) As Boolean
  329.  
  330.     WriteHeader = blnWriteHeader
  331. End Property
  332.  
  333. Public Property Let WriteHeader( _
  334. ByVal bln As Boolean _
  335. )
  336.  
  337.     If hFOut.handle = INVALID_HANDLE Then
  338.         blnWriteHeader = bln
  339.     Else
  340.         Err.Raise 90, , "Encoder already running!"
  341.     End If
  342. End Property
  343.  
  344. Public Property Get FormatTag( _
  345. ) As String
  346.  
  347.     FormatTag = strFormatTag
  348. End Property
  349.  
  350. Public Property Get FormatID( _
  351. ) As String
  352.  
  353.     FormatID = strFormatID
  354. End Property
  355.  
  356. Public Sub SetPCMFormat( _
  357.     ByVal samplerate As Long, _
  358.     ByVal Channels As Integer, _
  359.     Optional ByVal FormatID As String, _
  360.     Optional ByVal FormatTag As String _
  361. )
  362.  
  363.     Dim wfx As MMWAVEFORMATEX
  364.  
  365.     wfx = CreateWFX(samplerate, Channels, 16)
  366.  
  367.     If FormatID = "" Then
  368.         With wfx
  369.             strFormatID = Round(.nSamplesPerSec / 1000, 1) & " " & _
  370.                           .wBitsPerSample & " Bit " & _
  371.                           IIf(.nChannels = 2, "stereo", "mono")
  372.         End With
  373.     End If
  374.  
  375.     If FormatTag = "" Then
  376.         FormatTag = "PCM"
  377.     End If
  378.  
  379.     SetFormat VarPtr(wfx), Len(wfx), _
  380.               FormatID, FormatTag
  381. End Sub
  382.  
  383. Public Sub SetFormat( _
  384.     ByVal wfx_ptr As Long, _
  385.     ByVal wfx_len As Long, _
  386.     Optional FormatID As String, _
  387.     Optional FormatTag As String _
  388. )
  389.  
  390.     If 0 = IsBadReadPtr(ByVal wfx_ptr, wfx_len) Then
  391.         If wfx_len > UBound(btWfxOut) + 1 Then
  392.             ReDim btWfxOut(wfx_len - 1) As Byte
  393.         End If
  394.         CpyMem btWfxOut(0), ByVal wfx_ptr, wfx_len
  395.  
  396.         lngWFXOutLen = wfx_len
  397.  
  398.         strFormatID = FormatID
  399.         strFormatTag = FormatTag
  400.     End If
  401. End Sub
  402.  
  403. Public Function SelectFormat( _
  404.     ByVal samplerate As Long, _
  405.     ByVal Channels As Integer, _
  406.     Optional ByVal hwndOwner As Long, _
  407.     Optional ByVal Title As String = "WAV Format" _
  408. ) As SND_RESULT
  409.  
  410.     Dim btTitle()   As Byte
  411.     Dim btWFX()     As Byte
  412.     Dim wfx         As MMWAVEFORMATEX
  413.     Dim wfxinp      As MMWAVEFORMATEX
  414.     Dim udtFmtCh    As ACMFORMATCHOOSEA
  415.  
  416.     ReDim btWFX(WFXSize - 1) As Byte
  417.  
  418.     btTitle = StrConv(Title & Chr$(0), vbFromUnicode)
  419.  
  420.     With wfxinp
  421.         .nSamplesPerSec = samplerate
  422.         .nChannels = Channels
  423.         .wBitsPerSample = 16
  424.         .wFormatTag = WAVE_FORMAT_PCM
  425.         .nBlockAlign = .nChannels * (.wBitsPerSample / 8)
  426.         .nAvgBytesPerSec = .nSamplesPerSec * .nBlockAlign
  427.     End With
  428.  
  429.     CpyMem btWFX(0), wfxinp, Len(wfxinp)
  430.  
  431.     With udtFmtCh
  432.         .cbStruct = LenB(udtFmtCh)
  433.         .hwndOwner = hwndOwner
  434.         .pwfx = VarPtr(btWFX(0))
  435.         .cbwfx = WFXSize
  436.         .pwfxEnum = VarPtr(wfxinp)
  437.         .fdwEnum C .hwndOwner ibutes As Any, _
  438.     ByVal le            x As MMWAVEFORMATE((((((((((((mOSub SetFormat( _
  439.     ByVal wfx_pPtr(wfxi4vate Const DescripEenBi