[go: up one dir, main page]

File: vtx.c

package info (click to toggle)
cmus 2.10.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,040 kB
  • sloc: ansic: 38,844; sh: 1,578; makefile: 257; python: 157
file content (189 lines) | stat: -rw-r--r-- 4,978 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright 2014 Boris Timofeev <mashin87@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, see <http://www.gnu.org/licenses/>.
 */

#include "../comment.h"
#include "../debug.h"
#include "../ip.h"
#include "../utils.h"
#include "../xmalloc.h"
#include <ayemu.h>
#include <string.h>

struct vtx_private {
	ayemu_ay_t ay;
	ayemu_ay_reg_frame_t regs;
	ayemu_vtx_t *vtx;
	int pos;
	int left;
};

static const int sample_rate = 44100;
static const int channels = 2;
static const int bits = 16;

static int vtx_open(struct input_plugin_data *ip_data)
{
	struct vtx_private *priv;
	priv = xnew(struct vtx_private, 1);
	ip_data->private = priv;

	priv->vtx = ayemu_vtx_load_from_file(ip_data->filename);
	if (!priv->vtx) {
		d_print("error: failed to open file %s\n", ip_data->filename);
		free(priv);
		return -IP_ERROR_INTERNAL;
	}

	ayemu_init(&priv->ay);
	ayemu_set_sound_format(&priv->ay, sample_rate, channels, bits);
	ayemu_set_chip_type(&priv->ay, priv->vtx->chiptype, NULL);
	ayemu_set_chip_freq(&priv->ay, priv->vtx->chipFreq);
	ayemu_set_stereo(&priv->ay, priv->vtx->stereo, NULL);

	ip_data->sf = sf_bits(bits) | sf_rate(sample_rate) | sf_channels(channels) | sf_signed(1);
	ip_data->sf |= sf_host_endian();
	channel_map_init_stereo(ip_data->channel_map);

	priv->pos = 0;
	priv->left = 0;

	return 0;
}

static int vtx_close(struct input_plugin_data *ip_data)
{
	struct vtx_private *priv = ip_data->private;

	ayemu_vtx_free(priv->vtx);
	free(priv);
	ip_data->private = NULL;
	return 0;
}

static int vtx_read(struct input_plugin_data *ip_data, char *buffer, int count)
{
	struct vtx_private *priv = ip_data->private;
	int need = count;
	int donow = 0;

	while (need > 0) {
		if (priv->left > 0) {
			donow = min_i(need, priv->left);
			buffer = ayemu_gen_sound(&priv->ay, (char *)buffer, donow);
			priv->left -= donow;
			need -= donow;
		} else {
			if (priv->pos >= priv->vtx->frames)
				return 0;
			ayemu_vtx_getframe(priv->vtx, priv->pos++, priv->regs);
			ayemu_set_regs(&priv->ay, priv->regs);
			priv->left = (sample_rate / priv->vtx->playerFreq) * (channels * bits / 8);
		}
	}

	return count;
}

static int vtx_seek(struct input_plugin_data *ip_data, double offset)
{
	struct vtx_private *priv = ip_data->private;
	int sample = sample_rate * offset;
	int samples_per_frame = sample_rate / priv->vtx->playerFreq;
	priv->pos = sample / samples_per_frame;
	if (priv->pos >= priv->vtx->frames) {
		return 0;
	}
	ayemu_vtx_getframe(priv->vtx, priv->pos, priv->regs);
	priv->left = samples_per_frame - (sample % samples_per_frame);
	return 0;
}

static int vtx_read_comments(struct input_plugin_data *ip_data, struct keyval **comments)
{
	struct vtx_private *priv = ip_data->private;
	GROWING_KEYVALS(c);
	const char *str;

	str = priv->vtx->author;
	if (str && str[0])
		comments_add_const(&c, "artist", str);
	str = priv->vtx->from;
	if (str && str[0])
		comments_add_const(&c, "album", str);
	str = priv->vtx->title;
	if (str && str[0])
		comments_add_const(&c, "title", str);
	int year = priv->vtx->year;
	if (year > 0) {
		char buf[16] = {0};
		snprintf(buf, sizeof buf, "%d", year);
		comments_add_const(&c, "date", buf);
	}
	str = priv->vtx->comment;
	if (str && str[0])
		comments_add_const(&c, "comment", str);

	keyvals_terminate(&c);
	*comments = c.keyvals;
	return 0;
}

static int vtx_duration(struct input_plugin_data *ip_data)
{
	struct vtx_private *priv = ip_data->private;

	return (int)(priv->vtx->frames / priv->vtx->playerFreq);
}

static long vtx_bitrate(struct input_plugin_data *ip_data)
{
	return -IP_ERROR_FUNCTION_NOT_SUPPORTED;
}

static long vtx_current_bitrate(struct input_plugin_data *ip_data)
{
	return -IP_ERROR_FUNCTION_NOT_SUPPORTED;
}

static char *vtx_codec(struct input_plugin_data *ip_data)
{
	return xstrdup("vtx");
}

static char *vtx_codec_profile(struct input_plugin_data *ip_data)
{
	return NULL;
}

const struct input_plugin_ops ip_ops = {
	.open = vtx_open,
	.close = vtx_close,
	.read = vtx_read,
	.seek = vtx_seek,
	.read_comments = vtx_read_comments,
	.duration = vtx_duration,
	.bitrate = vtx_bitrate,
	.bitrate_current = vtx_current_bitrate,
	.codec = vtx_codec,
	.codec_profile = vtx_codec_profile
};

const int ip_priority = 50;
const char * const ip_extensions[] = {"vtx", NULL};
const char * const ip_mime_types[] = { NULL };
const struct input_plugin_opt ip_options[] = { { NULL } };
const unsigned ip_abi_version = IP_ABI_VERSION;