aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/sfx/device/alsa-midi.cpp
blob: 22f9fbc8d2462ab7d162b8930a3e88bea2c2ca0e (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 *
 */

#include <sfx_engine.h>
#include "../device.h"
#ifdef HAVE_ALSA

#include <alsa/asoundlib.h>

#define SCI_ALSA_MIDI_VERSION "0.1"

static snd_midi_event_t *parser = NULL;
static snd_seq_t *seq = NULL;
static int queue = -1;
static int delta = 0;
static int port_out = -1;
static int port_nr = 128;
static int subport_nr = 0;

static const char *seq_name = "default";

static void
_set_tempo(void) {
	int resolution = 60;
	int tempo = 1;
	snd_seq_queue_tempo_t *queue_tempo;

	snd_seq_queue_tempo_malloc(&queue_tempo);

	memset(queue_tempo, 0, snd_seq_queue_tempo_sizeof());
	snd_seq_queue_tempo_set_ppq(queue_tempo, resolution);
	snd_seq_queue_tempo_set_tempo(queue_tempo, 1000000 / tempo);

	snd_seq_set_queue_tempo(seq, queue, queue_tempo);

	snd_seq_queue_tempo_free(queue_tempo);

#if 0
	int tempo = 1000000 / 60;
	snd_seq_queue_tempo_t *queue_tempo;

	snd_seq_queue_tempo_malloc(&queue_tempo);
	snd_seq_queue_tempo_set_tempo(queue_tempo, tempo);
	snd_seq_queue_tempo_set_ppq(queue_tempo, 1);
	snd_seq_set_queue_tempo(seq, queue, queue_tempo);
	snd_seq_queue_tempo_free(queue_tempo);
#endif
}


static int
am_subscribe_to_ports(void) {
	if ((port_out = snd_seq_connect_to(seq, port_out, port_nr, subport_nr)) < 0) {
		fprintf(stderr, "[SFX] Could not connect to ALSA sequencer port: %s\n", snd_strerror(port_out));
		return SFX_ERROR;
	}
	return SFX_OK;
}


static int
aminit(midi_writer_t *self) {
	int err;

	snd_midi_event_new(4096, &parser);
	snd_midi_event_init(parser);

	sciprintf("[SFX] Initialising ALSA MIDI backend, v%s\n", SCI_ALSA_MIDI_VERSION);

	if (snd_seq_open(&seq, seq_name, SND_SEQ_OPEN_OUTPUT, SND_SEQ_NONBLOCK)) {
		fprintf(stderr, "[SFX] Failed to open ALSA MIDI sequencer '%s' for output\n",
		        seq_name);
		return SFX_ERROR;
	}

	if ((port_out = snd_seq_create_simple_port(seq, "FreeSCI",
	                SND_SEQ_PORT_CAP_WRITE |
	                SND_SEQ_PORT_CAP_SUBS_WRITE |
	                SND_SEQ_PORT_CAP_READ,
	                SND_SEQ_PORT_TYPE_MIDI_GENERIC)) < 0) {
		fprintf(stderr, "[SFX] Could not create ALSA sequencer port\n");
		return SFX_ERROR;
	}

	if (am_subscribe_to_ports())
		return SFX_ERROR;

	queue = snd_seq_alloc_queue(seq);
	_set_tempo();

	snd_seq_start_queue(seq, queue, NULL);

	if ((err = snd_seq_drain_output(seq))) {
		fflush(NULL);
		fprintf(stderr, "[SFX] Error while draining: %s\n",
		        snd_strerror(err));
		return SFX_ERROR;
	}

	return SFX_OK;
}

static int
amsetopt(midi_writer_t *self, char *name, char *value) {
	return SFX_ERROR;
}


static int
amwrite(midi_writer_t *self, unsigned char *buf, int len) {
	snd_seq_event_t evt;

#if 0
	{
		int i;
		fprintf(stderr, "[MID] ");
		for (i = 0; i < len; i++)
			fprintf(stderr, " %02x", buf[i]);
		fprintf(stderr, "\n");
	}
#endif

	snd_seq_ev_clear(&evt);
	snd_seq_ev_set_source(&evt, port_out);
	snd_seq_ev_set_subs(&evt); /* Broadcast to all subscribers */

	snd_midi_event_encode(parser, buf, len, &evt);
	snd_seq_ev_schedule_tick(&evt, queue, 0, delta);

	snd_seq_event_output_direct(seq, &evt);

#if 0
	{
		snd_seq_queue_status_t *status;
		snd_seq_queue_status_malloc(&status);

		snd_seq_get_queue_status(seq, queue, status);
		//snd_seq_tick_time_t snd_seq_queue_status_get_tick_time(const snd_seq_queue_status_t *info);
		fprintf(stderr, "Queue at %d/%d\n", delta, snd_seq_queue_status_get_tick_time(status));

		snd_seq_queue_status_free(status);
	}
#endif


	return SFX_OK;
}

static void
amdelay(midi_writer_t *self, int ticks) {
	delta += ticks;
}

static void
amreset_timer(midi_writer_t *self) {
	snd_seq_drain_output(seq);
	snd_seq_stop_queue(seq, queue, NULL);


	{
		snd_seq_event_t evt;
		snd_seq_ev_clear(&evt);
		snd_seq_ev_set_source(&evt, port_out);
		snd_seq_ev_set_subs(&evt); /* Broadcast to all subscribers */

		snd_seq_ev_set_queue_pos_tick(&evt, queue, 0);

		snd_seq_event_output_direct(seq, &evt);
	}
	delta = 0;



	snd_seq_start_queue(seq, queue, NULL);
}

static void
amclose(midi_writer_t *self) {
	snd_midi_event_free(parser);
	parser = NULL;
}


midi_writer_t sfx_device_midi_alsa = {
	"alsa",
	aminit,
	amsetopt,
	amwrite,
	amdelay,
	NULL,
	amreset_timer,
	amclose,
};

#endif