将 short[] 转换为可以作为音频播放的 Stream

Convert short[] to Stream which can be played as Audio(将 short[] 转换为可以作为音频播放的 Stream)
本文介绍了将 short[] 转换为可以作为音频播放的 Stream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

所以我有一个 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();
    }
}

这篇关于将 short[] 转换为可以作为音频播放的 Stream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to know if a field is numeric in Linq To SQL(如何在 Linq To SQL 中知道字段是否为数字)
Extract sql query from LINQ expressions(从 LINQ 表达式中提取 sql 查询)
LINQ Where in collection clause(LINQ Where in collection 子句)
Orderby() not ordering numbers correctly c#(Orderby() 没有正确排序数字 c#)
Why do I get quot;error: ... must be a reference typequot; in my C# generic method?(为什么我会收到“错误:...必须是引用类型?在我的 C# 泛型方法中?)
Strange LINQ Exception (Index out of bounds)(奇怪的 LINQ 异常(索引越界))