• Advertisement

How do i access the sound buffer for win32 ?

Recommended Posts

Advertisement

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->SubmitSourceBuffer(&buffer);
	pxaudio->StartEngine();

	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.

	Sleep(2000); 
	pxaudio->StopEngine();
	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

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


  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By Nimmagadda Subba Rao
      Hi,
         I am a CAM developer working with C++ and C# for the past 5 years. I started working on DirectX from past 6 months. I developed a touch screen control viewer using Direct2D. I am working on 3D viewer currently. I am very slow with working on Direct3D. I want to be a gaming developer. As i am new to this i want to know what are the possibilities to explore in this area. How to start developing gaming engines? Is it through tutorials? I heard suggestions from my friends that going for an MS helps. I am not sure on which path to choose. Is it better to go for higher studies and start exploring? I am currently working in India. I want to go to Canada and settle there. Are there any good universities there to learn about graphics programming? Sorry if I am asking too many questions but i want to know the options to choose to get ahead. 
    • By Bokchee 88
      I am animator by hand, and i am doing game animation for at least 8 years so far. During the last 2 years, i came with a idea for game and maybe some day, i want to start indie game company. As i am thinking to start game company, i am also thinking what kind of value i can give to the company. For example, am experience in animation,sales(I was selling web development services, before i jumped to gaming), bit of rigging- just not for production, i am learning on the side as well. The rest of the gaming production, like modeling, concept art, texturing, i am total noob or to say better, i am no near interest to do modeling for example, don't have such a patience to do it. But before characters and things are made for animating, what the hell i am would do?
      Also, what is the ideal size of the founding team of a game company? Positions to be filled mostly are, Concept artist, Modeler/Texture artist, programmer, animator-rigger. And later would need more people to join, like more animators, programmers, sound, fx,etc.
       
      And lastly, do i need to have something,like a prototype, to show people and get them interest, or should i ask someone i know, for skill that i lack, for example, Modeling would be great, texturing and rigging, and to start all together from scratch?  
    • By Terry Jin
      Hi everyone! 

      I am from an indie studio that has received funding for our concept and is ready to create the next generation 2D Pokemon-inspired MMORPG called Phantasy World. This ad is for a volunteer position but hopefully will transition into something more. Our vision is to create a game that draws inspiration from the series but is dramatically different in both aesthetics and gameplay as the work would be our own.
       
      We are hoping that you can help us make this a reality and are looking for game developers familiar with the unreal engine and would be happy to work on a 2D top down game. Sprite artists are also welcome as we are in desperate need of talented artists! Join our discord and let's have a chat! https://discord.gg/hfDxwDX

      Here's some of our in game sprites for playable characters while moving around the game world! Hope to see you soon!
       


    • By Karol Plewa
      Hi, 
       
      I am working on a project where I'm trying to use Forward Plus Rendering on point lights. I have a simple reflective scene with many point lights moving around it. I am using effects file (.fx) to keep my shaders in one place. I am having a problem with Compute Shader code. I cannot get it to work properly and calculate the tiles and lighting properly. 
       
      Is there anyone that is wishing to help me set up my compute shader?
      Thank you in advance for any replies and interest!
    • By Katie Byrne
      F.OB: Forward Operating Base is a military themed base building and resource management game.

      I am currently about 6 weeks into development. still implementing core game mechanics. The game is playable in a basic sense right now but there are no objectives or missions or anything motivating really lol. 

      I have been inspired by games such as prison architect and rimworld. both in visual style and general gameplay ideas. Here is a quick 2 minute video i put together that hopefully communicates the general feel of the game and what gameplay is like. Any feedback or criticism is welcome
       
       
      I have been keeping a development diary on my youtube channel and i hope to have more developments on this project very soon. 

      UPDATE FEBRUARY 2018 - DOWNLOAD LINK FOR BUILD 0.38 AVAILABLE
      So i decided to upload the current build. hopefully get some feedback.

      Its still incredibly incomplete and poorly documented so please feel free to ask me any questions about how to do tihngs. I am working on better documentation and more intuitive interactions.

      Any feedback would be a huge help. positive or negative.

      https://drive.google.com/open?id=1WpgO5h1nHyTh5AGYX7kxeC9qnDZ_ByAX


      It's just a zipped Unity build. simply unzip and run the .exe no need to install anything

      Oh yes, there will be bugs  but please let me know if you find any and i'll do my best to fix them 
  • Advertisement