Can we directly set the bitrate in a WAV file?

siva s 0 Reputation points
2025-08-08T06:38:36.89+00:00

I’m working with WAV audio files and would like to know if it's possible to directly set a specific bitrate (e.g., 128 kbps or 256 kbps) for a WAV file.

From what I understand, WAV files are typically uncompressed (PCM), and their bitrate is derived from parameters like sample rate, bit depth, and channel count.

  1. Can the bitrate be explicitly set in a WAV file header?
  2. If not, how is the bitrate calculated?
Developer technologies | C++
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 90,876 Reputation points
    2025-08-08T09:34:11.9933333+00:00

    You can use Microsoft Media Foundation and MF_MT_AUDIO_AVG_BYTES_PER_SECOND

    or with WinRT, interfaces like AudioEncodingProperties

    I did this test with MMF and help from ChatGPT to convert a .wav to 256 kbps :

    int TranscodeWav(const wchar_t* pwsInputPath, const wchar_t* pwsOutputPath)
    {
    	MFStartup(MF_VERSION);
    
    	IMFSourceReader* pReader = nullptr;
    	MFCreateSourceReaderFromURL(pwsInputPath, NULL, &pReader);
    
    	IMFMediaType* pMediaTypeOut = nullptr;
    	MFCreateMediaType(&pMediaTypeOut);
    	pMediaTypeOut->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio);
    	pMediaTypeOut->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_PCM);
    	
    	// From ChatGPT
    	pMediaTypeOut->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, 16000); // 16 kHz
    	pMediaTypeOut->SetUINT32(MF_MT_AUDIO_BITS_PER_SAMPLE, 16);      // 16‑bit depth
    	pMediaTypeOut->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, 1);          // Mono
    	// Average bytes per second = samplerate × channels × bits/sample ÷ 8
    	// Byte rate = 16000 * 1 * 16 / 8 = 32,000 bytes/s
    	// 256 kbps = 256000 bps (divide by 8 for bytes/sec)
    	pMediaTypeOut->SetUINT32(MF_MT_AUDIO_AVG_BYTES_PER_SECOND, 16000 * 1 * 16 / 8);
    
    	// Set the media type on the source reader's output
    	pReader->SetCurrentMediaType(MF_SOURCE_READER_FIRST_AUDIO_STREAM, NULL, pMediaTypeOut);
    
    	IMFSinkWriter* pWriter = nullptr;
    	DWORD nStreamIndex = 0;
    	MFCreateSinkWriterFromURL(pwsOutputPath, NULL, NULL, &pWriter);
    	pWriter->AddStream(pMediaTypeOut, &nStreamIndex);
    	pWriter->BeginWriting();
    
    	// Transcode loop: read samples, optionally convert timestamps, write to output
    	DWORD nFlags = 0;
    	LONGLONG timestamp = 0;
    	while (true)
    	{
    		IMFSample* pSample = nullptr;
    		pReader->ReadSample(MF_SOURCE_READER_FIRST_AUDIO_STREAM, 0, nullptr, &nFlags, &timestamp, &pSample);
    		if (nFlags & MF_SOURCE_READERF_ENDOFSTREAM) break;
    		if (pSample)
    		{
    			pWriter->WriteSample(nStreamIndex, pSample);
    			pSample->Release();
    		}
    	}
    
    	pWriter->Finalize();
    
    	pWriter->Release();
    	pMediaTypeOut->Release();
    	pReader->Release();
    	MFShutdown();
    	return 0;
    }
    

    with :

    #include <mfapi.h>
    #include <mfidl.h>
    #include <mferror.h>
    #include <mfreadwrite.h>
    
    #pragma comment(lib, "Mfplat")
    #pragma comment(lib, "mfreadwrite")
    #pragma comment(lib, "mfuuid")
    
    
    0 comments No comments

  2. Gade Harika (INFOSYS LIMITED) 330 Reputation points Microsoft External Staff
    2025-08-11T05:11:21.1866667+00:00

    Thank you for sharing the details.

    WAV files (especially PCM format) do not have an explicit "bitrate" field in their header. Instead, the bitrate is implicitly calculated based on:

    Sample rate (e.g., 44100 Hz)

    Bit depth (e.g., 16-bit)

    Number of channels (e.g., 2 for stereo)

    So, you cannot directly set a bitrate like 128 kbps or 256 kbps in the header. Instead, you control the bitrate by adjusting those three parameters.

    Bitrate = Sample Rate × Bit Depth × Channels

    For example:

    44100 Hz × 16 bits × 2 channels = 1,411,200 bits/sec1411 kbps

    This is much higher than compressed formats like MP3, which is why WAV files are larger.

    Let me know if the issue persists after following these steps. I’ll be happy to assist further if needed.

    If the issue has been resolved, kindly mark the response as answered.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.