summaryrefslogtreecommitdiff
path: root/libs/midi++2/jack_midiport.cc
blob: 4fc24e87119e6ab0d3fd504f61419969516e3537 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
  Copyright (C) 2006 Paul Davis 
  Written by Dave Robillard
 
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
 
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
 
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
  $Id: alsa_sequencer_midiport.cc 244 2006-01-06 04:59:17Z essej $
*/

#include <fcntl.h>
#include <cerrno>
#include <cassert>

#include <midi++/types.h>
#include <midi++/jack.h>
#include <midi++/port_request.h>

using namespace std;
using namespace MIDI;

JACK_MidiPort::JACK_MidiPort(PortRequest & req, jack_client_t* jack_client)
	: Port(req)
	, _jack_client(jack_client)
	, _jack_input_port(NULL)
	, _jack_output_port(NULL)
	, _last_read_index(0)
{
	int err = create_ports(req);

	if (!err) {
		req.status = PortRequest::OK;
		_ok = true;
	} else {
		req.status = PortRequest::Unknown;
	}
}

JACK_MidiPort::~JACK_MidiPort()
{
	// FIXME: remove port
}

void
JACK_MidiPort::cycle_start (nframes_t nframes)
{
	Port::cycle_start(nframes);
	assert(_nframes_this_cycle == nframes);
	_last_read_index = 0;
	jack_midi_clear_buffer(jack_port_get_buffer(_jack_output_port, nframes), nframes);
}

int
JACK_MidiPort::write(byte * msg, size_t msglen, timestamp_t timestamp)
{
	assert(_currently_in_cycle);
	assert(timestamp < _nframes_this_cycle);
	assert(_jack_output_port);

	// FIXME: return value correct?
	return jack_midi_event_write (
		jack_port_get_buffer(_jack_output_port, _nframes_this_cycle),
		timestamp, msg, msglen, _nframes_this_cycle);
}

int
JACK_MidiPort::read(byte * buf, size_t max, timestamp_t timestamp)
{
	assert(_currently_in_cycle);
	assert(timestamp < _nframes_this_cycle);
	assert(_jack_input_port);

	jack_midi_event_t ev;

	int err = jack_midi_event_get (&ev,
		jack_port_get_buffer(_jack_input_port, _nframes_this_cycle),
		_last_read_index++, _nframes_this_cycle);
	
	if (!err) {
		memcpy(buf, ev.buffer, ev.size);
		return ev.size;
	} else {
		return 0;
	}
}

int
JACK_MidiPort::create_ports(PortRequest & req)
{
	assert(!_jack_input_port);
	assert(!_jack_output_port);
	
	jack_nframes_t nframes = jack_get_buffer_size(_jack_client);

	bool ret = true;

	if (req.mode == O_RDWR || req.mode == O_WRONLY) {
		_jack_output_port = jack_port_register(_jack_client,
			string(req.tagname).append("_out").c_str(),
			JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
  		jack_midi_reset_new_port(
			jack_port_get_buffer(_jack_output_port, nframes), nframes);
		ret = ret && (_jack_output_port != NULL);
	}

	if (req.mode == O_RDWR || req.mode == O_RDONLY) {
		_jack_input_port = jack_port_register(_jack_client,
			string(req.tagname).append("_in").c_str(),
			JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
  		jack_midi_reset_new_port(
			jack_port_get_buffer(_jack_input_port, nframes), nframes);
		ret = ret && (_jack_input_port != NULL);
	}

	return ret ? 0 : -1;
}