1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
/* dxsound.c: DirectX sound I/O
Copyright (c) 2003-2004 Marek Januszewski, Philip Kendall
$Id: dxsound.c 3752 2008-08-20 00:50:02Z specu $
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 2 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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Author contact information:
E-mail: philip-fuse@shadowmagic.org.uk
*/
#include <config.h>
#include <windows.h>
#include <mmsystem.h>
#include <dsound.h>
#include "settings.h"
#include "sound.h"
#include "ui/ui.h"
/* same as for SDL Sound */
#define MAX_AUDIO_BUFFER 8192*5
static LPDIRECTSOUND lpDS; /* DirectSound object */
static LPDIRECTSOUNDBUFFER lpDSBuffer; /* sound buffer */
static DWORD nextpos; /* next position in circular buffer */
static int sixteenbit;
int
sound_lowlevel_init( const char *device, int *freqptr, int *stereoptr )
{
WAVEFORMATEX pcmwf; /* waveformat struct */
DSBUFFERDESC dsbd; /* buffer description */
/* Initialize COM */
CoInitialize(NULL);
/* create DirectSound object */
if( CoCreateInstance( &CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
&IID_IDirectSound, (void**)&lpDS ) != DS_OK ) {
settings_current.sound = 0;
ui_error( UI_ERROR_ERROR, "Couldn't create DirectSound object.");
CoUninitialize();
return 1;
}
/* initialize it */
if( IDirectSound_Initialize( lpDS, NULL ) != DS_OK ) {
settings_current.sound = 0;
ui_error( UI_ERROR_ERROR, "Couldn't initialize DirectSound." );
CoUninitialize();
return 1;
}
/* set normal cooperative level */
if( IDirectSound_SetCooperativeLevel( lpDS, GetDesktopWindow(),
DSSCL_NORMAL ) != DS_OK ) {
settings_current.sound = 0;
ui_error( UI_ERROR_ERROR, "Couldn't set DirectSound cooperation level." );
IDirectSound_Release( lpDS );
CoUninitialize();
return 1;
}
/* create wave format description */
memset( &pcmwf, 0, sizeof( WAVEFORMATEX ) );
pcmwf.cbSize = 0;
if( settings_current.sound_force_8bit ) {
pcmwf.wBitsPerSample = 8;
sixteenbit = 0;
} else {
pcmwf.wBitsPerSample = 16;
sixteenbit = 1;
}
pcmwf.nChannels = *stereoptr ? 2 : 1;
pcmwf.nBlockAlign = pcmwf.nChannels * ( sixteenbit ? 2 : 1 );
pcmwf.nSamplesPerSec = *freqptr;
pcmwf.nAvgBytesPerSec = pcmwf.nSamplesPerSec * pcmwf.nBlockAlign;
pcmwf.wFormatTag = WAVE_FORMAT_PCM;
/* create sound buffer description */
memset( &dsbd, 0, sizeof( DSBUFFERDESC ) );
dsbd.dwBufferBytes = MAX_AUDIO_BUFFER;
dsbd.dwFlags = DSBCAPS_GLOBALFOCUS | DSBCAPS_CTRLVOLUME |
DSBCAPS_CTRLFREQUENCY | DSBCAPS_STATIC | DSBCAPS_LOCSOFTWARE;
dsbd.dwSize = sizeof( DSBUFFERDESC );
dsbd.lpwfxFormat = &pcmwf;
/* attempt to create the buffer */
if( IDirectSound_CreateSoundBuffer( lpDS, &dsbd, &lpDSBuffer, NULL )
!= DS_OK ) {
settings_current.sound = 0;
ui_error( UI_ERROR_ERROR, "Couldn't create DirectSound buffer." );
IDirectSound_Release( lpDS );
CoUninitialize();
return 1;
}
/* play buffer */
if( IDirectSoundBuffer_Play( lpDSBuffer, 0, 0, DSBPLAY_LOOPING ) != DS_OK ) {
settings_current.sound = 0;
ui_error( UI_ERROR_ERROR, "Couldn't play sound." );
IDirectSoundBuffer_Release( lpDSBuffer );
IDirectSound_Release( lpDS );
CoUninitialize();
return 1;
}
nextpos = 0;
return 0;
}
void
sound_lowlevel_end( void )
{
if( IDirectSoundBuffer_Stop( lpDSBuffer ) != DS_OK ) {
settings_current.sound = 0;
ui_error( UI_ERROR_ERROR, "Couldn't stop sound." );
}
IDirectSoundBuffer_Release( lpDSBuffer );
IDirectSound_Release( lpDS );
CoUninitialize();
}
/* Copying data to the buffer */
void
sound_lowlevel_frame( libspectrum_signed_word *data, int len )
{
HRESULT hres;
int i1, i2;
int lsb = 1;
/* two pair because of circular buffer */
UCHAR *ucbuffer1, *ucbuffer2;
DWORD length1, length2;
DWORD playcursor;
long cursordiff;
if( sixteenbit )
len *= 2;
while( len ) {
while( 1 ) {
IDirectSoundBuffer_GetCurrentPosition( lpDSBuffer, &playcursor, NULL );
cursordiff = (long)nextpos - (long)playcursor;
if( playcursor > nextpos )
cursordiff += MAX_AUDIO_BUFFER;
if( cursordiff < len * 6 )
break;
Sleep(10);
}
/* lock the buffer */
hres = IDirectSoundBuffer_Lock( lpDSBuffer, nextpos, (DWORD)len,
(void **)&ucbuffer1, &length1,
(void **)&ucbuffer2, &length2,
0 );
if( hres != DS_OK ) return; /* couldn't get a lock on the buffer */
/* write to the first part of buffer */
for( i1 = 0; i1 < length1 && len > 0; i1++ ) {
if( sixteenbit ) {
if( lsb ) {
ucbuffer1[ i1 ] = *data & 0xff;
} else {
ucbuffer1[ i1 ] = *data >> 8;
data++;
}
lsb = !lsb;
} else {
ucbuffer1[ i1 ] = ( ( *data++ ) >> 8 ) ^ 0x80;
}
nextpos++;
len--;
}
if( ucbuffer2 ) {
/* write to the second part of buffer */
for( i2 = 0; i2 < length2 && len > 0; i2++ ) {
if( sixteenbit ) {
if( lsb ) {
ucbuffer2[ i2 ] = *data & 0xff;
} else {
ucbuffer2[ i2 ] = *data >> 8;
data++;
}
lsb = !lsb;
} else {
ucbuffer2[ i2 ] = ( ( *data++ ) >> 8 ) ^ 0x80;
}
nextpos++;
len--;
}
} else {
i2 = 0;
}
if( nextpos >= MAX_AUDIO_BUFFER ) nextpos -= MAX_AUDIO_BUFFER;
/* unlock the buffer */
IDirectSoundBuffer_Unlock( lpDSBuffer, ucbuffer1, i1, ucbuffer2, i2 );
}
}
|