summaryrefslogtreecommitdiff
path: root/libs/midi++2/jack_midiport.cc
blob: 11cd70a0513a78db25e8c7c1d5c1116ad42b9e82 (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
129
130
131
132
133
/*
  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.
*/

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

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

using namespace std;
using namespace MIDI;

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

	if (!err) {
		_ok = true;
	} 
}

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));
}

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);
}

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++);
	
	if (!err) {
		memcpy(buf, ev.buffer, ev.size);
		return ev.size;
	} else {
		return 0;
	}
}

int
JACK_MidiPort::create_ports(const XMLNode& node)
{
	Descriptor desc (node);

	assert(!_jack_input_port);
	assert(!_jack_output_port);
	
	jack_nframes_t nframes = jack_get_buffer_size(_jack_client);

	bool ret = true;

	if (desc.mode == O_RDWR || desc.mode == O_WRONLY) {
		_jack_output_port = jack_port_register(_jack_client,
						       string(desc.tag).append("_out").c_str(),
						       JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
  		jack_midi_clear_buffer(jack_port_get_buffer(_jack_output_port, nframes));
		ret = ret && (_jack_output_port != NULL);
	}
	
	if (desc.mode == O_RDWR || desc.mode == O_RDONLY) {
		_jack_input_port = jack_port_register(_jack_client,
						      string(desc.tag).append("_in").c_str(),
						      JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
  		jack_midi_clear_buffer(jack_port_get_buffer(_jack_input_port, nframes));
		ret = ret && (_jack_input_port != NULL);
	}

	return ret ? 0 : -1;
}

XMLNode& 
JACK_MidiPort::get_state () const
{
	XMLNode& root (Port::get_state ());
	return root;
}

void
JACK_MidiPort::set_state (const XMLNode& node)
{
}