dsoundaudio.c revision 02e777a6f939d73f5285178584feffb3648fe8d5
/*
* QEMU DirectSound audio driver
*
* Copyright (c) 2005 Vassili Karpov (malc)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*
* SEAL 1.07 by Carlos 'pel' Hasan was used as documentation
*/
#define _WIN32_DCOM
#include <windows.h>
#include <objbase.h>
#include <dsound.h>
#include "Builtins.h"
#include "../../vl_vbox.h"
#include "audio.h"
#define AUDIO_CAP "dsound"
#include "audio_int.h"
/* #define DEBUG_DSOUND */
static struct {
int lock_retries;
int restore_retries;
int getstatus_retries;
int set_primary;
int bufsize_in;
int bufsize_out;
int latency_millis;
} conf = {
1,
1,
1,
0,
16384,
16384,
{
44100,
2,
},
10
};
typedef struct {
} dsound;
static dsound glob_dsound;
typedef struct {
int first_time;
#ifdef DEBUG_DSOUND
#endif
typedef struct {
int first_time;
{
const char *str = "BUG";
switch (hr) {
case DS_OK:
str = "The method succeeded";
break;
#ifdef DS_NO_VIRTUALIZATION
case DS_NO_VIRTUALIZATION:
str = "The buffer was created, but another 3D algorithm was substituted";
break;
#endif
#ifdef DS_INCOMPLETE
case DS_INCOMPLETE:
str = "The method succeeded, but not all the optional effects were obtained";
break;
#endif
#ifdef DSERR_ACCESSDENIED
case DSERR_ACCESSDENIED:
str = "The request failed because access was denied";
break;
#endif
#ifdef DSERR_ALLOCATED
case DSERR_ALLOCATED:
str = "The request failed because resources, such as a priority level, were already in use by another caller";
break;
#endif
#ifdef DSERR_ALREADYINITIALIZED
case DSERR_ALREADYINITIALIZED:
str = "The object is already initialized";
break;
#endif
#ifdef DSERR_BADFORMAT
case DSERR_BADFORMAT:
str = "The specified wave format is not supported";
break;
#endif
#ifdef DSERR_BADSENDBUFFERGUID
case DSERR_BADSENDBUFFERGUID:
str = "The GUID specified in an audiopath file does not match a valid mix-in buffer";
break;
#endif
#ifdef DSERR_BUFFERLOST
case DSERR_BUFFERLOST:
str = "The buffer memory has been lost and must be restored";
break;
#endif
#ifdef DSERR_BUFFERTOOSMALL
case DSERR_BUFFERTOOSMALL:
str = "The buffer size is not great enough to enable effects processing";
break;
#endif
#ifdef DSERR_CONTROLUNAVAIL
case DSERR_CONTROLUNAVAIL:
str = "The buffer control (volume, pan, and so on) requested by the caller is not available. Controls must be specified when the buffer is created, using the dwFlags member of DSBUFFERDESC";
break;
#endif
#ifdef DSERR_DS8_REQUIRED
case DSERR_DS8_REQUIRED:
str = "A DirectSound object of class CLSID_DirectSound8 or later is required for the requested functionality. For more information, see IDirectSound8 Interface";
break;
#endif
#ifdef DSERR_FXUNAVAILABLE
case DSERR_FXUNAVAILABLE:
str = "The effects requested could not be found on the system, or they are in the wrong order or in the wrong location; for example, an effect expected in hardware was found in software";
break;
#endif
#ifdef DSERR_GENERIC
case DSERR_GENERIC :
str = "An undetermined error occurred inside the DirectSound subsystem";
break;
#endif
#ifdef DSERR_INVALIDCALL
case DSERR_INVALIDCALL:
str = "This function is not valid for the current state of this object";
break;
#endif
#ifdef DSERR_INVALIDPARAM
case DSERR_INVALIDPARAM:
str = "An invalid parameter was passed to the returning function";
break;
#endif
#ifdef DSERR_NOAGGREGATION
case DSERR_NOAGGREGATION:
str = "The object does not support aggregation";
break;
#endif
#ifdef DSERR_NODRIVER
case DSERR_NODRIVER:
str = "No sound driver is available for use, or the given GUID is not a valid DirectSound device ID";
break;
#endif
#ifdef DSERR_NOINTERFACE
case DSERR_NOINTERFACE:
str = "The requested COM interface is not available";
break;
#endif
#ifdef DSERR_OBJECTNOTFOUND
case DSERR_OBJECTNOTFOUND:
str = "The requested object was not found";
break;
#endif
#ifdef DSERR_OTHERAPPHASPRIO
case DSERR_OTHERAPPHASPRIO:
str = "Another application has a higher priority level, preventing this call from succeeding";
break;
#endif
#ifdef DSERR_OUTOFMEMORY
case DSERR_OUTOFMEMORY:
str = "The DirectSound subsystem could not allocate sufficient memory to complete the caller's request";
break;
#endif
#ifdef DSERR_PRIOLEVELNEEDED
case DSERR_PRIOLEVELNEEDED:
str = "A cooperative level of DSSCL_PRIORITY or higher is required";
break;
#endif
#ifdef DSERR_SENDLOOP
case DSERR_SENDLOOP:
str = "A circular loop of send effects was detected";
break;
#endif
#ifdef DSERR_UNINITIALIZED
case DSERR_UNINITIALIZED:
str = "The Initialize method has not been called or has not been called successfully before other methods were called";
break;
#endif
#ifdef DSERR_UNSUPPORTED
case DSERR_UNSUPPORTED:
str = "The function called is not supported at this time";
break;
#endif
default:
return;
}
#ifdef VBOX
#endif
}
const char *fmt,
...
)
{
}
const char *typ,
const char *fmt,
...
)
{
}
{
}
#ifdef DEBUG_DSOUND
{
}
#endif
{
int i;
for (i = 0; i < conf.restore_retries; ++i) {
switch (hr) {
case DS_OK:
return 0;
case DSERR_BUFFERLOST:
continue;
default:
return -1;
}
}
dolog ("%d attempts to restore playback buffer failed\n", i);
return -1;
}
{
case AUD_FMT_S8:
break;
case AUD_FMT_U8:
break;
case AUD_FMT_S16:
break;
case AUD_FMT_U16:
break;
default:
return -1;
}
return 0;
}
{
dolog ("Invalid wave format, tag is not PCM, but %d\n",
wfx->wFormatTag);
return -1;
}
if (!wfx->nSamplesPerSec) {
dolog ("Invalid wave format, frequency is zero\n");
return -1;
}
case 1:
break;
case 2:
break;
default:
dolog (
"Invalid wave format, number of channels is not 1 or 2, but %d\n",
);
return -1;
}
switch (wfx->wBitsPerSample) {
case 8:
break;
case 16:
break;
default:
dolog ("Invalid wave format, bits per sample is not 8 or 16, but %d\n",
return -1;
}
return 0;
}
#include "dsound_template.h"
#define DSBTYPE_IN
#include "dsound_template.h"
{
int i;
for (i = 0; i < conf.getstatus_retries; ++i) {
return -1;
}
if (*statusp & DSERR_BUFFERLOST) {
if (dsound_restore_out (dsb)) {
return -1;
}
continue;
}
break;
}
return 0;
}
{
return -1;
}
return 0;
}
{
int src_len2 = 0;
}
if (src_len1) {
// mixeng_sniff_and_clear (hw, src1, dst, src_len1);
}
if (src_len2) {
// mixeng_sniff_and_clear (hw, src2, dst, src_len2);
}
}
{
int err;
err = dsound_lock_out (
dsb,
0,
1
);
if (err) {
return;
}
#ifdef DEBUG_DSOUND
dolog ("clear %p,%ld,%ld %p,%ld,%ld\n",
#endif
}
}
}
static void dsound_close (dsound *s)
{
if (s->dsound_primary_buffer) {
}
s->dsound_primary_buffer = NULL;
}
}
static int dsound_open (dsound *s)
{
int err;
hwnd = GetForegroundWindow ();
s->dsound,
hwnd,
);
#ifndef VBOX
hwnd);
#else
#endif
return -1;
}
if (!conf.set_primary) {
return 0;
}
if (err) {
return -1;
}
dsbd.dwBufferBytes = 0;
s->dsound,
&dsbd,
);
#ifndef VBOX
#else
LogRel(("DSound: Could not create primary playback buffer\n"));
#endif
return -1;
}
#ifndef VBOX
#else
LogRel(("DSound: Could not set primary playback buffer format\n"));
#endif
}
&wfx,
sizeof (wfx),
);
#ifndef VBOX
#else
LogRel(("DSound: Could not get primary playback buffer format\n"));
#endif
goto fail0;
}
#ifdef DEBUG_DSOUND
dolog ("Primary\n");
print_wave_format (&wfx);
#endif
if (err) {
goto fail0;
}
return 0;
dsound_close (s);
return -1;
}
{
if (!dsb) {
dolog ("Attempt to control voice without a buffer\n");
return 0;
}
switch (cmd) {
case VOICE_ENABLE:
return -1;
}
if (status & DSBSTATUS_PLAYING) {
dolog ("warning: Voice is already playing\n");
return 0;
}
return -1;
}
break;
case VOICE_DISABLE:
return -1;
}
if (status & DSBSTATUS_PLAYING) {
return -1;
}
}
else {
dolog ("warning: Voice is not playing\n");
}
break;
}
return 0;
}
{
}
{
int err;
int bufsize;
if (!dsb) {
dolog ("Attempt to run empty with playback buffer\n");
return 0;
}
dsb,
&ppos,
);
return 0;
}
if (ds->first_time) {
if (conf.latency_millis) {
ds->first_time = 0;
old_pos +=
}
else {
}
#ifdef DEBUG_DSOUND
#endif
}
else {
#ifdef DEBUG_DSOUND
dolog ("old_pos == ppos\n");
#endif
return 0;
}
#ifdef DEBUG_DSOUND
#endif
}
}
else {
}
}
dolog ("len=%d bufsize=%d old_pos=%ld ppos=%ld\n",
return 0;
}
if (!len) {
return 0;
}
#ifdef DEBUG_DSOUND
#endif
err = dsound_lock_out (
dsb,
len,
0
);
if (err) {
return 0;
}
}
}
#ifdef DEBUG_DSOUND
dolog ("played %lu mixed %lu diff %ld sec %f\n",
#endif
return decr;
}
{
if (!dscb) {
dolog ("Attempt to control capture voice without a buffer\n");
return -1;
}
switch (cmd) {
case VOICE_ENABLE:
return -1;
}
if (status & DSCBSTATUS_CAPTURING) {
dolog ("warning: Voice is already capturing\n");
return 0;
}
/* clear ?? */
return -1;
}
break;
case VOICE_DISABLE:
return -1;
}
if (status & DSCBSTATUS_CAPTURING) {
return -1;
}
}
else {
dolog ("warning: Voice is not capturing\n");
}
break;
}
return 0;
}
{
}
{
int err;
int hwshift;
if (!dscb) {
dolog ("Attempt to run without capture buffer\n");
return 0;
}
if (!dead) {
return 0;
}
dscb,
&cpos,
);
return 0;
}
if (ds->first_time) {
ds->first_time = 0;
ldebug ("warning: Misaligned capture read position %ld(%d)\n",
}
}
ldebug ("warning: Misaligned capture position %ld(%d)\n",
}
if (!len) {
return 0;
}
err = dsound_lock_in (
dscb,
&p1,
&p2,
&blen1,
&blen2,
0
);
if (err) {
return 0;
}
#ifndef VBOX
}
}
#else
}
}
#endif
return decr;
}
static void dsound_audio_fini (void *opaque)
{
if (!s->dsound) {
return;
}
}
if (!s->dsound_capture) {
return;
}
}
s->dsound_capture = NULL;
}
static void *dsound_audio_init (void)
{
int err;
dsound *s = &glob_dsound;
#ifndef VBOX
#else
LogRel(("DSound: Could not initialize COM\n"));
#endif
return NULL;
}
hr = CoCreateInstance (
NULL,
(void **) &s->dsound
);
#ifndef VBOX
#else
LogRel(("DSound: Could not create DirectSound instance\n"));
#endif
return NULL;
}
#ifndef VBOX
#else
LogRel(("DSound: Could not initialize DirectSound\n"));
#endif
}
return NULL;
}
hr = CoCreateInstance (
NULL,
(void **) &s->dsound_capture
);
#ifndef VBOX
#else
LogRel(("DSound: Could not create DirectSoundCapture instance\n"));
#endif
}
else {
#ifndef VBOX
#else
LogRel(("DSound: Could not initialize DirectSoundCapture\n"));
#endif
"Could not release DirectSoundCapture\n");
}
s->dsound_capture = NULL;
}
}
err = dsound_open (s);
if (err) {
dsound_audio_fini (s);
return NULL;
}
return s;
}
static struct audio_option dsound_options[] = {
"Number of times to attempt locking the buffer", NULL, 0},
"Number of times to attempt restoring the buffer", NULL, 0},
"Number of times to attempt getting status of the buffer", NULL, 0},
"Set the parameters of primary buffer", NULL, 0},
"(undocumented)", NULL, 0},
"Primary buffer frequency", NULL, 0},
"Primary buffer number of channels (1 - mono, 2 - stereo)", NULL, 0},
"Primary buffer format", NULL, 0},
"(undocumented)", NULL, 0},
"(undocumented)", NULL, 0},
};
static struct audio_pcm_ops dsound_pcm_ops = {
};
struct audio_driver dsound_audio_driver = {
INIT_FIELD (descr = )
"DirectSound http://wikipedia.org/wiki/DirectSound",
};