Jump to content
  • Advertisement
the incredible smoker

How do i access the sound buffer for win32 ?

This topic is 447 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


You don't have direct access to the kernel mixer from user-mode applications. Instead, use a low-level API such as XAudio. This allows you to submit your own buffers to be mixed to the audio output.

Share this post

Link to post
Share on other sites

XAudio is a system component that, I believe, is installed as part of Windows.

Any applications that play sound effectively need to go through the system-supplied interfaces, be it XAudio or something equivalent.

Applications generally don't call drivers directly; there is always at least one abstraction layer between them.

DirectSound, SDL, Core Audio and DirectShow are examples of alternatives for XAudio, but not one of them provides you direct access to the primary sound buffer. And for a good reason.


Share this post

Link to post
Share on other sites

you can do it the way he does, video below. He also has your same interest in mind of not having users need to install stuff, so this probably is what you need.


Edited by MarcusAseth

Share this post

Link to post
Share on other sites

XAudio is effectively the successor to DirectSound, and it is a separate library in the same sense as XAudio is. BOTH of them are pre-installed in modern Windows versions.

Moreover, XAudio is easier to use than DS, and has lower latency as well :D


Edited by Nik02

Share this post

Link to post
Share on other sites

This small XAudio2 example should work as is on Windows 10. For earlier versions, change the dll name as needed. This is not production-ready code because it does not perform any error checking whatsoever. In particular, one should check for success on every API call before proceeding, and in case of heap allocation, check that the memory was actually available. Also, typically audio APIs are used in applications with event loops (as in windows desktop apps), so that playing a single sound buffer is not the last thing the program does before exiting.

// XAudioDemo.cpp : Defines the entry point for the console application.
// NO WARRANTY! The code provided is just for example purposes.
// The code below skips almost all error checking so it should not be used in production applications as is.
// In real code we should check for failure and success for all API calls and memory allocations.

// -Niko

#include "stdafx.h"

In addition to the default console app headers, we include the following in stdafx.h:

#include <Windows.h>
#include "XAudio2.h"
#include <cmath>

typedef HRESULT WINAPI xaudio2_create(IXAudio2**, UINT32, XAUDIO2_PROCESSOR);

int main()
	CoInitializeEx(0, 0);
	HMODULE XAudioLib = LoadLibrary(L"xaudio2_9.dll"); // for windows 10, use XAudio 2.9
	xaudio2_create* xaudiocreate;
	xaudiocreate = (xaudio2_create*)GetProcAddress(XAudioLib, "XAudio2Create");

	IXAudio2 *pxaudio = NULL;
	xaudiocreate(&pxaudio, 0, XAUDIO2_DEFAULT_PROCESSOR);

	IXAudio2MasteringVoice *pMasteringVoice = NULL;
	pxaudio->CreateMasteringVoice(&pMasteringVoice, XAUDIO2_DEFAULT_CHANNELS, 22050);

	IXAudio2SourceVoice *pSourceVoice = NULL;
	WAVEFORMATEX sourceFormat;
	sourceFormat.cbSize = 0;
	sourceFormat.nAvgBytesPerSec = 22050 * 2;
	sourceFormat.nBlockAlign = 2; // 2 bytes per sample
	sourceFormat.nChannels = 1; // mono
	sourceFormat.nSamplesPerSec = 22050;
	sourceFormat.wBitsPerSample = 16;
	sourceFormat.wFormatTag = WAVE_FORMAT_PCM;

	pxaudio->CreateSourceVoice(&pSourceVoice, &sourceFormat);

	XAUDIO2_BUFFER buffer = { 0 };
	buffer.Flags = XAUDIO2_END_OF_STREAM;

	INT16* sampleData = new INT16[22050 * 2]; // 2 seconds of 22050Hz mono data

	// sound synthesis
	for (int i = 0; i < 22050 * 2; ++i)
		double sv = std::sin((double)i / (double)22050 * 5000.0); // sine wave
		sampleData[i] = sv * 32767.0; // amplify from -1...1 to sample range

	buffer.AudioBytes = 22050 * 2 * 2;
	buffer.pAudioData = (BYTE*)sampleData;

	pSourceVoice->Start(0, XAUDIO2_COMMIT_NOW);

	// Since we are in a console app, we need to sleep here so as to not exit immediately.
	// In desktop apps, we could handle the XAudio voice callbacks instead.

	delete[] sampleData;

    return 0;


Share this post

Link to post
Share on other sites

There is a redistributable available for XP. However, XP in general is an unsafe platform nowadays because MS does not support it with security updates anymore.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!