Home

Add

Edit

Without Linenumbers

Code in Textfield

Download

  1. '################################################################################################################################################
    
  2. #INCLUDE "bass24.bi"
    
  3. #INCLUDE "bass24mix.bi"
    
  4. 
    
  5. 
    
  6. 
    
  7. '################################################################################################################################################
    
  8. Dim Shared G_GFX_Mux        As Any Ptr
    
  9. 
    
  10. 
    
  11. 
    
  12. '################################################################################################################################################
    
  13. Enum NHF_Audio_GURU
    
  14.     NHFA_GURU_NoError                   = 1
    
  15.     NHFA_GURU_UnknownError              = 0
    
  16.     NHFA_GURU_CantInitDevice            = -1000
    
  17.     NHFA_GURU_CantStartRecord           = -1001
    
  18.     NHFA_GURU_CantFree                  = -1002
    
  19.     NHFA_GURU_CantLoadFile              = -1003
    
  20. End Enum
    
  21. 
    
  22. 
    
  23. 
    
  24. '################################################################################################################################################
    
  25. Dim Shared NHF_Audio_OutSoundDidHandle  As HSTREAM
    
  26. Dim Shared NHF_Audio_OutSoundDaaHandle  As HSTREAM
    
  27. Dim Shared NHF_Audio_OutHandle          As HSTREAM
    
  28. 
    
  29. 
    
  30. 
    
  31. '################################################################################################################################################
    
  32. Dim Shared NHF_Audio_RecHandle              As HRECORD
    
  33. Dim Shared NHF_Audio_ThreadPtr              As Any Ptr
    
  34. Dim Shared NHF_Audio_ThreadEx               As Ubyte
    
  35. Dim Shared NHF_Audio_Mux                    As Any Ptr
    
  36. Dim Shared NHF_Audio_RecGFX_SpectrumW       As Integer
    
  37. Dim Shared NHF_Audio_RecGFX_SpectrumH       As Integer
    
  38. Dim Shared NHF_Audio_RecGFX_Img             As Any Ptr
    
  39. Dim Shared NHF_Audio_OutGFX_SpectrumW       As Integer
    
  40. Dim Shared NHF_Audio_OutGFX_SpectrumH       As Integer
    
  41. Dim Shared NHF_Audio_OutGFX_Img             As Any Ptr
    
  42. 
    
  43. 
    
  44. 
    
  45. '################################################################################################################################################
    
  46. Function NHF_Audio_Output_InitSounds() As NHF_Audio_GURU
    
  47. Dim TPath As String = "data/sound/"
    
  48. Dim TLP As String
    
  49. TLP = TPath & "did.wav"
    
  50. NHF_Audio_OutSoundDidHandle = BASS_StreamCreateFile(FALSE, Strptr(TLP), 0, 0, 0)
    
  51. If NHF_Audio_OutSoundDidHandle = 0 Then Return NHFA_GURU_CantLoadFile
    
  52. 'TLP = TPath & "daa."wav
    
  53. TLP = "/media/data20_md1/sound/-mp3-/m/martika___toy_soldiers.mp3"
    
  54. NHF_Audio_OutSoundDaaHandle = BASS_StreamCreateFile(FALSE, Strptr(TLP), 0, 0, 0)
    
  55. If NHF_Audio_OutSoundDaaHandle = 0 Then Return NHFA_GURU_CantLoadFile
    
  56. 
    
  57. Return NHFA_GURU_NoError
    
  58. End Function
    
  59. 
    
  60. 
    
  61. 
    
  62. '################################################################################################################################################
    
  63. Function NHF_Audio_Output_Init(V_Device As Integer = -1, V_FRQ As Uinteger = 44100) As NHF_Audio_GURU
    
  64. If BASS_Init(V_Device, V_FRQ, BASS_DEVICE_MONO, 0, 0) = 0 Then Return NHFA_GURU_CantInitDevice
    
  65. Dim RV As NHF_Audio_GURU
    
  66. RV = NHF_Audio_Output_InitSounds()
    
  67. 
    
  68. NHF_Audio_OutHandle = BASS_Mixer_StreamCreate(V_FRQ, BASS_DEVICE_MONO, 0)
    
  69. BASS_Mixer_StreamAddChannel(NHF_Audio_OutHandle, NHF_Audio_OutSoundDidHandle, BASS_MIXER_DOWNMIX)
    
  70. BASS_Mixer_StreamAddChannel(NHF_Audio_OutHandle, NHF_Audio_OutSoundDaaHandle, BASS_MIXER_DOWNMIX)
    
  71. BASS_Mixer_ChannelFlags(NHF_Audio_OutHandle, BASS_MIXER_BUFFER, BASS_MIXER_BUFFER)
    
  72. 
    
  73. NHF_Audio_OutGFX_SpectrumW = 256
    
  74. NHF_Audio_OutGFX_SpectrumH = 50
    
  75. NHF_Audio_OutGFX_Img = Imagecreate(NHF_Audio_OutGFX_SpectrumW, NHF_Audio_OutGFX_SpectrumH, 32)
    
  76. Return RV
    
  77. Return NHFA_GURU_NoError
    
  78. End Function
    
  79. 
    
  80. '------------------------------------------------------------------------------------------------------------------------------------------------
    
  81. Function NHF_Audio_Output_Term() As NHF_Audio_GURU
    
  82. If NHF_Audio_OutHandle <> 0 Then BASS_StreamFree(NHF_Audio_OutHandle): NHF_Audio_OutHandle = 0
    
  83. If NHF_Audio_OutSoundDaaHandle <> 0 Then BASS_StreamFree(NHF_Audio_OutSoundDaaHandle): NHF_Audio_OutSoundDaaHandle = 0
    
  84. If NHF_Audio_OutSoundDidHandle <> 0 Then BASS_StreamFree(NHF_Audio_OutSoundDidHandle): NHF_Audio_OutSoundDidHandle = 0
    
  85. If BASS_Free() <> TRUE Then Return NHFA_GURU_CantFree
    
  86. Return NHFA_GURU_NoError
    
  87. End Function
    
  88. 
    
  89. 
    
  90. 
    
  91. '################################################################################################################################################
    
  92. Function NHF_Audio_InputCallback(Byval V_Handle As HRECORD, Byval V_Buffer As Any Ptr, Byval V_Length As Uinteger, Byval V_UserData As Any Ptr) As Integer
    
  93. 
    
  94. Return 1
    
  95. End Function
    
  96. 
    
  97. '------------------------------------------------------------------------------------------------------------------------------------------------
    
  98. Sub NHF_Audio_Thread()
    
  99. Dim TFFT(512) As Single
    
  100. Dim TDat(512) As Short
    
  101. Dim X As Uinteger
    
  102. Do
    
  103.     
    
  104.     BASS_ChannelGetData(NHF_Audio_RecHandle, @TFFT(0), BASS_DATA_FFT512)
    
  105.     BASS_ChannelGetData(NHF_Audio_RecHandle, @TDat(0), 512)
    
  106.     Mutexlock(G_GFX_Mux)
    
  107.     If NHF_Audio_ThreadEx = 1 Then Mutexunlock(G_GFX_Mux): Exit Do
    
  108.     If NHF_Audio_RecGFX_Img <> 0 Then
    
  109.         Line NHF_Audio_RecGFX_Img, (0, 0)-(NHF_Audio_RecGFX_SpectrumW - 1, NHF_Audio_RecGFX_SpectrumH - 1), &H000000, BF
    
  110.         For X = 1 To 255
    
  111.             Line NHF_Audio_RecGFX_Img, (X, NHF_Audio_RecGFX_SpectrumH - 1)-(X, (NHF_Audio_RecGFX_SpectrumH - 1) - NHF_Audio_RecGFX_SpectrumH * TFFT(X) * ((X + 1) / 20) * 5), &H666666
    
  112.             TFFT(X) = 0
    
  113.         Next
    
  114.         For X = 1 To 256 - 1
    
  115.             Line NHF_Audio_RecGFX_Img, (X, NHF_Audio_RecGFX_SpectrumH / 2 - (NHF_Audio_RecGFX_SpectrumH - 2) / 65535 * TDat(X))-((X + 1), NHF_Audio_RecGFX_SpectrumH / 2 - (NHF_Audio_RecGFX_SpectrumH - 2) / 65535 * TDat(X + 1)), &HFFFFFF
    
  116.             TDat(X) = 0
    
  117.         Next
    
  118.         TDat(256) = 0
    
  119.         Line NHF_Audio_RecGFX_Img, (0, 0)-(NHF_Audio_RecGFX_SpectrumW - 1, NHF_Audio_RecGFX_SpectrumH - 1), &H0000FF, B
    
  120.     End If
    
  121.     Mutexunlock(G_GFX_Mux)
    
  122.     
    
  123.     BASS_Mixer_ChannelGetData(NHF_Audio_OutHandle, @TFFT(0), BASS_DATA_FFT512)
    
  124.     BASS_Mixer_ChannelGetData(NHF_Audio_OutHandle, @TDat(0), 512)
    
  125.     Mutexlock(G_GFX_Mux)
    
  126.     If NHF_Audio_OutGFX_Img <> 0 Then
    
  127.         Line NHF_Audio_OutGFX_Img, (0, 0)-(NHF_Audio_OutGFX_SpectrumW - 1, NHF_Audio_OutGFX_SpectrumH - 1), &H000000, BF
    
  128.         For X = 1 To 255
    
  129.             Line NHF_Audio_OutGFX_Img, (X, NHF_Audio_OutGFX_SpectrumH - 1)-(X, (NHF_Audio_OutGFX_SpectrumH - 1) - NHF_Audio_OutGFX_SpectrumH * TFFT(X) * ((X + 1) / 20) * 5), &H666666
    
  130.         Next
    
  131.         For X = 1 To 256 - 1
    
  132.             Line NHF_Audio_OutGFX_Img, (X, NHF_Audio_OutGFX_SpectrumH / 2 - (NHF_Audio_OutGFX_SpectrumH - 2) / 65535 * TDat(X))-((X + 1), NHF_Audio_OutGFX_SpectrumH / 2 - (NHF_Audio_OutGFX_SpectrumH - 2) / 65535 * TDat(X + 1)), &HFFFFFF
    
  133.         Next
    
  134.         Line NHF_Audio_OutGFX_Img, (0, 0)-(NHF_Audio_OutGFX_SpectrumW - 1, NHF_Audio_OutGFX_SpectrumH - 1), &H0000FF, B
    
  135.     End If
    
  136.     Mutexunlock(G_GFX_Mux)
    
  137.     
    
  138.     Sleep 20, 1
    
  139. Loop
    
  140. End Sub
    
  141. 
    
  142. '------------------------------------------------------------------------------------------------------------------------------------------------
    
  143. Function NHF_Audio_Input_Init(V_Device As Integer = -1, V_FRQ As Uinteger = 8000) As NHF_Audio_GURU
    
  144. If BASS_RecordInit(V_Device) = 0 Then Return NHFA_GURU_CantInitDevice
    
  145. NHF_Audio_Mux = Mutexcreate()
    
  146. NHF_Audio_RecGFX_SpectrumW = 256
    
  147. NHF_Audio_RecGFX_SpectrumH = 50
    
  148. NHF_Audio_RecGFX_Img = Imagecreate(NHF_Audio_RecGFX_SpectrumW, NHF_Audio_RecGFX_SpectrumH, 32)
    
  149. NHF_Audio_RecHandle = BASS_RecordStart(V_FRQ, 1, &HA0000, Cast(RECORDPROC Ptr, @NHF_Audio_InputCallback), 0)
    
  150. If NHF_Audio_RecHandle = 0 Then
    
  151.     Imagedestroy(NHF_Audio_RecGFX_Img)
    
  152.     Return NHFA_GURU_CantStartRecord
    
  153. End If
    
  154. NHF_Audio_ThreadEx = 0
    
  155. NHF_Audio_ThreadPtr = Threadcreate(Cast(Any Ptr, @NHF_Audio_Thread))
    
  156. Return NHFA_GURU_NoError
    
  157. End Function
    
  158. 
    
  159. '------------------------------------------------------------------------------------------------------------------------------------------------
    
  160. Function NHF_Audio_Input_Term() As NHF_Audio_GURU
    
  161. If BASS_RecordFree() <> TRUE Then Return NHFA_GURU_CantFree
    
  162. Mutexlock(NHF_Audio_Mux)
    
  163. Imagedestroy(NHF_Audio_RecGFX_Img)
    
  164. NHF_Audio_RecGFX_Img = 0
    
  165. NHF_Audio_ThreadEx = 1
    
  166. Mutexunlock(NHF_Audio_Mux)
    
  167. Threadwait(NHF_Audio_ThreadPtr)
    
  168. Mutexdestroy(NHF_Audio_Mux)
    
  169. Return NHFA_GURU_NoError
    
  170. End Function
    
  171. 
    
  172. 
    
  173. 
    
  174. '################################################################################################################################################
    
  175. Screenres 800, 600, 32
    
  176. 
    
  177. 
    
  178. 
    
  179. '################################################################################################################################################
    
  180. G_GFX_Mux = Mutexcreate()
    
  181. NHF_Audio_Output_Init()
    
  182. NHF_Audio_Input_Init()
    
  183. 
    
  184. BASS_ChannelPlay(NHF_Audio_OutSoundDaaHandle, FALSE)
    
  185. BASS_ChannelPlay(NHF_Audio_OutHandle, FALSE)
    
  186. 
    
  187. 
    
  188. '################################################################################################################################################
    
  189. Do
    
  190.     Mutexlock(G_GFX_Mux)
    
  191.     If Inkey() = Chr(27) Then Mutexunlock(G_GFX_Mux): Exit Do
    
  192.     Screenlock()
    
  193.     Cls()
    
  194.     Put (0, 0), NHF_Audio_RecGFX_Img, Pset
    
  195.     Put (0, NHF_Audio_RecGFX_SpectrumH), NHF_Audio_OutGFX_Img, Pset
    
  196.     Screenunlock()
    
  197.     Mutexunlock(G_GFX_Mux)
    
  198.     Sleep 10, 1
    
  199. Loop
    
  200. 
    
  201. Screen 0
    
  202. 
    
  203. 
    
  204. 
    
  205. '################################################################################################################################################
    
  206. Print "Try exit..."
    
  207. NHF_Audio_Input_Term()
    
  208. NHF_Audio_Output_Term()
    
  209. Mutexdestroy(G_GFX_Mux)
    
  210. Print "END!"
    
  211. 
    
  212. 
    
  213.