将 short[] 转换为可以作为音频播放的 Stream
- 作者: 犀牛鳄鱼V
- 来源: 51数据库
- 2022-12-15
问题描述
所以我有一个 short[] 数组,它代表 WAV 文件的原始数据.这意味着它不包含通常包含的任何页眉或页脚信息.为了播放这个音频,我需要将它转换为某种流,不幸的是,这个 short[] 数组中的数据是 Int16 并且许多值超过 255,因此无法转换为字节,也无法转换为溪流.有谁知道我将如何播放这些音频数据?
so what I have is a short[] array which represents the raw data of a WAV file. This means it doesn't include any header or footer information which is usually included. In order to play this audio, I need to convert it to a stream of some sort, unfortunately the data in this short[] array is Int16 and many of the values exceed 255, therefore cannot be converted to bytes and cannot be converted to a stream. Does anyone have any idea how I would be able to play this audio data?
推荐答案
您可以将短数组转换回字节数组:
You can convert the short array back to a byte array:
short[] sampleData = ... byte[] byteArray = new byte[sampleData.Length*2]; Buffer.BlockCopy(sampleData , 0, byteArray, 0, byteArray.Length);
然后您可以使用下面的 WaveMemoryStream 类创建波形流 - 为此您需要知道示例数据的波形格式.然后可以将该流保存为 WAV 文件或通过 SoundPlayer<播放/code>.
Then you can create a wave stream using the WaveMemoryStream class below - for that you will need to know the wave format of your sample data. This stream can then i.e be saved as a WAV file or played back by SoundPlayer.
public class WaveMemoryStream : Stream
{
public override bool CanSeek { get { return false; } }
public override bool CanWrite { get { return false; } }
public override bool CanRead { get { return true; } }
public override long Length { get { return _waveStream.Length; } }
public override long Position { get { return _waveStream.Position; } set { _waveStream.Position = value; } }
private MemoryStream _waveStream;
public WaveMemoryStream(byte[] sampleData, int audioSampleRate, ushort audioBitsPerSample, ushort audioChannels)
{
_waveStream = new MemoryStream();
WriteHeader(_waveStream, sampleData.Length, audioSampleRate, audioBitsPerSample, audioChannels);
WriteSamples(_waveStream, sampleData);
_waveStream.Position = 0;
}
public void WriteHeader(Stream stream, int length, int audioSampleRate, ushort audioBitsPerSample, ushort audioChannels)
{
BinaryWriter bw = new BinaryWriter(stream);
bw.Write(new char[4] { 'R', 'I', 'F', 'F' });
int fileSize = 36 + length;
bw.Write(fileSize);
bw.Write(new char[8] { 'W', 'A', 'V', 'E', 'f', 'm', 't', ' ' });
bw.Write((int)16);
bw.Write((short)1);
bw.Write((short)audioChannels);
bw.Write(audioSampleRate);
bw.Write((int)(audioSampleRate * ((audioBitsPerSample * audioChannels) / 8)));
bw.Write((short)((audioBitsPerSample * audioChannels) / 8));
bw.Write((short)audioBitsPerSample);
bw.Write(new char[4] { 'd', 'a', 't', 'a' });
bw.Write(length);
}
public void WriteSamples(Stream stream, byte[] sampleData)
{
BinaryWriter bw = new BinaryWriter(stream);
bw.Write(sampleData, 0, sampleData.Length);
}
public override int Read(byte[] buffer, int offset, int count)
{
return _waveStream.Read(buffer, offset, count);
}
public virtual void WriteTo(Stream stream)
{
int bytesRead = 0;
byte[] buffer = new byte[8192];
do
{
bytesRead = Read(buffer, 0, buffer.Length);
stream.Write(buffer, 0, bytesRead);
} while (bytesRead > 0);
stream.Flush();
}
public override void Flush()
{
_waveStream.Flush();
}
public override long Seek(long offset, SeekOrigin origin)
{
return _waveStream.Seek(offset, origin);
}
public override void SetLength(long value)
{
throw new NotImplementedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
}
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
