Yet Another (WA)SAPI Output Plugin for Winamp (YASAPI)

Copyright © 2015-2016 by Peter Belkner (http://home.snafu.de/pbelkner/)

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

The "Yet Another (WA)SAPI Output Plugin for Winamp" (YASAPI) utilizes the "Windows Audio Session API" (WASAPI). WASAPI's exclusive mode for rendering audio is a native way on Windows to render audio undisturbed, similar to Steinberg's "Audio Stream Input/Output" (ASIO). The YASAPI output plugin may serve as a replacement for any other Winamp output plugin.

Home:   http://out-yasapi.sourceforge.net/
Project:   http://sourceforge.net/projects/out-yasapi/
Download:   http://sourceforge.net/projects/out-yasapi/files/out-yasapi/
Winamp Forum:   http://forums.winamp.com/showthread.php?t=380396
 
Input Plugin:   http://in-ffsox.sourceforge.net/

Content

  1. History
  2. Introduction
  3. Implementation
  4. Configuration
  5. Debug

1. History

2016-06-20 1.7.0 (Limited) support for un-plugging the default device (as configured via System control) when playing.
2016-06-19 1.6.14
  • Disconnect on pause and undeflow when option Disconnect is choosen.
  • Removed timer for disconnecting gapless sessions because it is not needed any longer.
  • Fixed crashing of the configuration dialog in case the device selected in the previous session is invalid now.
2016-06-14 1.6.11 Fixed combining pause with flush (skip).
2016-06-13 1.6.9
  • For gapless mode it is configurable after which time interval the connection should be released when in idle state.
  • Reworked the tracing facilities of the debug version.
2016-06-12 1.6.8 For the debug version it may be configured that the trace is written to a file in the AppData/Winamp/Plugin directory rather then dispalying it in a console.
2016-06-12 1.6.7 In gapless mode, when a playlist ends the device is not released. It is waiting for the next track to start. This situation is un-distinguishable from an ordinary underflow situation and hence playback ends in an undeflow state. This may cause trouble when in such a situation the user decides to change the output plugin (via configuration). This new version applys to exact this situation: If in case the user changes the output plugin and this plugin is found in an underflow state this plugin is closed (and hence the device released).
2016-06-12 1.6.6 Fixes regrding gapless playback.
2016-06-12 1.6.5
  • Stopping player when Winamp closes and not already stopped (needed for gapless mode).
  • Guarding against locking zero sized device buffers.
  • Reworked About dialog.
2016-05-29 1.6.4 Added a common option in order to define whether 4, 6, or 8 channels should be interpreted as qadrophnic (3.1), 5.1, or 7.1 (disabled) or sorround (3.1 sorround), 5.1 sorround, or 7.1 sorround (enabled), respectively.
2016-05-26 1.6.3
  • Fixed AUDCLNT_E_BUFFER_SIZE_ERROR appearing in eclusive/pull mode.
  • Fixed a potential forever-loop which may have appeared at the end of a track.
  • Fixed crashing as client of the WinampMatrixMixer output plugin.
2016-05-22 1.6.0
  • Gapless playback.
  • A common option for switching on/off gapless playback (disabled by default).
  • A device option for choosing whether the time offset for gapless playback is maintained as a 64-bit integer value (representing the playback position) or as a 64-bit floating point value (representing the quotient taken from the playback position and a frequency). For a deeper understanding regarding position and frequency values you may refer to the documentation of the IAudioClock::GetPosition() and IAudioClock::GetFrequency() methods. We feel that as long as a constant frequency value can be assumed choosing the time offset as beeing maintained as a 64-bit integer value is preferable.
  • A debug version for SSE2.
2016-05-17 1.5.4 Solved the dead-lock caused by saving the configuration.
2016-05-17 1.5.1-
1.5.3
Improved dealing with draining the ring buffer and end of track (eot).
2016-05-16 1.5.0
  • The Default Device is listed in the respective configuration dialog's drop down box as an option to choose.
  • In case the Default Device is choosen from the drop down box (and saved),
    • the configuration is saved for the physical device which was configured at the time (configuration time) as Default Device from Window's System Control when the configuration was loaded to the dialog,
    • each time a new track starts, the physical device configured at this time (play time) as Default Device from Window's System Control is used to play the track (please note that this device may differ from the one configured at configuration time).
Please note that this is a complete re-write. Everything was thrown into peaces, something into larger building blocks and something into dust. The larger building blocks, which greatly remained intakt, could be re-used. The dust had to be thrown away and substituted by a new development. Please be careful in using this new version, thoroughly test it and re-configure it, if needed.
2016-04-09 1.0.7 In order to have YASAPI in a well defined state from the very first beginning, reset the IAudioClient interface at start-up.
2016-01-13 1.0.6
  • Detect end of track by respective "isplaying" request.
  • Fixed bug regarding synchronization of Share Mode and Strategy options.
2016-01-09 1.0.0
2016-01-03 0.16.0
  • Guarded against writing when paused.
  • Stopping audio client when underflow.
  • From the configuration dialog, dropped the device list page in favour of a combobox on top of the dialog.
  • Slightly adapted default values.
2015-12-21 0.15.0 Brought back, as an option, a call to IAudioClient::IsFormatSupported which is disabled by default.
2015-12-10 0.14.1 Increased default ring buffer size to 2.5.
2015-12-08 0.14.0
  • Introduced an (by default enabled) option "Write Block" that in case the plugin's "write" method delivers more data than actually could be written to the ring buffer it should block and wait until enough data are consumed from it rather then returning immediately.
  • For automatic mode, removed restrictions for switching from exclusive to shared mode.
2015-12-06 0.12.0 Fixed interplay with the "NotSo Fatso" input plugin.
2015-12-04 0.11.0
  • Reorganized the configuration dialog:
    • Focused again on the most important two (per device) parameters "Mode" (share/exclusive) and "Strategy" (push/pull).
    • Duplicated those two parameters from the "Device Options" page to the dialog's top-region.
    • Moved the former top-region parameters to a new "General" tab-control page.
    • Added a "Store" button with similar functionality as the "OK" button except that it doesn't close the dialog.
  • Fixed several bugs.
2015-11-29 0.10.0
  • Reorganized configuration of device period.
  • Added progess bars to the configuration dialog in order to visualize the load of the ring and the shared buffers.
  • Added an option for dis-/enabling the visualization.
  • Added an (experimental) function for balancing the shared buffer.
  • Added some more tracing to the debug version.
  • Split the verbose debugging into verbose level 1 ond and level 2 (even more verbose).
2015-11-23 0.9.1
  • Added an option for configuring whether calculation of the buffer sizes should be based on the default or on the minimum device period as proposed by WASAPI. Made default device period the default. So long the calculation was silently based on the minimum device period.
  • Added tooltips to the configuration dialog's controls.
2015-11-22 0.9.0
  • Complete re-write based on a new architecture. The basic idea is to serialize all requests and dispatch them into just one worker thread. In particular the serialization takes into account asynchronous requests resulting from the WASAPI device.
  • Round up ("ceil") the size of the ring buffer to the next multiple of Winamp's packet size (576 samples).
  • Special treatment of double buffering, i.e. pull in exclusive mode.
  • Additional options to configure the debug version.
Note:  You should re-configure at least the buffer sizes. As a rule of thumb all buffer sizes should be 1.0 except the ring buffer's size which should be just a small amount greater then 1.0.
2015-11-02 0.8.3
  • Improved thread synchronization during start-up.
  • Fixed a bug regarding recovery from error AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED of the IAudioClient::Initialize() method.
2015-10-31 0.8.2
2015-10-30 0.8.1 New share mode "automatic" which is similar to exclusive except that it falls back to shared in case exclusive fails.
2015-10-29 0.8.0
2015-10-26 0.7.1
  • Automatic re-sampling in shared mode seems to work now.
  • Added an "Exit Winamp" option to the error message dialog box.
2015-10-26 0.7.0
  • Support for multiple devices.
  • Configuration options per device.
  • Various fixes.
Note: Prior configuration for the default device may be lost. Please re-configure.
2015-10-03 0.6.1 Corrected a silly bug in testing for share mode.
2015-10-03 0.6.0 Add AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM flag when in shared mode (doesn't have any effect on Vista).
2015-02-14 0.5.0
  • Utilization of the IAudioClock interface instead of the GetTickCount() or the timeGetTime() functions for reporting the elapsed time to Winamp.
  • Option for controlling whether "written time" should be calculated.
2015-01-30 0.4.0
  • Option for treating mono as stero (default switched on).
  • New default options for buffer configuration.
  • Some fixes.
2015-01-25 0.3.0
  • Support for pull strategy.
  • Support for volume control.
2015-01-24 0.2.1
  • Component Object Model (COM) error messages are displayed with the file and the line number where they occur.
  • Compiled with shared "MSVCR110.DLL" (instead of static as before).
2015-01-24 0.2.0
  • Improved configuration for buffer sizes.
  • Provided a SSE2 version.
  • Compiled with the C compiler from "Visual Studio 10 Express" (instead of "Visual Studio 9 Express" as before).
2015-01-23 0.1.0 Initial release.

2. Introduction

According to Microsoft:

The Core Audio APIs were introduced in Windows Vista. This is a new set of user-mode audio components provides client applications with improved audio capabilities. These capabilities include the following:

https://msdn.microsoft.com/en-us/library/windows/desktop/dd370784%28v=vs.85%29.aspx

Of special interest is that there is an exclusive mode for rendering audio, i.e. there is a mode where an application has exclusive access to the audio device without being disturbed by third parties.

The aim of the YASAPI plugin is to make the WASAPI exclusive mode for audio rendering available to the users of Winamp.

The name of the plugin was choosen because there are already at least two WASAPI output plugins for Winamp:

3. Implementation

There are two sides the YASAPI plugin has to take into account:

The implementation on the WASAPI side follows along the lines of an example provided by Microsoft. The startegy shown in this example is not only applicable for shared mode streams but also for exclusive mode ones, i.e. not the share mode should be emphasized but what is known as the push model. There is also an example demonstrating the pull model in conjunction with the exclusive mode. In that sence there are four strategies:

The YASAPI plugin implements the first two strategies, i.e. the ones based on the push model.

The push model, in principle, works as follows:

  1. Query the size of the buffer shared with the audio device.
  2. Fill in completety the buffer shared with the audio device.
  3. Start playing.
  4. Loop until the track is played:
    1. Sleep half of the time corresponding to the size of the buffer shared with the audio device.
    2. Into the buffer shared with the audio device, fill in the gap which was growing free by playing during sleep.
  5. Sleep until the possibly remaining filled rest of the buffer shared with the audio device was played.
  6. Stop playing.

But the YASAPI plugin not only has to take into account the WASAPI side (the loop consisting of sleeping and writing to the audio device) but also the Winamp side because Winamp provides the audio samples which should be played in an completely unpredictable way.

The YASAPI plugin decouples the two sides by means of a ring or circular buffer. That way,

According to step 2 of the push model sketched above, it shoud become clear that the ring buffer should be at least as large (or larger) as the buffer the plugin's WASAPI component shares with the audio device.

4. Configuration

The YASAPI plugin comes with a configuration dialog which is described in the following. The dialog's top region presents the two most important parameters "Mode" (exclusive or share) and "Strategy" (push or pull)

Below those two parameters you find a tab-control consisting of four pages,

The tab-control's first page looks as follows:

It let's you configure

On the next page there are the following per device options:

On the final page you can configure the following per device buffer sizes:

From the above sketch of the push model it shold be clear that the following relation holds (which is enforced by the configuration dialog):

minimum size of the buffer shared with the device (provided by WASAPI) <= size of buffer shared with the device <= number of samples in the ring buffer before start playing <= size of ring buffer
As a rule of thumb all buffer sizes should be 1.0 except the ring buffer's size which should be just a small amount greater then 1.0.

Please note that no option takes effect before hitting the OK button.

5. Debug

In case something went wrong you may be interested in some more information. In order to utilize this there is a debug version of the plugin (distinguished by a lower case "d" at the and of the name, i.e. out_yasapid.dll) available from the download site.

Do the following:

  1. Close Winamp.
  2. Copy "out_yasapi-debug.dll" to Winamp's "Plugin" folder.
  3. Start Winamp.
  4. A console window should pop up tracing the initialiazation of the debug YASAPI plugin.
  5. From Winamps's preferences dialog choose "out_yasapi-debug.dll" as output plugin.
  6. When playing tracks in Winamp, the debug YASAPI plugin traces initialization and cleanup of each track.
  7. When shutting down Winamp, the cleanup of the YASAPI debug plugin is traced.
  8. After closing Winamp, the console window remains visible for another 5 s before vanishing.

The debug console.

You can get rid of the debug console by choosing the respective "Console" option from the debug YASAPI plugin's configuration dialog:

The following debug options can be configured: