1
/**************************************************************************
3
fmout.cc - class fmOut which handles the /dev/sequencer device
5
This file is part of LibKMid 0.9.5
6
Copyright (C) 1998,99,2000 Antonio Larrosa Jimenez
7
LibKMid's homepage : http://www.arrakis.es/~rlarrosa/libkmid.html
9
This library is free software; you can redistribute it and/or
10
modify it under the terms of the GNU Library General Public
11
License as published by the Free Software Foundation; either
12
version 2 of the License, or (at your option) any later version.
14
This library is distributed in the hope that it will be useful,
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
Library General Public License for more details.
19
You should have received a copy of the GNU Library General Public License
20
along with this library; see the file COPYING.LIB. If not, write to
21
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22
Boston, MA 02110-1301, USA.
24
Send comments and bug fixes to Antonio Larrosa <larrosa@kde.org>
26
***************************************************************************/
32
#include <sys/ioctl.h>
35
#include <sys/param.h>
45
FMOut::FMOut( int d, int total )
51
// Put opl=3 for opl/3 (better quality/ 6 voices)
52
// or opl=2 for fm output (less quality/ 18 voices, which is better imho) :
54
// But be aware that opl=3 is not intended to be fully supported by now
57
vm = new VoiceManager (nvoices);
64
if (deleteFMPatchesDirectory)
66
free((char *)FMPatchesDirectory);
67
deleteFMPatchesDirectory = 0;
68
FMPatchesDirectory="/etc";
72
void FMOut::openDev (int sqfd)
74
#ifdef HAVE_OSS_SUPPORT
80
printfdebug("ERROR: Could not open /dev/sequencer\n");
89
void FMOut::closeDev (void)
93
//if (seqfd>=0) close(seqfd);
97
void FMOut::initDev (void)
99
#ifdef HAVE_OSS_SUPPORT
102
uchar gm_reset[5]={0x7e, 0x7f, 0x09, 0x01, 0xf7};
103
sysex(gm_reset, sizeof(gm_reset));
104
for (chn=0;chn<16;chn++)
107
chnPatchChange(chn,0);
108
chnPressure(chn,127);
109
chnPitchBender(chn, 0x00, 0x40);
110
chnController(chn, CTL_MAIN_VOLUME,127);
111
chnController(chn, CTL_EXT_EFF_DEPTH, 0);
112
chnController(chn, CTL_CHORUS_DEPTH, 0);
113
chnController(chn, 0x4a, 127);
116
if (opl==3) ioctl(seqfd, SNDCTL_FM_4OP_ENABLE, &device);
117
SEQ_VOLUME_MODE(device,VOL_METHOD_LINEAR);
119
for (int i = 0; i < nvoices; i++)
121
SEQ_CONTROL(device, i, SEQ_VOLMODE, VOL_METHOD_LINEAR);
122
SEQ_STOP_NOTE(device, i, vm->note(i), 64);
127
void FMOut::loadFMPatches(void)
129
#ifdef HAVE_OSS_SUPPORT
130
char patchesfile[PATH_MAX];
131
char drumsfile[PATH_MAX];
133
struct sbi_instrument instr;
136
for ( i=0; i<256; i++ )
138
int stereoeffect=rand()%3;
144
snprintf(patchesfile, PATH_MAX, "%s/std.o3",FMPatchesDirectory);
149
snprintf(patchesfile, PATH_MAX, "%s/std.sb",FMPatchesDirectory);
152
fh=fopen(patchesfile,"rb");
153
if (fh==NULL) return;
157
fread(tmp,size,1,fh);
159
instr.key = ((strncmp(tmp, "4OP", 3) == 0))? OPL3_PATCH : FM_PATCH;
160
datasize = (strncmp(tmp, "4OP", 3) == 0)? 22 : 11;
163
// Let's get some stereo effect ...
164
tmp[46] = (tmp[46] & 0xcf) | ((++stereoeffect)<<4);
165
stereoeffect=stereoeffect%3;
167
instr.operators[j] = tmp[j+36];
168
SEQ_WRPATCH(&instr,sizeof(instr));
174
snprintf(drumsfile, PATH_MAX, "%s/drums.o3",FMPatchesDirectory);
178
snprintf(drumsfile, PATH_MAX, "%s/drums.sb",FMPatchesDirectory);
181
fh=fopen(drumsfile,"rb");
182
if (fh==NULL) return;
184
for (i=128;i<175;i++)
186
fread(tmp,size,1,fh);
188
instr.key = (strncmp(tmp, "4OP", 3) == 0)? OPL3_PATCH : FM_PATCH;
189
datasize = (strncmp(tmp, "4OP", 3) == 0)? 22 : 11;
192
// Let's get some stereo effect ...
193
tmp[46] = (tmp[46] & 0xcf) | ((++stereoeffect)<<4);
194
stereoeffect=stereoeffect%3;
196
instr.operators[j] = tmp[j+36];
197
SEQ_WRPATCH(&instr,sizeof(instr));
202
printfdebug("Patches loaded\n");
207
int FMOut::patch(int p)
209
if (patchloaded[p]==1) return p;
211
printfdebug("Not loaded %d!\n",p);
214
while ((p<256)&&(patchloaded[p]==0)) p++;
218
void FMOut::noteOn (uchar chn, uchar note, uchar vel)
222
noteOff(chn,note,vel);
226
if (chn==PERCUSSION_CHANNEL)
228
if (patchloaded[note+128]==0) return;
230
if (patchloaded[chnpatch[chn]]==0) return;
232
int v=vm->allocateVoice(chn,note);
234
if (chn==PERCUSSION_CHANNEL)
235
SEQ_SET_PATCH(device,v ,p=patch(note+128))
237
SEQ_SET_PATCH(device,v ,p=map->patch(chn,chnpatch[chn]));
238
SEQ_BENDER(device, v, chnbender[chn]);
240
SEQ_START_NOTE(device, v, note, vel);
241
// SEQ_CONTROL(device, v, CTL_MAIN_VOLUME, chncontroller[chn][CTL_MAIN_VOLUME]);
243
SEQ_CHN_PRESSURE(device, v , chnpressure[chn]);
247
printfdebug("Note ON >\t chn : %d\tnote : %d\tvel: %d\n",chn,note,vel);
251
void FMOut::noteOff (uchar chn, uchar note, uchar vel)
255
while ((i=vm->search(chn,note))!=-1)
257
SEQ_STOP_NOTE(device, i, note, vel);
258
vm->deallocateVoice(i);
262
printfdebug("Note OFF >\t chn : %d\tnote : %d\tvel: %d\n",chn,note,vel);
266
void FMOut::keyPressure (uchar chn, uchar note, uchar vel)
270
while ((i=vm->search(chn,note))!=-1)
271
SEQ_KEY_PRESSURE(device, i, note,vel);
274
void FMOut::chnPatchChange (uchar chn, uchar patch)
276
if (chn==PERCUSSION_CHANNEL) return;
279
while ((i=vm->search(chn))!=-1)
280
SEQ_SET_PATCH(device,i,map->patch(chn,patch));
285
void FMOut::chnPressure (uchar chn, uchar vel)
289
while ((i=vm->search(chn))!=-1)
290
SEQ_CHN_PRESSURE(device, i , vel);
292
chnpressure[chn]=vel;
295
void FMOut::chnPitchBender(uchar chn,uchar lsb, uchar msb)
297
chnbender[chn]=((int)msb<<7) | (lsb & 0x7F);
301
while ((i=vm->search(chn))!=-1)
302
SEQ_BENDER(device, i, chnbender[chn]);
306
void FMOut::chnController (uchar chn, uchar ctl, uchar v)
308
if ((ctl==11)||(ctl==7))
310
v=(v*volumepercentage)/100;
315
while ((i=vm->search(chn))!=-1)
316
SEQ_CONTROL(device, i, ctl, v);
318
chncontroller[chn][ctl]=v;
321
void FMOut::sysex(uchar *, ulong )
326
void FMOut::setFMPatchesDirectory(const char *dir)
328
if ((dir==NULL)||(dir[0]==0)) return;
329
if (deleteFMPatchesDirectory)
330
free((char *)FMPatchesDirectory);
332
FMPatchesDirectory = strdup(dir);
334
deleteFMPatchesDirectory=1;
337
void FMOut::setVolumePercentage ( int i )
339
#ifdef HAVE_OSS_SUPPORT
340
int fd=open("/dev/mixer0",O_RDWR,0);
345
if (ioctl(fd,MIXER_WRITE(SOUND_MIXER_SYNTH),&a) == -1)
346
printfdebug("ERROR writing to mixer\n");
353
const char *FMOut::FMPatchesDirectory = "/etc";
354
int FMOut::deleteFMPatchesDirectory = 0;