Jump to content
  • Advertisement
the incredible smoker

How do i access the sound buffer for win32 ?

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
  • Advertisement
  • Popular Tags

  • Similar Content

    • By Hellados
      Hello guys, my name is Giorgi and i'm newbie game developer i'm learning Pixel art and after pixel art  i want learn C# and don't know how and where start i'm bad with programming language and know only HTML/CSS
    • By N Drew
      I am working on a 2D SideScroller game in my own made game engine using SFML and C++.I am searching for 2D artists,especially pixel artist for making and animating characters,backgrounds and other props that can be made in any Drawing Program.The artist will become part of the team of Hammer Studios and he got a part of the Revenue Sharing.If you are interested send me a mail at:ghiurcutaandrei@gmail.com .If you are not an artist but you want to be a part of our Team,Soon we will be recruiting an C++ AI programmers that worked in SFML/OpenGL.
      We work together using Discord.

    • By Ike aka Dk
      Hello everyone 
      I am a programmer from Baku.
      I need a 3D Modeller for my shooter project in unity.I have 2 years Unity exp.
      Project will paid when we finish the work 
      If you interested write me on email:
    • By HawkDeath
      I'm trying mix two textures using own shader system, but I have a problem (I think) with uniforms.
      Code: https://github.com/HawkDeath/shader/tree/test
      To debug I use RenderDocs, but I did not receive good results. In the first attachment is my result, in the second attachment is what should be.
      PS. I base on this tutorial https://learnopengl.com/Getting-started/Textures.

    • By Cello Gaming
      Hello I am looking for a team who are willing to work on a Realistic, Large, Story-based, Open-World, Survival, Horror game. I'm looking for a team that is willing to stay with me through the whole project. 
      For this project I will be needing:
      3D Modeler: Somebody who has a good understanding on how to create 3D models through Blender, Maya, 3DS Max, or any other Modeling programs of their choosing Animator: Someone able to rig and animate humans, non-humans, and machines. Artist: Someone able to make art for loading screens, GUIs, and Cover art. Environment Artist (2-3): Someone able to create a large open world for the players to explore Writer: Someone who has experience writing story-lines and can write dialogue for this project Sound Engineer: Somebody who can create sounds for character movements, environment, and animals     Ideas for The Project: - Shipwrecked on an island after getting caught in a storm out on the ocean - Start on the boat with all your friends and a bunch of NPCs where the ship doesn't crash until you talk to the captain - Able to search wreckage to find food, water, and a basic tool. - Locked areas in the wreckage that you need special tools or codes to open. - Secret chests you can find on the island through maps that you can find in abandoned houses, shacks, and wreckage of pirate ships. - Fishing rods to go fishing for different kinds of fish. - Cooking system to cook food on a grill or campfire which requires propane tanks or wood to continue burning - Underwater and land caves with plenty of resources - Mutant bosses that you can fight above or under water - Underwater life (Sharks, Fish, Large underwater Squid boss)   These are just ideas they may or may not be implemented in the final product. Any feedback on ideas for the game would be appreciated.   We will be setting up a kickstarter for people to support this, if this project gets up going, so watch for that.  
    • By Luca Falco
      https://www.youtube.com/watch?v=UMoc52DEoC8     ABOUT THE GAME: Room54 is a first-person Horror/adventure  Videogame for pc,mac and linux users. the game is currently under development by a very small team, we put so much effort in this project and we are keep doing our best for that we are going to launch a kickstarter campaing in order to reach our ideal budget to complete the project, we hope the GameDev community can help us a lot                      STORY:   Daniel is a family father like many other, one day he decide with your wife and his daughter  to spend her winter holidays at their mountain house that they have recently buy in  mountain ,a  wonderful place surrounded by the nature of the woods,  Completely far away from the caotic city life. During their holidays Daniel and his family will understand that they are not welcome there and they will discover an  hided and disturbing part of the valley that they have never seen before                       GAMEPLAY:   You will play as Daniel, a father that will try to save his family, your gameplay will be focused on discovering secrets places around valley,investigating and trying to survive, you will find object that will help you solve enigma and to stay alive. The immersive audio and  environment will make you feel constantly follow by an high anxiety dose  during the game.                         SOCIAL PAGES:     Follow us to get the latest development news and insights     IndieDB:     http://www.indiedb.com/games/room54   Twitter:       https://twitter.com/Room54Thegame   facebook:   https://www.facebook.com/Room54/
    • By this. games
      Looking For: 2d Artist and Animator.
      Requirements: Experience in 2d art and ability to commit to 1-2 years of work.
      Payment: Rev-Share (Think Free as the product may not end up being sold)
      Description: An 3/4 view 2d game with a Sci-fi style. The game is based on a man who was put into stasis for punishment for his crimes. He has woken up 100 years later and must now regain his position at the top of the criminal underworld by killing his opponents. There will be a "Hub World" city and then PCG gang hideouts where you fight enemies. The assets required will be tile sets for the different hideouts, assets for the city ie buildings streets etc. and characters both the player and a number of enemies.
      Send resume and portfolio to thisdotgames@gmail.com if you are interested.
    • By Hive Entertainment
      Hey everyone we are looking for a 2d artist for health bars, inventory, items, weapons ect. Unfortunately I can not pay at this point in time however if you are interested please let me know and I am sure we can work something out. I am also looking for landscape modelers in 3d message me for more details or email me at hiveentertainmenttech@gmail.com

    • By Gold_fox
      While I’m VERY aware how illegal is is to steal game programs, there are so many cool ones so I have two questions, can all these cool mods for other games be stitched together, and is it legal to do so so long as it’s just for me and I NEVER share it.
    • By Hawkblood
      Just as the title implies, when I make a small change such as "x=4;" now to read "x=5;" the compiler doesn't build that part of the code and leaves me with the old value. It correctly compiles when I select "rebuild solution"....
      How do I fix this?
  • Advertisement
  • Popular Now

  • Forum Statistics

    • Total Topics
    • Total Posts

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!