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

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

XAudio is pretty easy to set up and use compared to the old DirectSound.  However, if you want something easy and portable then there are things like OpenAL or the still maintained OpenAL Soft.  Anything more powerful usually costs money.

Edited by Madcap

Share this post

Link to post
Share on other sites
On 25.9.2017 at 4:33 PM, the incredible smoker said:

How about windows XP ?

To go low-level on WinXP a try is to use Waveform WINAPI because it is built in while XAudio2 needs an extra install. I did use Waveform API in a toy project to record and playback Mic sounds and it worked

Edited by Shaarigan

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Announcements

  • Forum Statistics

    • Total Topics
    • Total Posts
  • Similar Content

    • By DreamPowered

      Hello everyone,

      I am happy to introduce you the FPS project I am working on with 2 of my friends: Warcry: Extermination

      We have been working on it for a few years now, and decided to move to Unreal Engine about a year ago, to finalize our prototype.

      The game is an old school FPS, somewhere between quake and Serious Sam, unforgiving like Dark Souls and in Dungeon like environments, that will maybe remind you Skyrim a bit.

      As for any good FPS out there, the main focus is on combat, however, we haven’t neglected exploration, platforms, traps and puzzles neither! The levels can be explored freely, with many secret locations to discover.

      In Warcry: Extermination, you will also be able to upgrade your weapons and acquire new powers, such as Dash, Hologram or even Slow motion. I will tell you more about it later !

      And here is how it looks in Unreal Engine 4 :

      We should be able to show you more next week!
    • By Geri
      Anime Maker is an ultimately simple and but powerfull crossplatform software to create animated cartoons and anime.

      Download your favorite anime fanworks (characters, backgrounds) from internet, then open them with Anime Maker to create an Anime from them!

      Anime Maker offers shot based timeline management with 27 image layers, simple BONE ANIMATION for moving your heroes, dozens of effect including fire, water, snowfall, fountain, and refraction.

      Make your own anime for amatheur or professional movie competitions! Create your own ultimate fanfictions from your favorit anime, impress your friends, create your own anime seasons for televisions, or video sharing sites!
      Your career as an anime artist starts today!
      Anime Maker offers all the feature that required to create an anime, including the folowing: 

      - Time-line and shot based video edition with copy/paste/delete abilities
      - Support for bmp, jpg, png formats with alpha map, and automatic alpha-map generation
      - 27 independent graphics layer to create your movie
      - 3 sound channel on the timeline
      - Wav and ogg files for audio
      - Mouse and touchscreen for input
      - AVI video export with antialiasing
      - x86 and ARM based computers and mobile phones
      - Windows, Linux and Android compatibility
      - 8 invididual bones on all layers, with 4 joints each
      - Real time motion movement recording for realible animation
      - Dozens of fire and particle effects with your own textures
      - Layer refraction effects (water waves, etc)
      - Anime Maker supports multiple CPU cores for great performance
      - Dedicated mouth speak layer management
      - More stable than video software usually
      - Very small memory footprint, and powersaving
      - Ability to morph, move, resize, color, blend your layers in real-time
      - WYSIWYG editor: what you see is what you will get on export
      - Supports cutting exported video into multiple files
      - Tooltips as easy usage guide
      - Instant start and quit
      - Multimedia cacheing to avoid memory waste
      - 16:9 ratio with custom shot sizes optimized for Anime
      - Colored and shadowed subtitles
    • By TheSomeone77177
      I need some help determining which methods of path finding i should use. The scenario is that i want 100-1000 agents (with steering behaviors) moving to a single location (the player) via a flow field. I am currently following this PDF for guidance (Supreme Commander 2 Pathfinding) but a lot of the technologies they use are meant for an RTS. Different groups of agents often have different goals to move toward to. Since i have a single, consistent goal, would i still implement things like an A* search through sectors based on an agent's location in the map (there's a lot of agents so..), or would i simply generate the flow fields in the sectors that are occupied?
      Basically, since i have a single target at all times, which technologies should i use and which should i forget about
  • Popular Now