- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
此代码可通过平台调用服务使用 winmm.dll 输出 2 秒的钢琴音,在 XP 上似乎可以正常工作,但在 windows 7 rc 中 waveoutopen 失败
根据 John Knoeller 的反馈更新
导入 System.Runtime.InteropServices
公共(public)类音频流
<StructLayout(LayoutKind.Sequential)> _
Private Structure WAVEHDR
Public lpData As Integer
Public dwBufferLength As Integer
Public dwBytesRecorded As Integer
Public dwUser As Integer
Public dwFlags As Integer
Public dwLoops As Integer
Public lpNext As Integer
Public Reserved As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> _
Private Structure WAVEFORMATEX
Public wFormatTag As Int16
Public nChannels As Int16
Public nSamplesPerSec As Int32
Public nAvgBytesPerSec As Int32
Public nBlockAlign As Int16
Public wBitsPerSample As Int16
Public cbSize As Int16
End Structure
Private Declare Function waveOutOpen Lib "winmm.dll" (ByRef lphWaveOut As Int32, ByVal uDeviceID As Int32, ByRef lpFormat As WAVEFORMATEX, ByVal dwCallback As WaveDelegate, ByVal dwInstance As Int32, ByVal dwFlags As Int32) As Int32
Private Declare Function waveOutClose Lib "winmm.dll" (ByVal hWaveOut As Int32) As Int32
Private Declare Function waveOutPrepareHeader Lib "winmm.dll" (ByVal hWaveOut As Int32, ByRef lpWaveOutHdr As WAVEHDR, ByVal uSize As Int32) As Int32
Private Declare Function waveOutUnprepareHeader Lib "winmm.dll" (ByVal hWaveOut As Int32, ByRef lpWaveOutHdr As WAVEHDR, ByVal uSize As Int32) As Int32
Private Declare Function waveOutWrite Lib "winmm.dll" (ByVal hWaveOut As Int32, ByRef lpWaveOutHdr As WAVEHDR, ByVal uSize As Int32) As Int32
Private Delegate Sub WaveDelegate(ByVal hwo As IntPtr, ByVal uMsg As Integer, ByVal dwInstance As Integer, ByRef wavhdr As WAVEHDR, ByVal dwParam2 As Integer)
Private Const WAVE_MAPPER = -1&
Private Const WAVE_FORMAT_PCM = 1
Private Const CALLBACK_FUNCTION = &H30000 ' to set up a callback to a function
Private Const WHDR_DONE = &H1 ' done bit
Private Const WHDR_PREPARED = &H2 ' set if this header has been prepared
Private Const WHDR_BEGINLOOP = &H4 ' loop start block
Private Const WHDR_ENDLOOP = &H8 ' loop end block
Private Const WHDR_INQUEUE = &H10 ' reserved for driver
Private Const MM_WOM_OPEN = &H3BB ' waveform output
Private Const MM_WOM_CLOSE = &H3BC
Private Const MM_WOM_DONE = &H3BD
Private Const WOM_OPEN = MM_WOM_OPEN
Private Const WOM_CLOSE = MM_WOM_CLOSE
Private Const WOM_DONE = MM_WOM_DONE
Private Const MMSYSERR_BASE = 0
Private Const MMSYSERR_NOERROR = 0 ' no error
Private Const MMSYSERR_ERROR = (MMSYSERR_BASE + 1) ' unspecified error
Private Const MMSYSERR_BADDEVICEID = (MMSYSERR_BASE + 2) ' device ID out of range
Private Const MMSYSERR_NOTENABLED = (MMSYSERR_BASE + 3) ' driver failed enable
Private Const MMSYSERR_ALLOCATED = (MMSYSERR_BASE + 4) ' device already allocated
Private Const MMSYSERR_INVALHANDLE = (MMSYSERR_BASE + 5) ' device handle is invalid
Private Const MMSYSERR_NODRIVER = (MMSYSERR_BASE + 6) ' no device driver present
Private Const MMSYSERR_NOMEM = (MMSYSERR_BASE + 7) ' memory allocation error
Private Const MMSYSERR_NOTSUPPORTED = (MMSYSERR_BASE + 8) ' function isn't supported
Private Const MMSYSERR_BADERRNUM = (MMSYSERR_BASE + 9) ' error value out of range
Private Const MMSYSERR_INVALFLAG = (MMSYSERR_BASE + 10) ' invalid flag passed
Private Const MMSYSERR_INVALPARAM = (MMSYSERR_BASE + 11) ' invalid parameter passed
Private Const MMSYSERR_HANDLEBUSY = (MMSYSERR_BASE + 12) ' handle being used simultaneously on another thread (eg callback) */
Private Const MMSYSERR_INVALIDALIAS = (MMSYSERR_BASE + 13) ' specified alias not found
Private Const MMSYSERR_BADDB = (MMSYSERR_BASE + 14) ' bad registry database
Private Const MMSYSERR_KEYNOTFOUND = (MMSYSERR_BASE + 15) ' registry key not found
Private Const MMSYSERR_READERROR = (MMSYSERR_BASE + 16) ' registry read error
Private Const MMSYSERR_WRITEERROR = (MMSYSERR_BASE + 17) ' registry write error
Private Const MMSYSERR_DELETEERROR = (MMSYSERR_BASE + 18) ' registry delete error
Private Const MMSYSERR_VALNOTFOUND = (MMSYSERR_BASE + 19) ' registry value not found
Private Const MMSYSERR_NODRIVERCB = (MMSYSERR_BASE + 20) ' driver does not call DriverCallback
Private Const MMSYSERR_MOREDATA = (MMSYSERR_BASE + 21) ' more data to be returned
Private Const MMSYSERR_LASTERROR = (MMSYSERR_BASE + 21) ' last error in range
Private Const WAVERR_BASE = 32
Private Const WAVERR_BADFORMAT = (WAVERR_BASE + 0) ' unsupported wave format
Private Const WAVERR_STILLPLAYING = (WAVERR_BASE + 1) ' still something playing
Private Const WAVERR_UNPREPARED = (WAVERR_BASE + 2) ' header not prepared
Private Const WAVERR_SYNC = (WAVERR_BASE + 3) ' device is synchronous
Private Const WAVERR_LASTERROR = (WAVERR_BASE + 3) ' last error in range
Private FinishedPlaying As Boolean ' local flag to track playback status
Private mCallBack As WaveDelegate = AddressOf WaveCallBack ' function pointer to our callback function
Private pmem As IntPtr ' heap memory pointer
''' <summary>
''' Play a tone of a specified hz frequency for a specified duration in seconds
''' </summary>
Public Sub Play(ByVal Frequency As Single, ByVal Seconds As Single)
Dim wavFormat As WAVEFORMATEX
Dim wavHead As WAVEHDR
Dim hWaveOut As Int32
With wavFormat
.wFormatTag = WAVE_FORMAT_PCM
.nChannels = 2
.wBitsPerSample = 16
.nSamplesPerSec = 44100
.nBlockAlign = .nChannels * .wBitsPerSample / 8
.nAvgBytesPerSec = .nBlockAlign * .nSamplesPerSec
End With
Dim BufferSamples As Integer = wavFormat.nSamplesPerSec * Seconds
Dim BufferBytes As Integer = BufferSamples * wavFormat.nBlockAlign
'allocate memory on the heap
pmem = Marshal.AllocHGlobal(BufferBytes)
With wavHead
.lpData = pmem.ToInt32
.dwBufferLength = BufferBytes
End With
waveOutOpen(hWaveOut, WAVE_MAPPER, wavFormat, mCallBack, 0, CALLBACK_FUNCTION)
waveOutPrepareHeader(hWaveOut, wavHead, Len(wavHead))
FinishedPlaying = False
' fill buffer with specific frequency
Dim SamplesPerCycle As Double = wavFormat.nSamplesPerSec / Frequency
For i As Integer = 0 To BufferSamples - 1
' 16-bit samples are stored as 2's-complement signed integers, ranging from -32768 to 32767
Dim RotationPercent As Double = (i Mod SamplesPerCycle) / SamplesPerCycle
Dim RotationRadians As Double = RotationPercent * Math.PI * 2
Dim SampleValue As Int16 = Math.Sin(RotationRadians) * Int16.MaxValue
' blocks are 4 bytes - 2 bytes for left channel then 2 bytes for right channel
' left channel
Marshal.WriteInt16(pmem, i * wavFormat.nBlockAlign, SampleValue)
' right channel
Marshal.WriteInt16(pmem, (i * wavFormat.nBlockAlign) + 2, SampleValue)
Next
' play buffer
waveOutWrite(hWaveOut, wavHead, Len(wavHead))
Do While (Not FinishedPlaying)
Application.DoEvents()
Loop
waveOutUnprepareHeader(hWaveOut, wavHead, Len(wavHead))
waveOutClose(hWaveOut)
'free memory we allocated on the heap
Marshal.FreeHGlobal(pmem)
End Sub
''' <summary>
''' This is our handler for the waveout API callback
''' </summary>
Private Sub WaveCallBack(ByVal hwo As IntPtr, ByVal uMsg As Integer, ByVal dwInstance As Integer, ByRef wavhdr As WAVEHDR, ByVal dwParam2 As Integer)
Select Case uMsg
Case MM_WOM_OPEN
Debug.WriteLine("Open")
Case WOM_DONE
FinishedPlaying = True
Case Else
Debug.WriteLine(uMsg)
End Select
End Sub
''' <summary>
''' This is a convienient entry point to allow the class to be executed standalone (by configuring project properties)
''' </summary>
Public Shared Sub Main()
Dim BeatsPerMinute As Double = 120
Dim BeatsPerSecond As Double = BeatsPerMinute / 60
Dim ScaleSteps() As Integer = {0, 2, 2, 1, 2, 2, 2, 1} ' tone steps for major scale
Dim MyAudioStream As New AudioStream
Dim ToneFrequency As Double = 261.626 ' 261.626hz middle c piano tone
For t As Integer = 0 To ScaleSteps.Length - 1
For s As Integer = 1 To ScaleSteps(t)
ToneFrequency *= 1.05946309435929 ' Twelfth root of two for next tone
Next
MyAudioStream.Play(ToneFrequency, 1 / BeatsPerSecond) ' play tone for one second
Next
End Sub
最佳答案
你的缓冲区太小,改变这个
Dim BufferSize As Integer = 44100 * Length - 1
Dim BufferSize As Integer = 44100 * Length * 2 * 2
...
For i As Integer = 0 To (BufferSize/(2*2)) - 1
.nBlockAlign = 2*2
With wavFormat
.wFormatTag = WAVE_FORMAT_PCM
.nChannels = 2
.wBitsPerSample = 16
.nSamplesPerSec = 44100
.nBlockAlign = .nChannels * .wBitsPerSample/8
.nAvgBytesPerSec = .nBlockAlign * .nSamplesPerSec
End With
Dim BufferSamples As Integer = 44100 * Length
Dim BufferBytes As Integer = BufferSamples * wavFormat.nBlockAlign
pmem = Marshal.AllocHGlobal(BufferBytes)
With wavHead
.lpData = pmem.ToInt32
.dwBufferLength = BufferBytes
End With
waveOutOpen(hWaveOut, WAVE_MAPPER, wavFormat, mCallBack, 0, CALLBACK_FUNCTION)
waveOutPrepareHeader(hWaveOut, wavHead, Len(wavHead))
FinishedPlaying = False
' fill buffer
' Specific frequency:
FreqConst = 44100 / (Math.PI * 2) / Freq
For i As Integer = 0 To BufferSamples - 1
Dim IntValue As Int16 = Math.Sin((i Mod 44100) / FreqConst)
Marshal.WriteInt16(pmem, i*4, IntValue)
Marshal.WriteInt16(pmem, i*4+2, IntValue)
Next
关于vb.net - waveout 有时会出现内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2320860/
我有几个问题。我是 Visual Basic 这个领域的新手,所以不要取笑我。 1.) VB.NET之间有什么区别和 VB ? 2.) 我需要为 Windows 开发基本的应用程序。(如记事本)我应该
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我是框架 3.5 的新手。我注意到,在创建 Web 内容表单时,除了 aspx.vb 页面之外,它还会创建一个 aspx.designer.vb 页面。谁能向我解释一下它们之间的区别以及它们的用途吗?
我只是想知道 VB.NET 和 VB 2010 是否相同。 我只是想知道。 最佳答案 VB 2010 是 VB.Net 的最新版本。 Microsoft 在 VB 2005 版本中删除了 VB 的“.
我是框架 3.5 的新手。我注意到,在创建 Web 内容表单时,除了 aspx.vb 页面之外,它还会创建一个 aspx.designer.vb 页面。谁能向我解释一下它们之间的区别以及它们的用途吗?
我正在尝试将 VB 函数移植到 VB.NET,但我无法使该函数正常工作并正确更新。 rFormat = Format(Format(Value, fmt), String$(Len(fmt), "@"
如何在VB中注释多行代码/代码块? 最佳答案 VB 在语言级别上没有这样的构造。它有使用撇号字符的单行注释: ' hello world ' this is a comment Rem this is
我正在使用我在 VB2005 中创建的表单在按下按钮时打开程序,然后在文本字段中显示进程 ID(再次按下按钮时)。当我运行它时,表单将打开程序 (Notepad.exe) 但当我单击按钮查看进程 ID
我正在尝试添加一个从 vb.net 创建的 dll,并且想将其导入到现有的 vb 6 项目中,但它给了我错误“无法添加对指定文件的引用”。 。有人知道如何解决这个问题吗? 最佳答案 需要遵循以下步骤:
我有一个数据 GridView 。右键单击它会显示一个上下文菜单,但它始终位于右上角。我想要它,以便菜单出现在用户右键单击的单元格上。它可能是单元格 1 或单元格 2 或其他。 谢谢福尔坎 最佳答案
我只是在 Visual Studio 2010 中使用 Visual Basic。有人知道我将如何制作“浏览文件夹(或文件)”按钮吗?我对 VB 真的很陌生,我只是在寻找一些简单的帮助:) 最佳答案
这次感到困惑... 最简单的代码行有时可能起作用,有时却没有。首先,我认为问题在于我试图读取DWORD的值,但是由于我可以从某些键读取DWORD值,所以这一定不是问题。现在的问题似乎是,如果 key
我的代码中有此方法: Private Sub Display() Received.AppendText(" - " & RXArray) End Sub 这两个调用之间有什么区别:
我正在创建一个宏程序来记录和回放鼠标和键盘输入。录制效果很好,鼠标播放也一样,但是我在播放键盘输入时遇到了麻烦——特别是在释放之前按住一个键几秒钟。这不等同于重复按键。这是我尝试过的: 技巧 1:Me
我最近刚刚了解了 VB.NET 中静态局部变量的使用,并想知道它在延迟加载属性中的潜在用途。 考虑以下示例代码。 Public Class Foo Implements IFoo End Clas
VB 有一个 C# 没有的特性,在项目级别导入命名空间(我的项目>引用>导入命名空间)。当新人在源代码控制之外检查项目时,我们的自定义导入不包括在内。这个 VB 特定的导入命名空间存储在哪里? 最佳答
我已将我的问题缩小到这个简单的案例,但似乎无法找到发生了什么: 我有两个表单,一个只有一个按钮,另一个是空的。 单击按钮时,form1 隐藏和显示 form2 出现时,form2隐藏,form1再次显
为什么下面的简单代码会失败?无论我使用 LinearGradientMode 的哪个值,这段代码总是用从左到右的渐变填充路径。 graphPath 是在别处创建的 GraphicPath 对象(基本上
我可以多快替换字符串中的字符? 所以这个问题的背景是这样的:我们有几个应用程序通过套接字相互通信并与客户端的应用程序通信。这些套接字消息包含不可打印的字符(例如 chr(0)),需要用预定的字符串(例
如何从任何文件中读取原始字节数组... Dim bytes() as Byte ..然后将该字节数组写回新文件? 我需要它作为一个字节数组来做一些处理。 我目前正在使用: 阅读 Dim fInfo
我是一名优秀的程序员,十分优秀!