summaryrefslogtreecommitdiff
path: root/libs/backends/portaudio/winmmemidi_io.h
blob: 4d146981fb19d90a7da686cfa76a2ffe0bf51346 (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
134
135
136
137
138
139
140
141
/*
 * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
 *
 * 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.
 */

#ifndef WINMME_MIDI_IO_H
#define WINMME_MIDI_IO_H

#include <map>
#include <vector>
#include <string>
#include <stdint.h>
#include <pthread.h>

#include <boost/shared_ptr.hpp>
#include "pbd/ringbuffer.h"

#include "winmmemidi_input_device.h"
#include "winmmemidi_output_device.h"

#include "midi_device_info.h"

namespace ARDOUR {

struct WinMMEMIDIPacket {

#if 0
	WinMMEMIDIPacket (const WinMMEMIDIPacket& other)
	    : timeStamp (other.timeStamp)
	    , length (other.length)
	{
		if (length > 0) {
			memcpy (data, other.data, length);
		}
	}
#endif

	// MIDITimeStamp timeStamp;
	uint16_t length;
	uint8_t data[MaxWinMidiEventSize];
};

typedef std::vector<boost::shared_ptr<WinMMEMIDIPacket> > WinMMEMIDIQueue;

class WinMMEMidiIO {
public:
	WinMMEMidiIO ();
	~WinMMEMidiIO ();

	void start ();
	void stop ();

	bool dequeue_input_event (uint32_t port,
	                          uint64_t timestamp_start,
	                          uint64_t timestamp_end,
	                          uint64_t& timestamp,
	                          uint8_t* data,
	                          size_t& size);

	bool enqueue_output_event (uint32_t port,
	                           uint64_t timestamp,
	                           const uint8_t* data,
	                           const size_t size);

	uint32_t n_midi_inputs (void) const { return m_inputs.size(); }
	uint32_t n_midi_outputs (void) const { return m_outputs.size(); }

	std::vector<WinMMEMidiInputDevice*> get_inputs () { return m_inputs; }
	std::vector<WinMMEMidiOutputDevice*> get_outputs () { return m_outputs; }

	void update_device_info ();

	std::vector<MidiDeviceInfo*> get_device_info () { return m_device_info; }

	MidiDeviceInfo* get_device_info (const std::string& name);

	std::string port_id (uint32_t, bool input);
	std::string port_name (uint32_t, bool input);

	void set_enabled (bool yn = true) { m_enabled = yn; }
	bool enabled (void) const { return m_active && m_enabled; }

	void set_port_changed_callback (void (changed_callback (void*)), void *arg) {
		m_changed_callback = changed_callback;
		m_changed_arg = arg;
	}

private: // Methods

	void clear_device_info ();

	static bool get_input_name_from_index (int index, std::string& name);
	static bool get_output_name_from_index (int index, std::string& name);

	void discover ();
	void cleanup ();

	void create_input_devices ();
	void create_output_devices ();

	void destroy_input_devices ();
	void destroy_output_devices ();

	void start_devices ();
	void stop_devices ();

private: // Data

	std::vector<MidiDeviceInfo*> m_device_info;

	std::vector<WinMMEMidiInputDevice*> m_inputs;
	std::vector<WinMMEMidiOutputDevice*> m_outputs;

	bool              m_active;
	bool              m_enabled;
	bool              m_run;

	void (* m_changed_callback) (void*);
	void  * m_changed_arg;

	// protects access to m_inputs and m_outputs
	pthread_mutex_t m_device_lock;
};

} // namespace

#endif // WINMME_MIDI_IO_H