OSDN Git Service

refresh wwww
[proj16/16.git] / src / lib / 16_snd.c
1 /* Project 16 Source Code~\r
2  * Copyright (C) 2012-2016 sparky4 & pngwen & andrius4669 & joncampbell123\r
3  *\r
4  * This file is part of Project 16.\r
5  *\r
6  * Project 16 is free software; you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation; either version 3 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * Project 16 is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>, or\r
18  * write to the Free Software Foundation, Inc., 51 Franklin Street,\r
19  * Fifth Floor, Boston, MA 02110-1301 USA.\r
20  *\r
21  */\r
22 \r
23 #include "src/lib/16_snd.h"\r
24 \r
25 void opl2out(word reg, word data)\r
26 {\r
27         __asm\r
28         {\r
29                 mov     ax,reg\r
30                 mov     dx,word ptr [ADLIB_FM_ADDRESS]\r
31                 or      ah,ah\r
32                 jz      @@1\r
33                 add     dx,2\r
34 @@1:    out     dx,al\r
35                 mov     cx,6\r
36 @@2:    in      al,dx\r
37                 loop    @@2\r
38                 inc     dl\r
39                 mov     ax,data\r
40                 out     dx,al\r
41                 dec     dl\r
42                 mov     cx,36\r
43 @@3:    in      al,dx\r
44                 loop    @@3\r
45         }\r
46 }\r
47 \r
48 void opl3out(word reg, word data)\r
49 {\r
50         __asm\r
51         {\r
52                 mov     ax,reg\r
53                 mov     dx,word ptr [ADLIB_FM_ADDRESS]\r
54                 or      ah,ah\r
55                 jz      @@1\r
56                 add     dx,2\r
57 @@1:    out     dx,al\r
58                 inc     dl\r
59                 mov     ax,data\r
60                 out     dx,al\r
61                 dec     dl\r
62                 mov     cx,26\r
63 @@2:    in      al,dx\r
64                 loop    @@2\r
65         }\r
66 }\r
67 \r
68 void opl3exp(word data)\r
69 {\r
70         __asm\r
71         {\r
72                 mov     ax,data\r
73                 mov     dx,word ptr [ADLIB_FM_ADDRESS]\r
74                 add     dx,2\r
75                 out     dx,al\r
76                 mov     cx,6\r
77 @@1:    in      al,dx\r
78                 loop    @@1\r
79                 inc     dl\r
80                 mov     al,ah\r
81                 out     dx,al\r
82                 mov     cx,36\r
83 @@2:    in      al,dx\r
84                 loop    @@2\r
85         }\r
86 }\r
87 \r
88 /* Function: FMResest *******************************************************\r
89 *\r
90 *     Description:        quick and dirty sound card reset (zeros all\r
91 *                         registers).\r
92 *\r
93 */\r
94 void FMReset(void/*int percusiveMode*/)\r
95 {\r
96         int i;\r
97 \r
98         /* zero all registers */\r
99         for(i = MIN_REGISTER; i < MAX_REGISTER+1; i++) opl2out(i, 0);\r
100 \r
101         /* allow FM chips to control the waveform of each operator */\r
102         opl2out(0x01, 0x20);\r
103 \r
104         /* set rhythm enabled (6 melodic voices, 5 percussive) */\r
105         opl2out(0xBD, 0x20);\r
106 \r
107         //FMSetPercusiveMode(percusiveMode);\r
108 } /* End of FMReset */\r
109 \r
110 /* Function: FMKeyOff *******************************************************\r
111 *\r
112 *     Parameters:        voice - which voice to turn off.\r
113 *\r
114 *     Description:        turns off the specified voice.\r
115 *\r
116 */\r
117 void FMKeyOff(int voice)\r
118 {\r
119         int regNum;\r
120 \r
121         /* turn voice off */\r
122         regNum = 0xB0 + voice % 11;//NUMVOICE;\r
123         opl2out(regNum, 0x0E);\r
124 } /* End of FMKeyOff */\r
125 \r
126 /* Function: FMKeyOn *******************************************************\r
127 *\r
128 *     Parameters:        voice - which voice to turn on.\r
129 *                         freq - its frequency (note).\r
130 *                         octave - its octave.\r
131 *\r
132 *     Description:        turns on a voice of specfied frequency and\r
133 *                         octave.\r
134 *\r
135 */\r
136 void FMKeyOn(int voice, int freq, int octave)\r
137 {\r
138         int regNum, tmp;\r
139 \r
140         regNum = 0xA0 + voice % 11;//NUMVOICE;\r
141         opl2out(regNum, freq & 0xff);\r
142         regNum = 0xB0 + voice % 11;//NUMVOICE;\r
143         tmp = (freq >> 8) | (octave << 2) | 0x20;\r
144         opl2out(regNum, tmp);\r
145 } /* End of FMKeyOn */\r
146 \r
147 /* Function: FMSetVoice *****************************************************\r
148 *\r
149 *     Parameters:        voiceNum - which voice to set.\r
150 *                         ins - instrument to set voice.\r
151 *\r
152 *     Description:        sets the instrument of a voice.\r
153 *\r
154 */\r
155 void FMSetVoice(int voiceNum, FMInstrument *ins){\r
156         int opCellNum, cellOffset;\r
157 \r
158         voiceNum %= 11;//NUMVOICE;\r
159         cellOffset = voiceNum % 3 + ((voiceNum / 3) << 3);\r
160 \r
161         /* set sound characteristic */\r
162         opCellNum = 0x20 + (char)cellOffset;\r
163         opl2out(opCellNum, ins->SoundCharacteristic[0]);\r
164         opCellNum += 3;\r
165         opl2out(opCellNum, ins->SoundCharacteristic[1]);\r
166 \r
167         /* set level/output */\r
168         opCellNum = 0x40 + (char)cellOffset;\r
169         opl2out(opCellNum, ins->Level[0]);\r
170         opCellNum += 3;\r
171         opl2out(opCellNum, ins->Level[1]);\r
172 \r
173         /* set Attack/Decay */\r
174         opCellNum = 0x60 + (char)cellOffset;\r
175         opl2out(opCellNum, ins->AttackDecay[0]);\r
176         opCellNum += 3;\r
177         opl2out(opCellNum, ins->AttackDecay[1]);\r
178 \r
179         /* set Sustain/Release */\r
180         opCellNum = 0x80 + (char)cellOffset;\r
181         opl2out(opCellNum, ins->SustainRelease[0]);\r
182         opCellNum += 3;\r
183         opl2out(opCellNum, ins->SustainRelease[1]);\r
184 \r
185         /* set Wave Select */\r
186         opCellNum = 0xE0 + (char)cellOffset;\r
187         opl2out(opCellNum, ins->WaveSelect[0]);\r
188         opCellNum += 3;\r
189         opl2out(opCellNum, ins->WaveSelect[1]);\r
190 \r
191         /* set Feedback/Selectivity */\r
192         opCellNum = (byte)0xC0 + (byte)voiceNum;\r
193         opl2out(opCellNum, ins->Feedback);\r
194 } /* End of FMSetVoice */\r