home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Visual Basic new SourceCode and Projects / Audio Recorder 2.0 / modWave.bas < prev    next >
Encoding:
BASIC Source File  |  2000-02-05  |  11.5 KB  |  311 lines

  1. Attribute VB_Name = "modWave"
  2. Option Explicit
  3.  
  4. Public Rate As Long
  5.     
  6. Public Channels As Integer
  7.  
  8. Public Resolution As Integer
  9.  
  10. Public WaveStatusMsg As String * 255
  11.  
  12. Public WaveStatisticsMsg As String
  13.  
  14. Public WaveRecordingImmediate As Boolean
  15.  
  16. Public WaveRecordingStartTime As Date
  17.  
  18. Public WaveRecordingStopTime As Date
  19.  
  20. Public WaveRecordingReady As Boolean
  21.  
  22. Public WaveRecording As Boolean
  23.  
  24. Public WavePlaying As Boolean
  25.  
  26. Public WaveAutomaticSave As Boolean
  27.  
  28. Public WaveFileName As String
  29.  
  30. Public WaveMidiFileName As String
  31.  
  32. Public WaveLongFileName As String
  33. Public WaveShortFileName As String
  34. Public WaveRenameNecessary As Boolean
  35.  
  36. 'These were the public variables
  37. '===============================================================================
  38. Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrrtning As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
  39.    
  40. Private Declare Function GetShortPathName Lib "kernel32" _
  41.       Alias "GetShortPathNameA" (ByVal lpszLongPath As String, _
  42.       ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
  43.      
  44. Private Declare Function FindFirstFile& Lib "kernel32" _
  45.        Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData _
  46.        As WIN32_FIND_DATA)
  47.  
  48. Private Declare Function FindClose Lib "kernel32" _
  49.        (ByVal hFindFile As Long) As Long
  50.        
  51. Private Const MAX_PATH = 260
  52.  
  53. Private Type FILETIME ' 8 Bytes
  54.         dwLowDateTime As Long
  55.         dwHighDateTime As Long
  56. End Type
  57.   
  58. Private Type WIN32_FIND_DATA ' 318 Bytes
  59.         dwFileAttributes As Long
  60.         ftCreationTime As FILETIME
  61.         ftLastAccessTime As FILETIME
  62.         ftLastWriteTime As FILETIME
  63.         nFileSizeHigh As Long
  64.         nFileSizeLow As Long
  65.         dwReserved» As Long
  66.         dwReserved1 As Long
  67.         cFileName As String * MAX_PATH
  68.         cAlternate As String * 14
  69. End Type
  70.  
  71. Private Function FileExist(strFileName As String) As Boolean
  72.  
  73. Dim lpFindFileData As WIN32_FIND_DATA
  74. Dim hFindFirst As Long
  75.     hFindFirst = FindFirstFile(strFileName, lpFindFileData)
  76.     If hFindFirst > 0 Then
  77.         FindClose hFindFirst
  78.         FileExist = True
  79.     Else
  80.         FileExist = False
  81.     End If
  82. End Function
  83.  
  84. Public Function GetShortName(ByVal sLongFileName As String) As String
  85.     Dim lRetVal As Long, sShortPathName As String, iLen As Integer
  86.     'Set up buffer area for API function call return
  87.     sShortPathName = Space(255)
  88.     iLen = Len(sShortPathName)
  89.  
  90.     'Call the function
  91.     lRetVal = GetShortPathName(sLongFileName, sShortPathName, iLen)
  92.     If lRetVal = 0 Then 'The file does not exist, first create it!
  93.         Open sLongFileName For Random As #1
  94.         Close #1
  95.         lRetVal = GetShortPathName(sLongFileName, sShortPathName, iLen)
  96.         'Now another try!
  97.         Kill (sLongFileName)
  98.         'Delete file now!
  99.     End If
  100.     'Strip away unwanted characters.
  101.     GetShortName = Left(sShortPathName, lRetVal)
  102. End Function
  103.  
  104. Private Function Has_Space(sName As String) As Boolean
  105.     Dim b As Boolean
  106.     Dim i As Long
  107.         
  108.     b = False 'not yet any spaces found
  109.     i = InStr(sName, " ")
  110.     If i <> 0 Then b = True
  111.     Has_Space = b
  112. End Function
  113.   
  114. Public Sub WaveReset()
  115.     Dim rtn As String
  116.     Dim i As Long
  117.     
  118.     rtn = Space$(260)
  119.     'Close any MCI operations from previous VB programs
  120.     i = mciSendString("close all", rtn, Len(rtn), 0)
  121.     If i <> 0 Then MsgBox ("Closing all MCI operations failed!")
  122.     'Open a new WAV with MCI Command...
  123.     i = mciSendString("open new type waveaudio alias capture", rtn, Len(rtn), 0)
  124.     If i <> 0 Then MsgBox ("Opening new wave failed!")
  125. End Sub
  126.  
  127. Public Sub WaveSet()
  128.     Dim rtn As String
  129.     Dim i As Long
  130.     Dim settings As String
  131.     Dim Alignment As Integer
  132.        
  133.     rtn = Space$(260)
  134.   
  135.     Alignment = Channels * Resolution / 8
  136.     
  137.     settings = "set capture alignment " & CStr(Alignment) & " bitspersample " & CStr(Resolution) & " samplespersec " & CStr(Rate) & " channels " & CStr(Channels) & " bytespersec " & CStr(Alignment * Rate)
  138.  
  139.     'Samples Per Second that are supported:
  140.     '11025     low quality
  141.     '22050     medium quality
  142.     '44100     high quality (CD music quality)
  143.     'Bits per sample is 16 or 8
  144.     'Channels are 1 (mono) or 2 (stereo)
  145.  
  146.     i = mciSendString("seek capture to start", rtn, Len(rtn), 0) 'Always start at the beginning
  147.     If i <> 0 Then MsgBox ("Starting recording failed!")
  148.     'You can use at least the following combinations
  149.      
  150.     ' i = mciSendString("set capture alignment 4 bitspersample 16 samplespersec 44100 channels 2 bytespersec 176400", rtn, Len(rtn), 0)
  151.     ' i = mciSendString("set capture alignment 2 bitspersample 16 samplespersec 44100 channels 1 bytespersec 88200", rtn, Len(rtn), 0)
  152.     ' i = mciSendString("set capture alignment 4 bitspersample 16 samplespersec 22050 channels 2 bytespersec 88200", rtn, Len(rtn), 0)
  153.     ' i = mciSendString("set capture alignment 2 bitspersample 16 samplespersec 22050 channels 1 bytespersec 44100", rtn, Len(rtn), 0)
  154.     ' i = mciSendString("set capture alignment 4 bitspersample 16 samplespersec 11025 channels 2 bytespersec 44100", rtn, Len(rtn), 0)
  155.     ' i = mciSendString("set capture alignment 2 bitspersample 16 samplespersec 11025 channels 1 bytespersec 22050", rtn, Len(rtn), 0)
  156.     ' i = mciSendString("set capture alignment 2 bitspersample 8 samplespersec 11025 channels 2 bytespersec 22050", rtn, Len(rtn), 0)
  157.     ' i = mciSendString("set capture alignment 1 bitspersample 8 samplespersec 11025 channels 1 bytespersec 11025", rtn, Len(rtn), 0)
  158.     ' i = mciSendString("set capture alignment 2 bitspersample 8 samplespersec 8000 channels 2 bytespersec 16000", rtn, Len(rtn), 0)
  159.     ' i = mciSendString("set capture alignment 1 bitspersample 8 samplespersec 8000 channels 1 bytespersec 8000", rtn, Len(rtn), 0)
  160.     ' i = mciSendString("set capture alignment 2 bitspersample 8 samplespersec 6000 channels 2 bytespersec 12000", rtn, Len(rtn), 0)
  161.     ' i = mciSendString("set capture alignment 1 bitspersample 8 samplespersec 6000 channels 1 bytespersec 6000", rtn, Len(rtn), 0)
  162.     
  163.     i = mciSendString(settings, rtn, Len(rtn), 0)
  164.  
  165.     If i <> 0 Then MsgBox ("Settings for recording not consistent")
  166.     ' If the combination is not supported you get an error!
  167.  End Sub
  168.  
  169.  Public Sub WaveRecord()
  170.     Dim rtn As String
  171.     Dim i As Long
  172.     Dim msg As String
  173.     
  174.     rtn = Space$(260)
  175.     
  176.     If WaveMidiFileName <> "" Then
  177.   
  178.         If WaveRecordingImmediate Then MsgBox ("Midi file " & WaveMidiFileName & " will be recorded")
  179.         i = mciSendString("open " & WaveMidiFileName & " type sequencer alias midi", rtn, Len(rtn), 0)
  180.         If i <> 0 Then MsgBox ("Opening midi file failed!")
  181.  
  182.         i = mciSendString("play midi", rtn, Len(rtn), 0)  'Start the recording
  183.         If i <> 0 Then MsgBox ("Playing midi file failed!")
  184.     End If
  185.    
  186.     i = mciSendString("record capture", rtn, Len(rtn), 0)  'Start the recording
  187.     If i <> 0 Then MsgBox ("Recording not possible, please restart your computer...")
  188.  End Sub
  189.  
  190. Public Sub WaveSaveAs(sName As String)
  191.    Dim rtn As String
  192.    Dim i As Long
  193.    
  194.    'If file already exists then remove it
  195.    
  196.     If FileExist(sName) Then
  197.         Kill (sName)
  198.     End If
  199.  
  200.     'The mciSendString API call doesn't seem to like'
  201.     'long filenames that have spaces in them, so we
  202.     'will make another API call to get the short
  203.     'filename version.
  204.     'This is accomplished by the function GetShortName
  205.             
  206.     'MCI command to save the WAV file
  207.      If Has_Space(sName) Then
  208.         WaveShortFileName = GetShortName(sName)
  209.         WaveLongFileName = sName
  210.         WaveRenameNecessary = True
  211.         ' These are necessary in order to be able to rename file
  212.         i = mciSendString("save capture " & WaveShortFileName, rtn, Len(rtn), 0)
  213.      Else
  214.         i = mciSendString("save capture " & sName, rtn, Len(rtn), 0)
  215.      End If
  216.      If i <> 0 Then MsgBox ("Saving file failed, file name was: " & sName)
  217. End Sub
  218.  
  219. Public Sub WaveStop()
  220.     Dim rtn As String
  221.     Dim i As Long
  222.     i = mciSendString("stop capture", rtn, Len(rtn), 0)
  223.     If i <> 0 Then MsgBox ("Stopping recording failed!")
  224.     If WaveMidiFileName <> "" Then
  225.         i = mciSendString("stop midi", rtn, Len(rtn), 0)
  226.         If i <> 0 Then MsgBox ("Stopping playing midi file failed!")
  227.     End If
  228. End Sub
  229.  
  230. Public Sub WavePlay()
  231.     Dim rtn As String
  232.     Dim i As Long
  233.     i = mciSendString("play capture from 0", rtn, Len(rtn), 0)
  234.     If i <> 0 Then MsgBox ("Start playing failed!")
  235. End Sub
  236.  
  237. Public Sub WaveStatus()
  238.     Dim i As Long
  239.     WaveStatusMsg = Space(255)
  240.     i = mciSendString("status capture mode", WaveStatusMsg, 255, 0)
  241.     If i <> 0 Then MsgBox ("Failure getting wave status...")
  242.     WaveStatusMsg = "AudioRecorder: " & WaveStatusMsg
  243. End Sub
  244.  
  245. Public Sub WaveStatistics()
  246.     Dim mssg As String * 255
  247.     Dim i As Long
  248.     i = mciSendString("set capture time format ms", 0&, 0, 0)
  249.     If i <> 0 Then MsgBox ("Setting time format in milliseconds failed!")
  250.     i = mciSendString("status capture length", mssg, 255, 0)
  251.     mssg = CStr(CLng(mssg) / 1000)
  252.     If i <> 0 Then MsgBox ("Finding length recording in milliseconds failed!")
  253.     WaveStatisticsMsg = "Length recording " & Str(mssg) & " s"
  254.  
  255.     i = mciSendString("set capture time format bytes", 0&, 0, 0)
  256.     If i <> 0 Then MsgBox ("Setting time format in bytes failed!")
  257.     i = mciSendString("status capture length", mssg, 255, 0)
  258.     If i <> 0 Then MsgBox ("Finding length recording in bytes failed!")
  259.     WaveStatisticsMsg = WaveStatisticsMsg & " (" & Str(mssg) & " bytes)" & vbCrLf
  260.  
  261.     i = mciSendString("status capture channels", mssg, 255, 0)
  262.     If i <> 0 Then MsgBox ("Finding number of channels failed!")
  263.     If Str(mssg) = 1 Then
  264.         WaveStatisticsMsg = WaveStatisticsMsg & "Mono - "
  265.         ElseIf Str(mssg) = 2 Then
  266.             WaveStatisticsMsg = WaveStatisticsMsg & "Stereo - "
  267.     End If
  268.  
  269.     i = mciSendString("status capture bitspersample", mssg, 255, 0)
  270.     If i <> 0 Then MsgBox ("Finding resolution failed!")
  271.     WaveStatisticsMsg = WaveStatisticsMsg & Str(mssg) & " bits - "
  272.  
  273.     i = mciSendString("status capture samplespersec", mssg, 255, 0)
  274.     If i <> 0 Then MsgBox ("Finding sample rate failed!")
  275.     WaveStatisticsMsg = WaveStatisticsMsg & Str(mssg) & " samples per second " & vbCrLf & vbCrLf
  276. End Sub
  277.  
  278. Public Sub WaveClose()
  279.     Dim rtn As String
  280.     Dim i As Long
  281.     i = mciSendString("close capture", rtn, Len(rtn), 0)
  282.     If i <> 0 Then MsgBox ("Closing MCI failed!")
  283. End Sub
  284.  
  285. Public Function WavePosition() As Long
  286.     Dim rtn As String
  287.     Dim i As Long
  288.     Dim pos As String
  289.     rtn = Space(255)
  290.     pos = Space(255)
  291.     
  292.     i = mciSendString("set capture time format ms", rtn, Len(rtn), 0)
  293.     If i <> 0 Then MsgBox ("Setting format in milliseconds failed!")
  294.     i = mciSendString("status capture position", pos, 255, 0)
  295.     If i <> 0 Then MsgBox ("Finding position failed!")
  296.     If i <> 0 Then MsgBox ("Error in position")
  297.     WavePosition = CLng(pos)
  298. End Function
  299.  
  300. Public Sub WavePlayFrom(Position As Long)
  301.     Dim rtn As String
  302.     Dim i As Long
  303.     Dim pos As String
  304.     pos = CStr(Position)
  305.     i = mciSendString("set capture time format ms", 0&, 0, 0)
  306.     If i <> 0 Then MsgBox ("Setting format in milliseconds failed!")
  307.     i = mciSendString("play capture from " & pos, rtn, Len(rtn), 0)
  308.     If i <> 0 Then MsgBox ("Playing from indicated position failed!")
  309.     If i <> 0 Then MsgBox ("Play from position doesn't work....")
  310. End Sub
  311.