OSDN Git Service

updated copyleft and need to test and fix newer version of open watcom
[proj16/16.git] / src / lib / 16_snd.c
old mode 100644 (file)
new mode 100755 (executable)
index 47cab34..6c46ac3
@@ -1,31 +1,32 @@
-/* Project 16 Source Code~
- * Copyright (C) 2012-2015 sparky4 & pngwen & andrius4669
- *
- * This file is part of Project 16.
- *
- * Project 16 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 3 of the License, or
- * (at your option) any later version.
- *
- * Project 16 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.
- * 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, see <http://www.gnu.org/licenses/>, or
- * write to the Free Software Foundation, Inc., 51 Franklin Street,
- * Fifth Floor, Boston, MA 02110-1301 USA.
- *
- */
-
-#include "src/lib/16_snd.h"
-
-void opl2out(word reg, word data)
+/* Project 16 Source Code~\r
+ * Copyright (C) 2012-2022 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover\r
+ *\r
+ * This file is part of Project 16.\r
+ *\r
+ * Project 16 is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * Project 16 is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>, or\r
+ * write to the Free Software Foundation, Inc., 51 Franklin Street,\r
+ * Fifth Floor, Boston, MA 02110-1301 USA.\r
+ *\r
+ */\r
+\r
+#include "src/lib/16_snd.h"\r
+\r
+static void interrupt  (*t0OldService)(void);\r
+\r
+void opl2out(word reg, word data)\r
 {\r
-       __asm
+       __asm\r
        {\r
                mov     ax,reg\r
                mov     dx,word ptr [ADLIB_FM_ADDRESS]\r
@@ -43,12 +44,12 @@ void opl2out(word reg, word data)
                mov     cx,36\r
 @@3:    in      al,dx\r
                loop    @@3\r
-       }
+       }\r
 }\r
 \r
-void opl3out(word reg, word data)
+void opl3out(word reg, word data)\r
 {\r
-       __asm
+       __asm\r
        {\r
                mov     ax,reg\r
                mov     dx,word ptr [ADLIB_FM_ADDRESS]\r
@@ -63,12 +64,12 @@ void opl3out(word reg, word data)
                mov     cx,26\r
 @@2:    in      al,dx\r
                loop    @@2\r
-       }
+       }\r
 }\r
 \r
-void opl3exp(word data)
+void opl3exp(word data)\r
 {\r
-       __asm
+       __asm\r
        {\r
                mov     ax,data\r
                mov     dx,word ptr [ADLIB_FM_ADDRESS]\r
@@ -82,17 +83,17 @@ void opl3exp(word data)
                out     dx,al\r
                mov     cx,36\r
 @@2:    in      al,dx\r
-               loop    @@2
+               loop    @@2\r
        }\r
-}
-
+}\r
+\r
 /* Function: FMResest *******************************************************\r
 *\r
 *     Description:        quick and dirty sound card reset (zeros all\r
 *                         registers).\r
 *\r
 */\r
-void FMReset(void/*int percusiveMode*/)
+void FMReset(void/*int percusiveMode*/)\r
 {\r
        int i;\r
 \r
@@ -106,8 +107,8 @@ void FMReset(void/*int percusiveMode*/)
        opl2out(0xBD, 0x20);\r
 \r
        //FMSetPercusiveMode(percusiveMode);\r
-} /* End of FMReset */
-
+} /* End of FMReset */\r
+\r
 /* Function: FMKeyOff *******************************************************\r
 *\r
 *     Parameters:        voice - which voice to turn off.\r
@@ -115,7 +116,7 @@ void FMReset(void/*int percusiveMode*/)
 *     Description:        turns off the specified voice.\r
 *\r
 */\r
-void FMKeyOff(int voice)
+void FMKeyOff(int voice)\r
 {\r
        int regNum;\r
 \r
@@ -134,17 +135,17 @@ void FMKeyOff(int voice)
 *                         octave.\r
 *\r
 */\r
-void FMKeyOn(int voice, int freq, int octave)
+void FMKeyOn(int voice, int freq, int octave)\r
 {\r
        int regNum, tmp;\r
 \r
        regNum = 0xA0 + voice % 11;//NUMVOICE;\r
        opl2out(regNum, freq & 0xff);\r
        regNum = 0xB0 + voice % 11;//NUMVOICE;\r
-       tmp = (freq >> 8) | (octave << 2) | 0x20;
+       tmp = (freq >> 8) | (octave << 2) | 0x20;\r
        opl2out(regNum, tmp);\r
-} /* End of FMKeyOn */
-
+} /* End of FMKeyOn */\r
+\r
 /* Function: FMSetVoice *****************************************************\r
 *\r
 *     Parameters:        voiceNum - which voice to set.\r
@@ -192,4 +193,190 @@ void FMSetVoice(int voiceNum, FMInstrument *ins){
        /* set Feedback/Selectivity */\r
        opCellNum = (byte)0xC0 + (byte)voiceNum;\r
        opl2out(opCellNum, ins->Feedback);\r
-} /* End of FMSetVoice */
+} /* End of FMSetVoice */\r
+\r
+\r
+//newer sd\r
+\r
+\r
+struct glob_game_vars  *ggvv;\r
+// WARNING: subroutine call in interrupt handler. make sure you compile with -zu flag for large/compact memory models\r
+void interrupt SDL_irq0()\r
+{\r
+       ggvv->ca.sd.irq0_ticks++;\r
+       if ((ggvv->ca.sd.irq0_cnt += ggvv->ca.sd.irq0_add) >= ggvv->ca.sd.irq0_max) {\r
+               ggvv->ca.sd.irq0_cnt -= ggvv->ca.sd.irq0_max;\r
+               t0OldService();\r
+       }\r
+       else {\r
+               p8259_OCW2(0,P8259_OCW2_NON_SPECIFIC_EOI);\r
+       }\r
+}\r
+\r
+void SD_Initimf(global_game_variables_t *gvar)\r
+{\r
+       if (!init_adlib()) {\r
+               printf("Cannot init library\n");\r
+               return;\r
+       }\r
+       if (!probe_8254()) { /* we need the timer to keep time with the music */\r
+               printf("8254 timer not found\n");\r
+               return;\r
+       }\r
+\r
+       gvar->ca.sd.imf_delay_countdown=0;\r
+       gvar->ca.sd.imf_music=\r
+       gvar->ca.sd.imf_play_ptr=\r
+       gvar->ca.sd.imf_music_end=NULL;\r
+\r
+       SD_adlib_shut_up();\r
+       shutdown_adlib_opl3(); // NTS: Apparently the music won't play otherwise\r
+}\r
+\r
+void SD_imf_reset_music(global_game_variables_t *gvar)\r
+{\r
+       gvar->ca.sd.imf_music = gvar->ca.sd.imf_play_ptr = gvar->ca.sd.imf_music_end = NULL;\r
+       gvar->ca.sd.imf_delay_countdown = 0;\r
+}\r
+\r
+void SD_StartupTimer(global_game_variables_t *gvar)\r
+{\r
+       gvar->ca.sd.irq0_ticks=\r
+       gvar->ca.sd.irq0_cnt = 0;\r
+       gvar->ca.sd.irq0_add = 182;\r
+       gvar->ca.sd.irq0_max = 1000; /* about 18.2Hz */\r
+       gvar->ca.sd.tickrate = 700;\r
+\r
+       write_8254_system_timer(T8254_REF_CLOCK_HZ / gvar->ca.sd.tickrate);\r
+       t0OldService = _dos_getvect(8); /*IRQ0*/\r
+       _dos_setvect(8,SDL_irq0);\r
+\r
+       _cli();\r
+       gvar->ca.sd.irq0_ticks = gvar->ca.sd.ptick = 0;\r
+       _sti();\r
+}\r
+\r
+void SD_ShutdownTimer()\r
+{\r
+       _dos_setvect(8,t0OldService);\r
+}\r
+\r
+void SD_imf_free_music(global_game_variables_t *gvar)\r
+{\r
+#ifndef SD_USESCAMMPM\r
+       if (gvar->ca.sd.imf_music) free(gvar->ca.sd.imf_music);\r
+#else\r
+       MM_FreePtr(MEMPTRCONV gvar->ca.audiosegs[0], gvar);     //TODO make behave like id engine\r
+#endif\r
+       SD_imf_reset_music(gvar);\r
+}\r
+\r
+int SD_imf_load_music(const char *path, global_game_variables_t *gvar)\r
+{\r
+       unsigned long len;\r
+       unsigned char buf[8];\r
+       int fd;\r
+\r
+#ifndef SD_USESCAMMPM\r
+       SD_imf_free_music(gvar);\r
+#else\r
+       SD_imf_reset_music(gvar);\r
+#endif\r
+\r
+       fd = open(path,O_RDONLY|O_BINARY);\r
+       if (fd < 0) return 0;\r
+\r
+       len = lseek(fd,0,SEEK_END);\r
+       lseek(fd,0,SEEK_SET);\r
+       read(fd,buf,2);\r
+       if (buf[0] != 0 || buf[1] != 0) // type 1 IMF\r
+               len = *((uint16_t*)buf);\r
+       else\r
+               lseek(fd,0,SEEK_SET);\r
+\r
+       if (len == 0 || len > 65535UL) {\r
+               close(fd);\r
+               return 0;\r
+       }\r
+       len -= len & 3;\r
+\r
+#ifndef SD_USESCAMMPM\r
+       gvar->ca.sd.imf_music = malloc(len);\r
+#else\r
+       MM_GetPtr(MEMPTRCONV gvar->ca.audiosegs[0],len, gvar);\r
+       gvar->ca.sd.imf_music = (struct imf_entry *)gvar->ca.audiosegs[0];\r
+#endif\r
+       if (gvar->ca.sd.imf_music == NULL) {\r
+               close(fd);\r
+               return 0;\r
+       }\r
+       read(fd,gvar->ca.sd.imf_music,len);\r
+       close(fd);\r
+\r
+       gvar->ca.sd.imf_play_ptr = gvar->ca.sd.imf_music;\r
+       gvar->ca.sd.imf_music_end = gvar->ca.sd.imf_music + (len >> 2UL);\r
+       return 1;\r
+}\r
+\r
+void SD_imf_tick(global_game_variables_t *gvar)\r
+{\r
+       if (gvar->ca.sd.imf_delay_countdown == 0) {\r
+               do {\r
+                       adlib_write(gvar->ca.sd.imf_play_ptr->reg,gvar->ca.sd.imf_play_ptr->data);\r
+                       gvar->ca.sd.imf_delay_countdown = gvar->ca.sd.imf_play_ptr->delay;\r
+                       gvar->ca.sd.imf_play_ptr++;\r
+                       if (gvar->ca.sd.imf_play_ptr == gvar->ca.sd.imf_music_end)\r
+                       {\r
+//                             printf("replay\n");\r
+                               gvar->ca.sd.imf_play_ptr = gvar->ca.sd.imf_music;\r
+                       }\r
+               } while (gvar->ca.sd.imf_delay_countdown == 0);\r
+       }\r
+       else {\r
+               gvar->ca.sd.imf_delay_countdown--;\r
+       }\r
+}\r
+\r
+void SD_adlib_shut_up() {\r
+       int i;\r
+\r
+       memset(adlib_fm,0,sizeof(adlib_fm));\r
+       memset(&adlib_reg_bd,0,sizeof(adlib_reg_bd));\r
+       for (i=0;i < adlib_fm_voices;i++) {\r
+               struct adlib_fm_operator *f;\r
+               f = &adlib_fm[i].mod;\r
+               f->ch_a = f->ch_b = f->ch_c = f->ch_d = 1;\r
+               f = &adlib_fm[i].car;\r
+               f->ch_a = f->ch_b = f->ch_c = f->ch_d = 1;\r
+       }\r
+\r
+       for (i=0;i < adlib_fm_voices;i++) {\r
+               struct adlib_fm_operator *f;\r
+\r
+               f = &adlib_fm[i].mod;\r
+               f->mod_multiple = 1;\r
+               f->total_level = 63 - 16;\r
+               f->attack_rate = 15;\r
+               f->decay_rate = 4;\r
+               f->sustain_level = 0;\r
+               f->release_rate = 8;\r
+               f->f_number = 400;\r
+               f->sustain = 1;\r
+               f->octave = 4;\r
+               f->key_on = 0;\r
+\r
+               f = &adlib_fm[i].car;\r
+               f->mod_multiple = 1;\r
+               f->total_level = 63 - 16;\r
+               f->attack_rate = 15;\r
+               f->decay_rate = 4;\r
+               f->sustain_level = 0;\r
+               f->release_rate = 8;\r
+               f->f_number = 0;\r
+               f->sustain = 1;\r
+               f->octave = 0;\r
+               f->key_on = 0;\r
+       }\r
+\r
+       adlib_apply_all();\r
+}\r