How to check if IDirectSoundBuffer is really playing? (buffer underrun detection) - winapi

I want to detect buffer underrun situation in direct sound environment.
I use two sound buffers (primary and secondary). Sometimes (when server gives data) I call lock method of IDirectSoundBuffer interface for posting data to secondary sound buffer. If data from server do not have time to come, the sound buffer starts play again from start of the buffer (and repeat it until I send new data to buffer). Maybe it's flag DSBPLAY_LOOPING, but as I read (and test it), primary buffer cannot be created without this flag (there was error at Play method).
I try to get status of playing, but GetStatus method always returns the same status, even there is no new data and player repeat old data.
So, how can I detect buffer underrun situation (when there is no new data for playing and all old data is played)?
Thanks in advance.

IDirectSoundBuffer8::GetCurrentPosition is really the only way you can determine where it is playing from, but it's also only reliable on Windows Vista or later systems that report DSBCAPS_TRUEPLAYPOSITION.
A few things to note:
DirectSound is 'legacy', meaning it hasn't been actively worked on, promoted, or tested directly in ages. The last samples were shipped in the also now end-of-life DirectX SDK in November 2007. Versions of DirectSound prior to 8 are not supported for x64 native applications, and the DirectSound 8 headers/libs are in the Windows SDK as of the Windows SDK 7. It's not supported for Windows on ARM, or Windows Store apps, or Universal Windows apps. The documentation for DirectSound can only be found offline in the legacy DirectX SDK and is not on Microsoft Docs--the only DirectSound content still online is for driver writers.
DirectSound is also 'emulated' on modern versions of Windows so there's nothing actually 'direct' about it. The primary buffer is not actually connected directly to the audio hardware or used for mixing at all, so it's just another software buffer like a secondary buffer. It does emulate the legacy restrictions of primary buffers that applied back in Windows 9x/ME, but it doesn't' do much at all otherwise.
Starting with Windows Vista, LOC_HARDWARE buffers are no longer supported at all. Windows Vista did add support for multi-channel LOC_SOFTWARE buffers, which on Windows XP were only available as LOC_HARDWARE buffers.
Starting with Windows Vista, Effects (ID3L, EAX, etc.) are not supported through DirectSound.
TL;DR: Don't use DirectSound in new applications. It is only still supported at all for old software & games.
So, what is a developer supposed to use if not DirectSound?
(1) Windows Core Audio (WASAPI) is a good option if you can provide the sound data at a known data rate and format. If you need any real-time mixing or source-rate conversion, you have to do it yourself -or- you can use one of the many existing 3rd party audio libraries to do it that also send the final result to WASAPI. See Microsoft docs.
(2) XAudio version 2 is a good choice if you want to do real-time mixing, source-rate conversion, and software-based DSP effects. It is included in the operating system as of Windows 8, but to support Windows 7 you have to use some legacy distribution and SDKs. See Microoft docs and this blog.
Both WASPI and XAudio use a 'audio packet' model instead of a looping buffer for data submission. As long as a packet is pending processing, you won't have under-run.

Related

Windows driver APIs not written by Microsoft

I've been on a little adventure learning about how Windows platforms deal with sounds. Now I know a couple of things from taking an Operating System class and reading this:
IO devices such as a sound card communicate with the OS through a device driver. Microsoft supplies a standard so that in kernel mode the OS can make certain system calls (in this case Win32 api calls) to make an I/O device do something. Bytes or characters are streamed to over buses to whatever port the I/O device resides in. These are interpreted by the device controller as instructions for the physical hardware.
There are OS calls to prepare audio devices and stream data to them. Actually for some reason their are 2 ways to do this: MMSystem and DirectSound. I think the reason there are 2 has something to do with gaming? Like DIrectSound was more geared toward that while MMsystem was more standard apps that need audio?
Beyond that their are also other audio APIs NOT written by microsoft for the Win32 platform. ASIO and GSIF.
How can these non Microsoft standards exist on the Windows platform? How can they possibly communicate with hardware or the OS? With something like openGL(something else I toy with), a version of Windows supports a version openGL standard and so does you GPU. BAM, easy-peasy no problems, everything checks out.
How I see it can be is:
MMsystem and/or DirectSound are not part of Win32, but there is some even lower level windows kernel driver everyone is keeping a secret because it's really ugly or
These other driver API's are built on top of the Microsoft supplied ones
Can someone please clarify? Where am I mistaken, I am missing something. I own my old OS textbook still(Modern Operating Systems 3rd Ed. by Tanenbaum) so if you want to point at something in there, go for it.

It is possible to programmatically send information on the R/G/B data pins to a VGA monitor during the Vertical Blanking Interval (VBI)?

I'm trying to send information like close captioning to a special VGA monitor that can read the RGB data lines during the BVI. The data is used for synchronization with other devices.
I cannot find information regarding any Windows API to do so. Is there any OS that has these services? Is there any support in the standard video boards to do this kind of tweaking?
The closes I get is some DirectX API functions that can parse "Line 21" data from DVD streams but nothing to encode "Line 21" data to be send out of the VGA connector.
You'll need more information about exactly how the monitor is doing this. It may be using some sort of extension to DDC/CI. If so, you may be able to access it reasonably easily (starting with Vista, Windows has some functions for working with DDC, though mostly with the basic, widespread capabilities like adjusting brightness and contrast).
If you need to work with older versions of Windows than Vista, some third party software supports that.
It could also be doing something else entirely -- in which case, it's open to question not only whether Windows will support it, but whether you may need a special video card to use it at all. Windows can only use what the video card's driver (and ultimately, the hardware) support, so unless it is something well known like DDC, chances are pretty good that a generic video card won't support it.

Can a Windows audio driver sit atop the default windows driver and post process its output?

Is it possible to write a driver to sit on top of another driver, take the lower driver's output and post process it.
I wanted to write a driver to make sure the volume level was always constant.
In my head, this driver would site on top of the audio card driver and post process the output before handing back to the OS to send to the speakers.
I read about MS miniport and this seems to provide some basic guaranteed exposures.
The constant volume level implementation is not what I'm really concerned about in this question. I'm wondering if it is even possible to write a 'filter' driver for lack of a better word so I could do some sound post-processing.
Edit: OK, I'm guilty of not googling this first, turns out it's actually called a filter driver which makes me look like a moron. I would be a total noob at this though, so any good direction, or text would be appreciated.
For Windows Vista and beyond, it is quite challenging to write such an audio driver. That's because Windows Vista audio devices primarily use an I/O mechanism where the audio driver exposes the DMA buffers directly to the audio subsystem or application. So the audio engine writes directly into the DMA buffers of the audio adapter and there is no opportunity for a filter driver to inspect the samples being sent to the audio solution.
For Vista and beyond, you can achieve the same result with an sAPO which is a processing element that accompanies a driver package that allows the sAPO to inspect and modify audio samples.

I need to consume an ocx for voice recording and playblack

The current ocx controls I'm using for voice recording and playback are not compatible with Windows 7. I'm already feeling the pressure to produce a Windows 7 compatible version of my software. The author has already stated that he is not planning to write a Windows 7 compatible ocx.
I work from xharbour so I need to consume an OCX or write the whole thing (which I'd like to avoid and don't even know where to start). My basic needs are (1) to record dictation from the microphone with methods to pause and vox preferably, (2) save to file, (3) and later playback with methods to ff and rew.
Thank you,
Reinaldo.
I found this activeX that seems to works: http://www.download3k.com/Software-Development/Active-X/Download-Active-Audio-Record-Component.html
Reinaldo.

Sound processing: Should I use DirectSound or directly Win32 APIs?

I'm making an application where I will:
Record from the microphone and do some realtime processing on the input
Play an MP3 file (a regular song), but manipulating the output in realtime
Every now and then I'll need to play additional sounds over this song too, but I guess I can do that by simply adding the buffers.
In short, I need to have circular buffers for both recording and playing, and I need to be "feeding" the output buffer every 20 ms or so with the new data that is just about to be played.
I've been looking at DirectSound, and it doesn't seem to help a lot. The reading and writing to the output buffers seem very similar to Win32, the only place where it seems it'd help is in playing the "additional sounds" over the main song.
Should I use DirectSound, or should I go straight to raw Windows APIs?
Is DirectSound going to do anything for me?
Thanks in Advance!
The Directsound API's give you better realtime control. They are also the supported way to use sound in Windows. I was under the impression that the win32 api's were depracated, but I could be wrong on this.
This question is close to yours
https://stackoverflow.com/questions/314522/what-is-the-best-c-sound-api-for-windows
also
Is DirectSound the best audio abstraction layer for Windows?
last but not least, this is what microsoft has to say http://msdn.microsoft.com/en-us/library/dd370784(VS.85).aspx
Neither? :)
The story is that DirectSound is the replacement for waveOut, but DirectSound joined DirectInput as deprecated APIs in Vista and is replaced with WASAPI. DirectSound and waveOut are implemented on top of the User-Space WASAPI in Vista. On XP, waveOut and DirectSound feed to the same kernel level Mixer API.
To consolidate all of these interfaces take a look at something like OpenAL, it's a well supported audio standard along the same lines as OpenGL.
It sounds like you're going to be quite sensitive to latency. It might pay to look at ASIO
I found Harmony Central - Audio Programming. Also read w:DirectSound.
Windows Vista features a completely
re-written audio stack based on the
Universal Audio Architecture. Because
of the architectural changes in the
redesigned audio stack, a direct path
from DirectSound to the audio drivers
does not exist.
Because of Xbox 360 and Microsoft
Windows integration, Microsoft is
actively pushing developers to migrate
new applications to equivalent Xbox
audio APIs such as XAudio and XACT.
OpenAL looks promising.

Resources