1
// ------------------------------------------------------------------------
2
// midiio-aseq.cpp: Input and output of MIDI streams using
4
// Copyright (C) 2005 Pedro Lopez-Cabanillas
5
// Copyright (C) 2005 Kai Vehmanen
8
// eca-style-version: 3
10
// This program is free software; you can redistribute it and/or modify
11
// it under the terms of the GNU General Public License as published by
12
// the Free Software Foundation; either version 2 of the License, or
13
// (at your option) any later version.
15
// This program is distributed in the hope that it will be useful,
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
// GNU General Public License for more details.
20
// You should have received a copy of the GNU General Public License
21
// along with this program; if not, write to the Free Software
22
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
// ------------------------------------------------------------------------
28
#ifdef ECA_COMPILE_ALSA
34
#include "midiio-aseq.h"
35
#include "eca-logger.h"
37
MIDI_IO_ASEQ::MIDI_IO_ASEQ(const std::string& name) { label("alsaseq"); device_name_rep = name; }
39
MIDI_IO_ASEQ::~MIDI_IO_ASEQ(void) { if (is_open()) close(); }
41
void MIDI_IO_ASEQ::open(void)
43
int open_flags = 0, port_flags = 0;
48
open_flags = SND_SEQ_OPEN_INPUT;
49
port_flags = SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE;
54
open_flags = SND_SEQ_OPEN_OUTPUT;
55
port_flags = SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ;
60
open_flags = SND_SEQ_OPEN_DUPLEX;
61
port_flags = SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE |
62
SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ;
67
ECA_LOG_MSG(ECA_LOGGER::system_objects, "Opening ALSA sequencer");
68
int err = snd_seq_open(&seq_handle_repp, "default", open_flags, SND_SEQ_NONBLOCK);
70
toggle_open_state(false);
73
toggle_open_state(true);
77
snd_seq_set_client_name(seq_handle_repp, "ecasound");
78
// Create a simple port
79
port_rep = snd_seq_create_simple_port( seq_handle_repp, "ecasound",
81
SND_SEQ_PORT_TYPE_MIDI_GENERIC);
82
// Parse the device name, and connect it to the port when successful
84
err = snd_seq_parse_address(seq_handle_repp, &subs, device_name_rep.c_str());
88
snd_seq_connect_to(seq_handle_repp, port_rep, subs.client, subs.port);
91
snd_seq_connect_from(seq_handle_repp, port_rep, subs.client, subs.port);
94
snd_seq_connect_to(seq_handle_repp, port_rep, subs.client, subs.port);
95
snd_seq_connect_from(seq_handle_repp, port_rep, subs.client, subs.port);
99
// Create the encoder/decoder instance
100
err = snd_midi_event_new( buffer_size_rep = 16, &coder_repp );
102
finished_rep = false;
105
void MIDI_IO_ASEQ::close(void)
107
// Release the xxcoder instance
108
snd_midi_event_free( coder_repp );
110
snd_seq_delete_port( seq_handle_repp, port_rep );
111
// Close the sequencer client
112
snd_seq_close( seq_handle_repp );
113
toggle_open_state(false);
116
int MIDI_IO_ASEQ::poll_descriptor(void) const
120
npfds = snd_seq_poll_descriptors_count(seq_handle_repp, POLLIN|POLLOUT);
121
pfds = reinterpret_cast<struct pollfd*>(alloca(sizeof(*pfds) * npfds));
122
snd_seq_poll_descriptors(seq_handle_repp, pfds, npfds, POLLIN|POLLOUT);
127
bool MIDI_IO_ASEQ::finished(void) const { return finished_rep; }
129
long int MIDI_IO_ASEQ::read_bytes(void* target_buffer, long int bytes)
131
snd_seq_event_t *event;
132
int err = 0, position = 0;
133
if ( bytes > buffer_size_rep ) {
134
snd_midi_event_resize_buffer ( coder_repp, bytes );
135
buffer_size_rep = bytes;
138
if (snd_seq_event_input_pending(seq_handle_repp, 1) == 0) {
141
err = snd_seq_event_input(seq_handle_repp, &event);
145
if ( event->type == SND_SEQ_EVENT_CONTROLLER ||
146
event->type == SND_SEQ_EVENT_CONTROL14 ||
147
event->type == SND_SEQ_EVENT_NONREGPARAM ||
148
event->type == SND_SEQ_EVENT_REGPARAM ||
149
event->type == SND_SEQ_EVENT_SYSEX ) {
150
err = snd_midi_event_decode( coder_repp,
151
((unsigned char *)target_buffer) + position,
158
if ( position >= bytes) return position;
162
ECA_LOG_MSG(ECA_LOGGER::system_objects,
163
std::string("error while reading from ALSA sequencer: ") + snd_strerror(err));
167
long int MIDI_IO_ASEQ::write_bytes(void* target_buffer, long int bytes) {
170
if ( bytes > buffer_size_rep ) {
171
snd_midi_event_resize_buffer ( coder_repp, bytes );
172
buffer_size_rep = bytes;
174
snd_seq_ev_clear(&ev);
175
snd_seq_ev_set_source(&ev, port_rep);
176
snd_seq_ev_set_subs(&ev);
177
snd_seq_ev_set_direct(&ev);
178
err = snd_midi_event_encode( coder_repp,
179
(unsigned char *)target_buffer,
182
snd_seq_event_output(seq_handle_repp, &ev);
183
snd_seq_drain_output(seq_handle_repp);
190
void MIDI_IO_ASEQ::set_parameter(int param,
199
device_name_rep = value;
204
std::string MIDI_IO_ASEQ::get_parameter(int param) const
211
return device_name_rep;
217
* FIXME: This is an alternative to using the poll_descriptor()
220
bool MIDI_IO_ASEQ::pending_messages(unsigned long timeout) const
224
int npfds = snd_seq_poll_descriptors_count(seq_handle_repp, POLLIN);
225
pfds = (struct pollfd *)alloca(sizeof(*pfds) * npfds);
226
snd_seq_poll_descriptors(seq_handle_repp, pfds, npfds, POLLIN);
227
result = poll(pfds, npfds, timeout);
231
#endif /* COMPILE_ALSA */