00001
00002 #include "revolt.h"
00003 #include "sound.h"
00004
00005 static unsigned char Sample[4048*1024];
00006
00007 char NoSound = FALSE;
00008 char SoundDevice = 0;
00009 char SoundDeviceCount;
00010 volatile char SoundDeviceFlag;
00011 IDirectSound *DSObj;
00012 IDirectSoundBuffer *SBufferPrim;
00013 IDirectSoundBuffer *SBufferSec[DUPLICATESAMPLES][MAXSAMPLES];
00014 char SampleLoaded[MAXSAMPLES];
00015 char SampleUsed[DUPLICATESAMPLES][MAXSAMPLES];
00016 DWORD SampleLen[DUPLICATESAMPLES][MAXSAMPLES];
00017
00018
00019
00020
00021
00022
00023 BOOL CALLBACK FindSoundDeviceCallback(LPGUID lpGUID, LPSTR szName, LPSTR szDevice, LPVOID lParam)
00024 {
00025 HRESULT r;
00026
00027 if (SoundDevice == SoundDeviceCount++)
00028 {
00029 r = DirectSoundCreate(lpGUID, &DSObj, NULL);
00030
00031 if (r == DD_OK)
00032
00033 SoundDeviceFlag = 1;
00034 else
00035 SoundDeviceFlag = 2;
00036
00037 return DDENUMRET_CANCEL;
00038 }
00039
00040 return DDENUMRET_OK;
00041 }
00042
00043
00044
00045 char SetupSound( HWND hwnd )
00046 {
00047
00048 if (NoSound)
00049 return TRUE;
00050
00051 SoundDeviceFlag = SoundDeviceCount = 0;
00052 DirectSoundEnumerate( (LPDSENUMCALLBACK)FindSoundDeviceCallback, NULL);
00053
00054 while (!SoundDeviceFlag);
00055
00056
00057 for( short h = 0; h < MAXSAMPLES; h++ )
00058 SampleLoaded[h] = 0;
00059
00060 for( short i = 0; i < DUPLICATESAMPLES; i ++ )
00061 for( short j = 0; j < MAXSAMPLES; j++ )
00062 SampleUsed[i][j] = 0;
00063
00064
00065
00066 DSObj->SetCooperativeLevel( hwnd, DSSCL_NORMAL );
00067
00068 DSCAPS dscaps;
00069 dscaps.dwSize = sizeof(DSCAPS);
00070 DSObj->GetCaps(&dscaps);
00071 DSObj->SetSpeakerConfig( DSSPEAKER_STEREO );
00072
00073
00074
00075
00076 DSBUFFERDESC DSBD;
00077 ZeroMemory(&DSBD, sizeof(DSBUFFERDESC));
00078 DSBD.dwSize = sizeof(DSBUFFERDESC);
00079 DSBD.dwFlags = DSBCAPS_PRIMARYBUFFER ;
00080 DSBD.dwBufferBytes = 0;
00081 DSBD.lpwfxFormat = NULL;
00082
00083 DSObj->CreateSoundBuffer( &DSBD,&SBufferPrim,NULL );
00084
00085 LoadWAV( "wavs\\moto.wav", 0 );
00086
00087
00088 return TRUE;
00089 }
00090
00091
00092
00093
00094 char LoadWAV( char *FileName, short Num )
00095 {
00096 FILE *ap;
00097
00098 unsigned char TempBuffer[64];
00099
00100 if (NoSound)
00101 return TRUE;
00102
00103 ap = fopen( FileName, "rb" );
00104 if (!ap)
00105 return FALSE;
00106
00107 fread( &TempBuffer, 1, 4+8, ap );
00108
00109 fread( &TempBuffer, 1, 24, ap );
00110
00111
00112 DSBUFFERDESC DSBD2;
00113 PCMWAVEFORMAT pcmwf;
00114
00115 ZeroMemory(&pcmwf, sizeof(PCMWAVEFORMAT));
00116 pcmwf.wf.wFormatTag = WAVE_FORMAT_PCM;
00117 pcmwf.wf.nChannels = *(unsigned short*) &TempBuffer[10];
00118 pcmwf.wf.nSamplesPerSec = *(unsigned short*) &TempBuffer[12];
00119 pcmwf.wf.nBlockAlign = pcmwf.wf.nChannels;
00120 pcmwf.wf.nAvgBytesPerSec = pcmwf.wf.nSamplesPerSec * pcmwf.wf.nBlockAlign;
00121 pcmwf.wBitsPerSample = 8;
00122
00123 fread( &TempBuffer, 1, 8, ap );
00124
00125 ZeroMemory(&DSBD2, sizeof(DSBUFFERDESC));
00126 DSBD2.dwSize = sizeof(DSBUFFERDESC);
00127 DSBD2.dwFlags = DSBCAPS_CTRLALL | DSBCAPS_LOCSOFTWARE ;
00128 DSBD2.dwBufferBytes = *(unsigned long*) &TempBuffer[4];
00129 DSBD2.lpwfxFormat = (LPWAVEFORMATEX)&pcmwf;
00130
00131
00132
00133 fread( &Sample[0], 1, DSBD2.dwBufferBytes, ap );
00134
00135
00136 LPVOID SP, SP2;
00137 DWORD B, B2;
00138
00139 for( short i = 0; i < DUPLICATESAMPLES; i++ )
00140 {
00141
00142 DSObj->CreateSoundBuffer( &DSBD2, &SBufferSec[i][Num], NULL);
00143
00144 SBufferSec[i][Num]->SetVolume( DSBVOLUME_MAX );
00145
00146 SampleLen[i][Num] = DSBD2.dwBufferBytes;
00147 SBufferSec[i][Num]->Lock(0, DSBD2.dwBufferBytes, &SP, &B, &SP2, &B2, 0 );
00148 CopyMemory( SP,&Sample[0], B );
00149 SBufferSec[i][Num]->Unlock( SP, B, SP2, B2 );
00150 }
00151
00152 SampleLoaded[Num] = 1;
00153
00154 fclose (ap);
00155
00156 return TRUE;
00157 }
00158
00159
00160
00161
00162 short PlaySample( short Num, LONG Vol, LONG Freq, LONG Pan, char Loop )
00163 {
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 if (NoSound)
00174 return -1;
00175
00176 short Handle = -1;
00177
00178 if( SampleLoaded[Num] == 1 )
00179 {
00180
00181 for( short i = 0; i < DUPLICATESAMPLES; i++ )
00182 if( SampleUsed[i][Num] == 0 )
00183 {
00184 SampleUsed[i][Num] = 1;
00185 SBufferSec[i][Num]->SetVolume( Vol );
00186 SBufferSec[i][Num]->SetFrequency( Freq );
00187 SBufferSec[i][Num]->SetPan( Pan );
00188
00189 if( Loop == 1)
00190 SBufferSec[i][Num]->Play( NULL, NULL, DSBPLAY_LOOPING );
00191 else
00192 SBufferSec[i][Num]->Play( NULL, NULL, NULL );
00193
00194 Handle = i;
00195 i = DUPLICATESAMPLES;
00196 }
00197 }
00198
00199
00200 return Handle;
00201 }
00202
00203
00204
00205 char SoundManager()
00206 {
00207 DWORD PlayStatus;
00208
00209 if (NoSound)
00210 return TRUE;
00211
00212 for( short i = 0; i < DUPLICATESAMPLES; i ++ )
00213 for( short j = 0; j < MAXSAMPLES; j++ )
00214 if( SampleUsed[i][j] == 1 )
00215 {
00216 SBufferSec[i][j]->GetStatus( &PlayStatus );
00217 if( (PlayStatus & DSBSTATUS_PLAYING) == 0 )
00218 SampleUsed[i][j] = 0;
00219
00220 }
00221
00222 return TRUE;
00223 }
00224
00225
00226
00227
00228
00229 char StopSample( short Num, short Handle )
00230 {
00231 if (NoSound)
00232 return TRUE;
00233
00234 if( SampleLoaded[Num] == 1 )
00235 {
00236 if( SampleUsed[Handle][Num] == 1 )
00237 {
00238 SBufferSec[Handle][Num]->Stop();
00239 SampleUsed[Handle][Num] = 0;
00240 }
00241 }
00242
00243 return TRUE;
00244 }
00245
00246
00247
00248 char ChangeSample( short Num, short Handle, LONG Vol, LONG Freq, LONG Pan )
00249 {
00250
00251
00252
00253
00254
00255
00256
00257 if (NoSound)
00258 return TRUE;
00259
00260 if( SampleLoaded[Num] == 1 )
00261 {
00262 SBufferSec[Handle][Num]->SetVolume( Vol );
00263 SBufferSec[Handle][Num]->SetFrequency( Freq );
00264 SBufferSec[Handle][Num]->SetPan( Pan );
00265 }
00266
00267
00268 return TRUE;
00269 }
00270
00271
00272
00273 void KillSound( void )
00274 {
00275 if (NoSound)
00276 return;
00277
00278 RELEASE(DSObj);
00279 }
00280
00281
00282
00283
00284