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
15 //int DSoundSeg=0; // Seg length in samples
\r
17 static int LoopBlank()
\r
19 void *mema=NULL,*memb=NULL;
\r
20 DWORD sizea=0,sizeb=0;
\r
22 LoopBuffer->Lock(0,LoopLen<<2, &mema,&sizea, &memb,&sizeb, 0);
\r
24 if (mema) memset(mema,0,sizea);
\r
26 LoopBuffer->Unlock(mema,sizea, memb,sizeb);
\r
36 memset(&dsbd,0,sizeof(dsbd));
\r
37 memset(&wfx,0,sizeof(wfx));
\r
39 // Make wave format:
\r
40 wfx.wFormatTag=WAVE_FORMAT_PCM;
\r
41 wfx.nChannels=(unsigned short)((PicoOpt&8) ? 2 : 1); // Stereo/mono
\r
42 wfx.nSamplesPerSec=PsndRate;
\r
43 wfx.wBitsPerSample=16;
\r
45 wfx.nBlockAlign=(WORD)((wfx.nChannels*wfx.wBitsPerSample)>>3);
\r
46 wfx.nAvgBytesPerSec=wfx.nBlockAlign*wfx.nSamplesPerSec;
\r
48 // Make buffer for the next seg to put into the loop:
\r
49 DSoundNext=(short *)malloc(PsndLen<<2); if (DSoundNext==NULL) return 1;
\r
50 memset(DSoundNext,0,PsndLen<<2);
\r
52 // Create the DirectSound interface:
\r
53 DirectSoundCreate(NULL,&DSound,NULL);
\r
54 if (DSound==NULL) return 1;
\r
56 LoopLen=PsndLen<<1; // 2 segs
\r
59 LoopLen<<=1; // 4 segs
\r
60 DSound->SetCooperativeLevel(FrameWnd,DSSCL_PRIORITY);
\r
61 dsbd.dwFlags=DSBCAPS_GLOBALFOCUS; // Play in background
\r
64 // Create the looping buffer:
\r
65 dsbd.dwSize=sizeof(dsbd);
\r
66 dsbd.dwBufferBytes=LoopLen<<wfx.nChannels; // 16bit stereo?
\r
67 dsbd.lpwfxFormat=&wfx;
\r
69 DSound->CreateSoundBuffer(&dsbd,&LoopBuffer,NULL);
\r
70 if (LoopBuffer==NULL) return 1;
\r
73 LoopBuffer->Play(0,0,DSBPLAY_LOOPING);
\r
79 if (LoopBuffer) LoopBuffer->Stop();
\r
82 free(DSoundNext); DSoundNext=NULL;
\r
85 static int WriteSeg()
\r
87 void *mema=NULL,*memb=NULL;
\r
88 DWORD sizea=0,sizeb=0;
\r
90 // Lock the segment at 'LoopWrite' and copy the next segment in
\r
91 LoopBuffer->Lock(LoopWrite<<((PicoOpt&8) ? 2 : 1),PsndLen<<((PicoOpt&8) ? 2 : 1), &mema,&sizea, &memb,&sizeb, 0);
\r
93 if (mema) memcpy(mema,DSoundNext,sizea);
\r
95 LoopBuffer->Unlock(mema,sizea, memb,0);
\r
105 if (LoopBuffer==NULL) return 1;
\r
107 LoopBuffer->GetCurrentPosition(&play,NULL);
\r
108 pos=play>>((PicoOpt&8) ? 2 : 1);
\r
110 // 'LoopWrite' is the next seg in the loop that we want to write
\r
111 // First check that the sound 'play' pointer has moved out of it:
\r
112 if (pos>=LoopWrite && pos<LoopWrite+PsndLen) return 1; // No, it hasn't
\r
116 // Advance LoopWrite to next seg:
\r
117 LoopWrite+=PsndLen; if (LoopWrite+PsndLen>LoopLen) LoopWrite=0;
\r
124 LoopBuffer->Stop();
\r
127 void DSoundUnMute()
\r
129 LoopBuffer->Play(0,0,DSBPLAY_LOOPING);
\r