From d4056662a4c70bf2817d470694a3506bb35f0b7f Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Fri, 9 Mar 2007 22:50:48 +0000 Subject: Move the pcsound library to the top level, alongside textscreen. Subversion-branch: /trunk/chocolate-doom Subversion-revision: 849 --- Makefile.am | 2 +- configure.in | 2 +- pcsound/Makefile.am | 10 +++ pcsound/pcsound.c | 114 ++++++++++++++++++++++++++++ pcsound/pcsound.h | 45 +++++++++++ pcsound/pcsound_sdl.c | 179 ++++++++++++++++++++++++++++++++++++++++++++ pcsound/pcsound_win32.c | 115 ++++++++++++++++++++++++++++ src/Makefile.am | 6 +- src/i_pcsound.c | 2 +- src/pcsound/Makefile.am | 10 --- src/pcsound/pcsound.c | 114 ---------------------------- src/pcsound/pcsound.h | 45 ----------- src/pcsound/pcsound_sdl.c | 179 -------------------------------------------- src/pcsound/pcsound_win32.c | 115 ---------------------------- 14 files changed, 468 insertions(+), 470 deletions(-) create mode 100644 pcsound/Makefile.am create mode 100644 pcsound/pcsound.c create mode 100644 pcsound/pcsound.h create mode 100644 pcsound/pcsound_sdl.c create mode 100644 pcsound/pcsound_win32.c delete mode 100644 src/pcsound/Makefile.am delete mode 100644 src/pcsound/pcsound.c delete mode 100644 src/pcsound/pcsound.h delete mode 100644 src/pcsound/pcsound_sdl.c delete mode 100644 src/pcsound/pcsound_win32.c diff --git a/Makefile.am b/Makefile.am index 8e2a5e39..efa6b9fa 100644 --- a/Makefile.am +++ b/Makefile.am @@ -39,7 +39,7 @@ EXTRA_DIST= \ MAINTAINERCLEANFILES = $(AUX_DIST_GEN) docdir=$(prefix)/share/doc/@PACKAGE@ -SUBDIRS=textscreen src man setup +SUBDIRS=textscreen pcsound src man setup CMDLINE : src/ ./man/docgen -p $? > $@ diff --git a/configure.in b/configure.in index c43ab84c..2d2789ff 100644 --- a/configure.in +++ b/configure.in @@ -74,7 +74,7 @@ textscreen/examples/Makefile setup/Makefile man/Makefile src/Makefile -src/pcsound/Makefile +pcsound/Makefile src/chocolate-doom-res.rc setup/chocolate-setup-res.rc ]) diff --git a/pcsound/Makefile.am b/pcsound/Makefile.am new file mode 100644 index 00000000..77957d72 --- /dev/null +++ b/pcsound/Makefile.am @@ -0,0 +1,10 @@ + +AM_CFLAGS= @SDL_CFLAGS@ @SDLMIXER_CFLAGS@ + +noinst_LIBRARIES=libpcsound.a + +libpcsound_a_SOURCES = \ + pcsound.c pcsound.h \ + pcsound_sdl.c \ + pcsound_win32.c + diff --git a/pcsound/pcsound.c b/pcsound/pcsound.c new file mode 100644 index 00000000..9c18eb01 --- /dev/null +++ b/pcsound/pcsound.c @@ -0,0 +1,114 @@ +// Emacs style mode select -*- C++ -*- +//----------------------------------------------------------------------------- +// +// Copyright(C) 2007 Simon Howard +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +// 02111-1307, USA. +// +// DESCRIPTION: +// PC speaker interface. +// +//----------------------------------------------------------------------------- + +#include +#include +#include + +#include "pcsound.h" + +#ifdef _WIN32 +extern pcsound_driver_t pcsound_win32_driver; +#endif +extern pcsound_driver_t pcsound_sdl_driver; + +static pcsound_driver_t *drivers[] = +{ +#ifdef _WIN32 + &pcsound_win32_driver, +#endif + &pcsound_sdl_driver, + NULL, +}; + +static pcsound_driver_t *pcsound_driver = NULL; + +int PCSound_Init(pcsound_callback_func callback_func) +{ + char *driver_name; + int i; + + if (pcsound_driver != NULL) + { + return 1; + } + + // Check if the environment variable is set + + driver_name = getenv("PCSOUND_DRIVER"); + + if (driver_name != NULL) + { + for (i=0; drivers[i] != NULL; ++i) + { + if (!strcasecmp(drivers[i]->name, driver_name)) + { + // Found the driver! + + if (drivers[i]->init_func(callback_func)) + { + pcsound_driver = drivers[i]; + } + else + { + printf("Failed to initialise PC sound driver: %s\n", + drivers[i]->name); + break; + } + } + } + } + else + { + // Try all drivers until we find a working one + + for (i=0; drivers[i] != NULL; ++i) + { + if (drivers[i]->init_func(callback_func)) + { + pcsound_driver = drivers[i]; + break; + } + } + } + + if (pcsound_driver != NULL) + { + printf("Using PC sound driver: %s\n", pcsound_driver->name); + return 1; + } + else + { + printf("Failed to find a working PC sound driver.\n"); + return 0; + } +} + +void PCSound_Shutdown(void) +{ + pcsound_driver->shutdown_func(); + pcsound_driver = NULL; +} + diff --git a/pcsound/pcsound.h b/pcsound/pcsound.h new file mode 100644 index 00000000..1adee416 --- /dev/null +++ b/pcsound/pcsound.h @@ -0,0 +1,45 @@ +// Emacs style mode select -*- C++ -*- +//----------------------------------------------------------------------------- +// +// Copyright(C) 2007 Simon Howard +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +// 02111-1307, USA. +// +// DESCRIPTION: +// PC speaker interface. +// +//----------------------------------------------------------------------------- + +#ifndef PCSOUND_H +#define PCSOUND_H + +typedef struct pcsound_driver_s pcsound_driver_t; +typedef void (*pcsound_callback_func)(int *duration, int *frequency); +typedef int (*pcsound_init_func)(pcsound_callback_func callback); +typedef void (*pcsound_shutdown_func)(void); + +struct pcsound_driver_s +{ + char *name; + pcsound_init_func init_func; + pcsound_shutdown_func shutdown_func; +}; + +int PCSound_Init(pcsound_callback_func callback_func); +void PCSound_Shutdown(void); + +#endif /* #ifndef PCSOUND_H */ + diff --git a/pcsound/pcsound_sdl.c b/pcsound/pcsound_sdl.c new file mode 100644 index 00000000..50820394 --- /dev/null +++ b/pcsound/pcsound_sdl.c @@ -0,0 +1,179 @@ +// Emacs style mode select -*- C++ -*- +//----------------------------------------------------------------------------- +// +// Copyright(C) 2007 Simon Howard +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +// 02111-1307, USA. +// +// DESCRIPTION: +// PC speaker interface. +// +//----------------------------------------------------------------------------- + +#include +#include + +#include "SDL.h" +#include "SDL_mixer.h" + +#include "pcsound.h" + +#define SQUARE_WAVE_AMP 0x2000 + +static pcsound_callback_func callback; + +// Output sound format + +static int mixing_freq; +static Uint16 mixing_format; +static int mixing_channels; + +// Currently playing sound +// current_remaining is the number of remaining samples that must be played +// before we invoke the callback to get the next frequency. + +static int current_remaining; +static int current_freq; + +static int phase_offset = 0; + +// Mixer function that does the PC speaker emulation + +static void PCSound_Mix_Callback(void *udata, Uint8 *stream, int len) +{ + Sint16 *leftptr; + Sint16 *rightptr; + Sint16 this_value; + int oldfreq; + int i; + int nsamples; + + // Number of samples is quadrupled, because of 16-bit and stereo + + nsamples = len / 4; + + leftptr = (Sint16 *) stream; + rightptr = ((Sint16 *) stream) + 1; + + // Fill the output buffer + + for (i=0; i +#include + +#include "pcsound.h" + +static SDL_Thread *sound_thread_handle; +static int sound_thread_running; +static pcsound_callback_func callback; + +static int SoundThread(void *unused) +{ + int frequency; + int duration; + + while (sound_thread_running) + { + callback(&duration, &frequency); + + if (frequency != 0) + { + Beep(frequency, duration); + } + else + { + Sleep(duration); + } + } + + return 0; +} + +static int PCSound_Win32_Init(pcsound_callback_func callback_func) +{ + OSVERSIONINFO osvi; + BOOL result; + + // Temporarily disabled - the Windows scheduler is strange and + // stupid. + + return 0; + + // Find the OS version + + osvi.dwOSVersionInfoSize = sizeof(osvi); + + result = GetVersionEx(&osvi); + + if (!result) + { + return 0; + } + + // Beep() ignores its arguments on win9x, so this driver will + // not work there. + + if (osvi.dwPlatformId != VER_PLATFORM_WIN32_NT) + { + // TODO: Use _out() to write directly to the PC speaker on + // win9x: See PC/winsound.c in the Python standard library. + + return 0; + } + + // Start a thread to play sound. + + callback = callback_func; + sound_thread_running = 1; + + sound_thread_handle = SDL_CreateThread(SoundThread, NULL); + + return 1; +} + +static void PCSound_Win32_Shutdown(void) +{ + sound_thread_running = 0; + SDL_WaitThread(sound_thread_handle, NULL); +} + +pcsound_driver_t pcsound_win32_driver = +{ + "Windows", + PCSound_Win32_Init, + PCSound_Win32_Shutdown, +}; + +#endif /* #ifdef _WIN32 */ + diff --git a/src/Makefile.am b/src/Makefile.am index 53ef775c..4d694293 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -3,7 +3,7 @@ gamesdir = $(prefix)/games games_PROGRAMS = chocolate-doom chocolate-server -AM_CFLAGS = -I../textscreen @SDL_CFLAGS@ @SDLMIXER_CFLAGS@ @SDLNET_CFLAGS@ +AM_CFLAGS = -I../textscreen -I../pcsound @SDL_CFLAGS@ @SDLMIXER_CFLAGS@ @SDLNET_CFLAGS@ DEDSERV_FILES=\ d_dedicated.c \ @@ -22,8 +22,6 @@ z_native.c z_zone.h chocolate_server_SOURCES=$(DEDSERV_FILES) chocolate_server_LDADD = @LDFLAGS@ @SDL_LIBS@ @SDLNET_LIBS@ -SUBDIRS=pcsound - SOURCE_FILES=\ am_map.c am_map.h \ deh_ammo.c \ @@ -141,7 +139,7 @@ else chocolate_doom_SOURCES=$(SOURCE_FILES) endif -chocolate_doom_LDADD = ../textscreen/libtextscreen.a pcsound/libpcsound.a @LDFLAGS@ @SDL_LIBS@ @SDLMIXER_LIBS@ @SDLNET_LIBS@ +chocolate_doom_LDADD = ../textscreen/libtextscreen.a ../pcsound/libpcsound.a @LDFLAGS@ @SDL_LIBS@ @SDLMIXER_LIBS@ @SDLNET_LIBS@ EXTRA_DIST = \ chocolate_doom_icon.c \ diff --git a/src/i_pcsound.c b/src/i_pcsound.c index 926e5f1d..d9b76eec 100644 --- a/src/i_pcsound.c +++ b/src/i_pcsound.c @@ -35,7 +35,7 @@ #include "w_wad.h" #include "z_zone.h" -#include "pcsound/pcsound.h" +#include "pcsound.h" static boolean pcs_initialised = false; diff --git a/src/pcsound/Makefile.am b/src/pcsound/Makefile.am deleted file mode 100644 index 77957d72..00000000 --- a/src/pcsound/Makefile.am +++ /dev/null @@ -1,10 +0,0 @@ - -AM_CFLAGS= @SDL_CFLAGS@ @SDLMIXER_CFLAGS@ - -noinst_LIBRARIES=libpcsound.a - -libpcsound_a_SOURCES = \ - pcsound.c pcsound.h \ - pcsound_sdl.c \ - pcsound_win32.c - diff --git a/src/pcsound/pcsound.c b/src/pcsound/pcsound.c deleted file mode 100644 index 9c18eb01..00000000 --- a/src/pcsound/pcsound.c +++ /dev/null @@ -1,114 +0,0 @@ -// Emacs style mode select -*- C++ -*- -//----------------------------------------------------------------------------- -// -// Copyright(C) 2007 Simon Howard -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA -// 02111-1307, USA. -// -// DESCRIPTION: -// PC speaker interface. -// -//----------------------------------------------------------------------------- - -#include -#include -#include - -#include "pcsound.h" - -#ifdef _WIN32 -extern pcsound_driver_t pcsound_win32_driver; -#endif -extern pcsound_driver_t pcsound_sdl_driver; - -static pcsound_driver_t *drivers[] = -{ -#ifdef _WIN32 - &pcsound_win32_driver, -#endif - &pcsound_sdl_driver, - NULL, -}; - -static pcsound_driver_t *pcsound_driver = NULL; - -int PCSound_Init(pcsound_callback_func callback_func) -{ - char *driver_name; - int i; - - if (pcsound_driver != NULL) - { - return 1; - } - - // Check if the environment variable is set - - driver_name = getenv("PCSOUND_DRIVER"); - - if (driver_name != NULL) - { - for (i=0; drivers[i] != NULL; ++i) - { - if (!strcasecmp(drivers[i]->name, driver_name)) - { - // Found the driver! - - if (drivers[i]->init_func(callback_func)) - { - pcsound_driver = drivers[i]; - } - else - { - printf("Failed to initialise PC sound driver: %s\n", - drivers[i]->name); - break; - } - } - } - } - else - { - // Try all drivers until we find a working one - - for (i=0; drivers[i] != NULL; ++i) - { - if (drivers[i]->init_func(callback_func)) - { - pcsound_driver = drivers[i]; - break; - } - } - } - - if (pcsound_driver != NULL) - { - printf("Using PC sound driver: %s\n", pcsound_driver->name); - return 1; - } - else - { - printf("Failed to find a working PC sound driver.\n"); - return 0; - } -} - -void PCSound_Shutdown(void) -{ - pcsound_driver->shutdown_func(); - pcsound_driver = NULL; -} - diff --git a/src/pcsound/pcsound.h b/src/pcsound/pcsound.h deleted file mode 100644 index 1adee416..00000000 --- a/src/pcsound/pcsound.h +++ /dev/null @@ -1,45 +0,0 @@ -// Emacs style mode select -*- C++ -*- -//----------------------------------------------------------------------------- -// -// Copyright(C) 2007 Simon Howard -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA -// 02111-1307, USA. -// -// DESCRIPTION: -// PC speaker interface. -// -//----------------------------------------------------------------------------- - -#ifndef PCSOUND_H -#define PCSOUND_H - -typedef struct pcsound_driver_s pcsound_driver_t; -typedef void (*pcsound_callback_func)(int *duration, int *frequency); -typedef int (*pcsound_init_func)(pcsound_callback_func callback); -typedef void (*pcsound_shutdown_func)(void); - -struct pcsound_driver_s -{ - char *name; - pcsound_init_func init_func; - pcsound_shutdown_func shutdown_func; -}; - -int PCSound_Init(pcsound_callback_func callback_func); -void PCSound_Shutdown(void); - -#endif /* #ifndef PCSOUND_H */ - diff --git a/src/pcsound/pcsound_sdl.c b/src/pcsound/pcsound_sdl.c deleted file mode 100644 index 50820394..00000000 --- a/src/pcsound/pcsound_sdl.c +++ /dev/null @@ -1,179 +0,0 @@ -// Emacs style mode select -*- C++ -*- -//----------------------------------------------------------------------------- -// -// Copyright(C) 2007 Simon Howard -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA -// 02111-1307, USA. -// -// DESCRIPTION: -// PC speaker interface. -// -//----------------------------------------------------------------------------- - -#include -#include - -#include "SDL.h" -#include "SDL_mixer.h" - -#include "pcsound.h" - -#define SQUARE_WAVE_AMP 0x2000 - -static pcsound_callback_func callback; - -// Output sound format - -static int mixing_freq; -static Uint16 mixing_format; -static int mixing_channels; - -// Currently playing sound -// current_remaining is the number of remaining samples that must be played -// before we invoke the callback to get the next frequency. - -static int current_remaining; -static int current_freq; - -static int phase_offset = 0; - -// Mixer function that does the PC speaker emulation - -static void PCSound_Mix_Callback(void *udata, Uint8 *stream, int len) -{ - Sint16 *leftptr; - Sint16 *rightptr; - Sint16 this_value; - int oldfreq; - int i; - int nsamples; - - // Number of samples is quadrupled, because of 16-bit and stereo - - nsamples = len / 4; - - leftptr = (Sint16 *) stream; - rightptr = ((Sint16 *) stream) + 1; - - // Fill the output buffer - - for (i=0; i -#include - -#include "pcsound.h" - -static SDL_Thread *sound_thread_handle; -static int sound_thread_running; -static pcsound_callback_func callback; - -static int SoundThread(void *unused) -{ - int frequency; - int duration; - - while (sound_thread_running) - { - callback(&duration, &frequency); - - if (frequency != 0) - { - Beep(frequency, duration); - } - else - { - Sleep(duration); - } - } - - return 0; -} - -static int PCSound_Win32_Init(pcsound_callback_func callback_func) -{ - OSVERSIONINFO osvi; - BOOL result; - - // Temporarily disabled - the Windows scheduler is strange and - // stupid. - - return 0; - - // Find the OS version - - osvi.dwOSVersionInfoSize = sizeof(osvi); - - result = GetVersionEx(&osvi); - - if (!result) - { - return 0; - } - - // Beep() ignores its arguments on win9x, so this driver will - // not work there. - - if (osvi.dwPlatformId != VER_PLATFORM_WIN32_NT) - { - // TODO: Use _out() to write directly to the PC speaker on - // win9x: See PC/winsound.c in the Python standard library. - - return 0; - } - - // Start a thread to play sound. - - callback = callback_func; - sound_thread_running = 1; - - sound_thread_handle = SDL_CreateThread(SoundThread, NULL); - - return 1; -} - -static void PCSound_Win32_Shutdown(void) -{ - sound_thread_running = 0; - SDL_WaitThread(sound_thread_handle, NULL); -} - -pcsound_driver_t pcsound_win32_driver = -{ - "Windows", - PCSound_Win32_Init, - PCSound_Win32_Shutdown, -}; - -#endif /* #ifdef _WIN32 */ - -- cgit v1.2.3