| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1548 人关注过本帖
标题:[求助]该用哪些 API 函数来获取扬声器发出的声波呢?
只看楼主 加入收藏
leon2
Rank: 3Rank: 3
等 级:新手上路
威 望:7
帖 子:731
专家分:0
注 册:2005-3-18
结帖率:100%
收藏
 问题点数:0 回复次数:5 
[求助]该用哪些 API 函数来获取扬声器发出的声波呢?

本人最近做播放器,打算做一个声波指示,但是无从下手。我试过用一些以 wave 开头的 API 函数:

waveOutGetNumDevs
waveOutGetDevCaps
waveOutGetPitch
waveOutGetID
waveOutOpen
waveOutClose

但是我从未接触过这些“大腕”,所以我乱用,结果不行。
Windows Media Player 的声波指示是怎么做的呢?它用到了哪些 API 函数呢?

谢谢。不久我会在论坛上向大家展示这个播放器程序,当然,附源代码。
再次感谢!

搜索更多相关主题的帖子: 扬声器 API 声波 函数 获取 
2006-01-23 00:11
leon2
Rank: 3Rank: 3
等 级:新手上路
威 望:7
帖 子:731
专家分:0
注 册:2005-3-18
收藏
得分:0 
1 顶一下。

VB 论坛缺少精通这方面的人吗?
2006-01-23 16:07
xinfresh
Rank: 4
等 级:贵宾
威 望:13
帖 子:594
专家分:0
注 册:2006-1-13
收藏
得分:0 
我也正想做一个和声音有关的,在里面发了求助贴了,支持你,但帮不上忙,不好意思

E-mail:xinfresh@QQ:383094053校内:http:///getuser.do?id=234719042
2006-01-25 17:15
小伟的小伟
Rank: 1
等 级:禁止访问
帖 子:122
专家分:0
注 册:2006-1-25
收藏
得分:0 

sndPlaySound我知道这个
还有其他的

2006-01-25 20:00
leon2
Rank: 3Rank: 3
等 级:新手上路
威 望:7
帖 子:731
专家分:0
注 册:2005-3-18
收藏
得分:0 
顶!!
这个论坛缺少这方面的人才啊
2006-05-28 14:15
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
收藏
得分:0 

Dim fMovingSlider As Boolean
Private Sub Command1_Click()
Timer1.Interval = 100
Timer1.Enabled = True
FileSeek 0
Play
End Sub
Private Sub Command2_Click()
Timer1.Enabled = False
StopPlay
End Sub
Private Sub Form_Load()
Const sWavFile = "c:\windows\msremind.wav"
'Check if the file exists
If Dir(sWavFile) = "" Or sWavFile = "" Then
HScroll1.Enabled = False
MsgBox "File not found !" + vbCrLf + "Please choose a valid file !", vbCritical + vbOKOnly
Exit Sub
End If
HScroll1.Max = 100
HScroll1.SmallChange = 1
HScroll1.LargeChange = 5
Command1.Caption = "Play"
Command2.Caption = "Stop"
MsgBox "This method uses subclassing, so never press the Stop-button." + vbCrLf + "Unload this form properly by pressing the 'X'", vbInformation + vbOKOnly
'Initialize
Initialize Me.hwnd
'Open the file
OpenFile sWavFile
End Sub
Private Sub Form_Unload(Cancel As Integer)
'If we're playing then stop
If Playing() Then StopPlay
'Close the file
CloseFile
End Sub
Private Sub HScroll1_Change()
If fMovingSlider = True Then
fMovingSlider = False
'move to a new position
FileSeek (HScroll1.Value / 100) * Length
End If
End Sub
Private Sub HScroll1_Scroll()
fMovingSlider = True
End Sub
Private Sub Timer1_Timer()
On Error Resume Next
If (fMovingSlider) Then Exit Sub
If (Playing() = False) Then
Timer1.Enabled = False
End If
HScroll1.Value = (Position() / Length()) * 100
End Sub

'Add this code to a module:
Public Const CALLBACK_WINDOW = &H10000
Public Const MMIO_READ = &H0
Public Const MMIO_FINDCHUNK = &H10
Public Const MMIO_FINDRIFF = &H20
Public Const MM_WOM_DONE = &H3BD
Public Const MMSYSERR_NOERROR = 0
Public Const SEEK_CUR = 1
Public Const SEEK_END = 2
Public Const SEEK_SET = 0
Public Const TIME_BYTES = &H4
Public Const WHDR_DONE = &H1
Type mmioinfo
dwFlags As Long
fccIOProc As Long
pIOProc As Long
wErrorRet As Long
htask As Long
cchBuffer As Long
pchBuffer As String
pchNext As String
pchEndRead As String
pchEndWrite As String
lBufOffset As Long
lDiskOffset As Long
adwInfo(4) As Long
dwReserved1 As Long
dwReserved2 As Long
hmmio As Long
End Type
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
Type WAVEINCAPS
wMid As Integer
wPid As Integer
vDriverVersion As Long
szPname As String * 32
dwFormats As Long
wChannels As Integer
End Type
Type WAVEFORMAT
wFormatTag As Integer
nChannels As Integer
nSamplesPerSec As Long
nAvgBytesPerSec As Long
nBlockAlign As Integer
wBitsPerSample As Integer
cbSize As Integer
End Type
Type MMCKINFO
ckid As Long
ckSize As Long
fccType As Long
dwDataOffset As Long
dwFlags As Long
End Type
Type MMTIME
wType As Long
u As Long
x As Long
End Type
Declare Function waveOutGetPosition Lib "winmm.dll" (ByVal hWaveOut As Long, lpInfo As MMTIME, ByVal uSize As Long) As Long
Declare Function waveOutOpen Lib "winmm.dll" (hWaveOut As Long, ByVal uDeviceID As Long, ByVal format As String, ByVal dwCallback As Long, ByRef fPlaying As Boolean, ByVal dwFlags As Long) As Long
Declare Function waveOutPrepareHeader Lib "winmm.dll" (ByVal hWaveIn As Long, lpWaveInHdr As WAVEHDR, ByVal uSize As Long) As Long
Declare Function waveOutReset Lib "winmm.dll" (ByVal hWaveIn As Long) As Long
Declare Function waveOutUnprepareHeader Lib "winmm.dll" (ByVal hWaveIn As Long, lpWaveInHdr As WAVEHDR, ByVal uSize As Long) As Long
Declare Function waveOutClose Lib "winmm.dll" (ByVal hWaveIn As Long) As Long
Declare Function waveOutGetDevCaps Lib "winmm.dll" Alias "waveInGetDevCapsA" (ByVal uDeviceID As Long, lpCaps As WAVEINCAPS, ByVal uSize As Long) As Long
Declare Function waveOutGetNumDevs Lib "winmm.dll" () As Long
Declare Function waveOutGetErrorText Lib "winmm.dll" Alias "waveInGetErrorTextA" (ByVal err As Long, ByVal lpText As String, ByVal uSize As Long) As Long
Declare Function waveOutWrite Lib "winmm.dll" (ByVal hWaveOut As Long, lpWaveOutHdr As WAVEHDR, ByVal uSize As Long) As Long
Declare Function mmioClose Lib "winmm.dll" (ByVal hmmio As Long, ByVal uFlags As Long) As Long
Declare Function mmioDescend Lib "winmm.dll" (ByVal hmmio As Long, lpck As MMCKINFO, lpckParent As MMCKINFO, ByVal uFlags As Long) As Long
Declare Function mmioDescendParent Lib "winmm.dll" Alias "mmioDescend" (ByVal hmmio As Long, lpck As MMCKINFO, ByVal x As Long, ByVal uFlags As Long) As Long
Declare Function mmioOpen Lib "winmm.dll" Alias "mmioOpenA" (ByVal szFileName As String, lpmmioinfo As mmioinfo, ByVal dwOpenFlags As Long) As Long
Declare Function mmioRead Lib "winmm.dll" (ByVal hmmio As Long, ByVal pch As Long, ByVal cch As Long) As Long
Declare Function mmioReadString Lib "winmm.dll" Alias "mmioRead" (ByVal hmmio As Long, ByVal pch As String, ByVal cch As Long) As Long
Declare Function mmioSeek Lib "winmm.dll" (ByVal hmmio As Long, ByVal lOffset As Long, ByVal iOrigin As Long) As Long
Declare Function mmioStringToFOURCC Lib "winmm.dll" Alias "mmioStringToFOURCCA" (ByVal sz As String, ByVal uFlags As Long) As Long
Declare Function mmioAscend Lib "winmm.dll" (ByVal hmmio As Long, lpck As MMCKINFO, ByVal uFlags As Long) As Long
Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Declare Function GlobalLock Lib "kernel32" (ByVal hmem As Long) As Long
Declare Function GlobalFree Lib "kernel32" (ByVal hmem As Long) As Long
Declare Sub CopyStructFromPtr Lib "kernel32" Alias "RtlMoveMemory" (struct As Any, ByVal ptr As Long, ByVal cb As Long)
Declare Sub CopyPtrFromStruct Lib "kernel32" Alias "RtlMoveMemory" (ByVal ptr As Long, struct As Any, ByVal cb As Long)
Declare Sub CopyStructFromString Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, ByVal source As String, ByVal cb As Long)
Declare Function PostWavMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef hdr As WAVEHDR) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByRef lParam As WAVEHDR) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Const GWL_WNDPROC = -4
Dim lpPrevWndProc As Long
Const NUM_BUFFERS = 5
Const BUFFER_SECONDS = 0.1
Dim rc As Long ' Return code
Dim hmmioIn As Long ' file handle
Dim dataOffset As Long ' start of audio data in wave file
Dim audioLength As Long ' number of bytes in audio data
Dim pFormat As Long ' pointer to wave format
Dim formatBuffer As String * 50 ' buffer to hold the wave format
Dim startPos As Long ' sample where we started playback from
Dim format As WAVEFORMAT ' waveformat structure
Dim i As Long ' loop control variable
Dim j As Long ' loop control variable
Dim hmem(1 To NUM_BUFFERS) As Long ' memory handles
Dim pmem(1 To NUM_BUFFERS) As Long ' memory pointers
Dim hdr(1 To NUM_BUFFERS) As WAVEHDR ' wave headers
Dim bufferSize As Long ' size of output buffers
Dim fPlaying As Boolean ' is file currently playing
Dim fFileOpen As Boolean ' is file currently open
Dim hWaveOut As Long ' waveout handle
Dim msg As String * 250 ' message buffer
Dim hwnd As Long ' window handle
Public Sub Initialize(hwndIn As Long)
hwnd = hwndIn
lpPrevWndProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindowProc)
fPlaying = False
fFileOpen = False
startPos = 0
End Sub
Public Sub CloseFile()
mmioClose hmmioIn, 0
fFileOpen = False
End Sub
Public Sub OpenFile(soundfile As String)
Dim mmckinfoParentIn As MMCKINFO
Dim mmckinfoSubchunkIn As MMCKINFO
Dim mmioinf As mmioinfo

' close previously open file (if any)
CloseFile
If (soundfile = "") Then
Exit Sub
End If

' Open the input file
hmmioIn = mmioOpen(soundfile, mmioinf, MMIO_READ)
If (hmmioIn = 0) Then
MsgBox "Error opening input file, rc = " & mmioinf.wErrorRet
Exit Sub
End If

' Check if this is a wave file
mmckinfoParentIn.fccType = mmioStringToFOURCC("WAVE", 0)
rc = mmioDescendParent(hmmioIn, mmckinfoParentIn, 0, MMIO_FINDRIFF)
If (rc <> MMSYSERR_NOERROR) Then
CloseFile
MsgBox "Not a wave file"
Exit Sub
End If

' Get format info
mmckinfoSubchunkIn.ckid = mmioStringToFOURCC("fmt", 0)
rc = mmioDescend(hmmioIn, mmckinfoSubchunkIn, mmckinfoParentIn, MMIO_FINDCHUNK)
If (rc <> MMSYSERR_NOERROR) Then
CloseFile
MsgBox "Couldn't get format chunk"
Exit Sub
End If
rc = mmioReadString(hmmioIn, formatBuffer, mmckinfoSubchunkIn.ckSize)
If (rc = -1) Then
CloseFile
MsgBox "Error reading format"
Exit Sub
End If
rc = mmioAscend(hmmioIn, mmckinfoSubchunkIn, 0)
CopyStructFromString format, formatBuffer, Len(format)

' Find the data subchunk
mmckinfoSubchunkIn.ckid = mmioStringToFOURCC("data", 0)
rc = mmioDescend(hmmioIn, mmckinfoSubchunkIn, mmckinfoParentIn, MMIO_FINDCHUNK)
If (rc <> MMSYSERR_NOERROR) Then
CloseFile
MsgBox "Couldn't get data chunk"
Exit Sub
End If
dataOffset = mmioSeek(hmmioIn, 0, SEEK_CUR)

' Get the length of the audio
audioLength = mmckinfoSubchunkIn.ckSize

' Allocate audio buffers
bufferSize = format.nSamplesPerSec * format.nBlockAlign * format.nChannels * BUFFER_SECONDS
bufferSize = bufferSize - (bufferSize Mod format.nBlockAlign)
For i = 1 To (NUM_BUFFERS)
GlobalFree hmem(i)
hmem(i) = GlobalAlloc(0, bufferSize)
pmem(i) = GlobalLock(hmem(i))
Next

fFileOpen = True
End Sub
Public Function Play() As Boolean
If (fPlaying) Then
Play = True
Exit Function
End If

rc = waveOutOpen(hWaveOut, 0, formatBuffer, hwnd, True, CALLBACK_WINDOW)

If (rc <> MMSYSERR_NOERROR) Then
waveOutGetErrorText rc, msg, Len(msg)
MsgBox msg
Play = False
Exit Function
End If

For i = 1 To NUM_BUFFERS
hdr(i).lpData = pmem(i)
hdr(i).dwBufferLength = bufferSize
hdr(i).dwFlags = 0
hdr(i).dwLoops = 0
rc = waveOutPrepareHeader(hWaveOut, hdr(i), Len(hdr(i)))
If (rc <> MMSYSERR_NOERROR) Then
waveOutGetErrorText rc, msg, Len(msg)
MsgBox msg
End If
Next

fPlaying = True
Play = True

startPos = mmioSeek(hmmioIn, 0, SEEK_CUR) - dataOffset

For i = 1 To NUM_BUFFERS
PostWavMessage hwnd, MM_WOM_DONE, 0, hdr(i)
Next
End Function
Public Sub StopPlay()
fPlaying = False
FileSeek Position()
waveOutReset hWaveOut
End Sub
Public Function Length() As Long
Length = audioLength \ format.nBlockAlign
End Function
Public Function FileSeek(Position As Long) As Boolean
Dim bytepos As Long
FileSeek = False
bytepos = Position * format.nBlockAlign
If (fFileOpen = False) Or (bytepos < 0) Or (bytepos >= audioLength) Then
Exit Function
End If
rc = mmioSeek(hmmioIn, bytepos + dataOffset, SEEK_SET)
If (rc = MMSYSERR_NOERROR) Then
FileSeek = True
End If
startPos = rc
End Function
Public Function Position() As Long
Dim tm As MMTIME
tm.wType = TIME_BYTES
rc = waveOutGetPosition(hWaveOut, tm, Len(tm))
If (rc = MMSYSERR_NOERROR) Then
Position = (startPos + tm.u) \ format.nBlockAlign
Else
Position = (mmioSeek(hmmioIn, 0, SEEK_CUR) - dataOffset + bufferSize * NUM_BUFFERS) \ format.nBlockAlign
End If
End Function
Public Function Playing() As Boolean
Dim tm As MMTIME
tm.wType = TIME_BYTES
rc = waveOutGetPosition(hWaveOut, tm, Len(tm))
If (rc = MMSYSERR_NOERROR) Then
Playing = True
Else
Playing = False
End If
End Function
Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByRef wavhdr As WAVEHDR) As Long
Static dataRemaining As Long
If (uMsg = MM_WOM_DONE) Then
If (fPlaying = True) Then
dataRemaining = (dataOffset + audioLength - mmioSeek(hmmioIn, 0, SEEK_CUR))
If (bufferSize < dataRemaining) Then
rc = mmioRead(hmmioIn, wavhdr.lpData, bufferSize)
Else
rc = mmioRead(hmmioIn, wavhdr.lpData, dataRemaining)
fPlaying = False
End If
wavhdr.dwBufferLength = rc
rc = waveOutWrite(hWaveOut, wavhdr, Len(wavhdr))
Else
For i = 1 To NUM_BUFFERS
waveOutUnprepareHeader hWaveOut, hdr(i), Len(hdr(i))
Next
waveOutClose hWaveOut
End If
End If
WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, wavhdr)
End Function


我的msn: myfend@
2006-05-28 16:56
快速回复:[求助]该用哪些 API 函数来获取扬声器发出的声波呢?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.062126 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved