📄 win_snd.c
字号:
/* Not used with DirectSound */
void play_sound_data(WAVEHDR* pwhdr)
{
MMRESULT mmresult;
waveOut_fillbuffer(pwhdr);
mmresult=waveOutPrepareHeader(wave_output_device, pwhdr, sizeof(WAVEHDR));
if (mmresult!=MMSYSERR_NOERROR)
mmresult=0;
mmresult=waveOutWrite(wave_output_device, pwhdr, sizeof(WAVEHDR));
if (mmresult!=MMSYSERR_NOERROR)
mmresult=0;
}
/* Not used with DirectSound */
void unprepare_sound_data(WAVEHDR* pwhdr)
{
MMRESULT mmresult;
mmresult=waveOutUnprepareHeader(wave_output_device, pwhdr, sizeof(WAVEHDR));
if (mmresult!=MMSYSERR_NOERROR)
mmresult=0;
}
LONG get_sound_volume()
{
return dx_sound_volume;
}
void set_sound_volume(LONG new_volume)
{
if (new_volume<=100&&new_volume>=0)
dx_sound_volume=new_volume;
else
dx_sound_volume=100;
if (!wave_device_available)
return;
// if (sound_output_device==DIRECT_SOUND_DEVICE && lpdsb)
// IDirectSoundBuffer_SetVolume(lpdsb, DSBVOLUME_MIN + (new_volume) * (DSBVOLUME_MAX - DSBVOLUME_MIN) / 100 );
}
void pause_windows_sound_playback()
{
if (!wave_device_available)
return;
// if (sound_output_device==DIRECT_SOUND_DEVICE && lpdsb)
// IDirectSoundBuffer_Stop(lpdsb);
if (sound_output_device==WAVE_OUT_DEVICE)
waveOutPause(wave_output_device);
};
void resume_windows_sound_playback()
{
if (!wave_device_available)
return;
// if (sound_output_device==DIRECT_SOUND_DEVICE && lpdsb)
// IDirectSoundBuffer_Play(lpdsb, 0, 0, DSBPLAY_LOOPING);
if (sound_output_device==WAVE_OUT_DEVICE)
waveOutRestart(wave_output_device);
};
DWORD get_sound_freq()
{
DWORD sound_freq;
if (!wave_device_available)
return waveout_sample_rate;
if (sound_output_device==DIRECT_SOUND_DEVICE)
{
// if (IDirectSoundBuffer_GetFrequency(lpdsb, &sound_freq)==DS_OK)
// return sound_freq;
// else
return 0;
}
else
{
return waveout_sample_rate;
}
}
void s0initint8(void){}
void s0restoreint8(void){}
void s0soundoff(void){}
void s0setspkrt2(void){}
void s0settimer0(Uint4 t0v){}
void s0timer0(Uint4 t0v){}
void s0settimer2(Uint4 t2v){}
void s0timer2(Uint4 t2v){}
void s0soundinitglob(void){}
void s0soundkillglob(void){}
void s1initint8(void){}
void s1restoreint8(void){}
void waveOut_fillbuffer(WAVEHDR* pwhdr)
{
if (sound_output_device!=WAVE_OUT_DEVICE || !sound_buffer)
return;
/* make sure that 'buffer' has been completely filled */
fill_sound_buffer=TRUE;
s1fillbuffer();
/* copy the data in 'buffer' to the buffer associated with the waveheader */
farmemcpy(pwhdr->lpData,sound_buffer,sizeof(samp)*size);
fill_sound_buffer=FALSE;
last=0;
firsts=0;
}
void s1fillbuffer(void)
{
MMTIME mmtime;
MMRESULT mmresult;
samp *lpvAudioPtr1;
DWORD dwAudioBytes1;
samp *lpvAudioPtr2;
DWORD dwAudioBytes2;
HRESULT hRet;
DWORD i;
DWORD play_position;
DWORD write_position;
int bytes_to_fill;
/*
if (sound_card_capture_flag==1)
{
sound_card_capture_flag=0;
capture_sound_card();
}
else
{
if (sound_card_capture_flag==2)
{
sound_card_capture_flag=0;
release_sound_card();
}
}
*/
if (!wave_device_available)
{
/* Since there is no sound device available, there is no point in */
/* calling getsample() and filling a sound buffer, however, we still */
/* have to detect when sound is turned on or off... */
/* This code is copied from soundint(). */
/*
if (soundflag && !sndflag)
{
sndflag=musicflag=TRUE;
sound_card_capture_flag=1;
}
if (!soundflag && sndflag) {
sndflag=FALSE;
timer2(40);
setsoundt2();
soundoff();
sound_card_capture_flag=2;
}
*/
return;
}
if (sound_output_device==DIRECT_SOUND_DEVICE)
/* Using DirectSound */
{
if (sound_buffer_playing)
{
// hRet = IDirectSoundBuffer_GetCurrentPosition(lpdsb, &play_position, &write_position /*NULL */);
if (hRet!=DS_OK)
fatal_error(hRet, "IDirectSoundBuffer_GetCurrentPosition FAILED");
firsts=(Uint4) play_position;
}
else
firsts=0;
if (last>firsts)
bytes_to_fill=size-last+firsts;
else
bytes_to_fill=firsts-last;
if (bytes_to_fill>0) {
// hRet=IDirectSoundBuffer_Lock(lpdsb,last,bytes_to_fill,
// (void **)&lpvAudioPtr1,&dwAudioBytes1,
// (void **)&lpvAudioPtr2,&dwAudioBytes2,
// (DWORD)NULL);
if (hRet==DS_OK) {
for (i=0;i<dwAudioBytes1;i++)
lpvAudioPtr1[i]=getsample();
for (i=0;i<dwAudioBytes2;i++)
lpvAudioPtr2[i]=getsample();
IDirectSoundBuffer_Unlock(lpdsb,lpvAudioPtr1,dwAudioBytes1,lpvAudioPtr2,
dwAudioBytes2);
}
else
fatal_error(hRet, "IDirectSoundBuffer_Lock FAILED");
last=firsts;
}
}
else
{
if (fill_sound_buffer) /* Are we ready to write out another block of sound? */
/* If so, make sure the entire buffer is filled. */
{
firsts=size-1;
}
else /* Try to keep the calls to get sample() at a steady pace. */
{
mmtime.wType=TIME_BYTES;
mmresult=waveOutGetPosition(wave_output_device, &mmtime, sizeof(MMTIME));
if (mmresult!=MMSYSERR_NOERROR)
return;
firsts=(Uint4) mmtime.u.cb % size;
}
while (firsts>=last)
{
/* when not using DirectSound, the volume is applied when getsample() is called */
sound_buffer[last]=dx_sound_volume ? (getsample()-127) * dx_sound_volume / 100 + 127: 127;
last=last+1;
}
}
}
extern bool soundlevdoneflag,soundpausedflag;
void capture_sound_card()
{
soundinitglob(0,0,0,sound_length,sound_rate);
set_sound_volume(dx_sound_volume);
soundlevdoneflag=soundpausedflag=FALSE;
//soundstop();
//soundlevdone();
}
void release_sound_card()
{
pause_windows_sound_playback();
if (lpdsb)
{
// IDirectSoundBuffer_Release(lpdsb);
lpdsb=(LPDIRECTSOUNDBUFFER) NULL;
}
if (lpds)
{
// IDirectSound_Release(lpds);
lpds=(LPDIRECTSOUND) NULL;
}
if (wave_output_device)
{
waveOutReset(wave_output_device);
waveOutClose(wave_output_device);
wave_output_device=0;
}
wave_device_available=FALSE;
destroy_sound_buffers();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -