CLAM-Development  1.4.0
ALSAMIDIDevice.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #include "MIDIIn.hxx"
23 #include <alsa/asoundlib.h>
24 #include "MIDIDeviceList.hxx"
25 #include "MIDIDevice.hxx"
26 #include <sstream>
27 
28 namespace CLAM {
29 
30  class ALSAMIDIDevice: public MIDIDevice
31  {
32  private:
33  std::string mDevice;
34  snd_rawmidi_t *mHandleIn;
35  snd_rawmidi_t *mHandleOut;
36  snd_rawmidi_status_t *mStatusIn;
37  public:
38  ALSAMIDIDevice(const std::string& name,const std::string& device);
39  ~ALSAMIDIDevice();
40 
41  void ConcreteStart(void) throw(Err);
42  void ConcreteStop(void) throw(Err);
43 
44  void Read(void) throw(Err);
45  void Write(unsigned char* msg,int size) throw(Err);
46  };
47 
48  ALSAMIDIDevice::ALSAMIDIDevice(const std::string& name,const std::string& device):
49  MIDIDevice(name)
50  {
51  mHandleIn = NULL;
52  mHandleOut = NULL;
53  mStatusIn = NULL;
54  mDevice = device;
55  }
56 
57  void ALSAMIDIDevice::ConcreteStart(void) throw(Err)
58  {
59  snd_rawmidi_t** handleInRef = NULL;
60  snd_rawmidi_t** handleOutRef = NULL;
61 
62  if (mInputs.size()) handleInRef = &mHandleIn;
63  if (mOutputs.size()) handleOutRef = &mHandleOut;
64 
65  int err = snd_rawmidi_open(handleInRef,handleOutRef,mDevice.c_str(),
66  SND_RAWMIDI_NONBLOCK);
67  if (err)
68  {
69  std::string str("Could not open ALSAMIDIDevice ");
70  str += mDevice;
71  throw Err(str.c_str());
72  }
73  snd_rawmidi_status_malloc(&mStatusIn);
74 
75  }
76 
77  void ALSAMIDIDevice::ConcreteStop(void) throw(Err)
78  {
79  if (mHandleIn) {
80  snd_rawmidi_drain(mHandleIn);
81  snd_rawmidi_close(mHandleIn);
82  snd_rawmidi_status_free(mStatusIn);
83  }
84 
85  if(mHandleOut){
86  snd_rawmidi_drain(mHandleOut);
87  snd_rawmidi_close(mHandleOut);
88  }
89  }
90 
91  void ALSAMIDIDevice::Write(unsigned char* msg,int size) throw(Err)
92  {
93  snd_rawmidi_write(mHandleOut,msg,size);
94  snd_rawmidi_drain(mHandleOut);
95  }
96 
97  void ALSAMIDIDevice::Read(void) throw(Err)
98  {
99  unsigned char ch;
100  size_t n;
101  do
102  {
103  int err = snd_rawmidi_read(mHandleIn,&ch,1);
104  if (err!=-EAGAIN)
105  {
106  HandleRawByte(ch);
107  }
108  snd_rawmidi_status(mHandleIn,mStatusIn);
109  n = snd_rawmidi_status_get_avail(mStatusIn);
110  } while (n);
111  }
112 
113  ALSAMIDIDevice::~ALSAMIDIDevice()
114  {
115  Stop();
116  }
117 
118  class ALSAMIDIDeviceList: public MIDIDeviceList
119  {
120  static ALSAMIDIDeviceList sDevices;
121 
122  ALSAMIDIDeviceList()
123  :MIDIDeviceList(std::string("alsa"))
124  {
125  int card, dev;
126  snd_ctl_t *handle;
127  snd_ctl_card_info_t *info;
128 
129  snd_ctl_card_info_alloca(&info);
130  card = -1;
131  if (snd_card_next(&card) < 0 || card < 0)
132  return; // No cards found
133  while (card >= 0) {
134  std::stringstream namestr;
135  namestr << "hw:" << card;
136  std::string name(namestr.str());
137  if (snd_ctl_open(&handle, name.c_str(), 0) < 0)
138  continue; // Card control open error!
139 
140  if (snd_ctl_card_info(handle, info) < 0) {
141  snd_ctl_close(handle); // Card control read error!
142  continue;
143  }
144  dev = -1;
145  while (1) {
146  snd_ctl_rawmidi_next_device(handle, &dev);
147  if (dev < 0)
148  break;
149  std::stringstream dname;
150  dname << name << "," << dev;
151  mAvailableDevices.push_back(dname.str());
152  }
153  snd_ctl_close(handle);
154  if (snd_card_next(&card) < 0)
155  break;
156  }
157 
158  AddMe();
159  }
160  public:
161 
162  std::string DefaultDevice(void)
163  {
164  return "hw:0,0";
165  }
166 
167  MIDIDevice* Create(
168  const std::string& name,const std::string& device)
169  {
170  return new ALSAMIDIDevice(name,device);
171  }
172  };
173 
174  ALSAMIDIDeviceList ALSAMIDIDeviceList::sDevices;
175 }
176