Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>void SetShortToBuffer(short val,byte[] outArray,int Offset) { outArray[Offset] = (byte)(val &amp; 0x00FF); Offset++; outArray[Offset] = (byte)((val &gt;&gt; 8) &amp; 0x00FF); } byte[] ConvertShortArray(short[] Data,int Offset,int Count) { byte[] helper = new byte[Count * sizeof(short)]; int end = Offset+Count; int io=0; for (int i = Offset; i &lt; end; i++) { SetShortToBuffer(Data[i], helper, io); io+=sizeof(short); } return helper; } </code></pre> <p>In <code>C</code> this would not be an issue, you could simply tell the compiler that your previously declared short array should now be treated as a byte array (simple cast) but after failing to do so in <code>C#</code> outside of <code>unsafe</code> context I came up with this code :)</p> <p>You can use <code>ConvertShortArray</code> function to get chunks of data in case your wave is large</p> <p>EDIT:</p> <p>Quick and dirty wave header creator, not tested</p> <pre><code>byte[] CreateWaveFileHeader(int SizeOfData, short ChannelCount, uint SamplesPerSecond, short BitsPerSample) { short BlockAlign = (short)(ChannelCount * (BitsPerSample / 8)); uint AverageBytesPerSecond = SamplesPerSecond * BlockAlign; List&lt;byte&gt; pom = new List&lt;byte&gt;(); pom.AddRange(ASCIIEncoding.ASCII.GetBytes("RIFF")); pom.AddRange(BitConverter.GetBytes(SizeOfData + 36)); //Size + up to data pom.AddRange(ASCIIEncoding.ASCII.GetBytes("WAVEfmt ")); pom.AddRange(BitConverter.GetBytes(((uint)16))); //16 For PCM pom.AddRange(BitConverter.GetBytes(((short)1))); //PCM FMT pom.AddRange(BitConverter.GetBytes(((short)ChannelCount))); pom.AddRange(BitConverter.GetBytes((uint)SamplesPerSecond)); pom.AddRange(BitConverter.GetBytes((uint)AverageBytesPerSecond)); pom.AddRange(BitConverter.GetBytes((short)BlockAlign)); pom.AddRange(BitConverter.GetBytes((short)BitsPerSample)); pom.AddRange(ASCIIEncoding.ASCII.GetBytes("data")); pom.AddRange(BitConverter.GetBytes(SizeOfData)); return pom.ToArray(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload