5 #pragma warning (disable:4201)
\r
6 #include <mmsystem.h>
\r
10 static IDirectSound *DSound=NULL;
\r
11 static IDirectSoundBuffer *LoopBuffer=NULL;
\r
12 static int LoopLen=0,LoopWrite=0; // Next position in loop to write
\r
14 short *DSoundNext=NULL; // Buffer for next sound data to put in loop
\r
16 static int LoopBlank()
\r
18 void *mema=NULL,*memb=NULL;
\r
19 DWORD sizea=0,sizeb=0;
\r
21 LoopBuffer->Lock(0,LoopLen<<2, &mema,&sizea, &memb,&sizeb, 0);
\r
23 if (mema) memset(mema,0,sizea);
\r
25 LoopBuffer->Unlock(mema,sizea, memb,sizeb);
\r
35 memset(&dsbd,0,sizeof(dsbd));
\r
36 memset(&wfx,0,sizeof(wfx));
\r
38 // Make wave format:
\r
39 wfx.wFormatTag=WAVE_FORMAT_PCM;
\r
40 wfx.nChannels=(unsigned short)((PicoOpt&8) ? 2 : 1); // Stereo/mono
\r
41 wfx.nSamplesPerSec=PsndRate;
\r
42 wfx.wBitsPerSample=16;
\r
44 wfx.nBlockAlign=(WORD)((wfx.nChannels*wfx.wBitsPerSample)>>3);
\r
45 wfx.nAvgBytesPerSec=wfx.nBlockAlign*wfx.nSamplesPerSec;
\r
47 // Make buffer for the next seg to put into the loop:
\r
48 DSoundNext=(short *)malloc(PsndLen<<2); if (DSoundNext==NULL) return 1;
\r
49 memset(DSoundNext,0,PsndLen<<2);
\r
51 // Create the DirectSound interface:
\r
52 DirectSoundCreate(NULL,&DSound,NULL);
\r
53 if (DSound==NULL) return 1;
\r
55 LoopLen=PsndLen<<1; // 2 segs
\r
58 LoopLen<<=1; // 4 segs
\r
59 DSound->SetCooperativeLevel(FrameWnd,DSSCL_PRIORITY);
\r
60 dsbd.dwFlags=DSBCAPS_GLOBALFOCUS; // Play in background
\r
63 // Create the looping buffer:
\r
64 dsbd.dwSize=sizeof(dsbd);
\r
65 dsbd.dwBufferBytes=LoopLen<<wfx.nChannels; // 16bit stereo?
\r
66 dsbd.lpwfxFormat=&wfx;
\r
68 DSound->CreateSoundBuffer(&dsbd,&LoopBuffer,NULL);
\r
69 if (LoopBuffer==NULL) return 1;
\r
72 LoopBuffer->Play(0,0,DSBPLAY_LOOPING);
\r
78 if (LoopBuffer) LoopBuffer->Stop();
\r
81 free(DSoundNext); DSoundNext=NULL;
\r
84 static int WriteSeg()
\r
86 void *mema=NULL,*memb=NULL;
\r
87 DWORD sizea=0,sizeb=0;
\r
89 // Lock the segment at 'LoopWrite' and copy the next segment in
\r
90 LoopBuffer->Lock(LoopWrite<<((PicoOpt&8) ? 2 : 1),PsndLen<<((PicoOpt&8) ? 2 : 1), &mema,&sizea, &memb,&sizeb, 0);
\r
92 if (mema) memcpy(mema,DSoundNext,sizea);
\r
94 LoopBuffer->Unlock(mema,sizea, memb,0);
\r
104 if (LoopBuffer==NULL) return -1;
\r
106 LoopBuffer->GetCurrentPosition(&play,NULL);
\r
107 pos=play>>((PicoOpt&8) ? 2 : 1);
\r
109 // 'LoopWrite' is the next seg in the loop that we want to write
\r
110 // First check that the sound 'play' pointer has moved out of it:
\r
111 if (pos>=LoopWrite && pos<LoopWrite+PsndLen) return 1; // No, it hasn't
\r
115 // Advance LoopWrite to next seg:
\r
116 LoopWrite+=PsndLen; if (LoopWrite+PsndLen>LoopLen) LoopWrite=0;
\r
123 if (LoopBuffer==NULL) return;
\r
124 LoopBuffer->Stop();
\r
127 void DSoundUnMute()
\r
129 if (LoopBuffer==NULL) return;
\r
130 LoopBuffer->Play(0,0,DSBPLAY_LOOPING);
\r