X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Flibgame%2Fsound.c;h=1a31557a54863b2fc65435146d0ae0bb6a00e3e3;hb=8c373c85183bd059717b0a53ffb97dd472bd6741;hp=3b938200021f40a52504a665a164b3d65b66b99b;hpb=d0e7ff980c3d4d73ac39c2670177e6030b966760;p=rocksndiamonds.git diff --git a/src/libgame/sound.c b/src/libgame/sound.c index 3b938200..1a31557a 100644 --- a/src/libgame/sound.c +++ b/src/libgame/sound.c @@ -41,29 +41,24 @@ static struct SoundControl emptySoundControl = #if defined(PLATFORM_UNIX) static int stereo_volume[PSND_MAX_LEFT2RIGHT+1]; -static char premix_first_buffer[SND_BLOCKSIZE]; +static short premix_first_buffer[SND_BLOCKSIZE]; #if defined(AUDIO_STREAMING_DSP) -static char premix_left_buffer[SND_BLOCKSIZE]; -static char premix_right_buffer[SND_BLOCKSIZE]; -static int premix_last_buffer[SND_BLOCKSIZE]; +static short premix_left_buffer[SND_BLOCKSIZE]; +static short premix_right_buffer[SND_BLOCKSIZE]; +static long premix_last_buffer[SND_BLOCKSIZE]; #endif -static unsigned char playing_buffer[SND_BLOCKSIZE]; +static short playing_buffer[SND_BLOCKSIZE]; #endif /* forward declaration of internal functions */ #if defined(AUDIO_STREAMING_DSP) static void SoundServer_InsertNewSound(struct SoundControl); +static boolean InitAudioDevice_DSP(int, int); +#elif defined(PLATFORM_HPUX) +static boolean InitAudioDevice_HPUX(); #elif defined(PLATFORM_UNIX) static unsigned char linear_to_ulaw(int); static int ulaw_to_linear(unsigned char); -#endif - -#if defined(AUDIO_LINUX_IOCTL) -static boolean InitAudioDevice_Linux(); -#elif defined(PLATFORM_NETBSD) -static boolean InitAudioDevice_NetBSD(); -#elif defined(PLATFORM_HPUX) -static boolean InitAudioDevice_HPUX(); #elif defined(PLATFORM_MSDOS) static void SoundServer_InsertNewSound(struct SoundControl); static void SoundServer_StopSound(struct SoundControl); @@ -342,26 +337,16 @@ void SoundServer(void) if (playing_sounds || snd_ctrl.active) { - struct timeval delay = { 0, 0 }; - byte *sample_ptr; - long sample_size; - static long max_sample_size = 0; - static long fragment_size = DEFAULT_AUDIO_FRAGMENT_SIZE; - int sample_rate = DEFAULT_AUDIO_SAMPLE_RATE; - static boolean stereo = TRUE; - if (playing_sounds || (audio.device_fd = OpenAudioDevice(audio.device_name)) >= 0) { + struct timeval delay = { 0, 0 }; + static int fragment_size = DEFAULT_AUDIO_FRAGMENT_SIZE; + int sample_rate = DEFAULT_AUDIO_SAMPLE_RATE; + static boolean stereo = TRUE; + if (!playing_sounds) /* we just opened the audio device */ - { -#if defined(AUDIO_LINUX_IOCTL) - stereo = InitAudioDevice_Linux(fragment_size, sample_rate); -#elif defined(PLATFORM_NETBSD) - stereo = InitAudioDevice_NetBSD(fragment_size, sample_rate); -#endif - max_sample_size = fragment_size / (stereo ? 2 : 1); - } + stereo = InitAudioDevice_DSP(fragment_size, sample_rate); if (snd_ctrl.active) /* new sound has arrived */ SoundServer_InsertNewSound(snd_ctrl); @@ -370,12 +355,19 @@ void SoundServer(void) select(audio.soundserver_pipe[0] + 1, &sound_fdset, NULL, NULL, &delay) < 1) { + short *sample_ptr; + int sample_size; + int max_sample_size; + FD_SET(audio.soundserver_pipe[0], &sound_fdset); + max_sample_size = fragment_size / ((stereo ? 2 : 1) * sizeof(short)); + /* first clear the last premixing buffer */ - memset(premix_last_buffer, 0, fragment_size * sizeof(int)); + memset(premix_last_buffer, 0, + max_sample_size * (stereo ? 2 : 1) * sizeof(long)); - for(i=0;i> PSND_MAX_VOLUME_BITS; /* fill the last mixing buffer with stereo or mono sound */ if (stereo) { - int middle_pos = PSND_MAX_LEFT2RIGHT/2; - int left_volume = stereo_volume[middle_pos +playlist[i].stereo]; - int right_volume = stereo_volume[middle_pos -playlist[i].stereo]; + int middle_pos = PSND_MAX_LEFT2RIGHT / 2; + int left_volume = stereo_volume[middle_pos + playlist[i].stereo]; + int right_volume= stereo_volume[middle_pos - playlist[i].stereo]; for(j=0; j> PSND_MAX_LEFT2RIGHT_BITS; premix_right_buffer[j] = - (right_volume * (int)premix_first_buffer[j]) + (right_volume * premix_first_buffer[j]) >> PSND_MAX_LEFT2RIGHT_BITS; - premix_last_buffer[2*j+0] += premix_left_buffer[j]; - premix_last_buffer[2*j+1] += premix_right_buffer[j]; + + premix_last_buffer[2 * j + 0] += premix_left_buffer[j]; + premix_last_buffer[2 * j + 1] += premix_right_buffer[j]; } } else { - for(j=0;j255) - playing_buffer[i] = 255; + if (premix_last_buffer[i] < -65535) + playing_buffer[i] = -32767; + else if (premix_last_buffer[i] > 65535) + playing_buffer[i] = 32767; else - playing_buffer[i] = (premix_last_buffer[i]>>1)^0x80; + playing_buffer[i] = (short)(premix_last_buffer[i] >> 1); } /* finally play the sound fragment */ @@ -787,16 +782,17 @@ static void SoundServer_StopAllSounds() /* ------------------------------------------------------------------------- */ #if defined(AUDIO_LINUX_IOCTL) -static boolean InitAudioDevice_Linux(long fragment_size, int sample_rate) +static boolean InitAudioDevice_Linux(int fragment_size, int sample_rate) { /* "ioctl()" expects pointer to 'int' value for stereo flag (boolean is defined as 'char', which will not work here) */ + unsigned int fragment_spec = 0; + unsigned int audio_format = 0; + int fragment_size_query; int stereo = TRUE; - unsigned long fragment_spec = 0; /* determine logarithm (log2) of the fragment size */ - for (fragment_spec=0; (1 << fragment_spec) < fragment_size; - fragment_spec++); + for (fragment_spec=0; (1 << fragment_spec) < fragment_size; fragment_spec++); /* use two fragments (play one fragment, prepare the other); one fragment would result in interrupted audio output, more @@ -812,6 +808,11 @@ static boolean InitAudioDevice_Linux(long fragment_size, int sample_rate) Error(ERR_EXIT_SOUND_SERVER, "cannot set fragment size of /dev/dsp -- no sounds"); + audio_format = AFMT_S16_LE; + if (ioctl(audio.device_fd, SNDCTL_DSP_SETFMT, &audio_format) < 0) + Error(ERR_EXIT_SOUND_SERVER, + "cannot set audio format of /dev/dsp -- no sounds"); + /* try if we can use stereo sound */ if (ioctl(audio.device_fd, SNDCTL_DSP_STEREO, &stereo) < 0) { @@ -832,16 +833,19 @@ static boolean InitAudioDevice_Linux(long fragment_size, int sample_rate) "cannot set sample rate of /dev/dsp -- no sounds"); /* get the real fragmentation size; this should return 512 */ - if (ioctl(audio.device_fd, SNDCTL_DSP_GETBLKSIZE, &fragment_size) < 0) + if (ioctl(audio.device_fd, SNDCTL_DSP_GETBLKSIZE, &fragment_size_query) < 0) Error(ERR_EXIT_SOUND_SERVER, "cannot get fragment size of /dev/dsp -- no sounds"); + if (fragment_size_query != fragment_size) + Error(ERR_EXIT_SOUND_SERVER, + "cannot set fragment size of /dev/dsp -- no sounds"); return (boolean)stereo; } #endif /* AUDIO_LINUX_IOCTL */ #if defined(PLATFORM_NETBSD) -static boolean InitAudioDevice_NetBSD(long fragment_size, int sample_rate) +static boolean InitAudioDevice_NetBSD(int fragment_size, int sample_rate) { audio_info_t a_info; boolean stereo = TRUE; @@ -893,6 +897,19 @@ static boolean InitAudioDevice_HPUX() } #endif /* PLATFORM_HPUX */ +#if defined(PLATFORM_UNIX) +static boolean InitAudioDevice_DSP(int fragment_size, int sample_rate) +{ +#if defined(AUDIO_LINUX_IOCTL) + return InitAudioDevice_Linux(fragment_size, sample_rate); +#elif defined(PLATFORM_NETBSD) + return InitAudioDevice_NetBSD(fragment_size, sample_rate); +#elif defined(PLATFORM_HPUX) + return InitAudioDevice_HPUX(); +#endif +} +#endif /* PLATFORM_UNIX */ + #if defined(PLATFORM_UNIX) && !defined(AUDIO_STREAMING_DSP) /* these two are stolen from "sox"... :) */ @@ -1015,6 +1032,7 @@ static SoundInfo *Load_WAV(char *filename) byte sound_header_buffer[WAV_HEADER_SIZE]; char chunk_name[CHUNK_ID_LEN + 1]; int chunk_size; + short *data_ptr; FILE *file; int i; #endif @@ -1115,8 +1133,15 @@ static SoundInfo *Load_WAV(char *filename) return NULL; } + /* convert unsigned 8 bit sample data to signed 16 bit sample data */ + + data_ptr = checked_malloc(snd_info->data_len * sizeof(short)); + for (i=0; idata_len; i++) - ((byte *)snd_info->data_ptr)[i] = ((byte *)snd_info->data_ptr)[i] ^ 0x80; + data_ptr[i] = ((short)(((byte *)snd_info->data_ptr)[i] ^ 0x80)) << 8; + + free(snd_info->data_ptr); + snd_info->data_ptr = data_ptr; #endif /* PLATFORM_UNIX */