Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create valid wav file header for streams in memory

Tags:

c#

audio

wav

pcm

I have raw-headerless wav audio data as MemoryStreams.

Stream rawAudioStream = Producer.GetRawAudioFileStream(...);

I know those streams data format:

// WaveFormat(int rate, int bits, int channels);
WaveFormat waveformat = new WaveFormat(8000, 16, 1);

What I want is to add programmatically right header info for those memory streams without writing them to a physical file.

How can I do that?

PS: I checked the NAudio Library but only found a way to create a header by writing streams to really-physical files which is not suitable for my situation.

var waveformat = new WaveFormat(8000,16,1);

var reader = new RawSourceWaveStream(rawAudioMemStream, waveformat);

using (var convertedStream = WaveFormatConversionStream.CreatePcmStream(reader))    
{
    WaveFileWriter.CreateWaveFile(fileName, convertedStream);     
}

rawAudioMemStream.Close();
like image 667
Hippias Minor Avatar asked Mar 27 '14 18:03

Hippias Minor


People also ask

Do WAV files have a header?

WAV File Format The WAVE file format, being a subset of Microsoft's RIFF specification, starts with a file header followed by a sequence of data chunks. A WAVE file has a single “WAVE” chunk which consists of two sub-chunks: a “fmt” chunk - specifies the data format. a “data” chunk - contains the actual sample data.

Can you stream a WAV file?

WAV files are published in the standard format used for CD audio, so the format is widely supported by various media players. Stream WAV files in either Windows Media Player if you're using a Windows computer, or QuickTime if you're using Mac OS X.

What is the header of an audio file?

In computers and digital audio a header is a unit of information that precedes a data object. In file management, for example, a header is a region at the beginning of each file where bookkeeping information is kept.

How is data stored in a WAV file?

The data is stored "in the raw" with no preprocessing. The WAV format is a Microsoft proprietary format, and is a special case of the RIFF (Resource Interchange File Format) format. A WAV file contains three chunks of information: the RIFF chunk (12 bytes) identifies the file as a WAV file.


1 Answers

The below code will write a Wav header to the beginning of a MemoryStream. Which means you'll need to write the header to your stream first, and then you can write your samples. Otherwise the samples at the start of your stream will get overwritten with meta data,

// totalSampleCount needs to be the combined count of samples of all channels. So if the left and right channels contain 1000 samples each, then totalSampleCount should be 2000.
// isFloatingPoint should only be true if the audio data is in 32-bit floating-point format.

private void WriteWavHeader(MemoryStream stream, bool isFloatingPoint, ushort channelCount, ushort bitDepth, int sampleRate, int totalSampleCount)
{
    stream.Position = 0;

    // RIFF header.
    // Chunk ID.
    stream.Write(Encoding.ASCII.GetBytes("RIFF"), 0, 4);

    // Chunk size.
    stream.Write(BitConverter.GetBytes(((bitDepth / 8) * totalSampleCount) + 36), 0, 4);

    // Format.
    stream.Write(Encoding.ASCII.GetBytes("WAVE"), 0, 4);



    // Sub-chunk 1.
    // Sub-chunk 1 ID.
    stream.Write(Encoding.ASCII.GetBytes("fmt "), 0, 4);

    // Sub-chunk 1 size.
    stream.Write(BitConverter.GetBytes(16), 0, 4);

    // Audio format (floating point (3) or PCM (1)). Any other format indicates compression.
    stream.Write(BitConverter.GetBytes((ushort)(isFloatingPoint ? 3 : 1)), 0, 2);

    // Channels.
    stream.Write(BitConverter.GetBytes(channelCount), 0, 2);

    // Sample rate.
    stream.Write(BitConverter.GetBytes(sampleRate), 0, 4);

    // Bytes rate.
    stream.Write(BitConverter.GetBytes(sampleRate * channelCount * (bitDepth / 8)), 0, 4);

    // Block align.
    stream.Write(BitConverter.GetBytes((ushort)channelCount * (bitDepth / 8)), 0, 2);

    // Bits per sample.
    stream.Write(BitConverter.GetBytes(bitDepth), 0, 2);



    // Sub-chunk 2.
    // Sub-chunk 2 ID.
    stream.Write(Encoding.ASCII.GetBytes("data"), 0, 4);

    // Sub-chunk 2 size.
    stream.Write(BitConverter.GetBytes((bitDepth / 8) * totalSampleCount), 0, 4);
}
like image 139
Sam Avatar answered Sep 22 '22 23:09

Sam