gpt4 book ai didi

vb.net - 如何从 .wav 文件中读取字节 vb.net

转载 作者:行者123 更新时间:2023-12-02 15:40:28 25 4
gpt4 key购买 nike

我正在尝试使用 .wav 文件进行一些工作,并且我已经能够通过创建字节数组来随机播放文件和播放声音(请参阅下面的代码)我想知道是否有我可以用来从 .wav 文件获取字节的方法。我认为如果我可以从 .wav 文件中获取字节,我应该能够像处理随机噪声一样播放声音。这应该能让我弄清楚如何修改声音。

播放 .wav 文件:

    Dim SoundDevice = New Microsoft.DirectX.DirectSound.Device
Dim SbufferOriginal = New Microsoft.DirectX.DirectSound.SecondaryBuffer(SoundFilePath, SoundDevice)
Private Sub PlaySound()
Try
SbufferOriginal = New Microsoft.DirectX.DirectSound.SecondaryBuffer(SoundFilePath, SoundDevice)
SoundDevice.SetCooperativeLevel(Me.Handle, Microsoft.DirectX.DirectSound.CooperativeLevel.Normal)
SbufferOriginal.Play(0, Microsoft.DirectX.DirectSound.BufferPlayFlags.Looping)
Catch ex As Exception
'do something for exception
End Try
End Sub

使用直接声音播放随机噪音:

DSdev.SetCooperativeLevel(Me.Handle, CooperativeLevel.Normal)

DSformat = New WaveFormat()
DSformat.BitsPerSample = 8
DSformat.Channels = 1
DSformat.BlockAlign = 1
DSformat.FormatTag = WaveFormatTag.Pcm
DSformat.SamplesPerSecond = 8000
DSformat.AverageBytesPerSecond = DSformat.SamplesPerSecond *
DSformat.BlockAlign

'buffer description
DSdes = New BufferDescription(DSformat)
DSdes.BufferBytes = 3 * DSformat.AverageBytesPerSecond

'create the buffer
DSbuffer = New Microsoft.DirectX.DirectSound.SecondaryBuffer(DSdes, DSdev)

'generate ramdom data (white noise)
Dim rawsamples(22050) As Byte
Dim rnd1 = New System.Random()
Dim tmepno As Integer = 150





For j = 0 To 5
DSbuffer.Stop()
Dim i As Integer
For i = 0 To 22050
rawsamples(i) = 250
tmepno += 1
If tmepno = 255 Then
tmepno = 150
End If
'rnd1.Next(255)
Next i

' load audio samples to secondary buffer
DSbuffer.Write(0, rawsamples, LockFlag.EntireBuffer)

'play audio buffer
DSbuffer.Play(0, BufferPlayFlags.Default)
Threading.Thread.Sleep(250)
Next

我想做的是从 .wav 文件中获取字节数组,这样我就可以像播放随机噪音一样播放它。

提前致谢!

更新:

我编写了以下代码来使用从 .wav 文件读取的字节:

    Dim justsounddata(bytearray.GetLength(0) - 44 - 1) As Byte
Dim bitstring As String
For i = 0 To justsounddata.GetLength(0) - 1
'RichTextBox1.AppendText(bytearray(i))
justsounddata(justsounddata.GetLength(0) - 1 - i) = bytearray(i + 44)
bitstring &= bytearray(i)
Next

RichTextBox1.Text = bitstring

Dim workingvalue As String



DSdev.SetCooperativeLevel(Me.Handle, CooperativeLevel.Normal)

DSformat = New WaveFormat()

workingvalue = Mid(bitstring, 35, 2)
workingvalue = StrReverse(workingvalue)
DSformat.BitsPerSample = workingvalue
'CInt(bitspersample)

workingvalue = Mid(bitstring, 23, 2)
workingvalue = StrReverse(workingvalue)
DSformat.Channels = workingvalue

workingvalue = Mid(bitstring, 33, 2)
workingvalue = StrReverse(workingvalue)
DSformat.BlockAlign = workingvalue

workingvalue = Mid(bitstring, 9, 4)
'workingvalue = StrReverse(workingvalue)
DSformat.FormatTag = workingvalue

workingvalue = Mid(bitstring, 25, 4)
workingvalue = StrReverse(workingvalue)
DSformat.SamplesPerSecond = workingvalue

'CInt(samplesspersecond)
DSformat.AverageBytesPerSecond = DSformat.SamplesPerSecond * DSformat.BlockAlign
'CInt(bitrate)

'buffer description
DSdes = New BufferDescription(DSformat)
DSdes.BufferBytes = 3 * DSformat.AverageBytesPerSecond

'create the buffer
DSbuffer = New Microsoft.DirectX.DirectSound.SecondaryBuffer(DSdes, DSdev)

'generate ramdom data (white noise)
Dim rawsamples(22050) As Byte
Dim rnd1 = New System.Random()
Dim tmepno As Integer = 150


' load audio samples to secondary buffer
'DSbuffer.Write(0, rawsamples, LockFlag.EntireBuffer)

DSbuffer.Write(0, justsounddata, LockFlag.EntireBuffer)

'play audio buffer
'
DSbuffer.Play(0, BufferPlayFlags.Default)

错误出现在这一行:

DSbuffer = New Microsoft.DirectX.DirectSound.SecondaryBuffer(DSdes, DSdev)

错误是:“值不在预期范围内。”

我相信我已经从数组中为每个变量读取了正确的位。我还注意到了字节序。再次提前致谢:)

最佳答案

您可以使用File.ReadAllBytes读取文件的所有数据。或者使用FileStream读取文件。然后您可以使用 Serializer.Serialize 将数据放入类中。

关于vb.net - 如何从 .wav 文件中读取字节 vb.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16721461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com