ref: 1162b2c65c5bcff51e2d13aa2cfc2051529cfe68
parent: 63ff4879e6c7811d9e8cf357b4e44701964adf21
parent: f151517ba6b7e7caf7b49e8ceafbf0969959e068
author: Simon Howard <fraggle@gmail.com>
date: Fri Apr 30 16:53:31 EDT 2010
Merge from trunk. Subversion-branch: /branches/raven-branch Subversion-revision: 1924
--- a/.lvimrc
+++ b/.lvimrc
@@ -16,8 +16,9 @@
let tagfiles = findfile("tags", ".;", -1)
" Add tag files for libraries:
-call add(tagfiles, topdir . "textscreen/tags")
+call add(tagfiles, topdir . "opl/tags")
call add(tagfiles, topdir . "pcsound/tags")
+call add(tagfiles, topdir . "textscreen/tags")
for tagfile in tagfiles
" Don't go beyond the project top level when adding parent dirs:
--- a/BUGS
+++ b/BUGS
@@ -6,21 +6,6 @@
effects are cut off at the wrong distance. This needs further
investigation.
-* Music plays back differently.
-
- Vanilla Doom was typically played with a SoundBlaster (or compatible)
- card. It programmed the registers for the OPL music chip directly
- in order to emulate the various General MIDI instruments. However,
- Chocolate Doom uses the OS's native MIDI playback interfaces to play
- MIDI sound. As the OPL is programmed differently, the music sounds
- different to the original, even when using an original SoundBlaster
- card.
-
- This can be worked around in the future: OPL emulation code exists that
- simulates an OPL chip in software. Furthermore, depending on the OS,
- it may be possible to program the OPL directly in order to get the
- same sound.
-
* A small number of Doom bugs are almost impossible to emulate.
An example of this can be seen in Ledmeister's "Blackbug" demo which
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,812 @@
+2010-01-31 18:21:50 fraggle
+
+ Change Windows resource file to use PACKAGE_COPYRIGHT and
+ PACKAGE_LICENSE macros.
+
+2010-01-30 16:14:04 fraggle
+
+ Change directory to home directory before launching the game, so that
+ recorded demos go somewhere sensible.
+
+2010-01-30 16:04:24 fraggle
+
+ Set launch button as default button, so that it is possible to launch
+ the game by pressing return.
+
+2010-01-30 00:37:17 fraggle
+
+ Rename mus2mid functions to be consistent with coding standard.
+
+2010-01-29 23:28:35 fraggle
+
+ Remove unused PACKAGE_LONGDESC.
+
+2010-01-29 19:17:56 fraggle
+
+ When doing a MUS to MID conversion, allocate MIDI channels so that the
+ lowest-numbered MIDI channels are used before higher-numbered ones.
+ Fixes ear-piercing whistle sound in the MAP05 music when playing with
+ timidity and EAWPATS (thanks entryway / HackNeyed).
+
+2010-01-29 03:55:20 fraggle
+
+ Fix indentation/style etc. in mus2mid.c.
+
+2010-01-27 19:16:26 fraggle
+
+ Add tags files to svn:ignore properties.
+
+2010-01-26 19:21:18 fraggle
+
+ Minor fix of British spelling -> American.
+
+2010-01-26 19:18:18 fraggle
+
+ Fix glass hack windows where a linedef is flagged as two sided but has
+ only one side. Fixes WADs such as OTTAWAU.WAD (thanks Never_Again).
+
+2010-01-23 23:06:45 fraggle
+
+ Add menu item to launcher to open a terminal window that can be used
+ to start the game. Add missing 'edit' menu. Set svn:ignore property
+ for osx directory.
+
+2010-01-18 19:40:50 fraggle
+
+ Fix package source URL.
+
+2010-01-18 19:29:48 fraggle
+
+ Tweak package description slightly.
+
+2010-01-18 19:14:54 fraggle
+
+ Define project short description, copyright, maintainer and URL in
+ configure.in. Use these for the Info-gnustep.plist file. Add
+ generated .spec file for building RPM packages.
+
+2010-01-17 16:58:37 fraggle
+
+ Update NEWS.
+
+2010-01-17 16:31:03 fraggle
+
+ Restore the original cursor when shutting down video code, this should
+ hopefully fix the problem with the mouse cursor disappearing when
+ exiting on Win9x (thanks Janizdreg).
+
+2010-01-16 19:20:11 fraggle
+
+ Update TODO file.
+
+2010-01-15 19:29:28 fraggle
+
+ Don't open the configuration window when the launcher is first run;
+ display an error message if the user tries to launch the game without
+ an IWAD selected.
+
+2010-01-15 19:14:02 fraggle
+
+ Fix GNUstep info panel.
+
+2010-01-15 18:51:35 fraggle
+
+ Center the launcher window and config window on startup.
+
+2010-01-15 18:40:37 fraggle
+
+ Add wadfile.png for GNUstep build.
+
+2010-01-15 18:13:05 fraggle
+
+ Extend osx makefile to allow building of a working GNUstep
+ application, for testing purposes. Add GNUstep version of Info.plist,
+ remove app-skeleton directory and move contents up.
+
+2010-01-14 18:47:03 fraggle
+
+ In Chex Quest, use the radiation suit colormap instead of the red
+ colormaps that are usually used when taking damage (or using the
+ berserk pack). This matches the Vanilla chex.exe behavior (thanks
+ Fuzztooth).
+
+2010-01-12 20:16:25 fraggle
+
+ Oops.
+
+2010-01-12 20:15:34 fraggle
+
+ Strip executables when building Windows CE packages.
+
+2010-01-12 20:14:11 fraggle
+
+ Rearrange order of Makefile generation to alphabetical order.
+
+2010-01-12 20:12:56 fraggle
+
+ Move Makefile definitions for CC and STRIP into config.make, use
+ versions from autoconf.
+
+2010-01-12 20:09:54 fraggle
+
+ Set main menu title based on package name, not fixed string.
+
+2010-01-12 20:09:01 fraggle
+
+ Place commercial level name strings consecutively in the same array,
+ so that overflowing the end of one list accesses the start of the
+ next. This trick is used by pl2.wad for its MAP33 secret level.
+
+2010-01-12 01:32:24 fraggle
+
+ Add missing connection for plutonia.wad set button.
+
+2010-01-12 01:20:48 fraggle
+
+ Add document icon file and use for file associations.
+
+2010-01-11 19:10:42 fraggle
+
+ Insert new files into the command line at the correct location,
+ allowing multiple files to be opened at once.
+
+2010-01-11 01:35:04 fraggle
+
+ When launching a file from the finder, add the new file into the
+ command line at the appropriate position in the command line string.
+
+2010-01-10 20:46:15 fraggle
+
+ Change "@executable_path@" to "@executable_path"
+
+2010-01-10 18:48:21 fraggle
+
+ Install docs with a single cp, rather than using a for loop.
+
+2010-01-10 18:42:35 fraggle
+
+ Recursively copy library dependencies into destination package.
+ Identify libraries to be installed based on the path in which they are
+ located, rather than whether there is "libSDL" in the name. Use
+ install_name_tool to change the search path so that the system looks
+ for libraries in @executable_path@ rather than their location on the
+ machine upon which the program was built.
+
+2010-01-09 21:06:31 fraggle
+
+ Clear existing arguments when adding a file.
+
+2010-01-09 20:42:30 fraggle
+
+ Add file to command line when opened; add link from AppController to
+ LauncherManager.
+
+2010-01-09 18:54:04 fraggle
+
+ Initial code to identify file type by extension and add file to
+ command line.
+
+2010-01-09 18:38:48 fraggle
+
+ Hook in AppController as delegate for application, add file
+ associations to property list file.
+
+2010-01-05 17:20:58 fraggle
+
+ Add "clean" target to package makefiles.
+
+2010-01-05 15:52:12 fraggle
+
+ Move config.make up to pkg/ directory. Use static makefiles to
+ generate all packages, rather than dynamically generated makefiles.
+ Add pkg/osx to dist. Make OS X staging directory depend on top level
+ documentation files. Generate CMDLINE as part of standard build if it
+ is not already present. Set svn:ignore properties.
+
+2010-01-04 22:53:44 fraggle
+
+ Fix single space error when listing libraries.
+
+2010-01-04 22:45:45 fraggle
+
+ Copy binaries into app dir along with libraries.
+
+2010-01-04 22:24:48 fraggle
+
+ Include documentation files in package.
+
+2010-01-04 22:19:53 fraggle
+
+ Fix GNUstep build.
+
+2010-01-04 22:11:11 fraggle
+
+ Generate Info.plist and config.make in configure and remove temporary
+ versions. Include config.h from top level.
+
+2010-01-04 22:01:32 fraggle
+
+ Import OS X launcher code to trunk.
+
+2010-01-03 03:49:11 fraggle
+
+ Add quotes around $@ in autogen script (thanks exp[x])
+
+2009-12-28 20:57:20 fraggle
+
+ When recording low resolution (non-longtics) Vanilla demos, carry
+ forward the error from angleturn caused by the reduced resolution, so
+ that consecutive errors can accumulate, possibly making turning
+ slightly smoother.
+
+2009-12-27 01:42:13 fraggle
+
+ Oops.
+
+2009-12-27 00:11:18 fraggle
+
+ Allow DOOMWADDIR/DOOMWADPATH to contain the complete path to IWAD
+ files, as well as directories in which to search for IWAD files.
+
+2009-12-18 22:11:06 fraggle
+
+ Fix poor quality application icons seen when the game is running. Add
+ back 8-bit icon files alongside files including both 8-bit and high
+ quality 32-bit versions. Use the high quality icon files for resource
+ files includes, and the low quality ones for in-game SDL.
+
+2009-12-18 21:11:32 fraggle
+
+ Update generated source files containing icon data.
+
+2009-12-18 21:10:35 fraggle
+
+ Make ExecuteCommand() under Unix return a failure when the executable
+ cannot be executed.
+
+2009-12-14 20:57:04 fraggle
+
+ Use GetModuleFileNameW to get the (Unicode) path to the Doom
+ executable. This hopefully fixes problems with Unicode directory
+ names.
+
+2009-12-14 18:54:25 fraggle
+
+ Add Chocolate Doom/setup icons with scaled versions for various
+ different icon sizes (thanks MikeRS).
+
+2009-12-12 01:20:49 fraggle
+
+ Fix textscreen black border bug.
+
+2009-12-09 02:40:39 fraggle
+
+ Fix the setup tool on Windows Vista/7 to not prompt for elevated
+ permissions and to disable the "Program Compatibility Assistant"
+ (thanks hobbs and MikeRS).
+
+2009-11-29 22:50:17 fraggle
+
+ Add other missing files to dist.
+
+2009-11-29 22:25:51 fraggle
+
+ Include .lvimrc in dist.
+
+2009-11-21 03:56:59 fraggle
+
+ Add Makefile to build Win32 packages.
+
+2009-11-21 02:05:56 fraggle
+
+ Use execvp() rather than execv(), to look up Doom binary in the PATH
+ if necessary.
+
+2009-11-21 00:40:58 fraggle
+
+ Apply configuration file invalid key setting fix to setup code.
+
+2009-11-21 00:38:16 fraggle
+
+ Don't crash if key settings are set in a configuration file that are
+ out of range (thanks entryway).
+
+2009-11-21 00:24:59 fraggle
+
+ Fix crash with chocolate-setup under Windows (thanks Janizdreg).
+
+2009-11-19 21:49:13 fraggle
+
+ Rework the OS X MIDI disabling code, as SDL_mixer 1.2.11 fixes the
+ crash. Check and disable MIDI by default if using an older version of
+ SDL on OS X.
+
+2009-11-19 21:07:31 fraggle
+
+ Make chocolate-setup use its own location in the filesystem to find
+ the location of the chocolate-doom executable. Remove INSTALL_DIR
+ define.
+
+2009-11-05 19:57:55 fraggle
+
+ Perform bounds checking on values passed to TXT_UpdateScreenArea() to
+ avoid crashes.
+
+2009-10-26 19:28:12 fraggle
+
+ Initial hacks for compiling under SDL 1.3.
+
+2009-10-17 21:13:54 fraggle
+
+ Fix error in last change.
+
+2009-10-17 20:39:37 fraggle
+
+ Use M_StrToInt() when processing values passed with -spechit, so that
+ hex values can be specified.
+
+2009-10-17 20:29:46 fraggle
+
+ Import donut overrun emulation code from PrBoom+ (Thanks entryway).
+
+2009-10-16 19:10:30 fraggle
+
+ Fix compilation under MSVC (thanks entryway).
+
+2009-10-10 23:58:25 fraggle
+
+ Rename pkg/wince/Makefile to pkg/wince/GNUmakefile (it uses GNU
+ extensions).
+
+2009-10-10 22:46:14 fraggle
+
+ Add pkg directory to make dist.
+
+2009-10-10 02:02:58 fraggle
+
+ Don't crash when using the donut special type and the joining linedef
+ is one sided (thanks Alexander Waldmann).
+
+2009-10-05 21:25:53 fraggle
+
+ Fix desync in ep1-0500.lmp on 64-bit (thanks exp(x)).
+
+2009-10-05 00:38:14 fraggle
+
+ Provide pointer to STARTUPINFO structure when calling CreateProcessW,
+ to stop crash under normal Windows (not CE) when launching Doom from
+ the setup tools (thanks Janizdreg).
+
+2009-10-01 20:08:21 fraggle
+
+ Oops.
+
+2009-10-01 02:04:00 fraggle
+
+ Oops.
+
+2009-10-01 00:07:03 fraggle
+
+ Change British English spellings to American English, for consistency.
+
+2009-09-20 16:27:40 fraggle
+
+ Use "const char" in libtextscreen where appropriate (thanks entryway).
+
+2009-09-11 22:56:47 fraggle
+
+ Add (lack of) copyright notice for SDL workaround.
+
+2009-09-07 20:43:04 fraggle
+
+ Fix compilation under MacOS X.
+
+2009-09-06 19:15:52 fraggle
+
+ Fixes for MSVC compile (thanks entryway).
+
+2009-08-28 00:27:47 fraggle
+
+ Allow PGUP/PGDN to scroll up and down in scroll panes (thanks
+ LionsPhil).
+
+2009-07-20 23:27:59 fraggle
+
+ Remove redundant variable assignment (thanks Quasar/Yagisan)
+
+2009-07-20 01:37:41 fraggle
+
+ Save and display the loading disk icon as a fixed 16x16 square, from
+ an image drawn at the bottom right corner of the screen. This seems
+ to be the same as how Vanilla behaves, and fixes chook3.wad, that uses
+ an STDISK replacement with an offset that pushes the image to the
+ left.
+
+2009-07-13 23:43:06 fraggle
+
+ Add stdio.h include to fix MSVC build (thanks Kaiser)
+
+2009-07-12 17:47:12 fraggle
+
+ Fix compile with libsamplerate.
+
+2009-07-12 15:00:50 fraggle
+
+ On Windows CE, use the Windows API to find the amount of available
+ memory, so that at least two megabytes are always left available to
+ the OS.
+
+2009-07-11 12:15:32 fraggle
+
+ Add missing item to NEWS.
+
+2009-07-07 20:46:55 fraggle
+
+ Update NEWS.
+
+2009-07-07 20:38:00 fraggle
+
+ Fix launching of the game from the setup tool in Windows CE.
+
+2009-06-21 20:33:35 fraggle
+
+ Add Makefile for building CAB files, dependency calculation.
+
+2009-06-21 20:19:43 fraggle
+
+ Use correct filename for SDL_net DLL.
+
+2009-06-21 20:03:38 fraggle
+
+ Remove temporary files after generating CAB file.
+
+2009-06-20 23:13:44 fraggle
+
+ Add script to generate Windows CE install package.
+
+2009-06-16 20:47:13 fraggle
+
+ Automatically allocate a smaller zone size if it was not possible to
+ allocate the default 16 MiB.
+
+2009-06-13 18:10:18 fraggle
+
+ Don't post zero key events.
+
+2009-06-12 20:07:55 fraggle
+
+ On Windows CE systems without a keyboard, patch the default settings
+ to use hardware keys.
+
+2009-06-12 18:58:42 fraggle
+
+ Remove debug messages.
+
+2009-06-12 18:35:39 fraggle
+
+ Set the USER environment variable based on the owner information from
+ the registry.
+
+2009-06-12 18:34:27 fraggle
+
+ Always grab input on Windows CE.
+
+2009-06-11 22:34:36 fraggle
+
+ Include libc_wince.a in chocolate-server build.
+
+2009-06-11 20:41:20 fraggle
+
+ Grab the input in setup when reading a new key binding, so that
+ Windows CE buttons are read properly. Map buttons to PC function
+ keys.
+
+2009-06-11 19:19:05 fraggle
+
+ Include libc_wince.h on Windows CE.
+
+2009-06-11 19:18:12 fraggle
+
+ Declare getenv/putenv on Windows CE for recent SDL versions that do
+ not declare it.
+
+2009-06-10 20:03:08 fraggle
+
+ Add key bindings for pause, message refresh.
+
+2009-06-08 20:26:29 fraggle
+
+ Remove debugging code.
+
+2009-06-08 19:15:57 fraggle
+
+ Use SDL's getenv/putenv implementation, and populate at startup.
+
+2009-06-08 00:41:10 fraggle
+
+ Use CreateFileW instead of OpenFile (doesn't exist on Windows CE)
+
+2009-06-07 20:08:08 fraggle
+
+ Fix header includes (thanks exp[x])
+
+2009-06-07 19:18:02 fraggle
+
+ Don't add DirectX/Windib selector on Windows CE.
+
+2009-06-07 18:53:25 fraggle
+
+ Use home dir to store configuration and savegames under Windows CE.
+
+2009-06-07 18:33:19 fraggle
+
+ Fix setup tool display configuration dialog when fullscreen is not
+ supported.
+
+2009-06-07 18:10:05 fraggle
+
+ Make auto-adjust code switch to windowed mode if no fullscreen modes
+ are available.
+
+2009-06-07 17:41:46 fraggle
+
+ Catch errors when initialising SDL. Use the small textscreen font by
+ default on Windows CE if no fullscreen modes are available.
+
+2009-06-07 17:39:08 fraggle
+
+ Add missing SDL_thread include.
+
+2009-06-07 17:35:43 fraggle
+
+ Don't try to use the SDL DirectX driver under Windows CE.
+
+2009-06-07 16:21:41 fraggle
+
+ Fix setup tool compile on Windows CE.
+
+2009-06-07 16:15:40 fraggle
+
+ Remove call to setbuf.
+
+2009-06-07 15:35:27 fraggle
+
+ Add IWAD search dirs for Windows CE.
+
+2009-06-07 15:20:46 fraggle
+
+ Exit with an error on failure to allocate zone memory.
+
+2009-06-07 03:10:21 fraggle
+
+ Use MessageBoxW instead of MessageBox (doesn't exist on Windows CE)
+
+2009-06-07 02:59:49 fraggle
+
+ Add README file for Windows CE library.
+
+2009-06-07 02:56:21 fraggle
+
+ Detect Windows CE target and build/include libc_wince files as
+ necessary.
+
+2009-06-07 02:50:47 rtc_marine
+
+ - Update textscreen codeblocks project to include txt_scrollpane.* and
+ txt_smallfont.h
+
+2009-06-07 02:33:58 fraggle
+
+ Include libc_wince.h when on Windows CE.
+
+2009-06-07 02:32:15 fraggle
+
+ Add CPU affinity function for Windows CE.
+
+2009-06-07 02:27:58 fraggle
+
+ Add libc_wince.h header, and EISDIR error value.
+
+2009-06-07 02:27:30 fraggle
+
+ Use GetUserNameExW, not GetUserName (doesn't exist on WinCE)
+
+2009-06-07 02:26:45 fraggle
+
+ Fix compile with FEATURE_MULTIPLAYER disabled.
+
+2009-06-07 02:24:40 fraggle
+
+ Fix compile with FEATURE_SOUND disabled.
+
+2009-06-07 01:56:23 fraggle
+
+ Add Windows CE implementations of some ANSI C functions that are
+ missing.
+
+2009-06-06 22:13:44 fraggle
+
+ Don't check for Steam/CD installer versions on Windows CE.
+
+2009-06-05 17:58:48 fraggle
+
+ Add key binding variables for automap and weapon keys.
+
+2009-06-04 00:37:02 fraggle
+
+ Increase height of menu bindings dialog.
+
+2009-06-04 00:35:05 fraggle
+
+ Use newer keyboard bindings dialog layout from raven-branch.
+
+2009-06-04 00:20:37 fraggle
+
+ Add unique key groups for menu navigation and shortcuts.
+
+2009-06-04 00:20:06 fraggle
+
+ Use key for confirming menu messages, not typed char.
+
+2009-06-03 21:45:54 fraggle
+
+ Add dialog to setup tool for editing menu shortcuts.
+
+2009-06-03 21:18:04 fraggle
+
+ Add config file variables to increase/decrease screen size.
+
+2009-06-03 20:59:26 fraggle
+
+ Fix shortcut keys for menu items.
+
+2009-06-03 20:55:50 fraggle
+
+ Add configuration file entries for menu key bindings.
+
+2009-06-03 20:37:19 fraggle
+
+ Add key_ variables for the keys used to control the menu.
+
+2009-05-26 23:14:24 fraggle
+
+ Fix tags for functions using TXT_UNCAST_ARG.
+
+2009-05-26 22:13:18 fraggle
+
+ Set appropriate vim 'tags' variable for ctags files.
+
+2009-05-21 20:18:38 fraggle
+
+ Set display settings window position based on screen dimensions,
+ rather than hard coding position.
+
+2009-05-19 18:07:49 fraggle
+
+ Fix manpage documentation for DOOMWADPATH (thanks MikeRS)
+
+2009-05-18 19:30:49 fraggle
+
+ Fix A_BossDeath behavior in v1.9 emulation mode (thanks entryway)
+
+2009-05-17 14:54:19 fraggle
+
+ Always use an SDL buffer size that is a power of two. Reduce buffer
+ size to 70ms.
+
+2009-05-12 19:03:20 fraggle
+
+ Add option to "join game" dialog in setup tool to autojoin a LAN game.
+
+2009-05-12 19:01:27 fraggle
+
+ Make txt_inputboxes emit a "changed" signal when their value is
+ changed.
+
+2009-05-07 22:59:38 fraggle
+
+ Calculate SDL buffer size automatically based on sample rate.
+
+2009-05-05 01:00:53 fraggle
+
+ Better ASCII chart.
+
+2009-05-05 00:46:27 fraggle
+
+ Minor smallfont fixups.
+
+2009-05-01 22:05:57 fraggle
+
+ Add copyright headers to textscreen examples.
+
+2009-04-26 17:59:08 fraggle
+
+ More smallfont fixups.
+
+2009-04-23 20:58:11 fraggle
+
+ Fix up some extended ASCII characters.
+
+2009-04-23 19:19:52 fraggle
+
+ Oops.
+
+2009-04-23 19:18:43 fraggle
+
+ Add small textscreen font for low resolution displays, based on the
+ Atari-Small font by Tom Fine.
+
+2009-03-15 14:44:23 fraggle
+
+ Fix clipped sounds when using libsamplerate (thanks David Flater)
+
+2009-03-14 15:28:41 fraggle
+
+ Add check to allow sched_setaffinity code to work on older versions of
+ libc.
+
+2009-03-12 18:55:27 fraggle
+
+ Define INVALID_SET_FILE_POINTER if it is not defined, to fix
+ compilation under MSVC6 (thanks Quasar)
+
+2009-03-08 22:51:25 fraggle
+
+ Add "make doc" target to run Doxygen, and add a Doxyfile. Add @file
+ tags to start of header files so that Doxygen will process them.
+
+2009-03-07 00:35:08 fraggle
+
+ Add documentation for high-level txt_desktop.h functions.
+
+2009-03-07 00:24:45 fraggle
+
+ Add documentation for high-level textscreen functions.
+
+2009-03-06 20:01:32 fraggle
+
+ Fix signed/unsigned conversion warning.
+
+2009-03-03 19:26:20 fraggle
+
+ Look up SetProcessAffinityMask function at runtime, so that the
+ program should work under Win9x again.
+
+2009-01-30 23:53:47 fraggle
+
+ Fix layout of widgets within scroll panes. Scroll scroll panes in
+ response to keyboard events.
+
+2009-01-29 23:26:03 fraggle
+
+ Shrink text box slightly.
+
+2009-01-29 23:00:14 fraggle
+
+ Allow clicking within scroll bars to set position.
+
+2009-01-29 22:54:13 fraggle
+
+ Add scrollable pane widget to textscreen library.
+
+2009-01-17 14:05:31 fraggle
+
+ Fix '-mmap' command line parameter.
+
+2009-01-07 22:05:13 fraggle
+
+ Create the ~/.chocolate-doom/savegames directory on startup if it does
+ not exist.
+
+2009-01-07 21:51:37 fraggle
+
+ Replace -nommap with -mmap; do not use mmap()ed file access by
+ default. Fixes Plutonia 2, and several other minor things.
+
+2008-12-10 20:25:05 fraggle
+
+ Bump version to 1.2.1, update NEWS and ChangeLog.
+
2008-12-10 20:20:10 fraggle
Fix crash when playing Doom 1 levels.
--- a/Makefile.am
+++ b/Makefile.am
@@ -50,6 +50,7 @@
config.h \
CMDLINE \
HACKING \
+ README.OPL \
TODO \
BUGS \
rpm.spec
@@ -57,7 +58,9 @@
MAINTAINERCLEANFILES = $(AUX_DIST_GEN)
docdir=$(prefix)/share/doc/@PACKAGE@
-SUBDIRS=wince textscreen pcsound src man
+
+SUBDIRS=wince textscreen opl pcsound src man
+
DIST_SUBDIRS=pkg $(SUBDIRS)
if HAVE_PYTHON
--- a/NEWS
+++ b/NEWS
@@ -1,4 +1,4 @@
-...
+1.3.0 (2010-02-10):
* Chocolate Doom now runs on Windows Mobile/Windows CE!
* It is possible to rebind most/all of the keys that control the
@@ -35,6 +35,8 @@
* When recording shorttics demos, errors caused by the reduced
turning resolution are carried forward, possibly making turning
smoother.
+ * The source tarball can now be used to build an RPM package:
+ rpmbuild -tb chocolate-doom-VER.tar.gz
Compatibility:
* The A_BossDeath behavior in v1.9 emulation mode was fixed
--- /dev/null
+++ b/README.OPL
@@ -1,0 +1,107 @@
+== Chocolate Doom hardware OPL support notes ==
+
+Chocolate Doom is able to play MIDI music as it sounds in Vanilla Doom
+with an OPL chip (as found in the Yamaha Adlib card, the Sound Blaster
+and its clones). Most modern computers do not include an OPL chip any
+more, as CPUs are fast enough to do decent software MIDI synthesis.
+For this reason, a software OPL emulator is included as a substitute.
+
+However, no software emulator sounds exactly like a real (hardware)
+OPL chip, so if you do have a sound card with hardware OPL, here's how
+to configure Chocolate Doom to use it.
+
+=== Sound cards with OPL chips ===
+
+If you have an ISA sound card, it almost certainly includes an OPL
+chip. Modern computers don't have slots for ISA cards though, so you
+must be running a pretty old machine.
+
+If you have a PCI sound card, you probably don't have an OPL chip.
+However, there are some exceptions to this. The following cards are
+known to include "legacy" OPL support:
+
+ * C-Media CMI8738 (*)
+ * Forte Media FM801
+ * Cards based on the Yamaha YMF724 (*)
+
+Other cards that apparently have OPL support but have not been tested:
+
+ * S3 SonicVibes
+ * AZTech PCI 168 (AZT 3328 chipset)
+ * ESS Solo-1 sound cards (ES1938, ES1946, ES1969 chipset)
+ * Conexant Riptide Audio/Modem combo cards
+ * Cards based on the Crystal Semiconductors CS4281
+ * Cards based on the Avance Logic ALS300
+ * Cards based on the Avance Logic ALS4000
+
+If you desperately want hardware OPL music, you may be able to find
+one of these cards for sale cheap on eBay.
+
+For the cards listed above with (*) next to them, OPL support is
+disabled by default and must be explictly enabled in software.
+
+If your machine is not a PC, you don't have an OPL chip, and you will
+have to use the software OPL.
+
+=== Operating System support ===
+
+If you're certain that you have a sound card with hardware OPL, you
+may need to take extra steps to configure your operating system to
+allow access to it. To do hardware OPL, Chocolate Doom must access
+the chip directly, which is usually not possible in modern operating
+systems unless you are running as the superuser (root/Administrator).
+
+=== Windows 9x ===
+
+If you're running Windows 95, 98 or Me, there is no need to configure
+anything. Windows allows direct access to the OPL chip. You can
+confirm that hardware OPL is working by checking for this message in
+stdout.txt:
+
+ OPL_Init: Using driver 'Win32'.
+
+=== Windows NT (including 2000, XP and later) ===
+
+If you're running an NT-based system, it is not possible to directly
+access the OPL chip, even when running as Administrator. Fortunately,
+it is possible to use the "ioperm.sys" driver developed for Cygwin:
+
+ http://openwince.sourceforge.net/ioperm/
+
+It is not necessary to have Cygwin installed to use this. Copy the
+ioperm.sys file into the same directory as the Chocolate Doom
+executable and it should be automatically loaded.
+
+You can confirm that hardware OPL is working by checking for this
+message in stdout.txt:
+
+ OPL_Init: Using driver 'Win32'.
+
+=== Linux ===
+
+If you are using a system based on the Linux kernel, you can access
+the OPL chip directly, but you must be running as root. You can
+confirm that hardware OPL is working, by checking for this message on
+startup:
+
+ OPL_Init: Using driver 'Linux'.
+
+If you are using one of the PCI cards in the list above with a (*)
+next to it, you may need to manually enable FM legacy support. Add
+the following to your /etc/modprobe.conf file to do this:
+
+ options snd-ymfpci fm_port=0x388
+ options snd-cmipci fm_port=0x388
+
+=== OpenBSD/NetBSD ===
+
+You must be running as root to access the hardware OPL directly. You
+can confirm that hadware OPL is working by checking for this message
+on startup:
+
+ OPL_Init: Using driver 'OpenBSD'.
+
+=== FreeBSD ===
+
+There is no native OPL backend for FreeBSD yet. Sorry!
+
--- a/TODO
+++ b/TODO
@@ -1,6 +1,5 @@
Currently in progress:
-* OPL MIDI playback (see: opl-branch)
* Heretic/Hexen support (see: raven-branch)
* Strife support (see: strife-branch)
@@ -35,4 +34,23 @@
automatically download and play speedruns.
* DWANGO-like interface for finding players and setting up games.
* Video capture mode?
+
+== OPL TODO list ==
+
+Needs research:
+
+ * Strategy when no more voices are available is still wrong
+ * Scale levels don't exactly match Vanilla (off-by-one?)
+
+Bad MIDIs:
+
+ * doom2.wad MAP01
+ * gothicdm MAP05
+ * tnt.wad MAP30
+ * Alien Vendetta (title screen, MAP01, etc)
+
+Other tasks:
+
+ * Get a better software OPL emulator
+ * DMXOPTIONS opl3/phase option support.
--- a/codeblocks/config.h
+++ b/codeblocks/config.h
@@ -9,19 +9,19 @@
#define PACKAGE_NAME "Chocolate Doom"
/* Define to the full name and version of this package. */
-#define PACKAGE_STRING "Chocolate Doom 1.2.1"
+#define PACKAGE_STRING "Chocolate Doom 1.3.0"
/* Define to the one symbol short name of this package. */
#define PACKAGE_TARNAME "chocolate-doom"
/* Define to the version of this package. */
-#define PACKAGE_VERSION "1.2.1"
+#define PACKAGE_VERSION "1.3.0"
/* Define to 1 if you have the ANSI C header files. */
#define STDC_HEADERS 1
/* Version number of package */
-#define VERSION "1.2.1"
+#define VERSION "1.3.0"
/* Define to 1 if your processor stores words with the most significant byte
first (like Motorola and SPARC, unlike Intel and VAX). */
--- a/codeblocks/game-res.rc
+++ b/codeblocks/game-res.rc
@@ -1,8 +1,8 @@
1 ICON "../data/doom.ico"
1 VERSIONINFO
-PRODUCTVERSION 1,2,1,0
-FILEVERSION 1,2,1,0
+PRODUCTVERSION 1,3,0,0
+FILEVERSION 1,3,0,0
FILETYPE 1
{
BLOCK "StringFileInfo"
@@ -9,13 +9,13 @@
{
BLOCK "040904E4"
{
- VALUE "FileVersion", "1.2.1"
- VALUE "FileDescription", "1.2.1"
+ VALUE "FileVersion", "1.3.0"
+ VALUE "FileDescription", "1.3.0"
VALUE "InternalName", "Chocolate-Doom"
VALUE "CompanyName", "Chocolate-Doom"
VALUE "LegalCopyright", "GNU General Public License"
VALUE "ProductName", "Chocolate-Doom"
- VALUE "ProductVersion", "1.2.1"
+ VALUE "ProductVersion", "1.3.0"
}
}
}
--- a/codeblocks/setup-res.rc
+++ b/codeblocks/setup-res.rc
@@ -3,8 +3,8 @@
CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "setup-manifest.xml"
1 VERSIONINFO
-PRODUCTVERSION 1,2,1,0
-FILEVERSION 1,2,1,0
+PRODUCTVERSION 1,3,0,0
+FILEVERSION 1,3,0,0
FILETYPE 1
{
BLOCK "StringFileInfo"
@@ -11,13 +11,13 @@
{
BLOCK "040904E4"
{
- VALUE "FileVersion", "1.2.1"
+ VALUE "FileVersion", "1.3.0"
VALUE "FileDescription", "Chocolate-Doom Setup"
VALUE "InternalName", "chocolate-setup"
VALUE "CompanyName", "fraggle@gmail.com"
VALUE "LegalCopyright", "GNU General Public License"
VALUE "ProductName", "Chocolate-Doom Setup"
- VALUE "ProductVersion", "1.2.1"
+ VALUE "ProductVersion", "1.3.0"
}
}
}
--- a/configure.in
+++ b/configure.in
@@ -1,4 +1,4 @@
-AC_INIT(Chocolate Doom, 1.2.1, fraggle@gmail.com, chocolate-doom)
+AC_INIT(Chocolate Doom, 1.3.0, fraggle@gmail.com, chocolate-doom)
PACKAGE_SHORTDESC="Conservative Doom source port"
PACKAGE_COPYRIGHT="Copyright (C) 1993-2010"
@@ -76,7 +76,13 @@
AC_CHECK_LIB(samplerate, src_new)
AC_CHECK_HEADERS([linux/kd.h dev/isa/spkrio.h dev/speaker/speaker.h])
- AC_CHECK_FUNCS(mmap sched_setaffinity)
+ AC_CHECK_FUNCS(mmap sched_setaffinity ioperm)
+
+ # OpenBSD I/O i386 library for I/O port access.
+ # (64 bit has the same thing with a different name!)
+
+ AC_CHECK_LIB(i386, i386_iopl)
+ AC_CHECK_LIB(amd64, amd64_iopl)
])
AC_CHECK_TOOL(WINDRES, windres, )
@@ -138,6 +144,8 @@
AC_OUTPUT([
Makefile
man/Makefile
+opl/Makefile
+opl/examples/Makefile
pcsound/Makefile
pkg/Makefile
pkg/config.make
--- a/man/manpage.template
+++ b/man/manpage.template
@@ -28,6 +28,17 @@
options are "Linux" for the Linux console mode driver, "BSD" for the
NetBSD/OpenBSD PC speaker driver, and "SDL" for SDL-based emulated PC speaker
playback (using the digital output).
+.TP
+\fBOPL_DRIVER\fR
+When using OPL MIDI playback, this environment variable specifies an
+OPL backend driver to use. Valid options are "SDL" for an SDL-based
+software emulated OPL chip, "Linux" for the Linux hardware OPL driver,
+and "OpenBSD" for the OpenBSD/NetBSD hardware OPL driver.
+
+Generally speaking, a real hardware OPL chip sounds better than software
+emulation; however, modern machines do not often include one. If
+present, it may still require extra work to set up and elevated
+security privileges to access.
.SH FILES
.TP
\fB$HOME/.chocolate-doom/default.cfg\fR
--- a/msvc/config.h
+++ b/msvc/config.h
@@ -11,16 +11,16 @@
#define PACKAGE_NAME "Chocolate Doom"
/* Define to the full name and version of this package. */
-#define PACKAGE_STRING "Chocolate Doom 1.2.1"
+#define PACKAGE_STRING "Chocolate Doom 1.3.0"
/* Define to the one symbol short name of this package. */
#define PACKAGE_TARNAME "chocolate-doom"
/* Define to the version of this package. */
-#define PACKAGE_VERSION "1.2.1"
+#define PACKAGE_VERSION "1.3.0"
/* Version number of package */
-#define VERSION "1.2.1"
+#define VERSION "1.3.0"
/* Define to 1 if your processor stores words with the most significant byte
first (like Motorola and SPARC, unlike Intel and VAX). */
--- a/msvc/win32.rc
+++ b/msvc/win32.rc
@@ -32,8 +32,8 @@
#endif
1 VERSIONINFO
-PRODUCTVERSION 1,2,1,0
-FILEVERSION 1,2,1,0
+PRODUCTVERSION 1,3,0,0
+FILEVERSION 1,3,0,0
FILETYPE 1
BEGIN
BLOCK "StringFileInfo"
@@ -40,13 +40,13 @@
BEGIN
BLOCK "040904E4"
BEGIN
- VALUE "FileVersion", "1.2.1"
- VALUE "FileDescription", "Chocolate Doom 1.2.1"
+ VALUE "FileVersion", "1.3.0"
+ VALUE "FileDescription", "Chocolate Doom 1.3.0"
VALUE "InternalName", "chocolate-doom"
VALUE "CompanyName", "fraggle@gmail.com"
VALUE "LegalCopyright", "GNU General Public License"
VALUE "ProductName", "Chocolate Doom"
- VALUE "ProductVersion", "1.2.1"
+ VALUE "ProductVersion", "1.3.0"
END
END
END
--- /dev/null
+++ b/opl/.gitignore
@@ -1,0 +1,7 @@
+Makefile.in
+Makefile
+.deps
+libopl.a
+*.rc
+tags
+TAGS
--- /dev/null
+++ b/opl/Makefile.am
@@ -1,0 +1,19 @@
+
+AM_CFLAGS=@SDLMIXER_CFLAGS@
+
+SUBDIRS = . examples
+
+noinst_LIBRARIES=libopl.a
+
+libopl_a_SOURCES = \
+ opl_internal.h \
+ opl.c opl.h \
+ opl_linux.c \
+ opl_obsd.c \
+ opl_queue.c opl_queue.h \
+ opl_sdl.c \
+ opl_timer.c opl_timer.h \
+ opl_win32.c \
+ ioperm_sys.c ioperm_sys.h \
+ fmopl.c fmopl.h
+
--- /dev/null
+++ b/opl/examples/.gitignore
@@ -1,0 +1,7 @@
+Makefile.in
+Makefile
+.deps
+droplay
+*.exe
+tags
+TAGS
--- /dev/null
+++ b/opl/examples/Makefile.am
@@ -1,0 +1,8 @@
+
+AM_CFLAGS = -I..
+
+noinst_PROGRAMS=droplay
+
+droplay_LDADD = ../libopl.a @LDFLAGS@ @SDL_LIBS@ @SDLMIXER_LIBS@
+droplay_SOURCES = droplay.c
+
--- /dev/null
+++ b/opl/examples/droplay.c
@@ -1,0 +1,217 @@
+// Emacs style mode select -*- C++ -*-
+//-----------------------------------------------------------------------------
+//
+// Copyright(C) 2009 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:
+// Demonstration program for OPL library to play back DRO
+// format files.
+//
+//-----------------------------------------------------------------------------
+
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "SDL.h"
+
+#include "opl.h"
+
+#define HEADER_STRING "DBRAWOPL"
+#define ADLIB_PORT 0x388
+
+void WriteReg(unsigned int reg, unsigned int val)
+{
+ int i;
+
+ // This was recorded from an OPL2, but we are probably playing
+ // back on an OPL3, so we need to enable the original OPL2
+ // channels. Doom does this already, but other games don't.
+
+ if ((reg & 0xf0) == OPL_REGS_FEEDBACK)
+ {
+ val |= 0x30;
+ }
+
+ OPL_WritePort(OPL_REGISTER_PORT, reg);
+
+ for (i=0; i<6; ++i)
+ {
+ OPL_ReadPort(OPL_REGISTER_PORT);
+ }
+
+ OPL_WritePort(OPL_DATA_PORT, val);
+
+ for (i=0; i<35; ++i)
+ {
+ OPL_ReadPort(OPL_REGISTER_PORT);
+ }
+}
+
+void ClearAllRegs(void)
+{
+ int i;
+
+ for (i=0; i<=0xff; ++i)
+ {
+ WriteReg(i, 0x00);
+ }
+}
+
+void Init(void)
+{
+ if (SDL_Init(SDL_INIT_TIMER) < 0)
+ {
+ fprintf(stderr, "Unable to initialise SDL timer\n");
+ exit(-1);
+ }
+
+ if (!OPL_Init(ADLIB_PORT))
+ {
+ fprintf(stderr, "Unable to initialise OPL layer\n");
+ exit(-1);
+ }
+}
+
+void Shutdown(void)
+{
+ OPL_Shutdown();
+}
+
+struct timer_data
+{
+ int running;
+ FILE *fstream;
+};
+
+void TimerCallback(void *data)
+{
+ struct timer_data *timer_data = data;
+ int delay;
+
+ if (!timer_data->running)
+ {
+ return;
+ }
+
+ // Read data until we must make a delay.
+
+ for (;;)
+ {
+ int reg, val;
+
+ // End of file?
+
+ if (feof(timer_data->fstream))
+ {
+ timer_data->running = 0;
+ return;
+ }
+
+ reg = fgetc(timer_data->fstream);
+ val = fgetc(timer_data->fstream);
+
+ // Register value of 0 or 1 indicates a delay.
+
+ if (reg == 0x00)
+ {
+ delay = val;
+ break;
+ }
+ else if (reg == 0x01)
+ {
+ val |= (fgetc(timer_data->fstream) << 8);
+ delay = val;
+ break;
+ }
+ else
+ {
+ WriteReg(reg, val);
+ }
+ }
+
+ // Schedule the next timer callback.
+
+ OPL_SetCallback(delay, TimerCallback, timer_data);
+}
+
+void PlayFile(char *filename)
+{
+ struct timer_data timer_data;
+ int running;
+ char buf[8];
+
+ timer_data.fstream = fopen(filename, "rb");
+
+ if (timer_data.fstream == NULL)
+ {
+ fprintf(stderr, "Failed to open %s\n", filename);
+ exit(-1);
+ }
+
+ if (fread(buf, 1, 8, timer_data.fstream) < 8)
+ {
+ fprintf(stderr, "failed to read raw OPL header\n");
+ exit(-1);
+ }
+
+ if (strncmp(buf, HEADER_STRING, 8) != 0)
+ {
+ fprintf(stderr, "Raw OPL header not found\n");
+ exit(-1);
+ }
+
+ fseek(timer_data.fstream, 28, SEEK_SET);
+ timer_data.running = 1;
+
+ // Start callback loop sequence.
+
+ OPL_SetCallback(0, TimerCallback, &timer_data);
+
+ // Sleep until the playback finishes.
+
+ do
+ {
+ OPL_Lock();
+ running = timer_data.running;
+ OPL_Unlock();
+
+ SDL_Delay(100);
+ } while (running);
+
+ fclose(timer_data.fstream);
+}
+
+int main(int argc, char *argv[])
+{
+ if (argc < 2)
+ {
+ printf("Usage: %s <filename>\n", argv[0]);
+ exit(-1);
+ }
+
+ Init();
+
+ PlayFile(argv[1]);
+
+ ClearAllRegs();
+ Shutdown();
+
+ return 0;
+}
+
--- /dev/null
+++ b/opl/fmopl.c
@@ -1,0 +1,1155 @@
+/* This file is derived from fmopl.cpp from ScummVM.
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * LGPL licensed version of MAMEs fmopl (V0.37a modified) by
+ * Tatsuyuki Satoh. Included from LGPL'ed AdPlug.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+#include <math.h>
+
+#include "fmopl.h"
+
+#define PI 3.1415926539
+
+#define CLIP(value, min, max) \
+ ( (value) < (min) ? (min) : \
+ (value) > (max) ? (max) : (value) )
+
+/* -------------------- preliminary define section --------------------- */
+/* attack/decay rate time rate */
+#define OPL_ARRATE 141280 /* RATE 4 = 2826.24ms @ 3.6MHz */
+#define OPL_DRRATE 1956000 /* RATE 4 = 39280.64ms @ 3.6MHz */
+
+#define FREQ_BITS 24 /* frequency turn */
+
+/* counter bits = 20 , octerve 7 */
+#define FREQ_RATE (1<<(FREQ_BITS-20))
+#define TL_BITS (FREQ_BITS+2)
+
+/* final output shift , limit minimum and maximum */
+#define OPL_OUTSB (TL_BITS+3-16) /* OPL output final shift 16bit */
+#define OPL_MAXOUT (0x7fff<<OPL_OUTSB)
+#define OPL_MINOUT (-0x8000<<OPL_OUTSB)
+
+/* -------------------- quality selection --------------------- */
+
+/* sinwave entries */
+/* used static memory = SIN_ENT * 4 (byte) */
+#define SIN_ENT_SHIFT 11
+#define SIN_ENT (1<<SIN_ENT_SHIFT)
+
+/* output level entries (envelope,sinwave) */
+/* envelope counter lower bits */
+static int ENV_BITS;
+/* envelope output entries */
+static int EG_ENT;
+
+/* used dynamic memory = EG_ENT*4*4(byte)or EG_ENT*6*4(byte) */
+/* used static memory = EG_ENT*4 (byte) */
+static int EG_OFF; /* OFF */
+static int EG_DED;
+static int EG_DST; /* DECAY START */
+static int EG_AED;
+#define EG_AST 0 /* ATTACK START */
+
+#define EG_STEP (96.0/EG_ENT) /* OPL is 0.1875 dB step */
+
+/* LFO table entries */
+#define VIB_ENT 512
+#define VIB_SHIFT (32-9)
+#define AMS_ENT 512
+#define AMS_SHIFT (32-9)
+
+#define VIB_RATE_SHIFT 8
+#define VIB_RATE (1<<VIB_RATE_SHIFT)
+
+/* -------------------- local defines , macros --------------------- */
+
+/* register number to channel number , slot offset */
+#define SLOT1 0
+#define SLOT2 1
+
+/* envelope phase */
+#define ENV_MOD_RR 0x00
+#define ENV_MOD_DR 0x01
+#define ENV_MOD_AR 0x02
+
+/* -------------------- tables --------------------- */
+static const int slot_array[32] = {
+ 0, 2, 4, 1, 3, 5,-1,-1,
+ 6, 8,10, 7, 9,11,-1,-1,
+ 12,14,16,13,15,17,-1,-1,
+ -1,-1,-1,-1,-1,-1,-1,-1
+};
+
+static uint32_t KSL_TABLE[8 * 16];
+
+static const double KSL_TABLE_SEED[8 * 16] = {
+ /* OCT 0 */
+ 0.000, 0.000, 0.000, 0.000,
+ 0.000, 0.000, 0.000, 0.000,
+ 0.000, 0.000, 0.000, 0.000,
+ 0.000, 0.000, 0.000, 0.000,
+ /* OCT 1 */
+ 0.000, 0.000, 0.000, 0.000,
+ 0.000, 0.000, 0.000, 0.000,
+ 0.000, 0.750, 1.125, 1.500,
+ 1.875, 2.250, 2.625, 3.000,
+ /* OCT 2 */
+ 0.000, 0.000, 0.000, 0.000,
+ 0.000, 1.125, 1.875, 2.625,
+ 3.000, 3.750, 4.125, 4.500,
+ 4.875, 5.250, 5.625, 6.000,
+ /* OCT 3 */
+ 0.000, 0.000, 0.000, 1.875,
+ 3.000, 4.125, 4.875, 5.625,
+ 6.000, 6.750, 7.125, 7.500,
+ 7.875, 8.250, 8.625, 9.000,
+ /* OCT 4 */
+ 0.000, 0.000, 3.000, 4.875,
+ 6.000, 7.125, 7.875, 8.625,
+ 9.000, 9.750, 10.125, 10.500,
+ 10.875, 11.250, 11.625, 12.000,
+ /* OCT 5 */
+ 0.000, 3.000, 6.000, 7.875,
+ 9.000, 10.125, 10.875, 11.625,
+ 12.000, 12.750, 13.125, 13.500,
+ 13.875, 14.250, 14.625, 15.000,
+ /* OCT 6 */
+ 0.000, 6.000, 9.000, 10.875,
+ 12.000, 13.125, 13.875, 14.625,
+ 15.000, 15.750, 16.125, 16.500,
+ 16.875, 17.250, 17.625, 18.000,
+ /* OCT 7 */
+ 0.000, 9.000, 12.000, 13.875,
+ 15.000, 16.125, 16.875, 17.625,
+ 18.000, 18.750, 19.125, 19.500,
+ 19.875, 20.250, 20.625, 21.000
+};
+
+/* sustain level table (3db per step) */
+/* 0 - 15: 0, 3, 6, 9,12,15,18,21,24,27,30,33,36,39,42,93 (dB)*/
+
+static int SL_TABLE[16];
+
+static const uint32_t SL_TABLE_SEED[16] = {
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 31
+};
+
+#define TL_MAX (EG_ENT * 2) /* limit(tl + ksr + envelope) + sinwave */
+/* TotalLevel : 48 24 12 6 3 1.5 0.75 (dB) */
+/* TL_TABLE[ 0 to TL_MAX ] : plus section */
+/* TL_TABLE[ TL_MAX to TL_MAX+TL_MAX-1 ] : minus section */
+static int *TL_TABLE;
+
+/* pointers to TL_TABLE with sinwave output offset */
+static int **SIN_TABLE;
+
+/* LFO table */
+static int *AMS_TABLE;
+static int *VIB_TABLE;
+
+/* envelope output curve table */
+/* attack + decay + OFF */
+//static int ENV_CURVE[2*EG_ENT+1];
+//static int ENV_CURVE[2 * 4096 + 1]; // to keep it static ...
+static int *ENV_CURVE;
+
+
+/* multiple table */
+#define ML(a) (int)(a * 2)
+static const uint32_t MUL_TABLE[16]= {
+/* 1/2, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15 */
+ ML(0.50), ML(1.00), ML(2.00), ML(3.00), ML(4.00), ML(5.00), ML(6.00), ML(7.00),
+ ML(8.00), ML(9.00), ML(10.00), ML(10.00),ML(12.00),ML(12.00),ML(15.00),ML(15.00)
+};
+#undef ML
+
+/* dummy attack / decay rate ( when rate == 0 ) */
+static int RATE_0[16]=
+{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+
+/* -------------------- static state --------------------- */
+
+/* lock level of common table */
+static int num_lock = 0;
+
+/* work table */
+static void *cur_chip = NULL; /* current chip point */
+/* currenct chip state */
+/* static OPLSAMPLE *bufL,*bufR; */
+static OPL_CH *S_CH;
+static OPL_CH *E_CH;
+static OPL_SLOT *SLOT7_1, *SLOT7_2, *SLOT8_1, *SLOT8_2;
+
+static int outd[1];
+static int ams;
+static int vib;
+static int *ams_table;
+static int *vib_table;
+static int amsIncr;
+static int vibIncr;
+static int feedback2; /* connect for SLOT 2 */
+
+/* --------------------- rebuild tables ------------------- */
+
+#define ARRAYSIZE(x) (sizeof(x) / sizeof(*x))
+#define SC_KSL(mydb) ((uint32_t) (mydb / (EG_STEP / 2)))
+#define SC_SL(db) (int)(db * ((3 / EG_STEP) * (1 << ENV_BITS))) + EG_DST
+
+void OPLBuildTables(int ENV_BITS_PARAM, int EG_ENT_PARAM) {
+ int i;
+
+ ENV_BITS = ENV_BITS_PARAM;
+ EG_ENT = EG_ENT_PARAM;
+ EG_OFF = ((2 * EG_ENT)<<ENV_BITS); /* OFF */
+ EG_DED = EG_OFF;
+ EG_DST = (EG_ENT << ENV_BITS); /* DECAY START */
+ EG_AED = EG_DST;
+ //EG_STEP = (96.0/EG_ENT);
+
+ for (i = 0; i < ARRAYSIZE(KSL_TABLE_SEED); i++)
+ KSL_TABLE[i] = SC_KSL(KSL_TABLE_SEED[i]);
+
+ for (i = 0; i < ARRAYSIZE(SL_TABLE_SEED); i++)
+ SL_TABLE[i] = SC_SL(SL_TABLE_SEED[i]);
+}
+
+#undef SC_KSL
+#undef SC_SL
+
+/* --------------------- subroutines --------------------- */
+
+/* status set and IRQ handling */
+static inline void OPL_STATUS_SET(FM_OPL *OPL, int flag) {
+ /* set status flag */
+ OPL->status |= flag;
+ if(!(OPL->status & 0x80)) {
+ if(OPL->status & OPL->statusmask) { /* IRQ on */
+ OPL->status |= 0x80;
+ /* callback user interrupt handler (IRQ is OFF to ON) */
+ if(OPL->IRQHandler)
+ (OPL->IRQHandler)(OPL->IRQParam,1);
+ }
+ }
+}
+
+/* status reset and IRQ handling */
+static inline void OPL_STATUS_RESET(FM_OPL *OPL, int flag) {
+ /* reset status flag */
+ OPL->status &= ~flag;
+ if((OPL->status & 0x80)) {
+ if (!(OPL->status & OPL->statusmask)) {
+ OPL->status &= 0x7f;
+ /* callback user interrupt handler (IRQ is ON to OFF) */
+ if(OPL->IRQHandler) (OPL->IRQHandler)(OPL->IRQParam,0);
+ }
+ }
+}
+
+/* IRQ mask set */
+static inline void OPL_STATUSMASK_SET(FM_OPL *OPL, int flag) {
+ OPL->statusmask = flag;
+ /* IRQ handling check */
+ OPL_STATUS_SET(OPL,0);
+ OPL_STATUS_RESET(OPL,0);
+}
+
+/* ----- key on ----- */
+static inline void OPL_KEYON(OPL_SLOT *SLOT) {
+ /* sin wave restart */
+ SLOT->Cnt = 0;
+ /* set attack */
+ SLOT->evm = ENV_MOD_AR;
+ SLOT->evs = SLOT->evsa;
+ SLOT->evc = EG_AST;
+ SLOT->eve = EG_AED;
+}
+
+/* ----- key off ----- */
+static inline void OPL_KEYOFF(OPL_SLOT *SLOT) {
+ if( SLOT->evm > ENV_MOD_RR) {
+ /* set envelope counter from envleope output */
+
+ // WORKAROUND: The Kyra engine does something very strange when
+ // starting a new song. For each channel:
+ //
+ // * The release rate is set to "fastest".
+ // * Any note is keyed off.
+ // * A very low-frequency note is keyed on.
+ //
+ // Usually, what happens next is that the real notes is keyed
+ // on immediately, in which case there's no problem.
+ //
+ // However, if the note is again keyed off (because the channel
+ // begins on a rest rather than a note), the envelope counter
+ // was moved from the very lowest point on the attack curve to
+ // the very highest point on the release curve.
+ //
+ // Again, this might not be a problem, if the release rate is
+ // still set to "fastest". But in many cases, it had already
+ // been increased. And, possibly because of inaccuracies in the
+ // envelope generator, that would cause the note to "fade out"
+ // for quite a long time.
+ //
+ // What we really need is a way to find the correct starting
+ // point for the envelope counter, and that may be what the
+ // commented-out line below is meant to do. For now, simply
+ // handle the pathological case.
+
+ if (SLOT->evm == ENV_MOD_AR && SLOT->evc == EG_AST)
+ SLOT->evc = EG_DED;
+ else if( !(SLOT->evc & EG_DST) )
+ //SLOT->evc = (ENV_CURVE[SLOT->evc>>ENV_BITS]<<ENV_BITS) + EG_DST;
+ SLOT->evc = EG_DST;
+ SLOT->eve = EG_DED;
+ SLOT->evs = SLOT->evsr;
+ SLOT->evm = ENV_MOD_RR;
+ }
+}
+
+/* ---------- calcrate Envelope Generator & Phase Generator ---------- */
+
+/* return : envelope output */
+static inline uint32_t OPL_CALC_SLOT(OPL_SLOT *SLOT) {
+ /* calcrate envelope generator */
+ if((SLOT->evc += SLOT->evs) >= SLOT->eve) {
+ switch( SLOT->evm ) {
+ case ENV_MOD_AR: /* ATTACK -> DECAY1 */
+ /* next DR */
+ SLOT->evm = ENV_MOD_DR;
+ SLOT->evc = EG_DST;
+ SLOT->eve = SLOT->SL;
+ SLOT->evs = SLOT->evsd;
+ break;
+ case ENV_MOD_DR: /* DECAY -> SL or RR */
+ SLOT->evc = SLOT->SL;
+ SLOT->eve = EG_DED;
+ if(SLOT->eg_typ) {
+ SLOT->evs = 0;
+ } else {
+ SLOT->evm = ENV_MOD_RR;
+ SLOT->evs = SLOT->evsr;
+ }
+ break;
+ case ENV_MOD_RR: /* RR -> OFF */
+ SLOT->evc = EG_OFF;
+ SLOT->eve = EG_OFF + 1;
+ SLOT->evs = 0;
+ break;
+ }
+ }
+ /* calcrate envelope */
+ return SLOT->TLL + ENV_CURVE[SLOT->evc>>ENV_BITS] + (SLOT->ams ? ams : 0);
+}
+
+/* set algorythm connection */
+static void set_algorythm(OPL_CH *CH) {
+ int *carrier = &outd[0];
+ CH->connect1 = CH->CON ? carrier : &feedback2;
+ CH->connect2 = carrier;
+}
+
+/* ---------- frequency counter for operater update ---------- */
+static inline void CALC_FCSLOT(OPL_CH *CH, OPL_SLOT *SLOT) {
+ int ksr;
+
+ /* frequency step counter */
+ SLOT->Incr = CH->fc * SLOT->mul;
+ ksr = CH->kcode >> SLOT->KSR;
+
+ if( SLOT->ksr != ksr ) {
+ SLOT->ksr = ksr;
+ /* attack , decay rate recalcration */
+ SLOT->evsa = SLOT->AR[ksr];
+ SLOT->evsd = SLOT->DR[ksr];
+ SLOT->evsr = SLOT->RR[ksr];
+ }
+ SLOT->TLL = SLOT->TL + (CH->ksl_base>>SLOT->ksl);
+}
+
+/* set multi,am,vib,EG-TYP,KSR,mul */
+static inline void set_mul(FM_OPL *OPL, int slot, int v) {
+ OPL_CH *CH = &OPL->P_CH[slot>>1];
+ OPL_SLOT *SLOT = &CH->SLOT[slot & 1];
+
+ SLOT->mul = MUL_TABLE[v & 0x0f];
+ SLOT->KSR = (v & 0x10) ? 0 : 2;
+ SLOT->eg_typ = (v & 0x20) >> 5;
+ SLOT->vib = (v & 0x40);
+ SLOT->ams = (v & 0x80);
+ CALC_FCSLOT(CH, SLOT);
+}
+
+/* set ksl & tl */
+static inline void set_ksl_tl(FM_OPL *OPL, int slot, int v) {
+ OPL_CH *CH = &OPL->P_CH[slot>>1];
+ OPL_SLOT *SLOT = &CH->SLOT[slot & 1];
+ int ksl = v >> 6; /* 0 / 1.5 / 3 / 6 db/OCT */
+
+ SLOT->ksl = ksl ? 3-ksl : 31;
+ SLOT->TL = (int)((v & 0x3f) * (0.75 / EG_STEP)); /* 0.75db step */
+
+ if(!(OPL->mode & 0x80)) { /* not CSM latch total level */
+ SLOT->TLL = SLOT->TL + (CH->ksl_base >> SLOT->ksl);
+ }
+}
+
+/* set attack rate & decay rate */
+static inline void set_ar_dr(FM_OPL *OPL, int slot, int v) {
+ OPL_CH *CH = &OPL->P_CH[slot>>1];
+ OPL_SLOT *SLOT = &CH->SLOT[slot & 1];
+ int ar = v >> 4;
+ int dr = v & 0x0f;
+
+ SLOT->AR = ar ? &OPL->AR_TABLE[ar << 2] : RATE_0;
+ SLOT->evsa = SLOT->AR[SLOT->ksr];
+ if(SLOT->evm == ENV_MOD_AR)
+ SLOT->evs = SLOT->evsa;
+
+ SLOT->DR = dr ? &OPL->DR_TABLE[dr<<2] : RATE_0;
+ SLOT->evsd = SLOT->DR[SLOT->ksr];
+ if(SLOT->evm == ENV_MOD_DR)
+ SLOT->evs = SLOT->evsd;
+}
+
+/* set sustain level & release rate */
+static inline void set_sl_rr(FM_OPL *OPL, int slot, int v) {
+ OPL_CH *CH = &OPL->P_CH[slot>>1];
+ OPL_SLOT *SLOT = &CH->SLOT[slot & 1];
+ int sl = v >> 4;
+ int rr = v & 0x0f;
+
+ SLOT->SL = SL_TABLE[sl];
+ if(SLOT->evm == ENV_MOD_DR)
+ SLOT->eve = SLOT->SL;
+ SLOT->RR = &OPL->DR_TABLE[rr<<2];
+ SLOT->evsr = SLOT->RR[SLOT->ksr];
+ if(SLOT->evm == ENV_MOD_RR)
+ SLOT->evs = SLOT->evsr;
+}
+
+/* operator output calcrator */
+
+#define OP_OUT(slot,env,con) slot->wavetable[((slot->Cnt + con)>>(24-SIN_ENT_SHIFT)) & (SIN_ENT-1)][env]
+/* ---------- calcrate one of channel ---------- */
+static inline void OPL_CALC_CH(OPL_CH *CH) {
+ uint32_t env_out;
+ OPL_SLOT *SLOT;
+
+ feedback2 = 0;
+ /* SLOT 1 */
+ SLOT = &CH->SLOT[SLOT1];
+ env_out=OPL_CALC_SLOT(SLOT);
+ if(env_out < (uint32_t)(EG_ENT - 1)) {
+ /* PG */
+ if(SLOT->vib)
+ SLOT->Cnt += (SLOT->Incr * vib) >> VIB_RATE_SHIFT;
+ else
+ SLOT->Cnt += SLOT->Incr;
+ /* connection */
+ if(CH->FB) {
+ int feedback1 = (CH->op1_out[0] + CH->op1_out[1]) >> CH->FB;
+ CH->op1_out[1] = CH->op1_out[0];
+ *CH->connect1 += CH->op1_out[0] = OP_OUT(SLOT, env_out, feedback1);
+ } else {
+ *CH->connect1 += OP_OUT(SLOT, env_out, 0);
+ }
+ } else {
+ CH->op1_out[1] = CH->op1_out[0];
+ CH->op1_out[0] = 0;
+ }
+ /* SLOT 2 */
+ SLOT = &CH->SLOT[SLOT2];
+ env_out=OPL_CALC_SLOT(SLOT);
+ if(env_out < (uint32_t)(EG_ENT - 1)) {
+ /* PG */
+ if(SLOT->vib)
+ SLOT->Cnt += (SLOT->Incr * vib) >> VIB_RATE_SHIFT;
+ else
+ SLOT->Cnt += SLOT->Incr;
+ /* connection */
+ outd[0] += OP_OUT(SLOT, env_out, feedback2);
+ }
+}
+
+/* ---------- calcrate rythm block ---------- */
+#define WHITE_NOISE_db 6.0
+static inline void OPL_CALC_RH(FM_OPL *OPL, OPL_CH *CH) {
+ uint32_t env_tam, env_sd, env_top, env_hh;
+ // This code used to do int(OPL->rnd.getRandomBit() * (WHITE_NOISE_db / EG_STEP)),
+ // but EG_STEP = 96.0/EG_ENT, and WHITE_NOISE_db=6.0. So, that's equivalent to
+ // int(OPL->rnd.getRandomBit() * EG_ENT/16). We know that EG_ENT is 4096, or 1024,
+ // or 128, so we can safely avoid any FP ops.
+ int whitenoise = (rand() & 1) * (EG_ENT>>4);
+
+ int tone8;
+
+ OPL_SLOT *SLOT;
+ int env_out;
+
+ /* BD : same as FM serial mode and output level is large */
+ feedback2 = 0;
+ /* SLOT 1 */
+ SLOT = &CH[6].SLOT[SLOT1];
+ env_out = OPL_CALC_SLOT(SLOT);
+ if(env_out < EG_ENT-1) {
+ /* PG */
+ if(SLOT->vib)
+ SLOT->Cnt += (SLOT->Incr * vib) >> VIB_RATE_SHIFT;
+ else
+ SLOT->Cnt += SLOT->Incr;
+ /* connection */
+ if(CH[6].FB) {
+ int feedback1 = (CH[6].op1_out[0] + CH[6].op1_out[1]) >> CH[6].FB;
+ CH[6].op1_out[1] = CH[6].op1_out[0];
+ feedback2 = CH[6].op1_out[0] = OP_OUT(SLOT, env_out, feedback1);
+ }
+ else {
+ feedback2 = OP_OUT(SLOT, env_out, 0);
+ }
+ } else {
+ feedback2 = 0;
+ CH[6].op1_out[1] = CH[6].op1_out[0];
+ CH[6].op1_out[0] = 0;
+ }
+ /* SLOT 2 */
+ SLOT = &CH[6].SLOT[SLOT2];
+ env_out = OPL_CALC_SLOT(SLOT);
+ if(env_out < EG_ENT-1) {
+ /* PG */
+ if(SLOT->vib)
+ SLOT->Cnt += (SLOT->Incr * vib) >> VIB_RATE_SHIFT;
+ else
+ SLOT->Cnt += SLOT->Incr;
+ /* connection */
+ outd[0] += OP_OUT(SLOT, env_out, feedback2) * 2;
+ }
+
+ // SD (17) = mul14[fnum7] + white noise
+ // TAM (15) = mul15[fnum8]
+ // TOP (18) = fnum6(mul18[fnum8]+whitenoise)
+ // HH (14) = fnum7(mul18[fnum8]+whitenoise) + white noise
+ env_sd = OPL_CALC_SLOT(SLOT7_2) + whitenoise;
+ env_tam =OPL_CALC_SLOT(SLOT8_1);
+ env_top = OPL_CALC_SLOT(SLOT8_2);
+ env_hh = OPL_CALC_SLOT(SLOT7_1) + whitenoise;
+
+ /* PG */
+ if(SLOT7_1->vib)
+ SLOT7_1->Cnt += (SLOT7_1->Incr * vib) >> (VIB_RATE_SHIFT-1);
+ else
+ SLOT7_1->Cnt += 2 * SLOT7_1->Incr;
+ if(SLOT7_2->vib)
+ SLOT7_2->Cnt += (CH[7].fc * vib) >> (VIB_RATE_SHIFT-3);
+ else
+ SLOT7_2->Cnt += (CH[7].fc * 8);
+ if(SLOT8_1->vib)
+ SLOT8_1->Cnt += (SLOT8_1->Incr * vib) >> VIB_RATE_SHIFT;
+ else
+ SLOT8_1->Cnt += SLOT8_1->Incr;
+ if(SLOT8_2->vib)
+ SLOT8_2->Cnt += ((CH[8].fc * 3) * vib) >> (VIB_RATE_SHIFT-4);
+ else
+ SLOT8_2->Cnt += (CH[8].fc * 48);
+
+ tone8 = OP_OUT(SLOT8_2,whitenoise,0 );
+
+ /* SD */
+ if(env_sd < (uint32_t)(EG_ENT - 1))
+ outd[0] += OP_OUT(SLOT7_1, env_sd, 0) * 8;
+ /* TAM */
+ if(env_tam < (uint32_t)(EG_ENT - 1))
+ outd[0] += OP_OUT(SLOT8_1, env_tam, 0) * 2;
+ /* TOP-CY */
+ if(env_top < (uint32_t)(EG_ENT - 1))
+ outd[0] += OP_OUT(SLOT7_2, env_top, tone8) * 2;
+ /* HH */
+ if(env_hh < (uint32_t)(EG_ENT-1))
+ outd[0] += OP_OUT(SLOT7_2, env_hh, tone8) * 2;
+}
+
+/* ----------- initialize time tabls ----------- */
+static void init_timetables(FM_OPL *OPL, int ARRATE, int DRRATE) {
+ int i;
+ double rate;
+
+ /* make attack rate & decay rate tables */
+ for (i = 0; i < 4; i++)
+ OPL->AR_TABLE[i] = OPL->DR_TABLE[i] = 0;
+ for (i = 4; i <= 60; i++) {
+ rate = OPL->freqbase; /* frequency rate */
+ if(i < 60)
+ rate *= 1.0 + (i & 3) * 0.25; /* b0-1 : x1 , x1.25 , x1.5 , x1.75 */
+ rate *= 1 << ((i >> 2) - 1); /* b2-5 : shift bit */
+ rate *= (double)(EG_ENT << ENV_BITS);
+ OPL->AR_TABLE[i] = (int)(rate / ARRATE);
+ OPL->DR_TABLE[i] = (int)(rate / DRRATE);
+ }
+ for (i = 60; i < 76; i++) {
+ OPL->AR_TABLE[i] = EG_AED-1;
+ OPL->DR_TABLE[i] = OPL->DR_TABLE[60];
+ }
+}
+
+/* ---------- generic table initialize ---------- */
+static int OPLOpenTable(void) {
+ int s,t;
+ double rate;
+ int i,j;
+ double pom;
+
+ /* allocate dynamic tables */
+ if((TL_TABLE = (int *)malloc(TL_MAX * 2 * sizeof(int))) == NULL)
+ return 0;
+
+ if((SIN_TABLE = (int **)malloc(SIN_ENT * 4 * sizeof(int *))) == NULL) {
+ free(TL_TABLE);
+ return 0;
+ }
+
+ if((AMS_TABLE = (int *)malloc(AMS_ENT * 2 * sizeof(int))) == NULL) {
+ free(TL_TABLE);
+ free(SIN_TABLE);
+ return 0;
+ }
+
+ if((VIB_TABLE = (int *)malloc(VIB_ENT * 2 * sizeof(int))) == NULL) {
+ free(TL_TABLE);
+ free(SIN_TABLE);
+ free(AMS_TABLE);
+ return 0;
+ }
+ /* make total level table */
+ for (t = 0; t < EG_ENT - 1 ; t++) {
+ rate = ((1 << TL_BITS) - 1) / pow(10.0, EG_STEP * t / 20); /* dB -> voltage */
+ TL_TABLE[ t] = (int)rate;
+ TL_TABLE[TL_MAX + t] = -TL_TABLE[t];
+ }
+ /* fill volume off area */
+ for (t = EG_ENT - 1; t < TL_MAX; t++) {
+ TL_TABLE[t] = TL_TABLE[TL_MAX + t] = 0;
+ }
+
+ /* make sinwave table (total level offet) */
+ /* degree 0 = degree 180 = off */
+ SIN_TABLE[0] = SIN_TABLE[SIN_ENT /2 ] = &TL_TABLE[EG_ENT - 1];
+ for (s = 1;s <= SIN_ENT / 4; s++) {
+ pom = sin(2 * PI * s / SIN_ENT); /* sin */
+ pom = 20 * log10(1 / pom); /* decibel */
+ j = (int) (pom / EG_STEP); /* TL_TABLE steps */
+
+ /* degree 0 - 90 , degree 180 - 90 : plus section */
+ SIN_TABLE[ s] = SIN_TABLE[SIN_ENT / 2 - s] = &TL_TABLE[j];
+ /* degree 180 - 270 , degree 360 - 270 : minus section */
+ SIN_TABLE[SIN_ENT / 2 + s] = SIN_TABLE[SIN_ENT - s] = &TL_TABLE[TL_MAX + j];
+ }
+ for (s = 0;s < SIN_ENT; s++) {
+ SIN_TABLE[SIN_ENT * 1 + s] = s < (SIN_ENT / 2) ? SIN_TABLE[s] : &TL_TABLE[EG_ENT];
+ SIN_TABLE[SIN_ENT * 2 + s] = SIN_TABLE[s % (SIN_ENT / 2)];
+ SIN_TABLE[SIN_ENT * 3 + s] = (s / (SIN_ENT / 4)) & 1 ? &TL_TABLE[EG_ENT] : SIN_TABLE[SIN_ENT * 2 + s];
+ }
+
+
+ ENV_CURVE = (int *)malloc(sizeof(int) * (2*EG_ENT+1));
+
+ /* envelope counter -> envelope output table */
+ for (i=0; i < EG_ENT; i++) {
+ /* ATTACK curve */
+ pom = pow(((double)(EG_ENT - 1 - i) / EG_ENT), 8) * EG_ENT;
+ /* if( pom >= EG_ENT ) pom = EG_ENT-1; */
+ ENV_CURVE[i] = (int)pom;
+ /* DECAY ,RELEASE curve */
+ ENV_CURVE[(EG_DST >> ENV_BITS) + i]= i;
+ }
+ /* off */
+ ENV_CURVE[EG_OFF >> ENV_BITS]= EG_ENT - 1;
+ /* make LFO ams table */
+ for (i=0; i < AMS_ENT; i++) {
+ pom = (1.0 + sin(2 * PI * i / AMS_ENT)) / 2; /* sin */
+ AMS_TABLE[i] = (int)((1.0 / EG_STEP) * pom); /* 1dB */
+ AMS_TABLE[AMS_ENT + i] = (int)((4.8 / EG_STEP) * pom); /* 4.8dB */
+ }
+ /* make LFO vibrate table */
+ for (i=0; i < VIB_ENT; i++) {
+ /* 100cent = 1seminote = 6% ?? */
+ pom = (double)VIB_RATE * 0.06 * sin(2 * PI * i / VIB_ENT); /* +-100sect step */
+ VIB_TABLE[i] = (int)(VIB_RATE + (pom * 0.07)); /* +- 7cent */
+ VIB_TABLE[VIB_ENT + i] = (int)(VIB_RATE + (pom * 0.14)); /* +-14cent */
+ }
+ return 1;
+}
+
+static void OPLCloseTable(void) {
+ free(TL_TABLE);
+ free(SIN_TABLE);
+ free(AMS_TABLE);
+ free(VIB_TABLE);
+ free(ENV_CURVE);
+}
+
+/* CSM Key Controll */
+static inline void CSMKeyControll(OPL_CH *CH) {
+ OPL_SLOT *slot1 = &CH->SLOT[SLOT1];
+ OPL_SLOT *slot2 = &CH->SLOT[SLOT2];
+ /* all key off */
+ OPL_KEYOFF(slot1);
+ OPL_KEYOFF(slot2);
+ /* total level latch */
+ slot1->TLL = slot1->TL + (CH->ksl_base>>slot1->ksl);
+ slot1->TLL = slot1->TL + (CH->ksl_base>>slot1->ksl);
+ /* key on */
+ CH->op1_out[0] = CH->op1_out[1] = 0;
+ OPL_KEYON(slot1);
+ OPL_KEYON(slot2);
+}
+
+/* ---------- opl initialize ---------- */
+static void OPL_initalize(FM_OPL *OPL) {
+ int fn;
+
+ /* frequency base */
+ OPL->freqbase = (OPL->rate) ? ((double)OPL->clock / OPL->rate) / 72 : 0;
+ /* Timer base time */
+ OPL->TimerBase = 1.0/((double)OPL->clock / 72.0 );
+ /* make time tables */
+ init_timetables(OPL, OPL_ARRATE, OPL_DRRATE);
+ /* make fnumber -> increment counter table */
+ for( fn=0; fn < 1024; fn++) {
+ OPL->FN_TABLE[fn] = (uint32_t)(OPL->freqbase * fn * FREQ_RATE * (1<<7) / 2);
+ }
+ /* LFO freq.table */
+ OPL->amsIncr = (int)(OPL->rate ? (double)AMS_ENT * (1 << AMS_SHIFT) / OPL->rate * 3.7 * ((double)OPL->clock/3600000) : 0);
+ OPL->vibIncr = (int)(OPL->rate ? (double)VIB_ENT * (1 << VIB_SHIFT) / OPL->rate * 6.4 * ((double)OPL->clock/3600000) : 0);
+}
+
+/* ---------- write a OPL registers ---------- */
+void OPLWriteReg(FM_OPL *OPL, int r, int v) {
+ OPL_CH *CH;
+ int slot;
+ uint32_t block_fnum;
+
+ switch(r & 0xe0) {
+ case 0x00: /* 00-1f:controll */
+ switch(r & 0x1f) {
+ case 0x01:
+ /* wave selector enable */
+ if(OPL->type&OPL_TYPE_WAVESEL) {
+ OPL->wavesel = v & 0x20;
+ if(!OPL->wavesel) {
+ /* preset compatible mode */
+ int c;
+ for(c=0; c<OPL->max_ch; c++) {
+ OPL->P_CH[c].SLOT[SLOT1].wavetable = &SIN_TABLE[0];
+ OPL->P_CH[c].SLOT[SLOT2].wavetable = &SIN_TABLE[0];
+ }
+ }
+ }
+ return;
+ case 0x02: /* Timer 1 */
+ OPL->T[0] = (256-v) * 4;
+ break;
+ case 0x03: /* Timer 2 */
+ OPL->T[1] = (256-v) * 16;
+ return;
+ case 0x04: /* IRQ clear / mask and Timer enable */
+ if(v & 0x80) { /* IRQ flag clear */
+ OPL_STATUS_RESET(OPL, 0x7f);
+ } else { /* set IRQ mask ,timer enable*/
+ uint8_t st1 = v & 1;
+ uint8_t st2 = (v >> 1) & 1;
+ /* IRQRST,T1MSK,t2MSK,EOSMSK,BRMSK,x,ST2,ST1 */
+ OPL_STATUS_RESET(OPL, v & 0x78);
+ OPL_STATUSMASK_SET(OPL,((~v) & 0x78) | 0x01);
+ /* timer 2 */
+ if(OPL->st[1] != st2) {
+ double interval = st2 ? (double)OPL->T[1] * OPL->TimerBase : 0.0;
+ OPL->st[1] = st2;
+ if (OPL->TimerHandler) (OPL->TimerHandler)(OPL->TimerParam + 1, interval);
+ }
+ /* timer 1 */
+ if(OPL->st[0] != st1) {
+ double interval = st1 ? (double)OPL->T[0] * OPL->TimerBase : 0.0;
+ OPL->st[0] = st1;
+ if (OPL->TimerHandler) (OPL->TimerHandler)(OPL->TimerParam + 0, interval);
+ }
+ }
+ return;
+ }
+ break;
+ case 0x20: /* am,vib,ksr,eg type,mul */
+ slot = slot_array[r&0x1f];
+ if(slot == -1)
+ return;
+ set_mul(OPL,slot,v);
+ return;
+ case 0x40:
+ slot = slot_array[r&0x1f];
+ if(slot == -1)
+ return;
+ set_ksl_tl(OPL,slot,v);
+ return;
+ case 0x60:
+ slot = slot_array[r&0x1f];
+ if(slot == -1)
+ return;
+ set_ar_dr(OPL,slot,v);
+ return;
+ case 0x80:
+ slot = slot_array[r&0x1f];
+ if(slot == -1)
+ return;
+ set_sl_rr(OPL,slot,v);
+ return;
+ case 0xa0:
+ switch(r) {
+ case 0xbd:
+ /* amsep,vibdep,r,bd,sd,tom,tc,hh */
+ {
+ uint8_t rkey = OPL->rythm ^ v;
+ OPL->ams_table = &AMS_TABLE[v & 0x80 ? AMS_ENT : 0];
+ OPL->vib_table = &VIB_TABLE[v & 0x40 ? VIB_ENT : 0];
+ OPL->rythm = v & 0x3f;
+ if(OPL->rythm & 0x20) {
+ /* BD key on/off */
+ if(rkey & 0x10) {
+ if(v & 0x10) {
+ OPL->P_CH[6].op1_out[0] = OPL->P_CH[6].op1_out[1] = 0;
+ OPL_KEYON(&OPL->P_CH[6].SLOT[SLOT1]);
+ OPL_KEYON(&OPL->P_CH[6].SLOT[SLOT2]);
+ } else {
+ OPL_KEYOFF(&OPL->P_CH[6].SLOT[SLOT1]);
+ OPL_KEYOFF(&OPL->P_CH[6].SLOT[SLOT2]);
+ }
+ }
+ /* SD key on/off */
+ if(rkey & 0x08) {
+ if(v & 0x08)
+ OPL_KEYON(&OPL->P_CH[7].SLOT[SLOT2]);
+ else
+ OPL_KEYOFF(&OPL->P_CH[7].SLOT[SLOT2]);
+ }/* TAM key on/off */
+ if(rkey & 0x04) {
+ if(v & 0x04)
+ OPL_KEYON(&OPL->P_CH[8].SLOT[SLOT1]);
+ else
+ OPL_KEYOFF(&OPL->P_CH[8].SLOT[SLOT1]);
+ }
+ /* TOP-CY key on/off */
+ if(rkey & 0x02) {
+ if(v & 0x02)
+ OPL_KEYON(&OPL->P_CH[8].SLOT[SLOT2]);
+ else
+ OPL_KEYOFF(&OPL->P_CH[8].SLOT[SLOT2]);
+ }
+ /* HH key on/off */
+ if(rkey & 0x01) {
+ if(v & 0x01)
+ OPL_KEYON(&OPL->P_CH[7].SLOT[SLOT1]);
+ else
+ OPL_KEYOFF(&OPL->P_CH[7].SLOT[SLOT1]);
+ }
+ }
+ }
+ return;
+
+ default:
+ break;
+ }
+ /* keyon,block,fnum */
+ if((r & 0x0f) > 8)
+ return;
+ CH = &OPL->P_CH[r & 0x0f];
+ if(!(r&0x10)) { /* a0-a8 */
+ block_fnum = (CH->block_fnum & 0x1f00) | v;
+ } else { /* b0-b8 */
+ int keyon = (v >> 5) & 1;
+ block_fnum = ((v & 0x1f) << 8) | (CH->block_fnum & 0xff);
+ if(CH->keyon != keyon) {
+ if((CH->keyon=keyon)) {
+ CH->op1_out[0] = CH->op1_out[1] = 0;
+ OPL_KEYON(&CH->SLOT[SLOT1]);
+ OPL_KEYON(&CH->SLOT[SLOT2]);
+ } else {
+ OPL_KEYOFF(&CH->SLOT[SLOT1]);
+ OPL_KEYOFF(&CH->SLOT[SLOT2]);
+ }
+ }
+ }
+ /* update */
+ if(CH->block_fnum != block_fnum) {
+ int blockRv = 7 - (block_fnum >> 10);
+ int fnum = block_fnum & 0x3ff;
+ CH->block_fnum = block_fnum;
+ CH->ksl_base = KSL_TABLE[block_fnum >> 6];
+ CH->fc = OPL->FN_TABLE[fnum] >> blockRv;
+ CH->kcode = CH->block_fnum >> 9;
+ if((OPL->mode & 0x40) && CH->block_fnum & 0x100)
+ CH->kcode |=1;
+ CALC_FCSLOT(CH,&CH->SLOT[SLOT1]);
+ CALC_FCSLOT(CH,&CH->SLOT[SLOT2]);
+ }
+ return;
+ case 0xc0:
+ /* FB,C */
+ if((r & 0x0f) > 8)
+ return;
+ CH = &OPL->P_CH[r&0x0f];
+ {
+ int feedback = (v >> 1) & 7;
+ CH->FB = feedback ? (8 + 1) - feedback : 0;
+ CH->CON = v & 1;
+ set_algorythm(CH);
+ }
+ return;
+ case 0xe0: /* wave type */
+ slot = slot_array[r & 0x1f];
+ if(slot == -1)
+ return;
+ CH = &OPL->P_CH[slot>>1];
+ if(OPL->wavesel) {
+ CH->SLOT[slot&1].wavetable = &SIN_TABLE[(v & 0x03) * SIN_ENT];
+ }
+ return;
+ }
+}
+
+/* lock/unlock for common table */
+static int OPL_LockTable(void) {
+ num_lock++;
+ if(num_lock>1)
+ return 0;
+ /* first time */
+ cur_chip = NULL;
+ /* allocate total level table (128kb space) */
+ if(!OPLOpenTable()) {
+ num_lock--;
+ return -1;
+ }
+ return 0;
+}
+
+static void OPL_UnLockTable(void) {
+ if(num_lock)
+ num_lock--;
+ if(num_lock)
+ return;
+ /* last time */
+ cur_chip = NULL;
+ OPLCloseTable();
+}
+
+/*******************************************************************************/
+/* YM3812 local section */
+/*******************************************************************************/
+
+/* ---------- update one of chip ----------- */
+void YM3812UpdateOne(FM_OPL *OPL, int16_t *buffer, int length, int interleave) {
+ int i;
+ int data;
+ int16_t *buf = buffer;
+ uint32_t amsCnt = OPL->amsCnt;
+ uint32_t vibCnt = OPL->vibCnt;
+ uint8_t rythm = OPL->rythm & 0x20;
+ OPL_CH *CH, *R_CH;
+
+
+ if((void *)OPL != cur_chip) {
+ cur_chip = (void *)OPL;
+ /* channel pointers */
+ S_CH = OPL->P_CH;
+ E_CH = &S_CH[9];
+ /* rythm slot */
+ SLOT7_1 = &S_CH[7].SLOT[SLOT1];
+ SLOT7_2 = &S_CH[7].SLOT[SLOT2];
+ SLOT8_1 = &S_CH[8].SLOT[SLOT1];
+ SLOT8_2 = &S_CH[8].SLOT[SLOT2];
+ /* LFO state */
+ amsIncr = OPL->amsIncr;
+ vibIncr = OPL->vibIncr;
+ ams_table = OPL->ams_table;
+ vib_table = OPL->vib_table;
+ }
+ R_CH = rythm ? &S_CH[6] : E_CH;
+ for(i = 0; i < length; i++) {
+ /* channel A channel B channel C */
+ /* LFO */
+ ams = ams_table[(amsCnt += amsIncr) >> AMS_SHIFT];
+ vib = vib_table[(vibCnt += vibIncr) >> VIB_SHIFT];
+ outd[0] = 0;
+ /* FM part */
+ for(CH=S_CH; CH < R_CH; CH++)
+ OPL_CALC_CH(CH);
+ /* Rythn part */
+ if(rythm)
+ OPL_CALC_RH(OPL, S_CH);
+ /* limit check */
+ data = CLIP(outd[0], OPL_MINOUT, OPL_MAXOUT);
+ /* store to sound buffer */
+ buf[i << interleave] = data >> OPL_OUTSB;
+ }
+
+ OPL->amsCnt = amsCnt;
+ OPL->vibCnt = vibCnt;
+}
+
+/* ---------- reset a chip ---------- */
+void OPLResetChip(FM_OPL *OPL) {
+ int c,s;
+ int i;
+
+ /* reset chip */
+ OPL->mode = 0; /* normal mode */
+ OPL_STATUS_RESET(OPL, 0x7f);
+ /* reset with register write */
+ OPLWriteReg(OPL, 0x01,0); /* wabesel disable */
+ OPLWriteReg(OPL, 0x02,0); /* Timer1 */
+ OPLWriteReg(OPL, 0x03,0); /* Timer2 */
+ OPLWriteReg(OPL, 0x04,0); /* IRQ mask clear */
+ for(i = 0xff; i >= 0x20; i--)
+ OPLWriteReg(OPL,i,0);
+ /* reset OPerator parameter */
+ for(c = 0; c < OPL->max_ch ;c++ ) {
+ OPL_CH *CH = &OPL->P_CH[c];
+ /* OPL->P_CH[c].PAN = OPN_CENTER; */
+ for(s = 0; s < 2; s++ ) {
+ /* wave table */
+ CH->SLOT[s].wavetable = &SIN_TABLE[0];
+ /* CH->SLOT[s].evm = ENV_MOD_RR; */
+ CH->SLOT[s].evc = EG_OFF;
+ CH->SLOT[s].eve = EG_OFF + 1;
+ CH->SLOT[s].evs = 0;
+ }
+ }
+}
+
+/* ---------- Create a virtual YM3812 ---------- */
+/* 'rate' is sampling rate and 'bufsiz' is the size of the */
+FM_OPL *OPLCreate(int type, int clock, int rate) {
+ char *ptr;
+ FM_OPL *OPL;
+ int state_size;
+ int max_ch = 9; /* normaly 9 channels */
+
+ if( OPL_LockTable() == -1)
+ return NULL;
+ /* allocate OPL state space */
+ state_size = sizeof(FM_OPL);
+ state_size += sizeof(OPL_CH) * max_ch;
+
+ /* allocate memory block */
+ ptr = (char *)calloc(state_size, 1);
+ if(ptr == NULL)
+ return NULL;
+
+ /* clear */
+ memset(ptr, 0, state_size);
+ OPL = (FM_OPL *)ptr; ptr += sizeof(FM_OPL);
+ OPL->P_CH = (OPL_CH *)ptr; ptr += sizeof(OPL_CH) * max_ch;
+
+ /* set channel state pointer */
+ OPL->type = type;
+ OPL->clock = clock;
+ OPL->rate = rate;
+ OPL->max_ch = max_ch;
+
+ /* init grobal tables */
+ OPL_initalize(OPL);
+
+ /* reset chip */
+ OPLResetChip(OPL);
+ return OPL;
+}
+
+/* ---------- Destroy one of vietual YM3812 ---------- */
+void OPLDestroy(FM_OPL *OPL) {
+ OPL_UnLockTable();
+ free(OPL);
+}
+
+/* ---------- Option handlers ---------- */
+void OPLSetTimerHandler(FM_OPL *OPL, OPL_TIMERHANDLER TimerHandler,int channelOffset) {
+ OPL->TimerHandler = TimerHandler;
+ OPL->TimerParam = channelOffset;
+}
+
+void OPLSetIRQHandler(FM_OPL *OPL, OPL_IRQHANDLER IRQHandler, int param) {
+ OPL->IRQHandler = IRQHandler;
+ OPL->IRQParam = param;
+}
+
+void OPLSetUpdateHandler(FM_OPL *OPL, OPL_UPDATEHANDLER UpdateHandler,int param) {
+ OPL->UpdateHandler = UpdateHandler;
+ OPL->UpdateParam = param;
+}
+
+/* ---------- YM3812 I/O interface ---------- */
+int OPLWrite(FM_OPL *OPL,int a,int v) {
+ if(!(a & 1)) { /* address port */
+ OPL->address = v & 0xff;
+ } else { /* data port */
+ if(OPL->UpdateHandler)
+ OPL->UpdateHandler(OPL->UpdateParam,0);
+ OPLWriteReg(OPL, OPL->address,v);
+ }
+ return OPL->status >> 7;
+}
+
+unsigned char OPLRead(FM_OPL *OPL,int a) {
+ if(!(a & 1)) { /* status port */
+ return OPL->status & (OPL->statusmask | 0x80);
+ }
+
+ return 0;
+}
+
+int OPLTimerOver(FM_OPL *OPL, int c) {
+ if(c) { /* Timer B */
+ OPL_STATUS_SET(OPL, 0x20);
+ } else { /* Timer A */
+ OPL_STATUS_SET(OPL, 0x40);
+ /* CSM mode key,TL controll */
+ if(OPL->mode & 0x80) { /* CSM mode total level latch and auto key on */
+ int ch;
+ if(OPL->UpdateHandler)
+ OPL->UpdateHandler(OPL->UpdateParam,0);
+ for(ch = 0; ch < 9; ch++)
+ CSMKeyControll(&OPL->P_CH[ch]);
+ }
+ }
+ /* reload timer */
+ if (OPL->TimerHandler)
+ (OPL->TimerHandler)(OPL->TimerParam + c, (double)OPL->T[c] * OPL->TimerBase);
+ return OPL->status >> 7;
+}
+
+FM_OPL *makeAdlibOPL(int rate) {
+ // We need to emulate one YM3812 chip
+ int env_bits = FMOPL_ENV_BITS_HQ;
+ int eg_ent = FMOPL_EG_ENT_HQ;
+
+ OPLBuildTables(env_bits, eg_ent);
+ return OPLCreate(OPL_TYPE_YM3812, 3579545, rate);
+}
+
--- /dev/null
+++ b/opl/fmopl.h
@@ -1,0 +1,167 @@
+/* This file is derived from fmopl.h from ScummVM.
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * LGPL licensed version of MAMEs fmopl (V0.37a modified) by
+ * Tatsuyuki Satoh. Included from LGPL'ed AdPlug.
+ */
+
+
+#ifndef OPL_FMOPL_H
+#define OPL_FMOPL_H
+
+#include "inttypes.h"
+
+enum {
+ FMOPL_ENV_BITS_HQ = 16,
+ FMOPL_ENV_BITS_MQ = 8,
+ FMOPL_ENV_BITS_LQ = 8,
+ FMOPL_EG_ENT_HQ = 4096,
+ FMOPL_EG_ENT_MQ = 1024,
+ FMOPL_EG_ENT_LQ = 128
+};
+
+typedef void (*OPL_TIMERHANDLER)(int channel,double interval_Sec);
+typedef void (*OPL_IRQHANDLER)(int param,int irq);
+typedef void (*OPL_UPDATEHANDLER)(int param,int min_interval_us);
+
+#define OPL_TYPE_WAVESEL 0x01 /* waveform select */
+
+/* Saving is necessary for member of the 'R' mark for suspend/resume */
+/* ---------- OPL one of slot ---------- */
+typedef struct fm_opl_slot {
+ int TL; /* total level :TL << 8 */
+ int TLL; /* adjusted now TL */
+ uint8_t KSR; /* key scale rate :(shift down bit) */
+ int *AR; /* attack rate :&AR_TABLE[AR<<2] */
+ int *DR; /* decay rate :&DR_TABLE[DR<<2] */
+ int SL; /* sustain level :SL_TABLE[SL] */
+ int *RR; /* release rate :&DR_TABLE[RR<<2] */
+ uint8_t ksl; /* keyscale level :(shift down bits) */
+ uint8_t ksr; /* key scale rate :kcode>>KSR */
+ unsigned int mul; /* multiple :ML_TABLE[ML] */
+ unsigned int Cnt; /* frequency count */
+ unsigned int Incr; /* frequency step */
+
+ /* envelope generator state */
+ uint8_t eg_typ;/* envelope type flag */
+ uint8_t evm; /* envelope phase */
+ int evc; /* envelope counter */
+ int eve; /* envelope counter end point */
+ int evs; /* envelope counter step */
+ int evsa; /* envelope step for AR :AR[ksr] */
+ int evsd; /* envelope step for DR :DR[ksr] */
+ int evsr; /* envelope step for RR :RR[ksr] */
+
+ /* LFO */
+ uint8_t ams; /* ams flag */
+ uint8_t vib; /* vibrate flag */
+ /* wave selector */
+ int **wavetable;
+} OPL_SLOT;
+
+/* ---------- OPL one of channel ---------- */
+typedef struct fm_opl_channel {
+ OPL_SLOT SLOT[2];
+ uint8_t CON; /* connection type */
+ uint8_t FB; /* feed back :(shift down bit)*/
+ int *connect1; /* slot1 output pointer */
+ int *connect2; /* slot2 output pointer */
+ int op1_out[2]; /* slot1 output for selfeedback */
+
+ /* phase generator state */
+ unsigned int block_fnum; /* block+fnum */
+ uint8_t kcode; /* key code : KeyScaleCode */
+ unsigned int fc; /* Freq. Increment base */
+ unsigned int ksl_base; /* KeyScaleLevel Base step */
+ uint8_t keyon; /* key on/off flag */
+} OPL_CH;
+
+/* OPL state */
+typedef struct fm_opl_f {
+ uint8_t type; /* chip type */
+ int clock; /* master clock (Hz) */
+ int rate; /* sampling rate (Hz) */
+ double freqbase; /* frequency base */
+ double TimerBase; /* Timer base time (==sampling time) */
+ uint8_t address; /* address register */
+ uint8_t status; /* status flag */
+ uint8_t statusmask; /* status mask */
+ unsigned int mode; /* Reg.08 : CSM , notesel,etc. */
+
+ /* Timer */
+ int T[2]; /* timer counter */
+ uint8_t st[2]; /* timer enable */
+
+ /* FM channel slots */
+ OPL_CH *P_CH; /* pointer of CH */
+ int max_ch; /* maximum channel */
+
+ /* Rythm sention */
+ uint8_t rythm; /* Rythm mode , key flag */
+
+ /* time tables */
+ int AR_TABLE[76]; /* atttack rate tables */
+ int DR_TABLE[76]; /* decay rate tables */
+ unsigned int FN_TABLE[1024];/* fnumber -> increment counter */
+
+ /* LFO */
+ int *ams_table;
+ int *vib_table;
+ int amsCnt;
+ int amsIncr;
+ int vibCnt;
+ int vibIncr;
+
+ /* wave selector enable flag */
+ uint8_t wavesel;
+
+ /* external event callback handler */
+ OPL_TIMERHANDLER TimerHandler; /* TIMER handler */
+ int TimerParam; /* TIMER parameter */
+ OPL_IRQHANDLER IRQHandler; /* IRQ handler */
+ int IRQParam; /* IRQ parameter */
+ OPL_UPDATEHANDLER UpdateHandler; /* stream update handler */
+ int UpdateParam; /* stream update parameter */
+} FM_OPL;
+
+/* ---------- Generic interface section ---------- */
+#define OPL_TYPE_YM3526 (0)
+#define OPL_TYPE_YM3812 (OPL_TYPE_WAVESEL)
+
+void OPLBuildTables(int ENV_BITS_PARAM, int EG_ENT_PARAM);
+
+FM_OPL *OPLCreate(int type, int clock, int rate);
+void OPLDestroy(FM_OPL *OPL);
+void OPLSetTimerHandler(FM_OPL *OPL, OPL_TIMERHANDLER TimerHandler, int channelOffset);
+void OPLSetIRQHandler(FM_OPL *OPL, OPL_IRQHANDLER IRQHandler, int param);
+void OPLSetUpdateHandler(FM_OPL *OPL, OPL_UPDATEHANDLER UpdateHandler, int param);
+
+void OPLResetChip(FM_OPL *OPL);
+int OPLWrite(FM_OPL *OPL, int a, int v);
+unsigned char OPLRead(FM_OPL *OPL, int a);
+int OPLTimerOver(FM_OPL *OPL, int c);
+void OPLWriteReg(FM_OPL *OPL, int r, int v);
+void YM3812UpdateOne(FM_OPL *OPL, int16_t *buffer, int length, int interleave);
+
+// Factory method
+FM_OPL *makeAdlibOPL(int rate);
+
+#endif
+
--- /dev/null
+++ b/opl/ioperm_sys.c
@@ -1,0 +1,361 @@
+// Emacs style mode select -*- C++ -*-
+//-----------------------------------------------------------------------------
+//
+// Copyright(C) 2002, 2003 Marcel Telka
+// Copyright(C) 2009 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:
+// Interface to the ioperm.sys driver, based on code from the
+// Cygwin ioperm library.
+//
+//-----------------------------------------------------------------------------
+
+#ifdef _WIN32
+
+#include <stdio.h>
+
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#include <winioctl.h>
+
+#include <errno.h>
+
+#include "ioperm_sys.h"
+
+#define IOPERM_FILE L"\\\\.\\ioperm"
+
+#define IOCTL_IOPERM \
+ CTL_CODE(FILE_DEVICE_UNKNOWN, 0xA00, METHOD_BUFFERED, FILE_ANY_ACCESS)
+
+struct ioperm_data
+{
+ unsigned long from;
+ unsigned long num;
+ int turn_on;
+};
+
+// Function pointers for advapi32.dll. This DLL does not exist on
+// Windows 9x, so they are dynamically loaded from the DLL at runtime.
+
+static SC_HANDLE WINAPI (*MyOpenSCManagerW)(wchar_t *lpMachineName,
+ wchar_t *lpDatabaseName,
+ DWORD dwDesiredAccess) = NULL;
+static SC_HANDLE WINAPI (*MyCreateServiceW)(SC_HANDLE hSCManager,
+ wchar_t *lpServiceName,
+ wchar_t *lpDisplayName,
+ DWORD dwDesiredAccess,
+ DWORD dwServiceType,
+ DWORD dwStartType,
+ DWORD dwErrorControl,
+ wchar_t *lpBinaryPathName,
+ wchar_t *lpLoadOrderGroup,
+ LPDWORD lpdwTagId,
+ wchar_t *lpDependencies,
+ wchar_t *lpServiceStartName,
+ wchar_t *lpPassword);
+static SC_HANDLE WINAPI (*MyOpenServiceW)(SC_HANDLE hSCManager,
+ wchar_t *lpServiceName,
+ DWORD dwDesiredAccess);
+static BOOL WINAPI (*MyStartServiceW)(SC_HANDLE hService,
+ DWORD dwNumServiceArgs,
+ wchar_t **lpServiceArgVectors);
+static BOOL WINAPI (*MyControlService)(SC_HANDLE hService,
+ DWORD dwControl,
+ LPSERVICE_STATUS lpServiceStatus);
+static BOOL WINAPI (*MyCloseServiceHandle)(SC_HANDLE hSCObject);
+static BOOL WINAPI (*MyDeleteService)(SC_HANDLE hService);
+
+static struct
+{
+ char *name;
+ void **fn;
+} dll_functions[] = {
+ { "OpenSCManagerW", (void **) &MyOpenSCManagerW },
+ { "CreateServiceW", (void **) &MyCreateServiceW },
+ { "OpenServiceW", (void **) &MyOpenServiceW },
+ { "StartServiceW", (void **) &MyStartServiceW },
+ { "ControlService", (void **) &MyControlService },
+ { "CloseServiceHandle", (void **) &MyCloseServiceHandle },
+ { "DeleteService", (void **) &MyDeleteService },
+};
+
+// Globals
+
+static SC_HANDLE scm = NULL;
+static SC_HANDLE svc = NULL;
+static int service_was_created = 0;
+static int service_was_started = 0;
+
+static int LoadLibraryPointers(void)
+{
+ HMODULE dll;
+ int i;
+
+ // Already loaded?
+
+ if (MyOpenSCManagerW != NULL)
+ {
+ return 1;
+ }
+
+ dll = LoadLibraryW(L"advapi32.dll");
+
+ if (dll == NULL)
+ {
+ fprintf(stderr, "LoadLibraryPointers: Failed to open advapi32.dll\n");
+ return 0;
+ }
+
+ for (i = 0; i < sizeof(dll_functions) / sizeof(*dll_functions); ++i)
+ {
+ *dll_functions[i].fn = GetProcAddress(dll, dll_functions[i].name);
+
+ if (*dll_functions[i].fn == NULL)
+ {
+ fprintf(stderr, "LoadLibraryPointers: Failed to get address "
+ "for '%s'\n", dll_functions[i].name);
+ return 0;
+ }
+ }
+
+ return 1;
+}
+
+int IOperm_EnablePortRange(unsigned int from, unsigned int num, int turn_on)
+{
+ HANDLE h;
+ struct ioperm_data ioperm_data;
+ DWORD BytesReturned;
+ BOOL r;
+
+ h = CreateFileW(IOPERM_FILE, GENERIC_READ, 0, NULL,
+ OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+
+ if (h == INVALID_HANDLE_VALUE)
+ {
+ errno = ENODEV;
+ return -1;
+ }
+
+ ioperm_data.from = from;
+ ioperm_data.num = num;
+ ioperm_data.turn_on = turn_on;
+
+ r = DeviceIoControl(h, IOCTL_IOPERM,
+ &ioperm_data, sizeof ioperm_data,
+ NULL, 0,
+ &BytesReturned, NULL);
+
+ if (!r)
+ {
+ errno = EPERM;
+ }
+
+ CloseHandle(h);
+
+ return r != 0;
+}
+
+// Load ioperm.sys driver.
+// Returns 1 for success, 0 for failure.
+// Remember to call IOperm_UninstallDriver to uninstall the driver later.
+
+int IOperm_InstallDriver(void)
+{
+ wchar_t driver_path[MAX_PATH];
+ int error;
+ int result = 1;
+
+ if (!LoadLibraryPointers())
+ {
+ return 0;
+ }
+
+ scm = MyOpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
+
+ if (scm == NULL)
+ {
+ error = GetLastError();
+ fprintf(stderr, "IOperm_InstallDriver: OpenSCManager failed (%i).\n",
+ error);
+ return 0;
+ }
+
+ // Get the full path to the driver file.
+
+ GetFullPathNameW(L"ioperm.sys", MAX_PATH, driver_path, NULL);
+
+ // Create the service.
+
+ svc = MyCreateServiceW(scm,
+ L"ioperm",
+ L"ioperm support for Cygwin driver",
+ SERVICE_ALL_ACCESS,
+ SERVICE_KERNEL_DRIVER,
+ SERVICE_AUTO_START,
+ SERVICE_ERROR_NORMAL,
+ driver_path,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL);
+
+ if (svc == NULL)
+ {
+ error = GetLastError();
+
+ if (error != ERROR_SERVICE_EXISTS)
+ {
+ fprintf(stderr,
+ "IOperm_InstallDriver: Failed to create service (%i).\n",
+ error);
+ }
+ else
+ {
+ svc = MyOpenServiceW(scm, L"ioperm", SERVICE_ALL_ACCESS);
+
+ if (svc == NULL)
+ {
+ error = GetLastError();
+
+ fprintf(stderr,
+ "IOperm_InstallDriver: Failed to open service (%i).\n",
+ error);
+ }
+ }
+
+ if (svc == NULL)
+ {
+ MyCloseServiceHandle(scm);
+ return 0;
+ }
+ }
+ else
+ {
+ service_was_created = 1;
+ }
+
+ // Start the service. If the service already existed, it might have
+ // already been running as well.
+
+ if (!MyStartServiceW(svc, 0, NULL))
+ {
+ error = GetLastError();
+
+ if (error != ERROR_SERVICE_ALREADY_RUNNING)
+ {
+ fprintf(stderr, "IOperm_InstallDriver: Failed to start service (%i).\n",
+ error);
+
+ result = 0;
+ }
+ else
+ {
+ printf("IOperm_InstallDriver: ioperm driver already running.\n");
+ }
+ }
+ else
+ {
+ printf("IOperm_InstallDriver: ioperm driver installed.\n");
+ service_was_started = 1;
+ }
+
+ // If we failed to start the driver running, we need to clean up
+ // before finishing.
+
+ if (result == 0)
+ {
+ IOperm_UninstallDriver();
+ }
+
+ return result;
+}
+
+int IOperm_UninstallDriver(void)
+{
+ SERVICE_STATUS stat;
+ int result = 1;
+ int error;
+
+ // If we started the service, stop it.
+
+ if (service_was_started)
+ {
+ if (!MyControlService(svc, SERVICE_CONTROL_STOP, &stat))
+ {
+ error = GetLastError();
+
+ if (error == ERROR_SERVICE_NOT_ACTIVE)
+ {
+ fprintf(stderr,
+ "IOperm_UninstallDriver: Service not active? (%i)\n",
+ error);
+ }
+ else
+ {
+ fprintf(stderr,
+ "IOperm_UninstallDriver: Failed to stop service (%i).\n",
+ error);
+ result = 0;
+ }
+ }
+ }
+
+ // If we created the service, delete it.
+
+ if (service_was_created)
+ {
+ if (!MyDeleteService(svc))
+ {
+ error = GetLastError();
+
+ fprintf(stderr,
+ "IOperm_UninstallDriver: DeleteService failed (%i).\n",
+ error);
+
+ result = 0;
+ }
+ else if (service_was_started)
+ {
+ printf("IOperm_UnInstallDriver: ioperm driver uninstalled.\n");
+ }
+ }
+
+ // Close handles.
+
+ if (svc != NULL)
+ {
+ MyCloseServiceHandle(svc);
+ svc = NULL;
+ }
+
+ if (scm != NULL)
+ {
+ MyCloseServiceHandle(scm);
+ scm = NULL;
+ }
+
+ service_was_created = 0;
+ service_was_started = 0;
+
+ return result;
+}
+
+#endif /* #ifndef _WIN32 */
+
--- /dev/null
+++ b/opl/ioperm_sys.h
@@ -1,0 +1,36 @@
+// Emacs style mode select -*- C++ -*-
+//-----------------------------------------------------------------------------
+//
+// Copyright(C) 2002, 2003 Marcel Telka
+// Copyright(C) 2009 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:
+// Interface to the ioperm.sys driver, based on code from the
+// Cygwin ioperm library.
+//
+//-----------------------------------------------------------------------------
+
+#ifndef IOPERM_SYS_H
+#define IOPERM_SYS_H
+
+int IOperm_EnablePortRange(unsigned int from, unsigned int num, int turn_on);
+int IOperm_InstallDriver(void);
+int IOperm_UninstallDriver(void);
+
+#endif /* #ifndef IOPERM_SYS_H */
+
--- /dev/null
+++ b/opl/opl.c
@@ -1,0 +1,466 @@
+// Emacs style mode select -*- C++ -*-
+//-----------------------------------------------------------------------------
+//
+// Copyright(C) 2009 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:
+// OPL interface.
+//
+//-----------------------------------------------------------------------------
+
+#include "config.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#ifdef _WIN32_WCE
+#include "libc_wince.h"
+#endif
+
+#include "SDL.h"
+
+#include "opl.h"
+#include "opl_internal.h"
+
+//#define OPL_DEBUG_TRACE
+
+#ifdef HAVE_IOPERM
+extern opl_driver_t opl_linux_driver;
+#endif
+#if defined(HAVE_LIBI386) || defined(HAVE_LIBAMD64)
+extern opl_driver_t opl_openbsd_driver;
+#endif
+#ifdef _WIN32
+extern opl_driver_t opl_win32_driver;
+#endif
+extern opl_driver_t opl_sdl_driver;
+
+static opl_driver_t *drivers[] =
+{
+#ifdef HAVE_IOPERM
+ &opl_linux_driver,
+#endif
+#if defined(HAVE_LIBI386) || defined(HAVE_LIBAMD64)
+ &opl_openbsd_driver,
+#endif
+#ifdef _WIN32
+ &opl_win32_driver,
+#endif
+ &opl_sdl_driver,
+ NULL
+};
+
+static opl_driver_t *driver = NULL;
+static int init_stage_reg_writes = 1;
+
+unsigned int opl_sample_rate = 22050;
+
+//
+// Init/shutdown code.
+//
+
+// Initialize the specified driver and detect an OPL chip. Returns
+// true if an OPL is detected.
+
+static int InitDriver(opl_driver_t *_driver, unsigned int port_base)
+{
+ // Initialize the driver.
+
+ if (!_driver->init_func(port_base))
+ {
+ return 0;
+ }
+
+ // The driver was initialized okay, so we now have somewhere
+ // to write to. It doesn't mean there's an OPL chip there,
+ // though. Perform the detection sequence to make sure.
+ // (it's done twice, like how Doom does it).
+
+ driver = _driver;
+ init_stage_reg_writes = 1;
+
+ if (!OPL_Detect() || !OPL_Detect())
+ {
+ printf("OPL_Init: No OPL detected using '%s' driver.\n", _driver->name);
+ _driver->shutdown_func();
+ driver = NULL;
+ return 0;
+ }
+
+ // Initialize all registers.
+
+ OPL_InitRegisters();
+
+ init_stage_reg_writes = 0;
+
+ printf("OPL_Init: Using driver '%s'.\n", driver->name);
+
+ return 1;
+}
+
+// Find a driver automatically by trying each in the list.
+
+static int AutoSelectDriver(unsigned int port_base)
+{
+ int i;
+
+ for (i=0; drivers[i] != NULL; ++i)
+ {
+ if (InitDriver(drivers[i], port_base))
+ {
+ return 1;
+ }
+ }
+
+ printf("OPL_Init: Failed to find a working driver.\n");
+
+ return 0;
+}
+
+// Initialize the OPL library. Returns true if initialized
+// successfully.
+
+int OPL_Init(unsigned int port_base)
+{
+ char *driver_name;
+ int i;
+
+ driver_name = getenv("OPL_DRIVER");
+
+ if (driver_name != NULL)
+ {
+ // Search the list until we find the driver with this name.
+
+ for (i=0; drivers[i] != NULL; ++i)
+ {
+ if (!strcmp(driver_name, drivers[i]->name))
+ {
+ if (InitDriver(drivers[i], port_base))
+ {
+ return 1;
+ }
+ else
+ {
+ printf("OPL_Init: Failed to initialize "
+ "driver: '%s'.\n", driver_name);
+ return 0;
+ }
+ }
+ }
+
+ printf("OPL_Init: unknown driver: '%s'.\n", driver_name);
+
+ return 0;
+ }
+ else
+ {
+ return AutoSelectDriver(port_base);
+ }
+}
+
+// Shut down the OPL library.
+
+void OPL_Shutdown(void)
+{
+ if (driver != NULL)
+ {
+ driver->shutdown_func();
+ driver = NULL;
+ }
+}
+
+// Set the sample rate used for software OPL emulation.
+
+void OPL_SetSampleRate(unsigned int rate)
+{
+ opl_sample_rate = rate;
+}
+
+void OPL_WritePort(opl_port_t port, unsigned int value)
+{
+ if (driver != NULL)
+ {
+#ifdef OPL_DEBUG_TRACE
+ printf("OPL_write: %i, %x\n", port, value);
+ fflush(stdout);
+#endif
+ driver->write_port_func(port, value);
+ }
+}
+
+unsigned int OPL_ReadPort(opl_port_t port)
+{
+ if (driver != NULL)
+ {
+ unsigned int result;
+
+#ifdef OPL_DEBUG_TRACE
+ printf("OPL_read: %i...\n", port);
+ fflush(stdout);
+#endif
+
+ result = driver->read_port_func(port);
+
+#ifdef OPL_DEBUG_TRACE
+ printf("OPL_read: %i -> %x\n", port, result);
+ fflush(stdout);
+#endif
+
+ return result;
+ }
+ else
+ {
+ return 0;
+ }
+}
+
+//
+// Higher-level functions, based on the lower-level functions above
+// (register write, etc).
+//
+
+unsigned int OPL_ReadStatus(void)
+{
+ return OPL_ReadPort(OPL_REGISTER_PORT);
+}
+
+// Write an OPL register value
+
+void OPL_WriteRegister(int reg, int value)
+{
+ int i;
+
+ OPL_WritePort(OPL_REGISTER_PORT, reg);
+
+ // For timing, read the register port six times after writing the
+ // register number to cause the appropriate delay
+
+ for (i=0; i<6; ++i)
+ {
+ // An oddity of the Doom OPL code: at startup initialization,
+ // the spacing here is performed by reading from the register
+ // port; after initialization, the data port is read, instead.
+
+ if (init_stage_reg_writes)
+ {
+ OPL_ReadPort(OPL_REGISTER_PORT);
+ }
+ else
+ {
+ OPL_ReadPort(OPL_DATA_PORT);
+ }
+ }
+
+ OPL_WritePort(OPL_DATA_PORT, value);
+
+ // Read the register port 24 times after writing the value to
+ // cause the appropriate delay
+
+ for (i=0; i<24; ++i)
+ {
+ OPL_ReadStatus();
+ }
+}
+
+// Detect the presence of an OPL chip
+
+int OPL_Detect(void)
+{
+ int result1, result2;
+ int i;
+
+ // Reset both timers:
+ OPL_WriteRegister(OPL_REG_TIMER_CTRL, 0x60);
+
+ // Enable interrupts:
+ OPL_WriteRegister(OPL_REG_TIMER_CTRL, 0x80);
+
+ // Read status
+ result1 = OPL_ReadStatus();
+
+ // Set timer:
+ OPL_WriteRegister(OPL_REG_TIMER1, 0xff);
+
+ // Start timer 1:
+ OPL_WriteRegister(OPL_REG_TIMER_CTRL, 0x21);
+
+ // Wait for 80 microseconds
+ // This is how Doom does it:
+
+ for (i=0; i<200; ++i)
+ {
+ OPL_ReadStatus();
+ }
+
+ OPL_Delay(1);
+
+ // Read status
+ result2 = OPL_ReadStatus();
+
+ // Reset both timers:
+ OPL_WriteRegister(OPL_REG_TIMER_CTRL, 0x60);
+
+ // Enable interrupts:
+ OPL_WriteRegister(OPL_REG_TIMER_CTRL, 0x80);
+
+ return (result1 & 0xe0) == 0x00
+ && (result2 & 0xe0) == 0xc0;
+}
+
+// Initialize registers on startup
+
+void OPL_InitRegisters(void)
+{
+ int r;
+
+ // Initialize level registers
+
+ for (r=OPL_REGS_LEVEL; r <= OPL_REGS_LEVEL + OPL_NUM_OPERATORS; ++r)
+ {
+ OPL_WriteRegister(r, 0x3f);
+ }
+
+ // Initialize other registers
+ // These two loops write to registers that actually don't exist,
+ // but this is what Doom does ...
+ // Similarly, the <= is also intenational.
+
+ for (r=OPL_REGS_ATTACK; r <= OPL_REGS_WAVEFORM + OPL_NUM_OPERATORS; ++r)
+ {
+ OPL_WriteRegister(r, 0x00);
+ }
+
+ // More registers ...
+
+ for (r=1; r < OPL_REGS_LEVEL; ++r)
+ {
+ OPL_WriteRegister(r, 0x00);
+ }
+
+ // Re-initialize the low registers:
+
+ // Reset both timers and enable interrupts:
+ OPL_WriteRegister(OPL_REG_TIMER_CTRL, 0x60);
+ OPL_WriteRegister(OPL_REG_TIMER_CTRL, 0x80);
+
+ // "Allow FM chips to control the waveform of each operator":
+ OPL_WriteRegister(OPL_REG_WAVEFORM_ENABLE, 0x20);
+
+ // Keyboard split point on (?)
+ OPL_WriteRegister(OPL_REG_FM_MODE, 0x40);
+}
+
+//
+// Timer functions.
+//
+
+void OPL_SetCallback(unsigned int ms, opl_callback_t callback, void *data)
+{
+ if (driver != NULL)
+ {
+ driver->set_callback_func(ms, callback, data);
+ }
+}
+
+void OPL_ClearCallbacks(void)
+{
+ if (driver != NULL)
+ {
+ driver->clear_callbacks_func();
+ }
+}
+
+void OPL_Lock(void)
+{
+ if (driver != NULL)
+ {
+ driver->lock_func();
+ }
+}
+
+void OPL_Unlock(void)
+{
+ if (driver != NULL)
+ {
+ driver->unlock_func();
+ }
+}
+
+typedef struct
+{
+ int finished;
+
+ SDL_mutex *mutex;
+ SDL_cond *cond;
+} delay_data_t;
+
+static void DelayCallback(void *_delay_data)
+{
+ delay_data_t *delay_data = _delay_data;
+
+ SDL_LockMutex(delay_data->mutex);
+ delay_data->finished = 1;
+
+ SDL_CondSignal(delay_data->cond);
+
+ SDL_UnlockMutex(delay_data->mutex);
+}
+
+void OPL_Delay(unsigned int ms)
+{
+ delay_data_t delay_data;
+
+ if (driver == NULL)
+ {
+ return;
+ }
+
+ // Create a callback that will signal this thread after the
+ // specified time.
+
+ delay_data.finished = 0;
+ delay_data.mutex = SDL_CreateMutex();
+ delay_data.cond = SDL_CreateCond();
+
+ OPL_SetCallback(ms, DelayCallback, &delay_data);
+
+ // Wait until the callback is invoked.
+
+ SDL_LockMutex(delay_data.mutex);
+
+ while (!delay_data.finished)
+ {
+ SDL_CondWait(delay_data.cond, delay_data.mutex);
+ }
+
+ SDL_UnlockMutex(delay_data.mutex);
+
+ // Clean up.
+
+ SDL_DestroyMutex(delay_data.mutex);
+ SDL_DestroyCond(delay_data.cond);
+}
+
+void OPL_SetPaused(int paused)
+{
+ if (driver != NULL)
+ {
+ driver->set_paused_func(paused);
+ }
+}
+
--- /dev/null
+++ b/opl/opl.h
@@ -1,0 +1,137 @@
+// Emacs style mode select -*- C++ -*-
+//-----------------------------------------------------------------------------
+//
+// Copyright(C) 2009 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:
+// OPL interface.
+//
+//-----------------------------------------------------------------------------
+
+
+#ifndef OPL_OPL_H
+#define OPL_OPL_H
+
+typedef void (*opl_callback_t)(void *data);
+
+typedef enum
+{
+ OPL_REGISTER_PORT = 0,
+ OPL_DATA_PORT = 1
+} opl_port_t;
+
+#define OPL_NUM_OPERATORS 21
+#define OPL_NUM_VOICES 9
+
+#define OPL_REG_WAVEFORM_ENABLE 0x01
+#define OPL_REG_TIMER1 0x02
+#define OPL_REG_TIMER2 0x03
+#define OPL_REG_TIMER_CTRL 0x04
+#define OPL_REG_FM_MODE 0x08
+
+// Operator registers (21 of each):
+
+#define OPL_REGS_TREMOLO 0x20
+#define OPL_REGS_LEVEL 0x40
+#define OPL_REGS_ATTACK 0x60
+#define OPL_REGS_SUSTAIN 0x80
+#define OPL_REGS_WAVEFORM 0xE0
+
+// Voice registers (9 of each):
+
+#define OPL_REGS_FREQ_1 0xA0
+#define OPL_REGS_FREQ_2 0xB0
+#define OPL_REGS_FEEDBACK 0xC0
+
+//
+// Low-level functions.
+//
+
+// Initialize the OPL subsystem.
+
+int OPL_Init(unsigned int port_base);
+
+// Shut down the OPL subsystem.
+
+void OPL_Shutdown(void);
+
+// Set the sample rate used for software emulation.
+
+void OPL_SetSampleRate(unsigned int rate);
+
+// Write to one of the OPL I/O ports:
+
+void OPL_WritePort(opl_port_t port, unsigned int value);
+
+// Read from one of the OPL I/O ports:
+
+unsigned int OPL_ReadPort(opl_port_t port);
+
+//
+// Higher-level functions.
+//
+
+// Read the cuurrent status byte of the OPL chip.
+
+unsigned int OPL_ReadStatus(void);
+
+// Write to an OPL register.
+
+void OPL_WriteRegister(int reg, int value);
+
+// Perform a detection sequence to determine that an
+// OPL chip is present.
+
+int OPL_Detect(void);
+
+// Initialize all registers, performed on startup.
+
+void OPL_InitRegisters(void);
+
+//
+// Timer callback functions.
+//
+
+// Set a timer callback. After the specified number of milliseconds
+// have elapsed, the callback will be invoked.
+
+void OPL_SetCallback(unsigned int ms, opl_callback_t callback, void *data);
+
+// Clear all OPL callbacks that have been set.
+
+void OPL_ClearCallbacks(void);
+
+// Begin critical section, during which, OPL callbacks will not be
+// invoked.
+
+void OPL_Lock(void);
+
+// End critical section.
+
+void OPL_Unlock(void);
+
+// Block until the specified number of milliseconds have elapsed.
+
+void OPL_Delay(unsigned int ms);
+
+// Pause the OPL callbacks.
+
+void OPL_SetPaused(int paused);
+
+#endif
+
--- /dev/null
+++ b/opl/opl_internal.h
@@ -1,0 +1,64 @@
+// Emacs style mode select -*- C++ -*-
+//-----------------------------------------------------------------------------
+//
+// Copyright(C) 2009 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:
+// OPL internal interface.
+//
+//-----------------------------------------------------------------------------
+
+
+#ifndef OPL_INTERNAL_H
+#define OPL_INTERNAL_H
+
+#include "opl.h"
+
+typedef int (*opl_init_func)(unsigned int port_base);
+typedef void (*opl_shutdown_func)(void);
+typedef unsigned int (*opl_read_port_func)(opl_port_t port);
+typedef void (*opl_write_port_func)(opl_port_t port, unsigned int value);
+typedef void (*opl_set_callback_func)(unsigned int ms,
+ opl_callback_t callback,
+ void *data);
+typedef void (*opl_clear_callbacks_func)(void);
+typedef void (*opl_lock_func)(void);
+typedef void (*opl_unlock_func)(void);
+typedef void (*opl_set_paused_func)(int paused);
+
+typedef struct
+{
+ char *name;
+
+ opl_init_func init_func;
+ opl_shutdown_func shutdown_func;
+ opl_read_port_func read_port_func;
+ opl_write_port_func write_port_func;
+ opl_set_callback_func set_callback_func;
+ opl_clear_callbacks_func clear_callbacks_func;
+ opl_lock_func lock_func;
+ opl_unlock_func unlock_func;
+ opl_set_paused_func set_paused_func;
+} opl_driver_t;
+
+// Sample rate to use when doing software emulation.
+
+extern unsigned int opl_sample_rate;
+
+#endif /* #ifndef OPL_INTERNAL_H */
+
--- /dev/null
+++ b/opl/opl_linux.c
@@ -1,0 +1,110 @@
+// Emacs style mode select -*- C++ -*-
+//-----------------------------------------------------------------------------
+//
+// Copyright(C) 2009 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:
+// OPL Linux interface.
+//
+//-----------------------------------------------------------------------------
+
+#include "config.h"
+
+#ifdef HAVE_IOPERM
+
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/io.h>
+
+#include "opl.h"
+#include "opl_internal.h"
+#include "opl_timer.h"
+
+static unsigned int opl_port_base;
+
+static int OPL_Linux_Init(unsigned int port_base)
+{
+ // Try to get permissions:
+
+ if (ioperm(port_base, 2, 1) < 0)
+ {
+ fprintf(stderr, "Failed to get I/O port permissions for 0x%x: %s\n",
+ port_base, strerror(errno));
+
+ if (errno == EPERM)
+ {
+ fprintf(stderr,
+ "\tYou may need to run the program as root in order\n"
+ "\tto acquire I/O port permissions for OPL MIDI playback.\n");
+ }
+
+ return 0;
+ }
+
+ opl_port_base = port_base;
+
+ // Start callback thread
+
+ if (!OPL_Timer_StartThread())
+ {
+ ioperm(port_base, 2, 0);
+ return 0;
+ }
+
+ return 1;
+}
+
+static void OPL_Linux_Shutdown(void)
+{
+ // Stop callback thread
+
+ OPL_Timer_StopThread();
+
+ // Release permissions
+
+ ioperm(opl_port_base, 2, 0);
+}
+
+static unsigned int OPL_Linux_PortRead(opl_port_t port)
+{
+ return inb(opl_port_base + port);
+}
+
+static void OPL_Linux_PortWrite(opl_port_t port, unsigned int value)
+{
+ outb(value, opl_port_base + port);
+}
+
+opl_driver_t opl_linux_driver =
+{
+ "Linux",
+ OPL_Linux_Init,
+ OPL_Linux_Shutdown,
+ OPL_Linux_PortRead,
+ OPL_Linux_PortWrite,
+ OPL_Timer_SetCallback,
+ OPL_Timer_ClearCallbacks,
+ OPL_Timer_Lock,
+ OPL_Timer_Unlock,
+ OPL_Timer_SetPaused
+};
+
+#endif /* #ifdef HAVE_IOPERM */
+
--- /dev/null
+++ b/opl/opl_obsd.c
@@ -1,0 +1,125 @@
+// Emacs style mode select -*- C++ -*-
+//-----------------------------------------------------------------------------
+//
+// Copyright(C) 2009 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:
+// OPL OpenBSD interface (also NetBSD)
+//
+//-----------------------------------------------------------------------------
+
+#include "config.h"
+
+// OpenBSD has a i386_iopl on i386 and amd64_iopl on x86_64,
+// even though they do the same thing. Take care of this
+// here, and map set_iopl to point to the appropriate name.
+
+#if defined(HAVE_LIBI386)
+
+#include <sys/types.h>
+#include <machine/sysarch.h>
+#include <i386/pio.h>
+#define set_iopl i386_iopl
+
+#elif defined(HAVE_LIBAMD64)
+
+#include <sys/types.h>
+#include <machine/sysarch.h>
+#include <amd64/pio.h>
+#define set_iopl amd64_iopl
+
+#else
+#define NO_OBSD_DRIVER
+#endif
+
+// If the above succeeded, proceed with the rest.
+
+#ifndef NO_OBSD_DRIVER
+
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include "opl.h"
+#include "opl_internal.h"
+#include "opl_timer.h"
+
+static unsigned int opl_port_base;
+
+static int OPL_OpenBSD_Init(unsigned int port_base)
+{
+ // Try to get permissions:
+
+ if (set_iopl(3) < 0)
+ {
+ fprintf(stderr, "Failed to get raise I/O privilege level: "
+ "check that you are running as root.\n");
+ return 0;
+ }
+
+ opl_port_base = port_base;
+
+ // Start callback thread
+
+ if (!OPL_Timer_StartThread())
+ {
+ set_iopl(0);
+ return 0;
+ }
+
+ return 1;
+}
+
+static void OPL_OpenBSD_Shutdown(void)
+{
+ // Stop callback thread
+
+ OPL_Timer_StopThread();
+
+ // Release I/O port permissions:
+
+ set_iopl(0);
+}
+
+static unsigned int OPL_OpenBSD_PortRead(opl_port_t port)
+{
+ return inb(opl_port_base + port);
+}
+
+static void OPL_OpenBSD_PortWrite(opl_port_t port, unsigned int value)
+{
+ outb(opl_port_base + port, value);
+}
+
+opl_driver_t opl_openbsd_driver =
+{
+ "OpenBSD",
+ OPL_OpenBSD_Init,
+ OPL_OpenBSD_Shutdown,
+ OPL_OpenBSD_PortRead,
+ OPL_OpenBSD_PortWrite,
+ OPL_Timer_SetCallback,
+ OPL_Timer_ClearCallbacks,
+ OPL_Timer_Lock,
+ OPL_Timer_Unlock,
+ OPL_Timer_SetPaused
+};
+
+#endif /* #ifndef NO_OBSD_DRIVER */
+
--- /dev/null
+++ b/opl/opl_queue.c
@@ -1,0 +1,280 @@
+// Emacs style mode select -*- C++ -*-
+//-----------------------------------------------------------------------------
+//
+// Copyright(C) 2009 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:
+// Queue of waiting callbacks, stored in a binary min heap, so that we
+// can always get the first callback.
+//
+//-----------------------------------------------------------------------------
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "opl_queue.h"
+
+#define MAX_OPL_QUEUE 64
+
+typedef struct
+{
+ opl_callback_t callback;
+ void *data;
+ unsigned int time;
+} opl_queue_entry_t;
+
+struct opl_callback_queue_s
+{
+ opl_queue_entry_t entries[MAX_OPL_QUEUE];
+ unsigned int num_entries;
+};
+
+opl_callback_queue_t *OPL_Queue_Create(void)
+{
+ opl_callback_queue_t *queue;
+
+ queue = malloc(sizeof(opl_callback_queue_t));
+ queue->num_entries = 0;
+
+ return queue;
+}
+
+void OPL_Queue_Destroy(opl_callback_queue_t *queue)
+{
+ free(queue);
+}
+
+int OPL_Queue_IsEmpty(opl_callback_queue_t *queue)
+{
+ return queue->num_entries == 0;
+}
+
+void OPL_Queue_Clear(opl_callback_queue_t *queue)
+{
+ queue->num_entries = 0;
+}
+
+void OPL_Queue_Push(opl_callback_queue_t *queue,
+ opl_callback_t callback, void *data,
+ unsigned int time)
+{
+ int entry_id;
+ int parent_id;
+
+ if (queue->num_entries >= MAX_OPL_QUEUE)
+ {
+ fprintf(stderr, "OPL_Queue_Push: Exceeded maximum callbacks\n");
+ return;
+ }
+
+ // Add to last queue entry.
+
+ entry_id = queue->num_entries;
+ ++queue->num_entries;
+
+ // Shift existing entries down in the heap.
+
+ while (entry_id > 0)
+ {
+ parent_id = (entry_id - 1) / 2;
+
+ // Is the heap condition satisfied?
+
+ if (time >= queue->entries[parent_id].time)
+ {
+ break;
+ }
+
+ // Move the existing entry down in the heap.
+
+ memcpy(&queue->entries[entry_id],
+ &queue->entries[parent_id],
+ sizeof(opl_queue_entry_t));
+
+ // Advance to the parent.
+
+ entry_id = parent_id;
+ }
+
+ // Insert new callback data.
+
+ queue->entries[entry_id].callback = callback;
+ queue->entries[entry_id].data = data;
+ queue->entries[entry_id].time = time;
+}
+
+int OPL_Queue_Pop(opl_callback_queue_t *queue,
+ opl_callback_t *callback, void **data)
+{
+ opl_queue_entry_t *entry;
+ int child1, child2;
+ int i, next_i;
+
+ // Empty?
+
+ if (queue->num_entries <= 0)
+ {
+ return 0;
+ }
+
+ // Store the result:
+
+ *callback = queue->entries[0].callback;
+ *data = queue->entries[0].data;
+
+ // Decrease the heap size, and keep pointer to the last entry in
+ // the heap, which must now be percolated down from the top.
+
+ --queue->num_entries;
+ entry = &queue->entries[queue->num_entries];
+
+ // Percolate down.
+
+ i = 0;
+
+ for (;;)
+ {
+ child1 = i * 2 + 1;
+ child2 = i * 2 + 2;
+
+ if (child1 < queue->num_entries
+ && queue->entries[child1].time < entry->time)
+ {
+ // Left child is less than entry.
+ // Use the minimum of left and right children.
+
+ if (child2 < queue->num_entries
+ && queue->entries[child2].time < queue->entries[child1].time)
+ {
+ next_i = child2;
+ }
+ else
+ {
+ next_i = child1;
+ }
+ }
+ else if (child2 < queue->num_entries
+ && queue->entries[child2].time < entry->time)
+ {
+ // Right child is less than entry. Go down the right side.
+
+ next_i = child2;
+ }
+ else
+ {
+ // Finished percolating.
+ break;
+ }
+
+ // Percolate the next value up and advance.
+
+ memcpy(&queue->entries[i],
+ &queue->entries[next_i],
+ sizeof(opl_queue_entry_t));
+ i = next_i;
+ }
+
+ // Store the old last-entry at its new position.
+
+ memcpy(&queue->entries[i], entry, sizeof(opl_queue_entry_t));
+
+ return 1;
+}
+
+unsigned int OPL_Queue_Peek(opl_callback_queue_t *queue)
+{
+ if (queue->num_entries > 0)
+ {
+ return queue->entries[0].time;
+ }
+ else
+ {
+ return 0;
+ }
+}
+
+#ifdef TEST
+
+#include <assert.h>
+
+static void PrintQueueNode(opl_callback_queue_t *queue, int node, int depth)
+{
+ int i;
+
+ if (node >= queue->num_entries)
+ {
+ return;
+ }
+
+ for (i=0; i<depth * 3; ++i)
+ {
+ printf(" ");
+ }
+
+ printf("%i\n", queue->entries[node].time);
+
+ PrintQueueNode(queue, node * 2 + 1, depth + 1);
+ PrintQueueNode(queue, node * 2 + 2, depth + 1);
+}
+
+static void PrintQueue(opl_callback_queue_t *queue)
+{
+ PrintQueueNode(queue, 0, 0);
+}
+
+int main()
+{
+ opl_callback_queue_t *queue;
+ int iteration;
+
+ queue = OPL_Queue_Create();
+
+ for (iteration=0; iteration<5000; ++iteration)
+ {
+ opl_callback_t callback;
+ void *data;
+ unsigned int time;
+ unsigned int newtime;
+ int i;
+
+ for (i=0; i<MAX_OPL_QUEUE; ++i)
+ {
+ time = rand() % 0x10000;
+ OPL_Queue_Push(queue, NULL, NULL, time);
+ }
+
+ time = 0;
+
+ for (i=0; i<MAX_OPL_QUEUE; ++i)
+ {
+ assert(!OPL_Queue_IsEmpty(queue));
+ newtime = OPL_Queue_Peek(queue);
+ assert(OPL_Queue_Pop(queue, &callback, &data));
+
+ assert(newtime >= time);
+ time = newtime;
+ }
+
+ assert(OPL_Queue_IsEmpty(queue));
+ assert(!OPL_Queue_Pop(queue, &callback, &data));
+ }
+}
+
+#endif
+
--- /dev/null
+++ b/opl/opl_queue.h
@@ -1,0 +1,45 @@
+// Emacs style mode select -*- C++ -*-
+//-----------------------------------------------------------------------------
+//
+// Copyright(C) 2009 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:
+// OPL callback queue.
+//
+//-----------------------------------------------------------------------------
+
+#ifndef OPL_QUEUE_H
+#define OPL_QUEUE_H
+
+#include "opl.h"
+
+typedef struct opl_callback_queue_s opl_callback_queue_t;
+
+opl_callback_queue_t *OPL_Queue_Create(void);
+int OPL_Queue_IsEmpty(opl_callback_queue_t *queue);
+void OPL_Queue_Clear(opl_callback_queue_t *queue);
+void OPL_Queue_Destroy(opl_callback_queue_t *queue);
+void OPL_Queue_Push(opl_callback_queue_t *queue,
+ opl_callback_t callback, void *data,
+ unsigned int time);
+int OPL_Queue_Pop(opl_callback_queue_t *queue,
+ opl_callback_t *callback, void **data);
+unsigned int OPL_Queue_Peek(opl_callback_queue_t *queue);
+
+#endif /* #ifndef OPL_QUEUE_H */
+
--- /dev/null
+++ b/opl/opl_sdl.c
@@ -1,0 +1,452 @@
+// Emacs style mode select -*- C++ -*-
+//-----------------------------------------------------------------------------
+//
+// Copyright(C) 2009 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:
+// OPL SDL interface.
+//
+//-----------------------------------------------------------------------------
+
+#include "config.h"
+
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <assert.h>
+
+#include "SDL.h"
+#include "SDL_mixer.h"
+
+#include "fmopl.h"
+
+#include "opl.h"
+#include "opl_internal.h"
+
+#include "opl_queue.h"
+
+#define MAX_SOUND_SLICE_TIME 100 /* ms */
+
+// When the callback mutex is locked using OPL_Lock, callback functions
+// are not invoked.
+
+static SDL_mutex *callback_mutex = NULL;
+
+// Queue of callbacks waiting to be invoked.
+
+static opl_callback_queue_t *callback_queue;
+
+// Mutex used to control access to the callback queue.
+
+static SDL_mutex *callback_queue_mutex = NULL;
+
+// Current time, in number of samples since startup:
+
+static int current_time;
+
+// If non-zero, playback is currently paused.
+
+static int opl_sdl_paused;
+
+// Time offset (in samples) due to the fact that callbacks
+// were previously paused.
+
+static unsigned int pause_offset;
+
+// OPL software emulator structure.
+
+static FM_OPL *opl_emulator = NULL;
+
+// Temporary mixing buffer used by the mixing callback.
+
+static int16_t *mix_buffer = NULL;
+
+// SDL parameters.
+
+static int sdl_was_initialized = 0;
+static int mixing_freq, mixing_channels;
+static Uint16 mixing_format;
+
+static int SDLIsInitialized(void)
+{
+ int freq, channels;
+ Uint16 format;
+
+ return Mix_QuerySpec(&freq, &format, &channels);
+}
+
+// Advance time by the specified number of samples, invoking any
+// callback functions as appropriate.
+
+static void AdvanceTime(unsigned int nsamples)
+{
+ opl_callback_t callback;
+ void *callback_data;
+
+ SDL_LockMutex(callback_queue_mutex);
+
+ // Advance time.
+
+ current_time += nsamples;
+
+ if (opl_sdl_paused)
+ {
+ pause_offset += nsamples;
+ }
+
+ // Are there callbacks to invoke now? Keep invoking them
+ // until there are none more left.
+
+ while (!OPL_Queue_IsEmpty(callback_queue)
+ && current_time >= OPL_Queue_Peek(callback_queue) + pause_offset)
+ {
+ // Pop the callback from the queue to invoke it.
+
+ if (!OPL_Queue_Pop(callback_queue, &callback, &callback_data))
+ {
+ break;
+ }
+
+ // The mutex stuff here is a bit complicated. We must
+ // hold callback_mutex when we invoke the callback (so that
+ // the control thread can use OPL_Lock() to prevent callbacks
+ // from being invoked), but we must not be holding
+ // callback_queue_mutex, as the callback must be able to
+ // call OPL_SetCallback to schedule new callbacks.
+
+ SDL_UnlockMutex(callback_queue_mutex);
+
+ SDL_LockMutex(callback_mutex);
+ callback(callback_data);
+ SDL_UnlockMutex(callback_mutex);
+
+ SDL_LockMutex(callback_queue_mutex);
+ }
+
+ SDL_UnlockMutex(callback_queue_mutex);
+}
+
+// Call the OPL emulator code to fill the specified buffer.
+
+static void FillBuffer(int16_t *buffer, unsigned int nsamples)
+{
+ unsigned int i;
+
+ // This seems like a reasonable assumption. mix_buffer is
+ // 1 second long, which should always be much longer than the
+ // SDL mix buffer.
+
+ assert(nsamples < mixing_freq);
+
+ YM3812UpdateOne(opl_emulator, mix_buffer, nsamples, 0);
+
+ // Mix into the destination buffer, doubling up into stereo.
+
+ for (i=0; i<nsamples; ++i)
+ {
+ buffer[i * 2] = mix_buffer[i];
+ buffer[i * 2 + 1] = mix_buffer[i];
+ }
+}
+
+// Callback function to fill a new sound buffer:
+
+static void OPL_Mix_Callback(void *udata,
+ Uint8 *byte_buffer,
+ int buffer_bytes)
+{
+ int16_t *buffer;
+ unsigned int buffer_len;
+ unsigned int filled = 0;
+
+ // Buffer length in samples (quadrupled, because of 16-bit and stereo)
+
+ buffer = (int16_t *) byte_buffer;
+ buffer_len = buffer_bytes / 4;
+
+ // Repeatedly call the FMOPL update function until the buffer is
+ // full.
+
+ while (filled < buffer_len)
+ {
+ unsigned int next_callback_time;
+ unsigned int nsamples;
+
+ SDL_LockMutex(callback_queue_mutex);
+
+ // Work out the time until the next callback waiting in
+ // the callback queue must be invoked. We can then fill the
+ // buffer with this many samples.
+
+ if (opl_sdl_paused || OPL_Queue_IsEmpty(callback_queue))
+ {
+ nsamples = buffer_len - filled;
+ }
+ else
+ {
+ next_callback_time = OPL_Queue_Peek(callback_queue) + pause_offset;
+
+ nsamples = next_callback_time - current_time;
+
+ if (nsamples > buffer_len - filled)
+ {
+ nsamples = buffer_len - filled;
+ }
+ }
+
+ SDL_UnlockMutex(callback_queue_mutex);
+
+ // Add emulator output to buffer.
+
+ FillBuffer(buffer + filled * 2, nsamples);
+ filled += nsamples;
+
+ // Invoke callbacks for this point in time.
+
+ AdvanceTime(nsamples);
+ }
+}
+
+static void OPL_SDL_Shutdown(void)
+{
+ Mix_HookMusic(NULL, NULL);
+
+ if (sdl_was_initialized)
+ {
+ Mix_CloseAudio();
+ SDL_QuitSubSystem(SDL_INIT_AUDIO);
+ OPL_Queue_Destroy(callback_queue);
+ free(mix_buffer);
+ sdl_was_initialized = 0;
+ }
+
+ if (opl_emulator != NULL)
+ {
+ OPLDestroy(opl_emulator);
+ opl_emulator = NULL;
+ }
+
+ if (callback_mutex != NULL)
+ {
+ SDL_DestroyMutex(callback_mutex);
+ callback_mutex = NULL;
+ }
+
+ if (callback_queue_mutex != NULL)
+ {
+ SDL_DestroyMutex(callback_queue_mutex);
+ callback_queue_mutex = NULL;
+ }
+}
+
+// Callback when a timer expires.
+
+static void TimerOver(void *data)
+{
+ int channel = (int) data;
+
+ OPLTimerOver(opl_emulator, channel);
+}
+
+// Callback invoked when the emulator code wants to set a timer.
+
+static void TimerHandler(int channel, double interval_seconds)
+{
+ unsigned int interval_samples;
+
+ interval_samples = (int) (interval_seconds * mixing_freq);
+
+ SDL_LockMutex(callback_queue_mutex);
+ OPL_Queue_Push(callback_queue, TimerOver, (void *) channel,
+ current_time - pause_offset + interval_samples);
+ SDL_UnlockMutex(callback_queue_mutex);
+}
+
+static unsigned int GetSliceSize(void)
+{
+ int limit;
+ int n;
+
+ limit = (opl_sample_rate * MAX_SOUND_SLICE_TIME) / 1000;
+
+ // Try all powers of two, not exceeding the limit.
+
+ for (n=0;; ++n)
+ {
+ // 2^n <= limit < 2^n+1 ?
+
+ if ((1 << (n + 1)) > limit)
+ {
+ return (1 << n);
+ }
+ }
+
+ // Should never happen?
+
+ return 1024;
+}
+
+static int OPL_SDL_Init(unsigned int port_base)
+{
+ // Check if SDL_mixer has been opened already
+ // If not, we must initialize it now
+
+ if (!SDLIsInitialized())
+ {
+ if (SDL_Init(SDL_INIT_AUDIO) < 0)
+ {
+ fprintf(stderr, "Unable to set up sound.\n");
+ return 0;
+ }
+
+ if (Mix_OpenAudio(opl_sample_rate, AUDIO_S16SYS, 2, GetSliceSize()) < 0)
+ {
+ fprintf(stderr, "Error initialising SDL_mixer: %s\n", Mix_GetError());
+
+ SDL_QuitSubSystem(SDL_INIT_AUDIO);
+ return 0;
+ }
+
+ SDL_PauseAudio(0);
+
+ // When this module shuts down, it has the responsibility to
+ // shut down SDL.
+
+ sdl_was_initialized = 1;
+ }
+ else
+ {
+ sdl_was_initialized = 0;
+ }
+
+ opl_sdl_paused = 0;
+ pause_offset = 0;
+
+ // Queue structure of callbacks to invoke.
+
+ callback_queue = OPL_Queue_Create();
+ current_time = 0;
+
+ // Get the mixer frequency, format and number of channels.
+
+ Mix_QuerySpec(&mixing_freq, &mixing_format, &mixing_channels);
+
+ // Only supports AUDIO_S16SYS
+
+ if (mixing_format != AUDIO_S16SYS || mixing_channels != 2)
+ {
+ fprintf(stderr,
+ "OPL_SDL only supports native signed 16-bit LSB, "
+ "stereo format!\n");
+
+ OPL_SDL_Shutdown();
+ return 0;
+ }
+
+ // Mix buffer:
+
+ mix_buffer = malloc(mixing_freq * 2);
+
+ // Create the emulator structure:
+
+ opl_emulator = makeAdlibOPL(mixing_freq);
+
+ if (opl_emulator == NULL)
+ {
+ fprintf(stderr, "Failed to initialize software OPL emulator!\n");
+ OPL_SDL_Shutdown();
+ return 0;
+ }
+
+ OPLSetTimerHandler(opl_emulator, TimerHandler, 0);
+
+ callback_mutex = SDL_CreateMutex();
+ callback_queue_mutex = SDL_CreateMutex();
+
+ // TODO: This should be music callback? or-?
+ Mix_HookMusic(OPL_Mix_Callback, NULL);
+
+ return 1;
+}
+
+static unsigned int OPL_SDL_PortRead(opl_port_t port)
+{
+ if (opl_emulator != NULL)
+ {
+ return OPLRead(opl_emulator, port);
+ }
+ else
+ {
+ return 0;
+ }
+}
+
+static void OPL_SDL_PortWrite(opl_port_t port, unsigned int value)
+{
+ if (opl_emulator != NULL)
+ {
+ OPLWrite(opl_emulator, port, value);
+ }
+}
+
+static void OPL_SDL_SetCallback(unsigned int ms,
+ opl_callback_t callback,
+ void *data)
+{
+ SDL_LockMutex(callback_queue_mutex);
+ OPL_Queue_Push(callback_queue, callback, data,
+ current_time - pause_offset + (ms * mixing_freq) / 1000);
+ SDL_UnlockMutex(callback_queue_mutex);
+}
+
+static void OPL_SDL_ClearCallbacks(void)
+{
+ SDL_LockMutex(callback_queue_mutex);
+ OPL_Queue_Clear(callback_queue);
+ SDL_UnlockMutex(callback_queue_mutex);
+}
+
+static void OPL_SDL_Lock(void)
+{
+ SDL_LockMutex(callback_mutex);
+}
+
+static void OPL_SDL_Unlock(void)
+{
+ SDL_UnlockMutex(callback_mutex);
+}
+
+static void OPL_SDL_SetPaused(int paused)
+{
+ opl_sdl_paused = paused;
+}
+
+opl_driver_t opl_sdl_driver =
+{
+ "SDL",
+ OPL_SDL_Init,
+ OPL_SDL_Shutdown,
+ OPL_SDL_PortRead,
+ OPL_SDL_PortWrite,
+ OPL_SDL_SetCallback,
+ OPL_SDL_ClearCallbacks,
+ OPL_SDL_Lock,
+ OPL_SDL_Unlock,
+ OPL_SDL_SetPaused
+};
+
--- /dev/null
+++ b/opl/opl_timer.c
@@ -1,0 +1,251 @@
+// Emacs style mode select -*- C++ -*-
+//-----------------------------------------------------------------------------
+//
+// Copyright(C) 2009 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:
+// OPL timer thread.
+// Once started using OPL_Timer_StartThread, the thread sleeps,
+// waking up to invoke callbacks set using OPL_Timer_SetCallback.
+//
+//-----------------------------------------------------------------------------
+
+#include "SDL.h"
+
+#include "opl_timer.h"
+#include "opl_queue.h"
+
+typedef enum
+{
+ THREAD_STATE_STOPPED,
+ THREAD_STATE_RUNNING,
+ THREAD_STATE_STOPPING,
+} thread_state_t;
+
+static SDL_Thread *timer_thread = NULL;
+static thread_state_t timer_thread_state;
+static int current_time;
+
+// If non-zero, callbacks are currently paused.
+
+static int opl_timer_paused;
+
+// Offset in milliseconds to adjust time due to the fact that playback
+// was paused.
+
+static unsigned int pause_offset = 0;
+
+// Queue of callbacks waiting to be invoked.
+// The callback queue mutex is held while the callback queue structure
+// or current_time is being accessed.
+
+static opl_callback_queue_t *callback_queue;
+static SDL_mutex *callback_queue_mutex;
+
+// The timer mutex is held while timer callback functions are being
+// invoked, so that the calling code can prevent clashes.
+
+static SDL_mutex *timer_mutex;
+
+// Returns true if there is a callback at the head of the queue ready
+// to be invoked. Otherwise, next_time is set to the time when the
+// timer thread must wake up again to check.
+
+static int CallbackWaiting(unsigned int *next_time)
+{
+ // If paused, just wait in 50ms increments until unpaused.
+ // Update pause_offset so after we unpause, the callback
+ // times will be right.
+
+ if (opl_timer_paused)
+ {
+ *next_time = current_time + 50;
+ pause_offset += 50;
+ return 0;
+ }
+
+ // If there are no queued callbacks, sleep for 50ms at a time
+ // until a callback is added.
+
+ if (OPL_Queue_IsEmpty(callback_queue))
+ {
+ *next_time = current_time + 50;
+ return 0;
+ }
+
+ // Read the time of the first callback in the queue.
+ // If the time for the callback has not yet arrived,
+ // we must sleep until the callback time.
+
+ *next_time = OPL_Queue_Peek(callback_queue) + pause_offset;
+
+ return *next_time <= current_time;
+}
+
+static unsigned int GetNextTime(void)
+{
+ opl_callback_t callback;
+ void *callback_data;
+ unsigned int next_time;
+ int have_callback;
+
+ // Keep running through callbacks until there are none ready to
+ // run. When we run out of callbacks, next_time will be set.
+
+ do
+ {
+ SDL_LockMutex(callback_queue_mutex);
+
+ // Check if the callback at the head of the list is ready to
+ // be invoked. If so, pop from the head of the queue.
+
+ have_callback = CallbackWaiting(&next_time);
+
+ if (have_callback)
+ {
+ OPL_Queue_Pop(callback_queue, &callback, &callback_data);
+ }
+
+ SDL_UnlockMutex(callback_queue_mutex);
+
+ // Now invoke the callback, if we have one.
+ // The timer mutex is held while the callback is invoked.
+
+ if (have_callback)
+ {
+ SDL_LockMutex(timer_mutex);
+ callback(callback_data);
+ SDL_UnlockMutex(timer_mutex);
+ }
+ } while (have_callback);
+
+ return next_time;
+}
+
+static int ThreadFunction(void *unused)
+{
+ unsigned int next_time;
+ unsigned int now;
+
+ // Keep running until OPL_Timer_StopThread is called.
+
+ while (timer_thread_state == THREAD_STATE_RUNNING)
+ {
+ // Get the next time that we must sleep until, and
+ // wait until that time.
+
+ next_time = GetNextTime();
+ now = SDL_GetTicks();
+
+ if (next_time > now)
+ {
+ SDL_Delay(next_time - now);
+ }
+
+ // Update the current time.
+
+ SDL_LockMutex(callback_queue_mutex);
+ current_time = next_time;
+ SDL_UnlockMutex(callback_queue_mutex);
+ }
+
+ timer_thread_state = THREAD_STATE_STOPPED;
+
+ return 0;
+}
+
+static void InitResources(void)
+{
+ callback_queue = OPL_Queue_Create();
+ timer_mutex = SDL_CreateMutex();
+ callback_queue_mutex = SDL_CreateMutex();
+}
+
+static void FreeResources(void)
+{
+ OPL_Queue_Destroy(callback_queue);
+ SDL_DestroyMutex(callback_queue_mutex);
+ SDL_DestroyMutex(timer_mutex);
+}
+
+int OPL_Timer_StartThread(void)
+{
+ InitResources();
+
+ timer_thread_state = THREAD_STATE_RUNNING;
+ current_time = SDL_GetTicks();
+ opl_timer_paused = 0;
+ pause_offset = 0;
+
+ timer_thread = SDL_CreateThread(ThreadFunction, NULL);
+
+ if (timer_thread == NULL)
+ {
+ timer_thread_state = THREAD_STATE_STOPPED;
+ FreeResources();
+
+ return 0;
+ }
+
+ return 1;
+}
+
+void OPL_Timer_StopThread(void)
+{
+ timer_thread_state = THREAD_STATE_STOPPING;
+
+ while (timer_thread_state != THREAD_STATE_STOPPED)
+ {
+ SDL_Delay(1);
+ }
+
+ FreeResources();
+}
+
+void OPL_Timer_SetCallback(unsigned int ms, opl_callback_t callback, void *data)
+{
+ SDL_LockMutex(callback_queue_mutex);
+ OPL_Queue_Push(callback_queue, callback, data,
+ current_time + ms - pause_offset);
+ SDL_UnlockMutex(callback_queue_mutex);
+}
+
+void OPL_Timer_ClearCallbacks(void)
+{
+ SDL_LockMutex(callback_queue_mutex);
+ OPL_Queue_Clear(callback_queue);
+ SDL_UnlockMutex(callback_queue_mutex);
+}
+
+void OPL_Timer_Lock(void)
+{
+ SDL_LockMutex(timer_mutex);
+}
+
+void OPL_Timer_Unlock(void)
+{
+ SDL_UnlockMutex(timer_mutex);
+}
+
+void OPL_Timer_SetPaused(int paused)
+{
+ SDL_LockMutex(callback_queue_mutex);
+ opl_timer_paused = paused;
+ SDL_UnlockMutex(callback_queue_mutex);
+}
+
--- /dev/null
+++ b/opl/opl_timer.h
@@ -1,0 +1,42 @@
+// Emacs style mode select -*- C++ -*-
+//-----------------------------------------------------------------------------
+//
+// Copyright(C) 2009 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:
+// OPL timer thread.
+//
+//-----------------------------------------------------------------------------
+
+#ifndef OPL_TIMER_H
+#define OPL_TIMER_H
+
+#include "opl.h"
+
+int OPL_Timer_StartThread(void);
+void OPL_Timer_StopThread(void);
+void OPL_Timer_SetCallback(unsigned int ms,
+ opl_callback_t callback,
+ void *data);
+void OPL_Timer_ClearCallbacks(void);
+void OPL_Timer_Lock(void);
+void OPL_Timer_Unlock(void);
+void OPL_Timer_SetPaused(int paused);
+
+#endif /* #ifndef OPL_TIMER_H */
+
--- /dev/null
+++ b/opl/opl_win32.c
@@ -1,0 +1,172 @@
+// Emacs style mode select -*- C++ -*-
+//-----------------------------------------------------------------------------
+//
+// Copyright(C) 2009 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:
+// OPL Win32 native interface.
+//
+//-----------------------------------------------------------------------------
+
+#include "config.h"
+
+#ifdef _WIN32
+
+#include <stdio.h>
+
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+
+#include "opl.h"
+#include "opl_internal.h"
+#include "opl_timer.h"
+
+#include "ioperm_sys.h"
+
+static unsigned int opl_port_base;
+
+// MingW?
+
+#if defined(__GNUC__) && defined(__i386__)
+
+static unsigned int OPL_Win32_PortRead(opl_port_t port)
+{
+ unsigned char result;
+
+ __asm__ volatile (
+ "movl %1, %%edx\n"
+ "inb %%dx, %%al\n"
+ "movb %%al, %0"
+ : "=m" (result)
+ : "r" (opl_port_base + port)
+ : "edx", "al", "memory"
+ );
+
+ return result;
+}
+
+static void OPL_Win32_PortWrite(opl_port_t port, unsigned int value)
+{
+ __asm__ volatile (
+ "movl %0, %%edx\n"
+ "movb %1, %%al\n"
+ "outb %%al, %%dx"
+ :
+ : "r" (opl_port_base + port), "r" ((unsigned char) value)
+ : "edx", "al"
+ );
+}
+
+// TODO: MSVC version
+// #elif defined(_MSC_VER) && defined(_M_IX6) ...
+
+#else
+
+// Not x86, or don't know how to do port R/W on this compiler.
+
+#define NO_PORT_RW
+
+static unsigned int OPL_Win32_PortRead(opl_port_t port)
+{
+ return 0;
+}
+
+static void OPL_Win32_PortWrite(opl_port_t port, unsigned int value)
+{
+}
+
+#endif
+
+static int OPL_Win32_Init(unsigned int port_base)
+{
+#ifndef NO_PORT_RW
+
+ OSVERSIONINFO version_info;
+
+ opl_port_base = port_base;
+
+ // Check the OS version.
+
+ memset(&version_info, 0, sizeof(version_info));
+ version_info.dwOSVersionInfoSize = sizeof(version_info);
+
+ GetVersionEx(&version_info);
+
+ // On NT-based systems, we must acquire I/O port permissions
+ // using the ioperm.sys driver.
+
+ if (version_info.dwPlatformId == VER_PLATFORM_WIN32_NT)
+ {
+ // Install driver.
+
+ if (!IOperm_InstallDriver())
+ {
+ return 0;
+ }
+
+ // Open port range.
+
+ if (!IOperm_EnablePortRange(opl_port_base, 2, 1))
+ {
+ IOperm_UninstallDriver();
+ return 0;
+ }
+ }
+
+ // Start callback thread
+
+ if (!OPL_Timer_StartThread())
+ {
+ IOperm_UninstallDriver();
+ return 0;
+ }
+
+ return 1;
+
+#endif
+
+ return 0;
+}
+
+static void OPL_Win32_Shutdown(void)
+{
+ // Stop callback thread
+
+ OPL_Timer_StopThread();
+
+ // Unload IOperm library.
+
+ IOperm_UninstallDriver();
+}
+
+opl_driver_t opl_win32_driver =
+{
+ "Win32",
+ OPL_Win32_Init,
+ OPL_Win32_Shutdown,
+ OPL_Win32_PortRead,
+ OPL_Win32_PortWrite,
+ OPL_Timer_SetCallback,
+ OPL_Timer_ClearCallbacks,
+ OPL_Timer_Lock,
+ OPL_Timer_Unlock,
+ OPL_Timer_SetPaused
+};
+
+#endif /* #ifdef _WIN32 */
+
--- a/pkg/win32/GNUmakefile
+++ b/pkg/win32/GNUmakefile
@@ -13,6 +13,8 @@
$(TOPLEVEL)/src/SDL_mixer.dll \
$(TOPLEVEL)/src/SDL_net.dll
+DOC_FILES += README.OPL
+
ZIP=$(PACKAGE_TARNAME)-$(PACKAGE_VERSION)-win32.zip
$(ZIP) : staging
--- a/rpm.spec.in
+++ b/rpm.spec.in
@@ -52,6 +52,7 @@
%doc NEWS
%doc AUTHORS
%doc README
+%doc README.OPL
%doc COPYING
%doc CMDLINE
%doc BUGS
--- a/src/.gitignore
+++ b/src/.gitignore
@@ -7,7 +7,7 @@
chocolate-hexen
chocolate-server
chocolate-setup
-doom-screensaver.desktop
*.exe
+*.desktop
tags
TAGS
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -10,6 +10,7 @@
@PROGRAM_PREFIX@setup
AM_CFLAGS = -I$(top_builddir)/textscreen \
+ -I$(top_builddir)/opl \
-I$(top_builddir)/pcsound \
@SDLMIXER_CFLAGS@ @SDLNET_CFLAGS@
@@ -115,6 +116,8 @@
i_pcsound.c \
i_sdlsound.c \
i_sdlmusic.c \
+i_oplmusic.c \
+midifile.c midifile.h \
mus2mid.c mus2mid.h
# Some games support dehacked patches, some don't:
@@ -131,10 +134,11 @@
$(top_builddir)/wince/libc_wince.a \
$(top_builddir)/textscreen/libtextscreen.a \
$(top_builddir)/pcsound/libpcsound.a \
+ $(top_builddir)/opl/libopl.a \
@LDFLAGS@ \
@SDL_LIBS@ \
@SDLMIXER_LIBS@ \
- @SDLNET_LIBS@
+ @SDLNET_LIBS@
if HAVE_WINDRES
@PROGRAM_PREFIX@doom_SOURCES=$(SOURCE_FILES_WITH_DEH) resource.rc
@@ -195,4 +199,7 @@
$(top_builddir)/data/convert-icon $^ $@
endif
+
+midiread : midifile.c
+ $(CC) -DTEST $(CFLAGS) @LDFLAGS@ $^ -o $@
--- a/src/doom/p_setup.c
+++ b/src/doom/p_setup.c
@@ -33,6 +33,7 @@
#include "deh_main.h"
#include "i_swap.h"
+#include "m_argv.h"
#include "m_bbox.h"
#include "g_game.h"
@@ -76,6 +77,7 @@
int numsides;
side_t* sides;
+static int totallines;
// BLOCKMAP
// Created from axis aligned bounding box
@@ -534,7 +536,6 @@
line_t** linebuffer;
int i;
int j;
- int total;
line_t* li;
sector_t* sector;
subsector_t* ss;
@@ -552,21 +553,21 @@
// count number of lines in each sector
li = lines;
- total = 0;
+ totallines = 0;
for (i=0 ; i<numlines ; i++, li++)
{
- total++;
+ totallines++;
li->frontsector->linecount++;
if (li->backsector && li->backsector != li->frontsector)
{
li->backsector->linecount++;
- total++;
+ totallines++;
}
}
// build line tables for each sector
- linebuffer = Z_Malloc (total*sizeof(line_t *), PU_LEVEL, 0);
+ linebuffer = Z_Malloc (totallines*sizeof(line_t *), PU_LEVEL, 0);
for (i=0; i<numsectors; ++i)
{
@@ -643,7 +644,88 @@
}
+// Pad the REJECT lump with extra data when the lump is too small,
+// to simulate a REJECT buffer overflow in Vanilla Doom.
+static void PadRejectArray(byte *array, unsigned int len)
+{
+ unsigned int i;
+ unsigned int byte_num;
+ byte *dest;
+ unsigned int padvalue;
+
+ // Values to pad the REJECT array with:
+
+ unsigned int rejectpad[4] =
+ {
+ ((totallines * 4 + 3) & ~3) + 24, // Size
+ 0, // Part of z_zone block header
+ 50, // PU_LEVEL
+ 0x1d4a11 // DOOM_CONST_ZONEID
+ };
+
+ // Copy values from rejectpad into the destination array.
+
+ dest = array;
+
+ for (i=0; i<len && i<sizeof(rejectpad); ++i)
+ {
+ byte_num = i % 4;
+ *dest = (rejectpad[i / 4] >> (byte_num * 8)) & 0xff;
+ ++dest;
+ }
+
+ // We only have a limited pad size. Print a warning if the
+ // REJECT lump is too small.
+
+ if (len > sizeof(rejectpad))
+ {
+ fprintf(stderr, "PadRejectArray: REJECT lump too short to pad! (%i > %i)\n",
+ len, sizeof(rejectpad));
+
+ // Pad remaining space with 0 (or 0xff, if specified on command line).
+
+ if (M_CheckParm("-reject_pad_with_ff"))
+ {
+ padvalue = 0xff;
+ }
+ else
+ {
+ padvalue = 0xf00;
+ }
+
+ memset(array + sizeof(rejectpad), padvalue, len - sizeof(rejectpad));
+ }
+}
+
+static void P_LoadReject(int lumpnum)
+{
+ int minlength;
+ int lumplen;
+
+ // Calculate the size that the REJECT lump *should* be.
+
+ minlength = (numsectors * numsectors + 7) / 8;
+
+ // If the lump meets the minimum length, it can be loaded directly.
+ // Otherwise, we need to allocate a buffer of the correct size
+ // and pad it with appropriate data.
+
+ lumplen = W_LumpLength(lumpnum);
+
+ if (lumplen >= minlength)
+ {
+ rejectmatrix = W_CacheLumpNum(lumpnum, PU_LEVEL);
+ }
+ else
+ {
+ rejectmatrix = Z_Malloc(minlength, PU_LEVEL, &rejectmatrix);
+ W_ReadLump(lumpnum, rejectmatrix);
+
+ PadRejectArray(rejectmatrix + lumplen, minlength - lumplen);
+ }
+}
+
//
// P_SetupLevel
//
@@ -719,9 +801,9 @@
P_LoadSubsectors (lumpnum+ML_SSECTORS);
P_LoadNodes (lumpnum+ML_NODES);
P_LoadSegs (lumpnum+ML_SEGS);
-
- rejectmatrix = W_CacheLumpNum (lumpnum+ML_REJECT,PU_LEVEL);
+
P_GroupLines ();
+ P_LoadReject (lumpnum+ML_REJECT);
bodyqueslot = 0;
deathmatch_p = deathmatchstarts;
--- a/src/doom/s_sound.c
+++ b/src/doom/s_sound.c
@@ -618,6 +618,15 @@
char namebuf[9];
void *handle;
+ // The Doom IWAD file has two versions of the intro music: d_intro
+ // and d_introa. The latter is used for OPL playback.
+
+ if (musicnum == mus_intro && (snd_musicdevice == SNDDEVICE_ADLIB
+ || snd_musicdevice == SNDDEVICE_SB))
+ {
+ musicnum = mus_introa;
+ }
+
if (musicnum <= mus_None || musicnum >= NUMMUSIC)
{
I_Error("Bad music number %d", musicnum);
--- /dev/null
+++ b/src/i_oplmusic.c
@@ -1,0 +1,1464 @@
+// Emacs style mode select -*- C++ -*-
+//-----------------------------------------------------------------------------
+//
+// Copyright(C) 1993-1996 Id Software, Inc.
+// Copyright(C) 2005 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:
+// System interface for music.
+//
+//-----------------------------------------------------------------------------
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "memio.h"
+#include "mus2mid.h"
+
+#include "deh_main.h"
+#include "i_sound.h"
+#include "i_swap.h"
+#include "m_misc.h"
+#include "w_wad.h"
+#include "z_zone.h"
+
+#include "opl.h"
+#include "midifile.h"
+
+// #define OPL_MIDI_DEBUG
+
+#define MAXMIDLENGTH (96 * 1024)
+#define GENMIDI_NUM_INSTRS 128
+
+#define GENMIDI_HEADER "#OPL_II#"
+#define GENMIDI_FLAG_FIXED 0x0001 /* fixed pitch */
+#define GENMIDI_FLAG_2VOICE 0x0004 /* double voice (OPL3) */
+
+typedef struct
+{
+ byte tremolo;
+ byte attack;
+ byte sustain;
+ byte waveform;
+ byte scale;
+ byte level;
+} PACKEDATTR genmidi_op_t;
+
+typedef struct
+{
+ genmidi_op_t modulator;
+ byte feedback;
+ genmidi_op_t carrier;
+ byte unused;
+ short base_note_offset;
+} PACKEDATTR genmidi_voice_t;
+
+typedef struct
+{
+ unsigned short flags;
+ byte fine_tuning;
+ byte fixed_note;
+
+ genmidi_voice_t voices[2];
+} PACKEDATTR genmidi_instr_t;
+
+// Data associated with a channel of a track that is currently playing.
+
+typedef struct
+{
+ // The instrument currently used for this track.
+
+ genmidi_instr_t *instrument;
+
+ // Volume level
+
+ int volume;
+
+ // Pitch bend value:
+
+ int bend;
+
+} opl_channel_data_t;
+
+// Data associated with a track that is currently playing.
+
+typedef struct
+{
+ // Data for each channel.
+
+ opl_channel_data_t channels[MIDI_CHANNELS_PER_TRACK];
+
+ // Track iterator used to read new events.
+
+ midi_track_iter_t *iter;
+
+ // Tempo control variables
+
+ unsigned int ticks_per_beat;
+ unsigned int ms_per_beat;
+} opl_track_data_t;
+
+typedef struct opl_voice_s opl_voice_t;
+
+struct opl_voice_s
+{
+ // Index of this voice:
+ int index;
+
+ // The operators used by this voice:
+ int op1, op2;
+
+ // Currently-loaded instrument data
+ genmidi_instr_t *current_instr;
+
+ // The voice number in the instrument to use.
+ // This is normally set to zero; if this is a double voice
+ // instrument, it may be one.
+ unsigned int current_instr_voice;
+
+ // The channel currently using this voice.
+ opl_channel_data_t *channel;
+
+ // The midi key that this voice is playing.
+ unsigned int key;
+
+ // The note being played. This is normally the same as
+ // the key, but if the instrument is a fixed pitch
+ // instrument, it is different.
+ unsigned int note;
+
+ // The frequency value being used.
+ unsigned int freq;
+
+ // The volume of the note being played on this channel.
+ unsigned int note_volume;
+
+ // The current volume (register value) that has been set for this channel.
+ unsigned int reg_volume;
+
+ // Next in linked list; a voice is always either in the
+ // free list or the allocated list.
+ opl_voice_t *next;
+};
+
+// Operators used by the different voices.
+
+static const int voice_operators[2][OPL_NUM_VOICES] = {
+ { 0x00, 0x01, 0x02, 0x08, 0x09, 0x0a, 0x10, 0x11, 0x12 },
+ { 0x03, 0x04, 0x05, 0x0b, 0x0c, 0x0d, 0x13, 0x14, 0x15 }
+};
+
+// Frequency values to use for each note.
+
+static const unsigned short frequency_curve[] = {
+
+ 0x133, 0x133, 0x134, 0x134, 0x135, 0x136, 0x136, 0x137, // -1
+ 0x137, 0x138, 0x138, 0x139, 0x139, 0x13a, 0x13b, 0x13b,
+ 0x13c, 0x13c, 0x13d, 0x13d, 0x13e, 0x13f, 0x13f, 0x140,
+ 0x140, 0x141, 0x142, 0x142, 0x143, 0x143, 0x144, 0x144,
+
+ 0x145, 0x146, 0x146, 0x147, 0x147, 0x148, 0x149, 0x149, // -2
+ 0x14a, 0x14a, 0x14b, 0x14c, 0x14c, 0x14d, 0x14d, 0x14e,
+ 0x14f, 0x14f, 0x150, 0x150, 0x151, 0x152, 0x152, 0x153,
+ 0x153, 0x154, 0x155, 0x155, 0x156, 0x157, 0x157, 0x158,
+
+ // These are used for the first seven MIDI note values:
+
+ 0x158, 0x159, 0x15a, 0x15a, 0x15b, 0x15b, 0x15c, 0x15d, // 0
+ 0x15d, 0x15e, 0x15f, 0x15f, 0x160, 0x161, 0x161, 0x162,
+ 0x162, 0x163, 0x164, 0x164, 0x165, 0x166, 0x166, 0x167,
+ 0x168, 0x168, 0x169, 0x16a, 0x16a, 0x16b, 0x16c, 0x16c,
+
+ 0x16d, 0x16e, 0x16e, 0x16f, 0x170, 0x170, 0x171, 0x172, // 1
+ 0x172, 0x173, 0x174, 0x174, 0x175, 0x176, 0x176, 0x177,
+ 0x178, 0x178, 0x179, 0x17a, 0x17a, 0x17b, 0x17c, 0x17c,
+ 0x17d, 0x17e, 0x17e, 0x17f, 0x180, 0x181, 0x181, 0x182,
+
+ 0x183, 0x183, 0x184, 0x185, 0x185, 0x186, 0x187, 0x188, // 2
+ 0x188, 0x189, 0x18a, 0x18a, 0x18b, 0x18c, 0x18d, 0x18d,
+ 0x18e, 0x18f, 0x18f, 0x190, 0x191, 0x192, 0x192, 0x193,
+ 0x194, 0x194, 0x195, 0x196, 0x197, 0x197, 0x198, 0x199,
+
+ 0x19a, 0x19a, 0x19b, 0x19c, 0x19d, 0x19d, 0x19e, 0x19f, // 3
+ 0x1a0, 0x1a0, 0x1a1, 0x1a2, 0x1a3, 0x1a3, 0x1a4, 0x1a5,
+ 0x1a6, 0x1a6, 0x1a7, 0x1a8, 0x1a9, 0x1a9, 0x1aa, 0x1ab,
+ 0x1ac, 0x1ad, 0x1ad, 0x1ae, 0x1af, 0x1b0, 0x1b0, 0x1b1,
+
+ 0x1b2, 0x1b3, 0x1b4, 0x1b4, 0x1b5, 0x1b6, 0x1b7, 0x1b8, // 4
+ 0x1b8, 0x1b9, 0x1ba, 0x1bb, 0x1bc, 0x1bc, 0x1bd, 0x1be,
+ 0x1bf, 0x1c0, 0x1c0, 0x1c1, 0x1c2, 0x1c3, 0x1c4, 0x1c4,
+ 0x1c5, 0x1c6, 0x1c7, 0x1c8, 0x1c9, 0x1c9, 0x1ca, 0x1cb,
+
+ 0x1cc, 0x1cd, 0x1ce, 0x1ce, 0x1cf, 0x1d0, 0x1d1, 0x1d2, // 5
+ 0x1d3, 0x1d3, 0x1d4, 0x1d5, 0x1d6, 0x1d7, 0x1d8, 0x1d8,
+ 0x1d9, 0x1da, 0x1db, 0x1dc, 0x1dd, 0x1de, 0x1de, 0x1df,
+ 0x1e0, 0x1e1, 0x1e2, 0x1e3, 0x1e4, 0x1e5, 0x1e5, 0x1e6,
+
+ 0x1e7, 0x1e8, 0x1e9, 0x1ea, 0x1eb, 0x1ec, 0x1ed, 0x1ed, // 6
+ 0x1ee, 0x1ef, 0x1f0, 0x1f1, 0x1f2, 0x1f3, 0x1f4, 0x1f5,
+ 0x1f6, 0x1f6, 0x1f7, 0x1f8, 0x1f9, 0x1fa, 0x1fb, 0x1fc,
+ 0x1fd, 0x1fe, 0x1ff, 0x200, 0x201, 0x201, 0x202, 0x203,
+
+ // First note of looped range used for all octaves:
+
+ 0x204, 0x205, 0x206, 0x207, 0x208, 0x209, 0x20a, 0x20b, // 7
+ 0x20c, 0x20d, 0x20e, 0x20f, 0x210, 0x210, 0x211, 0x212,
+ 0x213, 0x214, 0x215, 0x216, 0x217, 0x218, 0x219, 0x21a,
+ 0x21b, 0x21c, 0x21d, 0x21e, 0x21f, 0x220, 0x221, 0x222,
+
+ 0x223, 0x224, 0x225, 0x226, 0x227, 0x228, 0x229, 0x22a, // 8
+ 0x22b, 0x22c, 0x22d, 0x22e, 0x22f, 0x230, 0x231, 0x232,
+ 0x233, 0x234, 0x235, 0x236, 0x237, 0x238, 0x239, 0x23a,
+ 0x23b, 0x23c, 0x23d, 0x23e, 0x23f, 0x240, 0x241, 0x242,
+
+ 0x244, 0x245, 0x246, 0x247, 0x248, 0x249, 0x24a, 0x24b, // 9
+ 0x24c, 0x24d, 0x24e, 0x24f, 0x250, 0x251, 0x252, 0x253,
+ 0x254, 0x256, 0x257, 0x258, 0x259, 0x25a, 0x25b, 0x25c,
+ 0x25d, 0x25e, 0x25f, 0x260, 0x262, 0x263, 0x264, 0x265,
+
+ 0x266, 0x267, 0x268, 0x269, 0x26a, 0x26c, 0x26d, 0x26e, // 10
+ 0x26f, 0x270, 0x271, 0x272, 0x273, 0x275, 0x276, 0x277,
+ 0x278, 0x279, 0x27a, 0x27b, 0x27d, 0x27e, 0x27f, 0x280,
+ 0x281, 0x282, 0x284, 0x285, 0x286, 0x287, 0x288, 0x289,
+
+ 0x28b, 0x28c, 0x28d, 0x28e, 0x28f, 0x290, 0x292, 0x293, // 11
+ 0x294, 0x295, 0x296, 0x298, 0x299, 0x29a, 0x29b, 0x29c,
+ 0x29e, 0x29f, 0x2a0, 0x2a1, 0x2a2, 0x2a4, 0x2a5, 0x2a6,
+ 0x2a7, 0x2a9, 0x2aa, 0x2ab, 0x2ac, 0x2ae, 0x2af, 0x2b0,
+
+ 0x2b1, 0x2b2, 0x2b4, 0x2b5, 0x2b6, 0x2b7, 0x2b9, 0x2ba, // 12
+ 0x2bb, 0x2bd, 0x2be, 0x2bf, 0x2c0, 0x2c2, 0x2c3, 0x2c4,
+ 0x2c5, 0x2c7, 0x2c8, 0x2c9, 0x2cb, 0x2cc, 0x2cd, 0x2ce,
+ 0x2d0, 0x2d1, 0x2d2, 0x2d4, 0x2d5, 0x2d6, 0x2d8, 0x2d9,
+
+ 0x2da, 0x2dc, 0x2dd, 0x2de, 0x2e0, 0x2e1, 0x2e2, 0x2e4, // 13
+ 0x2e5, 0x2e6, 0x2e8, 0x2e9, 0x2ea, 0x2ec, 0x2ed, 0x2ee,
+ 0x2f0, 0x2f1, 0x2f2, 0x2f4, 0x2f5, 0x2f6, 0x2f8, 0x2f9,
+ 0x2fb, 0x2fc, 0x2fd, 0x2ff, 0x300, 0x302, 0x303, 0x304,
+
+ 0x306, 0x307, 0x309, 0x30a, 0x30b, 0x30d, 0x30e, 0x310, // 14
+ 0x311, 0x312, 0x314, 0x315, 0x317, 0x318, 0x31a, 0x31b,
+ 0x31c, 0x31e, 0x31f, 0x321, 0x322, 0x324, 0x325, 0x327,
+ 0x328, 0x329, 0x32b, 0x32c, 0x32e, 0x32f, 0x331, 0x332,
+
+ 0x334, 0x335, 0x337, 0x338, 0x33a, 0x33b, 0x33d, 0x33e, // 15
+ 0x340, 0x341, 0x343, 0x344, 0x346, 0x347, 0x349, 0x34a,
+ 0x34c, 0x34d, 0x34f, 0x350, 0x352, 0x353, 0x355, 0x357,
+ 0x358, 0x35a, 0x35b, 0x35d, 0x35e, 0x360, 0x361, 0x363,
+
+ 0x365, 0x366, 0x368, 0x369, 0x36b, 0x36c, 0x36e, 0x370, // 16
+ 0x371, 0x373, 0x374, 0x376, 0x378, 0x379, 0x37b, 0x37c,
+ 0x37e, 0x380, 0x381, 0x383, 0x384, 0x386, 0x388, 0x389,
+ 0x38b, 0x38d, 0x38e, 0x390, 0x392, 0x393, 0x395, 0x397,
+
+ 0x398, 0x39a, 0x39c, 0x39d, 0x39f, 0x3a1, 0x3a2, 0x3a4, // 17
+ 0x3a6, 0x3a7, 0x3a9, 0x3ab, 0x3ac, 0x3ae, 0x3b0, 0x3b1,
+ 0x3b3, 0x3b5, 0x3b7, 0x3b8, 0x3ba, 0x3bc, 0x3bd, 0x3bf,
+ 0x3c1, 0x3c3, 0x3c4, 0x3c6, 0x3c8, 0x3ca, 0x3cb, 0x3cd,
+
+ // The last note has an incomplete range, and loops round back to
+ // the start. Note that the last value is actually a buffer overrun
+ // and does not fit with the other values.
+
+ 0x3cf, 0x3d1, 0x3d2, 0x3d4, 0x3d6, 0x3d8, 0x3da, 0x3db, // 18
+ 0x3dd, 0x3df, 0x3e1, 0x3e3, 0x3e4, 0x3e6, 0x3e8, 0x3ea,
+ 0x3ec, 0x3ed, 0x3ef, 0x3f1, 0x3f3, 0x3f5, 0x3f6, 0x3f8,
+ 0x3fa, 0x3fc, 0x3fe, 0x36c,
+};
+
+// Mapping from MIDI volume level to OPL level value.
+
+static const unsigned int volume_mapping_table[] = {
+ 0, 1, 3, 5, 6, 8, 10, 11,
+ 13, 14, 16, 17, 19, 20, 22, 23,
+ 25, 26, 27, 29, 30, 32, 33, 34,
+ 36, 37, 39, 41, 43, 45, 47, 49,
+ 50, 52, 54, 55, 57, 59, 60, 61,
+ 63, 64, 66, 67, 68, 69, 71, 72,
+ 73, 74, 75, 76, 77, 79, 80, 81,
+ 82, 83, 84, 84, 85, 86, 87, 88,
+ 89, 90, 91, 92, 92, 93, 94, 95,
+ 96, 96, 97, 98, 99, 99, 100, 101,
+ 101, 102, 103, 103, 104, 105, 105, 106,
+ 107, 107, 108, 109, 109, 110, 110, 111,
+ 112, 112, 113, 113, 114, 114, 115, 115,
+ 116, 117, 117, 118, 118, 119, 119, 120,
+ 120, 121, 121, 122, 122, 123, 123, 123,
+ 124, 124, 125, 125, 126, 126, 127, 127
+};
+
+static boolean music_initialized = false;
+
+//static boolean musicpaused = false;
+static int current_music_volume;
+
+// GENMIDI lump instrument data:
+
+static genmidi_instr_t *main_instrs;
+static genmidi_instr_t *percussion_instrs;
+
+// Voices:
+
+static opl_voice_t voices[OPL_NUM_VOICES];
+static opl_voice_t *voice_free_list;
+static opl_voice_t *voice_alloced_list;
+
+// Track data for playing tracks:
+
+static opl_track_data_t *tracks;
+static unsigned int num_tracks = 0;
+static unsigned int running_tracks = 0;
+static boolean song_looping;
+
+// Configuration file variable, containing the port number for the
+// adlib chip.
+
+int opl_io_port = 0x388;
+
+// Load instrument table from GENMIDI lump:
+
+static boolean LoadInstrumentTable(void)
+{
+ byte *lump;
+
+ lump = W_CacheLumpName("GENMIDI", PU_STATIC);
+
+ // Check header
+
+ if (strncmp((char *) lump, GENMIDI_HEADER, strlen(GENMIDI_HEADER)) != 0)
+ {
+ W_ReleaseLumpName("GENMIDI");
+
+ return false;
+ }
+
+ main_instrs = (genmidi_instr_t *) (lump + strlen(GENMIDI_HEADER));
+ percussion_instrs = main_instrs + GENMIDI_NUM_INSTRS;
+
+ return true;
+}
+
+// Get the next available voice from the freelist.
+
+static opl_voice_t *GetFreeVoice(void)
+{
+ opl_voice_t *result;
+
+ // None available?
+
+ if (voice_free_list == NULL)
+ {
+ return NULL;
+ }
+
+ // Remove from free list
+
+ result = voice_free_list;
+ voice_free_list = voice_free_list->next;
+
+ // Add to allocated list
+
+ result->next = voice_alloced_list;
+ voice_alloced_list = result;
+
+ return result;
+}
+
+// Remove a voice from the allocated voices list.
+
+static void RemoveVoiceFromAllocedList(opl_voice_t *voice)
+{
+ opl_voice_t **rover;
+
+ rover = &voice_alloced_list;
+
+ // Search the list until we find the voice, then remove it.
+
+ while (*rover != NULL)
+ {
+ if (*rover == voice)
+ {
+ *rover = voice->next;
+ voice->next = NULL;
+ break;
+ }
+
+ rover = &(*rover)->next;
+ }
+}
+
+// Release a voice back to the freelist.
+
+static void ReleaseVoice(opl_voice_t *voice)
+{
+ opl_voice_t **rover;
+
+ voice->channel = NULL;
+ voice->note = 0;
+
+ // Remove from alloced list.
+
+ RemoveVoiceFromAllocedList(voice);
+
+ // Search to the end of the freelist (This is how Doom behaves!)
+
+ rover = &voice_free_list;
+
+ while (*rover != NULL)
+ {
+ rover = &(*rover)->next;
+ }
+
+ *rover = voice;
+ voice->next = NULL;
+}
+
+// Load data to the specified operator
+
+static void LoadOperatorData(int operator, genmidi_op_t *data,
+ boolean max_level)
+{
+ int level;
+
+ // The scale and level fields must be combined for the level register.
+ // For the carrier wave we always set the maximum level.
+
+ level = (data->scale & 0xc0) | (data->level & 0x3f);
+
+ if (max_level)
+ {
+ level |= 0x3f;
+ }
+
+ OPL_WriteRegister(OPL_REGS_LEVEL + operator, level);
+ OPL_WriteRegister(OPL_REGS_TREMOLO + operator, data->tremolo);
+ OPL_WriteRegister(OPL_REGS_ATTACK + operator, data->attack);
+ OPL_WriteRegister(OPL_REGS_SUSTAIN + operator, data->sustain);
+ OPL_WriteRegister(OPL_REGS_WAVEFORM + operator, data->waveform);
+}
+
+// Set the instrument for a particular voice.
+
+static void SetVoiceInstrument(opl_voice_t *voice,
+ genmidi_instr_t *instr,
+ unsigned int instr_voice)
+{
+ genmidi_voice_t *data;
+ unsigned int modulating;
+
+ // Instrument already set for this channel?
+
+ if (voice->current_instr == instr
+ && voice->current_instr_voice == instr_voice)
+ {
+ return;
+ }
+
+ voice->current_instr = instr;
+ voice->current_instr_voice = instr_voice;
+
+ data = &instr->voices[instr_voice];
+
+ // Are we usind modulated feedback mode?
+
+ modulating = (data->feedback & 0x01) == 0;
+
+ // Doom loads the second operator first, then the first.
+ // The carrier is set to minimum volume until the voice volume
+ // is set in SetVoiceVolume (below). If we are not using
+ // modulating mode, we must set both to minimum volume.
+
+ LoadOperatorData(voice->op2, &data->carrier, true);
+ LoadOperatorData(voice->op1, &data->modulator, !modulating);
+
+ // Set feedback register that control the connection between the
+ // two operators. Turn on bits in the upper nybble; I think this
+ // is for OPL3, where it turns on channel A/B.
+
+ OPL_WriteRegister(OPL_REGS_FEEDBACK + voice->index,
+ data->feedback | 0x30);
+
+ // Hack to force a volume update.
+
+ voice->reg_volume = 999;
+}
+
+static void SetVoiceVolume(opl_voice_t *voice, unsigned int volume)
+{
+ genmidi_voice_t *opl_voice;
+ unsigned int full_volume;
+ unsigned int op_volume;
+ unsigned int reg_volume;
+
+ voice->note_volume = volume;
+
+ opl_voice = &voice->current_instr->voices[voice->current_instr_voice];
+
+ // Multiply note volume and channel volume to get the actual volume.
+
+ full_volume = (volume_mapping_table[voice->note_volume]
+ * volume_mapping_table[voice->channel->volume]
+ * volume_mapping_table[current_music_volume]) / (127 * 127);
+
+ // The volume of each instrument can be controlled via GENMIDI:
+
+ op_volume = 0x3f - opl_voice->carrier.level;
+
+ // The volume value to use in the register:
+
+ reg_volume = (op_volume * full_volume) / 128;
+ reg_volume = (0x3f - reg_volume) | opl_voice->carrier.scale;
+
+ // Update the volume register(s) if necessary.
+
+ if (reg_volume != voice->reg_volume)
+ {
+ voice->reg_volume = reg_volume;
+
+ OPL_WriteRegister(OPL_REGS_LEVEL + voice->op2, reg_volume);
+
+ // If we are using non-modulated feedback mode, we must set the
+ // volume for both voices.
+ // Note that the same register volume value is written for
+ // both voices, always calculated from the carrier's level
+ // value.
+
+ if ((opl_voice->feedback & 0x01) != 0)
+ {
+ OPL_WriteRegister(OPL_REGS_LEVEL + voice->op1, reg_volume);
+ }
+ }
+}
+
+// Initialize the voice table and freelist
+
+static void InitVoices(void)
+{
+ int i;
+
+ // Start with an empty free list.
+
+ voice_free_list = NULL;
+
+ // Initialize each voice.
+
+ for (i=0; i<OPL_NUM_VOICES; ++i)
+ {
+ voices[i].index = i;
+ voices[i].op1 = voice_operators[0][i];
+ voices[i].op2 = voice_operators[1][i];
+ voices[i].current_instr = NULL;
+
+ // Add this voice to the freelist.
+
+ ReleaseVoice(&voices[i]);
+ }
+}
+
+// Set music volume (0 - 127)
+
+static void I_OPL_SetMusicVolume(int volume)
+{
+ unsigned int i;
+
+ // Internal state variable.
+
+ current_music_volume = volume;
+
+ // Update the volume of all voices.
+
+ for (i=0; i<OPL_NUM_VOICES; ++i)
+ {
+ if (voices[i].channel != NULL)
+ {
+ SetVoiceVolume(&voices[i], voices[i].note_volume);
+ }
+ }
+}
+
+static void VoiceKeyOff(opl_voice_t *voice)
+{
+ OPL_WriteRegister(OPL_REGS_FREQ_2 + voice->index, voice->freq >> 8);
+}
+
+// Get the frequency that we should be using for a voice.
+
+static void KeyOffEvent(opl_track_data_t *track, midi_event_t *event)
+{
+ opl_channel_data_t *channel;
+ unsigned int key;
+ unsigned int i;
+
+/*
+ printf("note off: channel %i, %i, %i\n",
+ event->data.channel.channel,
+ event->data.channel.param1,
+ event->data.channel.param2);
+*/
+
+ channel = &track->channels[event->data.channel.channel];
+ key = event->data.channel.param1;
+
+ // Turn off voices being used to play this key.
+ // If it is a double voice instrument there will be two.
+
+ for (i=0; i<OPL_NUM_VOICES; ++i)
+ {
+ if (voices[i].channel == channel && voices[i].key == key)
+ {
+ VoiceKeyOff(&voices[i]);
+
+ // Finished with this voice now.
+
+ ReleaseVoice(&voices[i]);
+ }
+ }
+}
+
+// Compare the priorities of channels, returning either -1, 0 or 1.
+
+static int CompareChannelPriorities(opl_channel_data_t *chan1,
+ opl_channel_data_t *chan2)
+{
+ // TODO ...
+
+ return 1;
+}
+
+// When all voices are in use, we must discard an existing voice to
+// play a new note. Find and free an existing voice. The channel
+// passed to the function is the channel for the new note to be
+// played.
+
+static opl_voice_t *ReplaceExistingVoice(opl_channel_data_t *channel)
+{
+ opl_voice_t *rover;
+ opl_voice_t *result;
+
+ // Check the allocated voices, if we find an instrument that is
+ // of a lower priority to the new instrument, discard it.
+ // If a voice is being used to play the second voice of an instrument,
+ // use that, as second voices are non-essential.
+ // Lower numbered MIDI channels implicitly have a higher priority
+ // than higher-numbered channels, eg. MIDI channel 1 is never
+ // discarded for MIDI channel 2.
+
+ result = NULL;
+
+ for (rover = voice_alloced_list; rover != NULL; rover = rover->next)
+ {
+ if (rover->current_instr_voice != 0
+ || (rover->channel > channel
+ && CompareChannelPriorities(channel, rover->channel) > 0))
+ {
+ result = rover;
+ break;
+ }
+ }
+
+ // If we didn't find a voice, find an existing voice being used to
+ // play a note on the same channel, and use that.
+
+ if (result == NULL)
+ {
+ for (rover = voice_alloced_list; rover != NULL; rover = rover->next)
+ {
+ if (rover->channel == channel)
+ {
+ result = rover;
+ break;
+ }
+ }
+ }
+
+ // Still nothing found? Give up and just use the first voice in
+ // the list.
+
+ if (result == NULL)
+ {
+ result = voice_alloced_list;
+ }
+
+ // Stop playing this voice playing and release it back to the free
+ // list.
+
+ VoiceKeyOff(result);
+ ReleaseVoice(result);
+
+ // Re-allocate the voice again and return it.
+
+ return GetFreeVoice();
+}
+
+
+static unsigned int FrequencyForVoice(opl_voice_t *voice)
+{
+ genmidi_voice_t *gm_voice;
+ unsigned int freq_index;
+ unsigned int octave;
+ unsigned int sub_index;
+ unsigned int note;
+
+ note = voice->note;
+
+ // Apply note offset.
+ // Don't apply offset if the instrument is a fixed note instrument.
+
+ gm_voice = &voice->current_instr->voices[voice->current_instr_voice];
+
+ if ((voice->current_instr->flags & GENMIDI_FLAG_FIXED) == 0)
+ {
+ note += (signed short) SHORT(gm_voice->base_note_offset);
+ }
+
+ // Avoid possible overflow due to base note offset:
+
+ if (note > 0x7f)
+ {
+ note = voice->note;
+ }
+
+ freq_index = 64 + 32 * note + voice->channel->bend;
+
+ // If this is the second voice of a double voice instrument, the
+ // frequency index can be adjusted by the fine tuning field.
+
+ if (voice->current_instr_voice != 0)
+ {
+ freq_index += (voice->current_instr->fine_tuning / 2) - 64;
+ }
+
+ // The first 7 notes use the start of the table, while
+ // consecutive notes loop around the latter part.
+
+ if (freq_index < 284)
+ {
+ return frequency_curve[freq_index];
+ }
+
+ sub_index = (freq_index - 284) % (12 * 32);
+ octave = (freq_index - 284) / (12 * 32);
+
+ // Once the seventh octave is reached, things break down.
+ // We can only go up to octave 7 as a maximum anyway (the OPL
+ // register only has three bits for octave number), but for the
+ // notes in octave 7, the first five bits have octave=7, the
+ // following notes have octave=6. This 7/6 pattern repeats in
+ // following octaves (which are technically impossible to
+ // represent anyway).
+
+ if (octave >= 7)
+ {
+ if (sub_index < 5)
+ {
+ octave = 7;
+ }
+ else
+ {
+ octave = 6;
+ }
+ }
+
+ // Calculate the resulting register value to use for the frequency.
+
+ return frequency_curve[sub_index + 284] | (octave << 10);
+}
+
+// Update the frequency that a voice is programmed to use.
+
+static void UpdateVoiceFrequency(opl_voice_t *voice)
+{
+ unsigned int freq;
+
+ // Calculate the frequency to use for this voice and update it
+ // if neccessary.
+
+ freq = FrequencyForVoice(voice);
+
+ if (voice->freq != freq)
+ {
+ OPL_WriteRegister(OPL_REGS_FREQ_1 + voice->index, freq & 0xff);
+ OPL_WriteRegister(OPL_REGS_FREQ_2 + voice->index, (freq >> 8) | 0x20);
+
+ voice->freq = freq;
+ }
+}
+
+// Program a single voice for an instrument. For a double voice
+// instrument (GENMIDI_FLAG_2VOICE), this is called twice for each
+// key on event.
+
+static void VoiceKeyOn(opl_channel_data_t *channel,
+ genmidi_instr_t *instrument,
+ unsigned int instrument_voice,
+ unsigned int key,
+ unsigned int volume)
+{
+ opl_voice_t *voice;
+
+ // Find a voice to use for this new note.
+
+ voice = GetFreeVoice();
+
+ // If there are no more voices left, we must decide what to do.
+ // If this is the first voice of the instrument, free an existing
+ // voice and use that. Otherwise, if this is the second voice,
+ // it isn't as important; just discard it.
+
+ if (voice == NULL)
+ {
+ if (instrument_voice == 0)
+ {
+ voice = ReplaceExistingVoice(channel);
+ }
+ else
+ {
+ return;
+ }
+ }
+
+ voice->channel = channel;
+ voice->key = key;
+
+ // Work out the note to use. This is normally the same as
+ // the key, unless it is a fixed pitch instrument.
+
+ if ((instrument->flags & GENMIDI_FLAG_FIXED) != 0)
+ {
+ voice->note = instrument->fixed_note;
+ }
+ else
+ {
+ voice->note = key;
+ }
+
+ // Program the voice with the instrument data:
+
+ SetVoiceInstrument(voice, instrument, instrument_voice);
+
+ // Set the volume level.
+
+ SetVoiceVolume(voice, volume);
+
+ // Write the frequency value to turn the note on.
+
+ voice->freq = 0;
+ UpdateVoiceFrequency(voice);
+}
+
+static void KeyOnEvent(opl_track_data_t *track, midi_event_t *event)
+{
+ genmidi_instr_t *instrument;
+ opl_channel_data_t *channel;
+ unsigned int key;
+ unsigned int volume;
+
+/*
+ printf("note on: channel %i, %i, %i\n",
+ event->data.channel.channel,
+ event->data.channel.param1,
+ event->data.channel.param2);
+*/
+
+ // The channel.
+
+ channel = &track->channels[event->data.channel.channel];
+ key = event->data.channel.param1;
+ volume = event->data.channel.param2;
+
+ // Percussion channel (10) is treated differently.
+
+ if (event->data.channel.channel == 9)
+ {
+ if (key < 35 || key > 81)
+ {
+ return;
+ }
+
+ instrument = &percussion_instrs[key - 35];
+ }
+ else
+ {
+ instrument = channel->instrument;
+ }
+
+ // Find and program a voice for this instrument. If this
+ // is a double voice instrument, we must do this twice.
+
+ VoiceKeyOn(channel, instrument, 0, key, volume);
+
+ if ((instrument->flags & GENMIDI_FLAG_2VOICE) != 0)
+ {
+ VoiceKeyOn(channel, instrument, 1, key, volume);
+ }
+}
+
+static void ProgramChangeEvent(opl_track_data_t *track, midi_event_t *event)
+{
+ int channel;
+ int instrument;
+
+ // Set the instrument used on this channel.
+
+ channel = event->data.channel.channel;
+ instrument = event->data.channel.param1;
+ track->channels[channel].instrument = &main_instrs[instrument];
+
+ // TODO: Look through existing voices that are turned on on this
+ // channel, and change the instrument.
+}
+
+static void SetChannelVolume(opl_channel_data_t *channel, unsigned int volume)
+{
+ unsigned int i;
+
+ channel->volume = volume;
+
+ // Update all voices that this channel is using.
+
+ for (i=0; i<OPL_NUM_VOICES; ++i)
+ {
+ if (voices[i].channel == channel)
+ {
+ SetVoiceVolume(&voices[i], voices[i].note_volume);
+ }
+ }
+}
+
+static void ControllerEvent(opl_track_data_t *track, midi_event_t *event)
+{
+ unsigned int controller;
+ unsigned int param;
+ opl_channel_data_t *channel;
+
+/*
+ printf("change controller: channel %i, %i, %i\n",
+ event->data.channel.channel,
+ event->data.channel.param1,
+ event->data.channel.param2);
+*/
+
+ channel = &track->channels[event->data.channel.channel];
+ controller = event->data.channel.param1;
+ param = event->data.channel.param2;
+
+ switch (controller)
+ {
+ case MIDI_CONTROLLER_MAIN_VOLUME:
+ SetChannelVolume(channel, param);
+ break;
+
+ default:
+#ifdef OPL_MIDI_DEBUG
+ fprintf(stderr, "Unknown MIDI controller type: %i\n", controller);
+#endif
+ break;
+ }
+}
+
+// Process a pitch bend event.
+
+static void PitchBendEvent(opl_track_data_t *track, midi_event_t *event)
+{
+ opl_channel_data_t *channel;
+ unsigned int i;
+
+ // Update the channel bend value. Only the MSB of the pitch bend
+ // value is considered: this is what Doom does.
+
+ channel = &track->channels[event->data.channel.channel];
+ channel->bend = event->data.channel.param2 - 64;
+
+ // Update all voices for this channel.
+
+ for (i=0; i<OPL_NUM_VOICES; ++i)
+ {
+ if (voices[i].channel == channel)
+ {
+ UpdateVoiceFrequency(&voices[i]);
+ }
+ }
+}
+
+// Process a meta event.
+
+static void MetaEvent(opl_track_data_t *track, midi_event_t *event)
+{
+ switch (event->data.meta.type)
+ {
+ // Things we can just ignore.
+
+ case MIDI_META_SEQUENCE_NUMBER:
+ case MIDI_META_TEXT:
+ case MIDI_META_COPYRIGHT:
+ case MIDI_META_TRACK_NAME:
+ case MIDI_META_INSTR_NAME:
+ case MIDI_META_LYRICS:
+ case MIDI_META_MARKER:
+ case MIDI_META_CUE_POINT:
+ case MIDI_META_SEQUENCER_SPECIFIC:
+ break;
+
+ // End of track - actually handled when we run out of events
+ // in the track, see below.
+
+ case MIDI_META_END_OF_TRACK:
+ break;
+
+ default:
+#ifdef OPL_MIDI_DEBUG
+ fprintf(stderr, "Unknown MIDI meta event type: %i\n",
+ event->data.meta.type);
+#endif
+ break;
+ }
+}
+
+// Process a MIDI event from a track.
+
+static void ProcessEvent(opl_track_data_t *track, midi_event_t *event)
+{
+ switch (event->event_type)
+ {
+ case MIDI_EVENT_NOTE_OFF:
+ KeyOffEvent(track, event);
+ break;
+
+ case MIDI_EVENT_NOTE_ON:
+ KeyOnEvent(track, event);
+ break;
+
+ case MIDI_EVENT_CONTROLLER:
+ ControllerEvent(track, event);
+ break;
+
+ case MIDI_EVENT_PROGRAM_CHANGE:
+ ProgramChangeEvent(track, event);
+ break;
+
+ case MIDI_EVENT_PITCH_BEND:
+ PitchBendEvent(track, event);
+ break;
+
+ case MIDI_EVENT_META:
+ MetaEvent(track, event);
+ break;
+
+ // SysEx events can be ignored.
+
+ case MIDI_EVENT_SYSEX:
+ case MIDI_EVENT_SYSEX_SPLIT:
+ break;
+
+ default:
+#ifdef OPL_MIDI_DEBUG
+ fprintf(stderr, "Unknown MIDI event type %i\n", event->event_type);
+#endif
+ break;
+ }
+}
+
+static void ScheduleTrack(opl_track_data_t *track);
+
+// Restart a song from the beginning.
+
+static void RestartSong(void)
+{
+ unsigned int i;
+
+ running_tracks = num_tracks;
+
+ for (i=0; i<num_tracks; ++i)
+ {
+ MIDI_RestartIterator(tracks[i].iter);
+ ScheduleTrack(&tracks[i]);
+ }
+}
+
+// Callback function invoked when another event needs to be read from
+// a track.
+
+static void TrackTimerCallback(void *arg)
+{
+ opl_track_data_t *track = arg;
+ midi_event_t *event;
+
+ // Get the next event and process it.
+
+ if (!MIDI_GetNextEvent(track->iter, &event))
+ {
+ return;
+ }
+
+ ProcessEvent(track, event);
+
+ // End of track?
+
+ if (event->event_type == MIDI_EVENT_META
+ && event->data.meta.type == MIDI_META_END_OF_TRACK)
+ {
+ --running_tracks;
+
+ // When all tracks have finished, restart the song.
+
+ if (running_tracks <= 0 && song_looping)
+ {
+ RestartSong();
+ }
+
+ return;
+ }
+
+ // Reschedule the callback for the next event in the track.
+
+ ScheduleTrack(track);
+}
+
+static void ScheduleTrack(opl_track_data_t *track)
+{
+ unsigned int nticks;
+ unsigned int ms;
+ static int total = 0;
+
+ // Get the number of milliseconds until the next event.
+
+ nticks = MIDI_GetDeltaTime(track->iter);
+ ms = (nticks * track->ms_per_beat) / track->ticks_per_beat;
+ total += ms;
+
+ // Set a timer to be invoked when the next event is
+ // ready to play.
+
+ OPL_SetCallback(ms, TrackTimerCallback, track);
+}
+
+// Initialize a channel.
+
+static void InitChannel(opl_track_data_t *track, opl_channel_data_t *channel)
+{
+ // TODO: Work out sensible defaults?
+
+ channel->instrument = &main_instrs[0];
+ channel->volume = 127;
+ channel->bend = 0;
+}
+
+// Start a MIDI track playing:
+
+static void StartTrack(midi_file_t *file, unsigned int track_num)
+{
+ opl_track_data_t *track;
+ unsigned int i;
+
+ track = &tracks[track_num];
+ track->iter = MIDI_IterateTrack(file, track_num);
+ track->ticks_per_beat = MIDI_GetFileTimeDivision(file);
+
+ // Default is 120 bpm.
+ // TODO: this is wrong
+
+ track->ms_per_beat = 500 * 260;
+
+ for (i=0; i<MIDI_CHANNELS_PER_TRACK; ++i)
+ {
+ InitChannel(track, &track->channels[i]);
+ }
+
+ // Schedule the first event.
+
+ ScheduleTrack(track);
+}
+
+// Start playing a mid
+
+static void I_OPL_PlaySong(void *handle, boolean looping)
+{
+ midi_file_t *file;
+ unsigned int i;
+
+ if (!music_initialized || handle == NULL)
+ {
+ return;
+ }
+
+ file = handle;
+
+ // Allocate track data.
+
+ tracks = malloc(MIDI_NumTracks(file) * sizeof(opl_track_data_t));
+
+ num_tracks = MIDI_NumTracks(file);
+ running_tracks = num_tracks;
+ song_looping = looping;
+
+ for (i=0; i<num_tracks; ++i)
+ {
+ StartTrack(file, i);
+ }
+}
+
+static void I_OPL_PauseSong(void)
+{
+ unsigned int i;
+
+ if (!music_initialized)
+ {
+ return;
+ }
+
+ // Pause OPL callbacks.
+
+ OPL_SetPaused(1);
+
+ // Turn off all main instrument voices (not percussion).
+ // This is what Vanilla does.
+
+ for (i=0; i<OPL_NUM_VOICES; ++i)
+ {
+ if (voices[i].channel != NULL
+ && voices[i].current_instr < percussion_instrs)
+ {
+ VoiceKeyOff(&voices[i]);
+ }
+ }
+}
+
+static void I_OPL_ResumeSong(void)
+{
+ if (!music_initialized)
+ {
+ return;
+ }
+
+ OPL_SetPaused(0);
+}
+
+static void I_OPL_StopSong(void)
+{
+ unsigned int i;
+
+ if (!music_initialized)
+ {
+ return;
+ }
+
+ OPL_Lock();
+
+ // Stop all playback.
+
+ OPL_ClearCallbacks();
+
+ // Free all voices.
+
+ for (i=0; i<OPL_NUM_VOICES; ++i)
+ {
+ if (voices[i].channel != NULL)
+ {
+ VoiceKeyOff(&voices[i]);
+ ReleaseVoice(&voices[i]);
+ }
+ }
+
+ // Free all track data.
+
+ for (i=0; i<num_tracks; ++i)
+ {
+ MIDI_FreeIterator(tracks[i].iter);
+ }
+
+ free(tracks);
+
+ tracks = NULL;
+ num_tracks = 0;
+
+ OPL_Unlock();
+}
+
+static void I_OPL_UnRegisterSong(void *handle)
+{
+ if (!music_initialized)
+ {
+ return;
+ }
+
+ if (handle != NULL)
+ {
+ MIDI_FreeFile(handle);
+ }
+}
+
+// Determine whether memory block is a .mid file
+
+static boolean IsMid(byte *mem, int len)
+{
+ return len > 4 && !memcmp(mem, "MThd", 4);
+}
+
+static boolean ConvertMus(byte *musdata, int len, char *filename)
+{
+ MEMFILE *instream;
+ MEMFILE *outstream;
+ void *outbuf;
+ size_t outbuf_len;
+ int result;
+
+ instream = mem_fopen_read(musdata, len);
+ outstream = mem_fopen_write();
+
+ result = mus2mid(instream, outstream);
+
+ if (result == 0)
+ {
+ mem_get_buf(outstream, &outbuf, &outbuf_len);
+
+ M_WriteFile(filename, outbuf, outbuf_len);
+ }
+
+ mem_fclose(instream);
+ mem_fclose(outstream);
+
+ return result;
+}
+
+static void *I_OPL_RegisterSong(void *data, int len)
+{
+ midi_file_t *result;
+ char *filename;
+
+ if (!music_initialized)
+ {
+ return NULL;
+ }
+
+ // MUS files begin with "MUS"
+ // Reject anything which doesnt have this signature
+
+ filename = M_TempFile("doom.mid");
+
+ if (IsMid(data, len) && len < MAXMIDLENGTH)
+ {
+ M_WriteFile(filename, data, len);
+ }
+ else
+ {
+ // Assume a MUS file and try to convert
+
+ ConvertMus(data, len, filename);
+ }
+
+ result = MIDI_LoadFile(filename);
+
+ if (result == NULL)
+ {
+ fprintf(stderr, "I_OPL_RegisterSong: Failed to load MID.\n");
+ }
+
+ // remove file now
+
+ remove(filename);
+
+ Z_Free(filename);
+
+ return result;
+}
+
+// Is the song playing?
+
+static boolean I_OPL_MusicIsPlaying(void)
+{
+ if (!music_initialized)
+ {
+ return false;
+ }
+
+ return num_tracks > 0;
+}
+
+// Shutdown music
+
+static void I_OPL_ShutdownMusic(void)
+{
+ if (music_initialized)
+ {
+ // Stop currently-playing track, if there is one:
+
+ I_OPL_StopSong();
+
+ OPL_Shutdown();
+
+ // Release GENMIDI lump
+
+ W_ReleaseLumpName("GENMIDI");
+
+ music_initialized = false;
+ }
+}
+
+// Initialize music subsystem
+
+static boolean I_OPL_InitMusic(void)
+{
+ OPL_SetSampleRate(snd_samplerate);
+
+ if (!OPL_Init(opl_io_port))
+ {
+ printf("Dude. The Adlib isn't responding.\n");
+ return false;
+ }
+
+ // Load instruments from GENMIDI lump:
+
+ if (!LoadInstrumentTable())
+ {
+ OPL_Shutdown();
+ return false;
+ }
+
+ InitVoices();
+
+ tracks = NULL;
+ num_tracks = 0;
+ music_initialized = true;
+
+ return true;
+}
+
+static snddevice_t music_opl_devices[] =
+{
+ SNDDEVICE_ADLIB,
+ SNDDEVICE_SB,
+};
+
+music_module_t music_opl_module =
+{
+ music_opl_devices,
+ arrlen(music_opl_devices),
+ I_OPL_InitMusic,
+ I_OPL_ShutdownMusic,
+ I_OPL_SetMusicVolume,
+ I_OPL_PauseSong,
+ I_OPL_ResumeSong,
+ I_OPL_RegisterSong,
+ I_OPL_UnRegisterSong,
+ I_OPL_PlaySong,
+ I_OPL_StopSong,
+ I_OPL_MusicIsPlaying,
+};
+
--- a/src/i_sdlmusic.c
+++ b/src/i_sdlmusic.c
@@ -335,8 +335,6 @@
static snddevice_t music_sdl_devices[] =
{
- SNDDEVICE_ADLIB,
- SNDDEVICE_SB,
SNDDEVICE_PAS,
SNDDEVICE_GUS,
SNDDEVICE_WAVEBLASTER,
--- a/src/i_sound.c
+++ b/src/i_sound.c
@@ -46,7 +46,7 @@
static sound_module_t *sound_module;
static music_module_t *music_module;
-int snd_musicdevice = SNDDEVICE_SB;
+int snd_musicdevice = SNDDEVICE_GENMIDI;
int snd_sfxdevice = SNDDEVICE_SB;
// Sound modules
@@ -54,6 +54,7 @@
extern sound_module_t sound_sdl_module;
extern sound_module_t sound_pcsound_module;
extern music_module_t music_sdl_module;
+extern music_module_t music_opl_module;
// DOS-specific options: These are unused but should be maintained
// so that the config file can be shared between chocolate
@@ -81,6 +82,7 @@
{
#ifdef FEATURE_SOUND
&music_sdl_module,
+ &music_opl_module,
#endif
NULL,
};
--- /dev/null
+++ b/src/midifile.c
@@ -1,0 +1,814 @@
+// Emacs style mode select -*- C++ -*-
+//-----------------------------------------------------------------------------
+//
+// Copyright(C) 2009 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:
+// Reading of MIDI files.
+//
+//-----------------------------------------------------------------------------
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+#include "doomtype.h"
+#include "i_swap.h"
+#include "midifile.h"
+
+#define HEADER_CHUNK_ID "MThd"
+#define TRACK_CHUNK_ID "MTrk"
+#define MAX_BUFFER_SIZE 0x10000
+
+typedef struct
+{
+ byte chunk_id[4];
+ unsigned int chunk_size;
+} PACKEDATTR chunk_header_t;
+
+typedef struct
+{
+ chunk_header_t chunk_header;
+ unsigned short format_type;
+ unsigned short num_tracks;
+ unsigned short time_division;
+} PACKEDATTR midi_header_t;
+
+typedef struct
+{
+ // Length in bytes:
+
+ unsigned int data_len;
+
+ // Events in this track:
+
+ midi_event_t *events;
+ int num_events;
+} midi_track_t;
+
+struct midi_track_iter_s
+{
+ midi_track_t *track;
+ unsigned int position;
+};
+
+struct midi_file_s
+{
+ midi_header_t header;
+
+ // All tracks in this file:
+ midi_track_t *tracks;
+ unsigned int num_tracks;
+
+ // Data buffer used to store data read for SysEx or meta events:
+ byte *buffer;
+ unsigned int buffer_size;
+};
+
+// Check the header of a chunk:
+
+static boolean CheckChunkHeader(chunk_header_t *chunk,
+ char *expected_id)
+{
+ boolean result;
+
+ result = (memcmp((char *) chunk->chunk_id, expected_id, 4) == 0);
+
+ if (!result)
+ {
+ fprintf(stderr, "CheckChunkHeader: Expected '%s' chunk header, "
+ "got '%c%c%c%c'\n",
+ expected_id,
+ chunk->chunk_id[0], chunk->chunk_id[1],
+ chunk->chunk_id[2], chunk->chunk_id[3]);
+ }
+
+ return result;
+}
+
+// Read a single byte. Returns false on error.
+
+static boolean ReadByte(byte *result, FILE *stream)
+{
+ int c;
+
+ c = fgetc(stream);
+
+ if (c == EOF)
+ {
+ fprintf(stderr, "ReadByte: Unexpected end of file\n");
+ return false;
+ }
+ else
+ {
+ *result = (byte) c;
+
+ return true;
+ }
+}
+
+// Read a variable-length value.
+
+static boolean ReadVariableLength(unsigned int *result, FILE *stream)
+{
+ int i;
+ byte b;
+
+ *result = 0;
+
+ for (i=0; i<4; ++i)
+ {
+ if (!ReadByte(&b, stream))
+ {
+ fprintf(stderr, "ReadVariableLength: Error while reading "
+ "variable-length value\n");
+ return false;
+ }
+
+ // Insert the bottom seven bits from this byte.
+
+ *result <<= 7;
+ *result |= b & 0x7f;
+
+ // If the top bit is not set, this is the end.
+
+ if ((b & 0x80) == 0)
+ {
+ return true;
+ }
+ }
+
+ fprintf(stderr, "ReadVariableLength: Variable-length value too "
+ "long: maximum of four bytes\n");
+ return false;
+}
+
+// Read a byte sequence into the data buffer.
+
+static void *ReadByteSequence(unsigned int num_bytes, FILE *stream)
+{
+ unsigned int i;
+ byte *result;
+
+ // Allocate a buffer:
+
+ result = malloc(num_bytes);
+
+ if (result == NULL)
+ {
+ fprintf(stderr, "ReadByteSequence: Failed to allocate buffer\n");
+ return NULL;
+ }
+
+ // Read the data:
+
+ for (i=0; i<num_bytes; ++i)
+ {
+ if (!ReadByte(&result[i], stream))
+ {
+ fprintf(stderr, "ReadByteSequence: Error while reading byte %u\n",
+ i);
+ free(result);
+ return NULL;
+ }
+ }
+
+ return result;
+}
+
+// Read a MIDI channel event.
+// two_param indicates that the event type takes two parameters
+// (three byte) otherwise it is single parameter (two byte)
+
+static boolean ReadChannelEvent(midi_event_t *event,
+ byte event_type, boolean two_param,
+ FILE *stream)
+{
+ byte b;
+
+ // Set basics:
+
+ event->event_type = event_type & 0xf0;
+ event->data.channel.channel = event_type & 0x0f;
+
+ // Read parameters:
+
+ if (!ReadByte(&b, stream))
+ {
+ fprintf(stderr, "ReadChannelEvent: Error while reading channel "
+ "event parameters\n");
+ return false;
+ }
+
+ event->data.channel.param1 = b;
+
+ // Second parameter:
+
+ if (two_param)
+ {
+ if (!ReadByte(&b, stream))
+ {
+ fprintf(stderr, "ReadChannelEvent: Error while reading channel "
+ "event parameters\n");
+ return false;
+ }
+
+ event->data.channel.param2 = b;
+ }
+
+ return true;
+}
+
+// Read sysex event:
+
+static boolean ReadSysExEvent(midi_event_t *event, int event_type,
+ FILE *stream)
+{
+ event->event_type = event_type;
+
+ if (!ReadVariableLength(&event->data.sysex.length, stream))
+ {
+ fprintf(stderr, "ReadSysExEvent: Failed to read length of "
+ "SysEx block\n");
+ return false;
+ }
+
+ // Read the byte sequence:
+
+ event->data.sysex.data = ReadByteSequence(event->data.sysex.length, stream);
+
+ if (event->data.sysex.data == NULL)
+ {
+ fprintf(stderr, "ReadSysExEvent: Failed while reading SysEx event\n");
+ return false;
+ }
+
+ return true;
+}
+
+// Read meta event:
+
+static boolean ReadMetaEvent(midi_event_t *event, FILE *stream)
+{
+ byte b;
+
+ event->event_type = MIDI_EVENT_META;
+
+ // Read meta event type:
+
+ if (!ReadByte(&b, stream))
+ {
+ fprintf(stderr, "ReadMetaEvent: Failed to read meta event type\n");
+ return false;
+ }
+
+ event->data.meta.type = b;
+
+ // Read length of meta event data:
+
+ if (!ReadVariableLength(&event->data.meta.length, stream))
+ {
+ fprintf(stderr, "ReadSysExEvent: Failed to read length of "
+ "SysEx block\n");
+ return false;
+ }
+
+ // Read the byte sequence:
+
+ event->data.meta.data = ReadByteSequence(event->data.meta.length, stream);
+
+ if (event->data.meta.data == NULL)
+ {
+ fprintf(stderr, "ReadSysExEvent: Failed while reading SysEx event\n");
+ return false;
+ }
+
+ return true;
+}
+
+static boolean ReadEvent(midi_event_t *event, unsigned int *last_event_type,
+ FILE *stream)
+{
+ byte event_type;
+
+ if (!ReadVariableLength(&event->delta_time, stream))
+ {
+ fprintf(stderr, "ReadEvent: Failed to read event timestamp\n");
+ return false;
+ }
+
+ if (!ReadByte(&event_type, stream))
+ {
+ fprintf(stderr, "ReadEvent: Failed to read event type\n");
+ return false;
+ }
+
+ // All event types have their top bit set. Therefore, if
+ // the top bit is not set, it is because we are using the "same
+ // as previous event type" shortcut to save a byte. Skip back
+ // a byte so that we read this byte again.
+
+ if ((event_type & 0x80) == 0)
+ {
+ event_type = *last_event_type;
+
+ if (fseek(stream, -1, SEEK_CUR) < 0)
+ {
+ fprintf(stderr, "ReadEvent: Unable to seek in stream\n");
+ return false;
+ }
+ }
+ else
+ {
+ *last_event_type = event_type;
+ }
+
+ // Check event type:
+
+ switch (event_type & 0xf0)
+ {
+ // Two parameter channel events:
+
+ case MIDI_EVENT_NOTE_OFF:
+ case MIDI_EVENT_NOTE_ON:
+ case MIDI_EVENT_AFTERTOUCH:
+ case MIDI_EVENT_CONTROLLER:
+ case MIDI_EVENT_PITCH_BEND:
+ return ReadChannelEvent(event, event_type, true, stream);
+
+ // Single parameter channel events:
+
+ case MIDI_EVENT_PROGRAM_CHANGE:
+ case MIDI_EVENT_CHAN_AFTERTOUCH:
+ return ReadChannelEvent(event, event_type, false, stream);
+
+ default:
+ break;
+ }
+
+ // Specific value?
+
+ switch (event_type)
+ {
+ case MIDI_EVENT_SYSEX:
+ case MIDI_EVENT_SYSEX_SPLIT:
+ return ReadSysExEvent(event, event_type, stream);
+
+ case MIDI_EVENT_META:
+ return ReadMetaEvent(event, stream);
+
+ default:
+ break;
+ }
+
+ fprintf(stderr, "ReadEvent: Unknown MIDI event type: 0x%x\n", event_type);
+ return false;
+}
+
+// Free an event:
+
+static void FreeEvent(midi_event_t *event)
+{
+ // Some event types have dynamically allocated buffers assigned
+ // to them that must be freed.
+
+ switch (event->event_type)
+ {
+ case MIDI_EVENT_SYSEX:
+ case MIDI_EVENT_SYSEX_SPLIT:
+ free(event->data.sysex.data);
+ break;
+
+ case MIDI_EVENT_META:
+ free(event->data.meta.data);
+ break;
+
+ default:
+ // Nothing to do.
+ break;
+ }
+}
+
+// Read and check the track chunk header
+
+static boolean ReadTrackHeader(midi_track_t *track, FILE *stream)
+{
+ size_t records_read;
+ chunk_header_t chunk_header;
+
+ records_read = fread(&chunk_header, sizeof(chunk_header_t), 1, stream);
+
+ if (records_read < 1)
+ {
+ return false;
+ }
+
+ if (!CheckChunkHeader(&chunk_header, TRACK_CHUNK_ID))
+ {
+ return false;
+ }
+
+ track->data_len = SDL_SwapBE32(chunk_header.chunk_size);
+
+ return true;
+}
+
+static boolean ReadTrack(midi_track_t *track, FILE *stream)
+{
+ midi_event_t *new_events;
+ midi_event_t *event;
+ unsigned int last_event_type;
+
+ track->num_events = 0;
+ track->events = NULL;
+
+ // Read the header:
+
+ if (!ReadTrackHeader(track, stream))
+ {
+ return false;
+ }
+
+ // Then the events:
+
+ last_event_type = 0;
+
+ for (;;)
+ {
+ // Resize the track slightly larger to hold another event:
+
+ new_events = realloc(track->events,
+ sizeof(midi_event_t) * (track->num_events + 1));
+
+ if (new_events == NULL)
+ {
+ return false;
+ }
+
+ track->events = new_events;
+
+ // Read the next event:
+
+ event = &track->events[track->num_events];
+ if (!ReadEvent(event, &last_event_type, stream))
+ {
+ return false;
+ }
+
+ ++track->num_events;
+
+ // End of track?
+
+ if (event->event_type == MIDI_EVENT_META
+ && event->data.meta.type == MIDI_META_END_OF_TRACK)
+ {
+ break;
+ }
+ }
+
+ return true;
+}
+
+// Free a track:
+
+static void FreeTrack(midi_track_t *track)
+{
+ unsigned int i;
+
+ for (i=0; i<track->num_events; ++i)
+ {
+ FreeEvent(&track->events[i]);
+ }
+
+ free(track->events);
+}
+
+static boolean ReadAllTracks(midi_file_t *file, FILE *stream)
+{
+ unsigned int i;
+
+ // Allocate list of tracks and read each track:
+
+ file->tracks = malloc(sizeof(midi_track_t) * file->num_tracks);
+
+ if (file->tracks == NULL)
+ {
+ return false;
+ }
+
+ memset(file->tracks, 0, sizeof(midi_track_t) * file->num_tracks);
+
+ // Read each track:
+
+ for (i=0; i<file->num_tracks; ++i)
+ {
+ if (!ReadTrack(&file->tracks[i], stream))
+ {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+// Read and check the header chunk.
+
+static boolean ReadFileHeader(midi_file_t *file, FILE *stream)
+{
+ size_t records_read;
+ unsigned int format_type;
+
+ records_read = fread(&file->header, sizeof(midi_header_t), 1, stream);
+
+ if (records_read < 1)
+ {
+ return false;
+ }
+
+ if (!CheckChunkHeader(&file->header.chunk_header, HEADER_CHUNK_ID)
+ || SDL_SwapBE32(file->header.chunk_header.chunk_size) != 6)
+ {
+ fprintf(stderr, "ReadFileHeader: Invalid MIDI chunk header! "
+ "chunk_size=%i\n",
+ SDL_SwapBE32(file->header.chunk_header.chunk_size));
+ return false;
+ }
+
+ format_type = SDL_SwapBE16(file->header.format_type);
+ file->num_tracks = SDL_SwapBE16(file->header.num_tracks);
+
+ if ((format_type != 0 && format_type != 1)
+ || file->num_tracks < 1)
+ {
+ fprintf(stderr, "ReadFileHeader: Only type 0/1 "
+ "MIDI files supported!\n");
+ return false;
+ }
+
+ return true;
+}
+
+void MIDI_FreeFile(midi_file_t *file)
+{
+ int i;
+
+ if (file->tracks != NULL)
+ {
+ for (i=0; i<file->num_tracks; ++i)
+ {
+ FreeTrack(&file->tracks[i]);
+ }
+
+ free(file->tracks);
+ }
+
+ free(file);
+}
+
+midi_file_t *MIDI_LoadFile(char *filename)
+{
+ midi_file_t *file;
+ FILE *stream;
+
+ file = malloc(sizeof(midi_file_t));
+
+ if (file == NULL)
+ {
+ return NULL;
+ }
+
+ file->tracks = NULL;
+ file->num_tracks = 0;
+ file->buffer = NULL;
+ file->buffer_size = 0;
+
+ // Open file
+
+ stream = fopen(filename, "rb");
+
+ if (stream == NULL)
+ {
+ fprintf(stderr, "MIDI_LoadFile: Failed to open '%s'\n", filename);
+ MIDI_FreeFile(file);
+ return NULL;
+ }
+
+ // Read MIDI file header
+
+ if (!ReadFileHeader(file, stream))
+ {
+ fclose(stream);
+ MIDI_FreeFile(file);
+ return NULL;
+ }
+
+ // Read all tracks:
+
+ if (!ReadAllTracks(file, stream))
+ {
+ fclose(stream);
+ MIDI_FreeFile(file);
+ return NULL;
+ }
+
+ fclose(stream);
+
+ return file;
+}
+
+// Get the number of tracks in a MIDI file.
+
+unsigned int MIDI_NumTracks(midi_file_t *file)
+{
+ return file->num_tracks;
+}
+
+// Start iterating over the events in a track.
+
+midi_track_iter_t *MIDI_IterateTrack(midi_file_t *file, unsigned int track)
+{
+ midi_track_iter_t *iter;
+
+ assert(track < file->num_tracks);
+
+ iter = malloc(sizeof(*iter));
+ iter->track = &file->tracks[track];
+ iter->position = 0;
+
+ return iter;
+}
+
+void MIDI_FreeIterator(midi_track_iter_t *iter)
+{
+ free(iter);
+}
+
+// Get the time until the next MIDI event in a track.
+
+unsigned int MIDI_GetDeltaTime(midi_track_iter_t *iter)
+{
+ if (iter->position < iter->track->num_events)
+ {
+ midi_event_t *next_event;
+
+ next_event = &iter->track->events[iter->position];
+
+ return next_event->delta_time;
+ }
+ else
+ {
+ return 0;
+ }
+}
+
+// Get a pointer to the next MIDI event.
+
+int MIDI_GetNextEvent(midi_track_iter_t *iter, midi_event_t **event)
+{
+ if (iter->position < iter->track->num_events)
+ {
+ *event = &iter->track->events[iter->position];
+ ++iter->position;
+
+ return 1;
+ }
+ else
+ {
+ return 0;
+ }
+}
+
+unsigned int MIDI_GetFileTimeDivision(midi_file_t *file)
+{
+ return file->header.time_division;
+}
+
+void MIDI_RestartIterator(midi_track_iter_t *iter)
+{
+ iter->position = 0;
+}
+
+#ifdef TEST
+
+static char *MIDI_EventTypeToString(midi_event_type_t event_type)
+{
+ switch (event_type)
+ {
+ case MIDI_EVENT_NOTE_OFF:
+ return "MIDI_EVENT_NOTE_OFF";
+ case MIDI_EVENT_NOTE_ON:
+ return "MIDI_EVENT_NOTE_ON";
+ case MIDI_EVENT_AFTERTOUCH:
+ return "MIDI_EVENT_AFTERTOUCH";
+ case MIDI_EVENT_CONTROLLER:
+ return "MIDI_EVENT_CONTROLLER";
+ case MIDI_EVENT_PROGRAM_CHANGE:
+ return "MIDI_EVENT_PROGRAM_CHANGE";
+ case MIDI_EVENT_CHAN_AFTERTOUCH:
+ return "MIDI_EVENT_CHAN_AFTERTOUCH";
+ case MIDI_EVENT_PITCH_BEND:
+ return "MIDI_EVENT_PITCH_BEND";
+ case MIDI_EVENT_SYSEX:
+ return "MIDI_EVENT_SYSEX";
+ case MIDI_EVENT_SYSEX_SPLIT:
+ return "MIDI_EVENT_SYSEX_SPLIT";
+ case MIDI_EVENT_META:
+ return "MIDI_EVENT_META";
+
+ default:
+ return "(unknown)";
+ }
+}
+
+void PrintTrack(midi_track_t *track)
+{
+ midi_event_t *event;
+ unsigned int i;
+
+ for (i=0; i<track->num_events; ++i)
+ {
+ event = &track->events[i];
+
+ if (event->delta_time > 0)
+ {
+ printf("Delay: %i ticks\n", event->delta_time);
+ }
+
+ printf("Event type: %s (%i)\n",
+ MIDI_EventTypeToString(event->event_type),
+ event->event_type);
+
+ switch(event->event_type)
+ {
+ case MIDI_EVENT_NOTE_OFF:
+ case MIDI_EVENT_NOTE_ON:
+ case MIDI_EVENT_AFTERTOUCH:
+ case MIDI_EVENT_CONTROLLER:
+ case MIDI_EVENT_PROGRAM_CHANGE:
+ case MIDI_EVENT_CHAN_AFTERTOUCH:
+ case MIDI_EVENT_PITCH_BEND:
+ printf("\tChannel: %i\n", event->data.channel.channel);
+ printf("\tParameter 1: %i\n", event->data.channel.param1);
+ printf("\tParameter 2: %i\n", event->data.channel.param2);
+ break;
+
+ case MIDI_EVENT_SYSEX:
+ case MIDI_EVENT_SYSEX_SPLIT:
+ printf("\tLength: %i\n", event->data.sysex.length);
+ break;
+
+ case MIDI_EVENT_META:
+ printf("\tMeta type: %i\n", event->data.meta.type);
+ printf("\tLength: %i\n", event->data.meta.length);
+ break;
+ }
+ }
+}
+
+int main(int argc, char *argv[])
+{
+ midi_file_t *file;
+ unsigned int i;
+
+ if (argc < 2)
+ {
+ printf("Usage: %s <filename>\n", argv[0]);
+ exit(1);
+ }
+
+ file = MIDI_LoadFile(argv[1]);
+
+ if (file == NULL)
+ {
+ fprintf(stderr, "Failed to open %s\n", argv[1]);
+ exit(1);
+ }
+
+ for (i=0; i<file->num_tracks; ++i)
+ {
+ printf("\n== Track %i ==\n\n", i);
+
+ PrintTrack(&file->tracks[i]);
+ }
+
+ return 0;
+}
+
+#endif
+
--- /dev/null
+++ b/src/midifile.h
@@ -1,0 +1,175 @@
+// Emacs style mode select -*- C++ -*-
+//-----------------------------------------------------------------------------
+//
+// Copyright(C) 2009 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:
+// MIDI file parsing.
+//
+//-----------------------------------------------------------------------------
+
+#ifndef MIDIFILE_H
+#define MIDIFILE_H
+
+typedef struct midi_file_s midi_file_t;
+typedef struct midi_track_iter_s midi_track_iter_t;
+
+#define MIDI_CHANNELS_PER_TRACK 16
+
+typedef enum
+{
+ MIDI_EVENT_NOTE_OFF = 0x80,
+ MIDI_EVENT_NOTE_ON = 0x90,
+ MIDI_EVENT_AFTERTOUCH = 0xa0,
+ MIDI_EVENT_CONTROLLER = 0xb0,
+ MIDI_EVENT_PROGRAM_CHANGE = 0xc0,
+ MIDI_EVENT_CHAN_AFTERTOUCH = 0xd0,
+ MIDI_EVENT_PITCH_BEND = 0xe0,
+
+ MIDI_EVENT_SYSEX = 0xf0,
+ MIDI_EVENT_SYSEX_SPLIT = 0xf7,
+ MIDI_EVENT_META = 0xff,
+} midi_event_type_t;
+
+typedef enum
+{
+ MIDI_CONTROLLER_BANK_SELECT = 0x0,
+ MIDI_CONTROLLER_MODULATION = 0x1,
+ MIDI_CONTROLLER_BREATH_CONTROL = 0x2,
+ MIDI_CONTROLLER_FOOT_CONTROL = 0x3,
+ MIDI_CONTROLLER_PORTAMENTO = 0x4,
+ MIDI_CONTROLLER_DATA_ENTRY = 0x5,
+
+ MIDI_CONTROLLER_MAIN_VOLUME = 0x7,
+ MIDI_CONTROLLER_PAN = 0xa
+} midi_controller_t;
+
+typedef enum
+{
+ MIDI_META_SEQUENCE_NUMBER = 0x0,
+
+ MIDI_META_TEXT = 0x1,
+ MIDI_META_COPYRIGHT = 0x2,
+ MIDI_META_TRACK_NAME = 0x3,
+ MIDI_META_INSTR_NAME = 0x4,
+ MIDI_META_LYRICS = 0x5,
+ MIDI_META_MARKER = 0x6,
+ MIDI_META_CUE_POINT = 0x7,
+
+ MIDI_META_CHANNEL_PREFIX = 0x20,
+ MIDI_META_END_OF_TRACK = 0x2f,
+
+ MIDI_META_SET_TEMPO = 0x51,
+ MIDI_META_SMPTE_OFFSET = 0x54,
+ MIDI_META_TIME_SIGNATURE = 0x58,
+ MIDI_META_KEY_SIGNATURE = 0x59,
+ MIDI_META_SEQUENCER_SPECIFIC = 0x7f,
+} midi_meta_event_type_t;
+
+typedef struct
+{
+ // Meta event type:
+
+ unsigned int type;
+
+ // Length:
+
+ unsigned int length;
+
+ // Meta event data:
+
+ byte *data;
+} midi_meta_event_data_t;
+
+typedef struct
+{
+ // Length:
+
+ unsigned int length;
+
+ // Event data:
+
+ byte *data;
+} midi_sysex_event_data_t;
+
+typedef struct
+{
+ // The channel number to which this applies:
+
+ unsigned int channel;
+
+ // Extra parameters:
+
+ unsigned int param1;
+ unsigned int param2;
+} midi_channel_event_data_t;
+
+typedef struct
+{
+ // Time between the previous event and this event.
+ unsigned int delta_time;
+
+ // Type of event:
+ midi_event_type_t event_type;
+
+ union
+ {
+ midi_channel_event_data_t channel;
+ midi_meta_event_data_t meta;
+ midi_sysex_event_data_t sysex;
+ } data;
+} midi_event_t;
+
+// Load a MIDI file.
+
+midi_file_t *MIDI_LoadFile(char *filename);
+
+// Free a MIDI file.
+
+void MIDI_FreeFile(midi_file_t *file);
+
+// Get the time division value from the MIDI header.
+
+unsigned int MIDI_GetFileTimeDivision(midi_file_t *file);
+
+// Get the number of tracks in a MIDI file.
+
+unsigned int MIDI_NumTracks(midi_file_t *file);
+
+// Start iterating over the events in a track.
+
+midi_track_iter_t *MIDI_IterateTrack(midi_file_t *file, unsigned int track_num);
+
+// Free an iterator.
+
+void MIDI_FreeIterator(midi_track_iter_t *iter);
+
+// Get the time until the next MIDI event in a track.
+
+unsigned int MIDI_GetDeltaTime(midi_track_iter_t *iter);
+
+// Get a pointer to the next MIDI event.
+
+int MIDI_GetNextEvent(midi_track_iter_t *iter, midi_event_t **event);
+
+// Reset an iterator to the beginning of a track.
+
+void MIDI_RestartIterator(midi_track_iter_t *iter);
+
+#endif /* #ifndef MIDIFILE_H */
+
--- a/src/setup/sound.c
+++ b/src/setup/sound.c
@@ -40,7 +40,7 @@
NUM_SFXMODES
} sfxmode_t;
-static char *sfxmode_strings[] =
+static char *sfxmode_strings[] =
{
"Disabled",
"Digital",
@@ -47,10 +47,11 @@
"PC speaker"
};
-typedef enum
+typedef enum
{
MUSICMODE_DISABLED,
- MUSICMODE_MIDI,
+ MUSICMODE_OPL,
+ MUSICMODE_NATIVE,
MUSICMODE_CD,
NUM_MUSICMODES
} musicmode_t;
@@ -58,7 +59,8 @@
static char *musicmode_strings[] =
{
"Disabled",
- "MIDI",
+ "OPL (Adlib/SB)",
+ "Native MIDI",
"CD audio"
};
@@ -65,7 +67,7 @@
// Config file variables:
int snd_sfxdevice = SNDDEVICE_SB;
-int snd_musicdevice = SNDDEVICE_SB;
+int snd_musicdevice = SNDDEVICE_GENMIDI;
int snd_samplerate = 22050;
static int numChannels = 8;
@@ -107,7 +109,10 @@
case MUSICMODE_DISABLED:
snd_musicdevice = SNDDEVICE_NONE;
break;
- case MUSICMODE_MIDI:
+ case MUSICMODE_NATIVE:
+ snd_musicdevice = SNDDEVICE_GENMIDI;
+ break;
+ case MUSICMODE_OPL:
snd_musicdevice = SNDDEVICE_SB;
break;
case MUSICMODE_CD:
@@ -138,20 +143,26 @@
{
snd_sfxmode = SFXMODE_DISABLED;
}
-
+
// Is music enabled?
- if (snd_musicdevice == SNDDEVICE_NONE)
+ if (snd_musicdevice == SNDDEVICE_GENMIDI)
{
- snd_musicmode = MUSICMODE_DISABLED;
+ snd_musicmode = MUSICMODE_NATIVE;
}
else if (snd_musicmode == SNDDEVICE_CD)
{
snd_musicmode = MUSICMODE_CD;
}
+ else if (snd_musicdevice == SNDDEVICE_SB
+ || snd_musicdevice == SNDDEVICE_ADLIB
+ || snd_musicdevice == SNDDEVICE_AWE32)
+ {
+ snd_musicmode = MUSICMODE_OPL;
+ }
else
{
- snd_musicmode = MUSICMODE_MIDI;
+ snd_musicmode = MUSICMODE_DISABLED;
}
// Doom has PC speaker sound effects, but others do not:
@@ -187,7 +198,7 @@
music_table = TXT_NewTable(2),
NULL);
- TXT_SetColumnWidths(sfx_table, 20, 5);
+ TXT_SetColumnWidths(sfx_table, 20, 14);
TXT_AddWidgets(sfx_table,
TXT_NewLabel("Sound effects"),
@@ -200,7 +211,7 @@
TXT_NewSpinControl(&sfxVolume, 0, 15),
NULL);
- TXT_SetColumnWidths(music_table, 20, 5);
+ TXT_SetColumnWidths(music_table, 20, 14);
TXT_AddWidgets(music_table,
TXT_NewLabel("Music"),
@@ -213,7 +224,6 @@
TXT_SignalConnect(sfx_mode_control, "changed", UpdateSndDevices, NULL);
TXT_SignalConnect(music_mode_control, "changed", UpdateSndDevices, NULL);
-
}
void BindSoundVariables(void)
--- a/textscreen/Makefile.am
+++ b/textscreen/Makefile.am
@@ -9,6 +9,8 @@
noinst_LIBRARIES=libtextscreen.a
+EXTRA_DIST=Doxyfile
+
libtextscreen_a_SOURCES = \
textscreen.h \
txt_checkbox.c txt_checkbox.h \
@@ -21,11 +23,11 @@
txt_button.c txt_button.h \
txt_label.c txt_label.h \
txt_radiobutton.c txt_radiobutton.h \
- txt_scrollpane.c txt_scrollpane.h \
+ txt_scrollpane.c txt_scrollpane.h \
txt_separator.c txt_separator.h \
txt_spinctrl.c txt_spinctrl.h \
txt_sdl.c txt_sdl.h \
- txt_smallfont.h \
+ txt_smallfont.h \
txt_strut.c txt_strut.h \
txt_table.c txt_table.h \
txt_widget.c txt_widget.h \