• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • libtdemid
 

libtdemid

  • libtdemid
midiout.cpp
1 /**************************************************************************
2 
3  midiout.cpp - class midiOut which handles external midi devices
4  This file is part of LibKMid 0.9.5
5  Copyright (C) 1997,98,99,2000 Antonio Larrosa Jimenez
6  LibKMid's homepage : http://www.arrakis.es/~rlarrosa/libtdemid.html
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Library General Public
10  License as published by the Free Software Foundation; either
11  version 2 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Library General Public License for more details.
17 
18  You should have received a copy of the GNU Library General Public License
19  along with this library; see the file COPYING.LIB. If not, write to
20  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  Boston, MA 02110-1301, USA.
22 
23  Send comments and bug fixes to Antonio Larrosa <larrosa@kde.org>
24 
25 ***************************************************************************/
26 #include "midiout.h"
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include "sndcard.h"
31 #include <errno.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <sys/param.h>
35 #include "midispec.h"
36 #include "alsaout.h"
37 
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41 
42 #include <sys/ioctl.h>
43 
44 SEQ_USE_EXTBUF();
45 
46 MidiOut::MidiOut(int d)
47 {
48  seqfd = -1;
49  devicetype=KMID_EXTERNAL_MIDI;
50  device= d;
51  volumepercentage=100;
52  map=new MidiMapper(NULL);
53  if (map==NULL) { printfdebug("ERROR : midiOut : Map is NULL\n"); return; };
54  _ok=1;
55 }
56 
57 MidiOut::~MidiOut()
58 {
59  delete map;
60  closeDev();
61 }
62 
63 void MidiOut::openDev (int sqfd)
64 {
65 #ifdef HAVE_OSS_SUPPORT
66  _ok=1;
67  seqfd=sqfd;
68  if (seqfd==-1)
69  {
70  printfdebug("ERROR: Could not open /dev/sequencer\n");
71  _ok=0;
72  return;
73  }
74 #endif
75 }
76 
77 void MidiOut::closeDev (void)
78 {
79  if (!ok()) return;
80 // if (deviceType()!=KMID_ALSA) allNotesOff();
81  SEQ_STOP_TIMER();
82  SEQ_DUMPBUF();
83  seqfd=-1;
84 }
85 
86 void MidiOut::initDev (void)
87 {
88 #ifdef HAVE_OSS_SUPPORT
89  int chn;
90  if (!ok()) return;
91  uchar gm_reset[5]={0x7e, 0x7f, 0x09, 0x01, 0xf7};
92  sysex(gm_reset, sizeof(gm_reset));
93  for (chn=0;chn<16;chn++)
94  {
95  chnmute[chn]=0;
96  chnPatchChange(chn,0);
97  chnPressure(chn,127);
98  chnPitchBender(chn, 0x00, 0x40);
99  chnController(chn, CTL_MAIN_VOLUME,110*volumepercentage);
100  chnController(chn, CTL_EXT_EFF_DEPTH, 0);
101  chnController(chn, CTL_CHORUS_DEPTH, 0);
102  chnController(chn, 0x4a, 127);
103  }
104 #endif
105 }
106 
107 void MidiOut::setMidiMapper(MidiMapper *_map)
108 {
109  delete map;
110  map=_map;
111 }
112 
113 void MidiOut::noteOn (uchar chn, uchar note, uchar vel)
114 {
115  if (vel==0)
116  {
117  noteOff(chn,note,vel);
118  }
119  else
120  {
121  SEQ_MIDIOUT(device, MIDI_NOTEON + map->channel(chn));
122  SEQ_MIDIOUT(device, map->key(chn,chnpatch[chn],note));
123  SEQ_MIDIOUT(device, vel);
124  }
125 #ifdef MIDIOUTDEBUG
126  printfdebug("Note ON >\t chn : %d\tnote : %d\tvel: %d\n",chn,note,vel);
127 #endif
128 }
129 
130 void MidiOut::noteOff (uchar chn, uchar note, uchar vel)
131 {
132  SEQ_MIDIOUT(device, MIDI_NOTEOFF + map->channel(chn));
133  SEQ_MIDIOUT(device, map->key(chn,chnpatch[chn],note));
134  SEQ_MIDIOUT(device, vel);
135 #ifdef MIDIOUTDEBUG
136  printfdebug("Note OFF >\t chn : %d\tnote : %d\tvel: %d\n",chn,note,vel);
137 #endif
138 }
139 
140 void MidiOut::keyPressure (uchar chn, uchar note, uchar vel)
141 {
142  SEQ_MIDIOUT(device, MIDI_KEY_PRESSURE + map->channel(chn));
143  SEQ_MIDIOUT(device, map->key(chn,chnpatch[chn],note));
144  SEQ_MIDIOUT(device, vel);
145 }
146 
147 void MidiOut::chnPatchChange (uchar chn, uchar patch)
148 {
149 #ifdef MIDIOUTDEBUG
150  printfdebug("PATCHCHANGE [%d->%d] %d -> %d\n",
151  chn,map->channel(chn),patch,map->patch(chn,patch));
152 #endif
153  SEQ_MIDIOUT(device, MIDI_PGM_CHANGE + map->channel(chn));
154  SEQ_MIDIOUT(device, map->patch(chn,patch));
155  chnpatch[chn]=patch;
156 }
157 
158 void MidiOut::chnPressure (uchar chn, uchar vel)
159 {
160  SEQ_MIDIOUT(device, MIDI_CHN_PRESSURE + map->channel(chn));
161  SEQ_MIDIOUT(device, vel);
162 
163  chnpressure[chn]=vel;
164 }
165 
166 void MidiOut::chnPitchBender(uchar chn,uchar lsb, uchar msb)
167 {
168  SEQ_MIDIOUT(device, MIDI_PITCH_BEND + map->channel(chn));
169  /*
170 #ifdef AT_HOME
171  short pbs=((short)msb<<7) | (lsb & 0x7F);
172  pbs=pbs-0x2000;
173  short pbs2=(((long)pbs*672)/4096);
174  printfdebug("Pitch Bender (%d): %d -> %d \n",chn,pbs,pbs2);
175  pbs2=pbs2+0x2000;
176  lsb=pbs2 & 0x7F;
177  msb=(pbs2 >> 7)&0x7F;
178 #endif
179  */
180  map->pitchBender(chn,lsb,msb);
181  SEQ_MIDIOUT(device, lsb);
182  SEQ_MIDIOUT(device, msb);
183  chnbender[chn]=(msb << 8) | (lsb & 0xFF);
184 }
185 
186 void MidiOut::chnController (uchar chn, uchar ctl, uchar v)
187 {
188  SEQ_MIDIOUT(device, MIDI_CTL_CHANGE + map->channel(chn));
189 #ifdef AT_HOME
190  if (ctl==11) ctl=7;
191 #endif
192  map->controller(chn,ctl,v);
193  if ((ctl==11)||(ctl==7))
194  {
195  v=(v*volumepercentage)/100;
196  if (v>127) v=127;
197  }
198 
199  SEQ_MIDIOUT(device, ctl);
200  SEQ_MIDIOUT(device, v);
201 
202  chncontroller[chn][ctl]=v;
203 }
204 
205 void MidiOut::sysex(uchar *data, ulong size)
206 {
207  ulong i=0;
208  SEQ_MIDIOUT(device, MIDI_SYSTEM_PREFIX);
209  while (i<size)
210  {
211  SEQ_MIDIOUT(device, *data);
212  data++;
213  i++;
214  }
215 #ifdef MIDIOUTDEBUG
216  printfdebug("sysex\n");
217 #endif
218 }
219 
220 void MidiOut::allNotesOff (void)
221 {
222  for (int i=0; i<16; i++)
223  {
224  chnController(i, 0x78, 0);
225  chnController(i, 0x79, 0);
226  };
227  sync(1);
228 }
229 
230 void MidiOut::channelSilence (uchar chn)
231 {
232  uchar i;
233  for ( i=0; i<127; i++)
234  {
235  noteOff(chn,i,0);
236  };
237  sync();
238 }
239 
240 void MidiOut::channelMute(uchar chn, int a)
241 {
242  if (a==1)
243  {
244  chnmute[chn]=a;
245  channelSilence(chn);
246  }
247  else if (a==0)
248  {
249  chnmute[chn]=a;
250  }
251  /* else ignore the call to this function */
252 }
253 
254 void MidiOut::seqbuf_dump (void)
255 {
256 #ifdef HAVE_OSS_SUPPORT
257  if (_seqbufptr && seqfd!=-1 && seqfd!=0)
258  if (write (seqfd, _seqbuf, _seqbufptr) == -1)
259  {
260  printfdebug("Error writing to /dev/sequencer in MidiOut::seq_buf_dump\n");
261  perror ("write /dev/sequencer in seqBufDump\n");
262  exit (-1);
263  }
264  _seqbufptr = 0;
265 #endif
266 }
267 
268 void MidiOut::seqbuf_clean(void)
269 {
270 #ifdef HAVE_OSS_SUPPORT
271  _seqbufptr=0;
272 #endif
273 }
274 
275 const char *MidiOut::midiMapFilename(void)
276 {
277  return (map!=NULL) ? map->filename() : "";
278 }
279 
280 const char * MidiOut::deviceName(void) const
281 {
282  switch (deviceType())
283  {
284  case (KMID_EXTERNAL_MIDI) : return "External Midi";
285  case (KMID_SYNTH) : return "Synth";
286  case (KMID_FM) : return "FM";
287  case (KMID_GUS) : return "GUS";
288  case (KMID_AWE) : return "AWE";
289  case (KMID_ALSA) : return reinterpret_cast<const AlsaOut *>(this)->deviceName();
290  }
291  return "Unknown";
292 }
293 
294 void MidiOut::sync(int i)
295 {
296  if (deviceType()==KMID_ALSA) { // XXX : sync should be virtual after next bic
297  reinterpret_cast<AlsaOut *>(this)->sync(i);
298  return;
299  }
300  SEQ_DUMPBUF();
301 }
AlsaOut
Sends MIDI events to a MIDI devices using ALSA.
Definition: alsaout.h:38
MidiMapper
A Midi Mapper class which defines the way MIDI events are translated (or "mapped") to different ones.
Definition: midimapper.h:60
MidiMapper::controller
void controller(uchar chn, uchar &ctl, uchar &v)
Returns the value which a given controller and its value should be mapped to when played on channel c...
Definition: midimapper.cpp:453
MidiMapper::channel
uchar channel(uchar chn)
Returns the channel which chn should be mapped to.
Definition: midimapper.h:177
MidiMapper::patch
uchar patch(uchar chn, uchar pgm)
Returns the patch which pgm used on channel chn should be mapped to.
Definition: midimapper.cpp:431
MidiMapper::key
uchar key(uchar chn, uchar pgm, uchar note)
Returns the key that key note playing a pgm patch on channel chn should be mapped to.
Definition: midimapper.cpp:423
MidiMapper::filename
const char * filename(void)
Returns the path and name of the file which the object loaded the mapper from.
Definition: midimapper.cpp:418
MidiMapper::pitchBender
void pitchBender(uchar chn, uchar &lsb, uchar &msb)
Returns the value which the pitch bender on channel chn should be mapped to.
Definition: midimapper.cpp:437
MidiOut::chnPitchBender
virtual void chnPitchBender(uchar chn, uchar lsb, uchar msb)
See DeviceManager::chnPitchBender()
Definition: midiout.cpp:166
MidiOut::deviceName
const char * deviceName(void) const
Returns the name and type of this MIDI device.
Definition: midiout.cpp:280
MidiOut::allNotesOff
void allNotesOff(void)
Send a All Notes Off event to every channel.
Definition: midiout.cpp:220
MidiOut::sync
void sync(int i=0)
Sends the buffer to the device and returns when it's played, so you can synchronize XXX: sync should ...
Definition: midiout.cpp:294
MidiOut::noteOn
virtual void noteOn(uchar chn, uchar note, uchar vel)
See DeviceManager::noteOn()
Definition: midiout.cpp:113
MidiOut::~MidiOut
virtual ~MidiOut()
Destructor.
Definition: midiout.cpp:57
MidiOut::deviceType
int deviceType() const
Definition: midiout.h:139
MidiOut::setMidiMapper
void setMidiMapper(MidiMapper *map)
Sets a MidiMapper object to be used to modify the midi events before sending them.
Definition: midiout.cpp:107
MidiOut::MidiOut
MidiOut(int d=0)
Constructor.
Definition: midiout.cpp:46
MidiOut::chnPatchChange
virtual void chnPatchChange(uchar chn, uchar patch)
See DeviceManager::chnPatchChange()
Definition: midiout.cpp:147
MidiOut::initDev
virtual void initDev()
Initializes the device sending generic standard midi events and controllers, such as changing the pat...
Definition: midiout.cpp:86
MidiOut::midiMapFilename
const char * midiMapFilename()
Returns the path to the file where the current used MidiMapper object reads the configuration from,...
Definition: midiout.cpp:275
MidiOut::openDev
virtual void openDev(int sqfd)
Opens the device.
Definition: midiout.cpp:63
MidiOut::closeDev
virtual void closeDev()
Closes the device.
Definition: midiout.cpp:77
MidiOut::chnPressure
virtual void chnPressure(uchar chn, uchar vel)
See DeviceManager::chnPressure()
Definition: midiout.cpp:158
MidiOut::channelMute
virtual void channelMute(uchar chn, int b)
Mute or "unmute" a given channel .
Definition: midiout.cpp:240
MidiOut::keyPressure
virtual void keyPressure(uchar chn, uchar note, uchar vel)
See DeviceManager::keyPressure()
Definition: midiout.cpp:140
MidiOut::noteOff
virtual void noteOff(uchar chn, uchar note, uchar vel)
See DeviceManager::noteOff()
Definition: midiout.cpp:130
MidiOut::channelSilence
virtual void channelSilence(uchar chn)
Mutes all notes being played on a given channel.
Definition: midiout.cpp:230
MidiOut::ok
int ok(void)
Returns true if everything's ok and false if there has been any problem.
Definition: midiout.h:231
MidiOut::chnController
virtual void chnController(uchar chn, uchar ctl, uchar v)
See DeviceManager::chnController()
Definition: midiout.cpp:186
MidiOut::sysex
virtual void sysex(uchar *data, ulong size)
See DeviceManager::sysex()
Definition: midiout.cpp:205

libtdemid

Skip menu "libtdemid"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

libtdemid

Skip menu "libtdemid"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for libtdemid by doxygen 1.9.1
This website is maintained by Timothy Pearson.