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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
|
/*
* ALSA hwdep SBI FM instrument loader
* Copyright (c) 2000 Uros Bizjak <uros@kss-loka.si>
*
* 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
*
*
* Oct. 2007 - Takashi Iwai <tiwai@suse.de>
* Changed to use hwdep instead of obsoleted seq-instr interface
*/
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <alsa/asoundlib.h>
#include <alsa/sound/asound_fm.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <unistd.h>
#define DATA_LEN_2OP 16
#define DATA_LEN_4OP 24
/* offsets for SBI params */
#define AM_VIB 0
#define KSL_LEVEL 2
#define ATTACK_DECAY 4
#define SUSTAIN_RELEASE 6
#define WAVE_SELECT 8
/* offset for SBI instrument */
#define CONNECTION 10
#define OFFSET_4OP 11
/* offsets for SBI extensions */
#define ECHO_DELAY 0
#define ECHO_ATTEN 1
#define CHORUS_SPREAD 2
#define TRNSPS 3
#define FIX_DUR 4
#define MODES 5
#define FIX_KEY 6
/* Options for the command */
#define HAS_ARG 1
static struct option long_opts[] = {
{"device", HAS_ARG, NULL, 'D'},
{"opl2", 0, NULL, '2'},
{"opl3", 0, NULL, '4'},
{"clear", 0, NULL, 'c'},
{"path", HAS_ARG, NULL, 'P'},
{"verbose", HAS_ARG, NULL, 'v'},
{"quiet", 0, NULL, 'q'},
{"version", 0, NULL, 'V'},
{0, 0, 0, 0},
};
/* Number of elements in an array */
#define NELEM(a) ( sizeof(a)/sizeof((a)[0]) )
enum {
FM_PATCH_UNKNOWN,
FM_PATCH_OPL2,
FM_PATCH_OPL3
};
/* Default file type */
static int file_type = FM_PATCH_UNKNOWN;
/* Default verbose level */
static int quiet;
static int verbose = 0;
/* Global declarations */
static snd_hwdep_t *handle;
static int iface;
#ifndef PATCHDIR
#define PATCHDIR "/usr/share/sounds/opl3"
#endif
static char *patchdir = PATCHDIR;
/* Function prototypes */
static void show_usage (void);
static void show_op (struct sbi_patch * instr, int type);
static int load_patch (struct sbi_patch * instr);
static int load_file (int bank, char *filename);
static int init_hwdep (const char *name);
/*
* Show usage message
*/
static void
show_usage () {
char **cpp;
static char *msg[] = {
"Usage: sbiload [options] [instfile [drumfile]]",
" sbiload [options] -c",
"",
" -D, --device=name - hwdep device string",
" -c, --clear - Clear patches and exit",
" -2, --opl2 - two operators file type (OPL2)",
" -4, --opl3 - four operators file type (OPL3)",
" -P, --path=path - Specify the patch path",
" (default path: " PATCHDIR ")",
" -v, --verbose=level - Verbose level (default = 0)",
" -q, --quiet - Be quiet, no error/warning messages",
" -V, --version - Show version",
};
for (cpp = msg; cpp < msg + NELEM (msg); cpp++) {
printf ("%s\n", *cpp);
}
}
/*
* Show version
*/
static void
show_version () {
printf("Version: " VERSION "\n");
}
/*
* Show instrument FM operators
*/
static void
show_op (struct sbi_patch * inst, int type) {
int i = 0;
int ofs = 0;
do {
unsigned char val;
val = inst->data[AM_VIB + ofs];
printf (" OP%i: flags: %s %s %s %s", i,
val & (1 << 7) ? "AM" : " ",
val & (1 << 6) ? "VIB" : " ",
val & (1 << 5) ? "EGT" : " ",
val & (1 << 4) ? "KSR" : " ");
val = inst->data[AM_VIB + ofs + 1];
printf ("\011OP%i: flags: %s %s %s %s\n", i + 1,
val & (1 << 7) ? "AM" : " ",
val & (1 << 6) ? "VIB" : " ",
val & (1 << 5) ? "EGT" : " ",
val & (1 << 4) ? "KSR" : "");
val = inst->data[AM_VIB + ofs];
printf (" OP%i: MULT = 0x%x", i, val & 0x0f);
val = inst->data[AM_VIB + ofs + 1];
printf ("\011\011OP%i: MULT = 0x%x\n", i + 1, val & 0x0f);
val = inst->data[KSL_LEVEL + ofs];
printf (" OP%i: KSL = 0x%x TL = 0x%.2x", i,
(val >> 6) & 0x03, val & 0x3f);
val = inst->data[KSL_LEVEL + ofs + 1];
printf ("\011OP%i: KSL = 0x%x TL = 0x%.2x\n", i + 1,
(val >> 6) & 0x03, val & 0x3f);
val = inst->data[ATTACK_DECAY + ofs];
printf (" OP%i: AR = 0x%x DL = 0x%x", i,
(val >> 4) & 0x0f, val & 0x0f);
val = inst->data[ATTACK_DECAY + ofs + 1];
printf ("\011OP%i: AR = 0x%x DL = 0x%x\n", i + 1,
(val >> 4) & 0x0f, val & 0x0f);
val = inst->data[SUSTAIN_RELEASE + ofs];
printf (" OP%i: SL = 0x%x RR = 0x%x", i,
(val >> 4) & 0x0f, val & 0x0f);
val = inst->data[SUSTAIN_RELEASE + ofs + 1];
printf ("\011OP%i: SL = 0x%x RR = 0x%x\n", i + 1,
(val >> 4) & 0x0f, val & 0x0f);
val = inst->data[WAVE_SELECT + ofs];
printf (" OP%i: WS = 0x%x", i, val & 0x07);
val = inst->data[WAVE_SELECT + ofs + 1];
printf ("\011\011OP%i: WS = 0x%x\n", i + 1, val & 0x07);
val = inst->data[CONNECTION + ofs];
printf (" FB = 0x%x, %s\n", (val >> 1) & 0x07,
val & (1 << 0) ? "parallel" : "serial");
i += 2;
ofs += OFFSET_4OP;
}
while (i == (type == FM_PATCH_OPL3) << 1);
printf (" Extended data:\n"
" ED = %.3i EA = %.3i CS = %.3i TR = %.3i\n"
" FD = %.3i MO = %.3i FK = %.3i\n",
inst->extension[ECHO_DELAY], inst->extension[ECHO_ATTEN],
inst->extension[CHORUS_SPREAD], inst->extension[TRNSPS],
inst->extension[FIX_DUR], inst->extension[MODES],
inst->extension[FIX_KEY]);
}
/*
* Send patch to destination port
*/
static int
load_patch (struct sbi_patch * inst) {
ssize_t ret;
ret = snd_hwdep_write(handle, inst, sizeof(*inst));
if (ret != sizeof(*inst)) {
if (!quiet)
fprintf (stderr, "Unable to write an instrument %.3i put event: %s\n",
inst->prog, snd_strerror (ret));
return -1;
}
if (verbose)
printf ("Loaded instrument %.3i, bank %.3i: %s\n",
inst->prog, inst->bank, inst->name);
return 0;
}
/*
* Parse standard .sb or .o3 file
*/
static void
load_sb (int bank, int fd) {
int len;
int prg;
struct sbi_patch inst;
int fm_instr_type;
len = (file_type == FM_PATCH_OPL3) ? DATA_LEN_4OP : DATA_LEN_2OP;
for (prg = 0;; prg++) {
inst.prog = prg;
inst.bank = bank;
if (read (fd, inst.key, 4) != 4)
break;
if (!memcmp (inst.key, "SBI\032", 4) || !memcmp (inst.key, "2OP\032", 4)) {
fm_instr_type = FM_PATCH_OPL2;
} else if (!strncmp (inst.key, "4OP\032", 4)) {
fm_instr_type = FM_PATCH_OPL3;
} else {
if (verbose)
printf ("%.3i: wrong instrument key!\n", prg);
fm_instr_type = FM_PATCH_UNKNOWN;
}
if (read (fd, &inst.name, sizeof(inst.name)) != sizeof(inst.name) ||
read (fd, &inst.extension, sizeof(inst.extension)) != sizeof(inst.extension) ||
read (fd, &inst.data, len) != len)
break;
if (fm_instr_type == FM_PATCH_UNKNOWN)
continue;
if (verbose > 1) {
printf ("%.3i: [%s] %s\n", inst.prog,
fm_instr_type == FM_PATCH_OPL2 ? "OPL2" : "OPL3",
inst.name);
show_op (&inst, fm_instr_type);
}
if (load_patch (&inst) < 0)
break;
}
return;
}
/*
* Load file
*/
static int
load_file (int bank, char *filename) {
int fd;
char path[1024];
char *name;
if (*filename != '/') {
snprintf(path, sizeof(path), "%s/%s", patchdir, filename);
name = path;
} else {
name = filename;
}
fd = open (name, O_RDONLY);
if (fd < 0) {
/* try to guess from the interface name */
const char *ext = iface == SND_HWDEP_IFACE_OPL2 ? "sb" : "o3";
if (*filename != '/')
snprintf(path, sizeof(path), "%s/%s.%s", patchdir, filename, ext);
else
snprintf(path, sizeof(path), "%s.%s", filename, ext);
name = path;
fd = open (name, O_RDONLY);
if (fd < 0) {
if (!quiet)
perror (filename);
return -1;
}
}
/* correct file type if not set explicitly */
if (file_type == FM_PATCH_UNKNOWN)
file_type = iface == SND_HWDEP_IFACE_OPL2 ? FM_PATCH_OPL2 : FM_PATCH_OPL3;
if (verbose)
fprintf (stderr, "Loading from %s\n", name);
load_sb(bank, fd);
close (fd);
return 0;
}
static void
clear_patches (void)
{
snd_hwdep_ioctl(handle, SNDRV_DM_FM_IOCTL_CLEAR_PATCHES, 0);
}
/*
* Open a hwdep device
*/
static int open_hwdep (const char *name)
{
int err;
snd_hwdep_info_t *info;
if ((err = snd_hwdep_open (&handle, name, SND_HWDEP_OPEN_WRITE)) < 0)
return err;
snd_hwdep_info_alloca(&info);
if (!snd_hwdep_info (handle, info)) {
iface = snd_hwdep_info_get_iface(info);
if (iface == SND_HWDEP_IFACE_OPL2 ||
iface == SND_HWDEP_IFACE_OPL3 ||
iface == SND_HWDEP_IFACE_OPL4)
return 0;
}
snd_hwdep_close(handle);
handle = NULL;
return -EINVAL;
}
static int
init_hwdep (const char *name) {
int err;
char tmpname[16];
if (!name || !*name) {
/* auto probe */
int card = -1;
snd_ctl_t *ctl;
while (!snd_card_next(&card) && card >= 0) {
int dev;
sprintf(tmpname, "hw:%d", card);
if (snd_ctl_open(&ctl, tmpname, 0) < 0)
continue;
dev = -1;
while (!snd_ctl_hwdep_next_device(ctl, &dev) && dev >= 0) {
sprintf(tmpname, "hw:%d,%d", card, dev);
if (!open_hwdep(tmpname)) {
snd_ctl_close(ctl);
return 0;
}
}
snd_ctl_close(ctl);
}
if (!quiet)
fprintf (stderr, "Can't find any OPL3 hwdep device\n");
return -1;
}
if (*name == '/') {
/* guess card and device numbers - for convenience to user
* from udev rules
*/
int card, device;
if (sscanf(name, "/dev/snd/hwC%dD%d", &card, &device) == 2) {
if (card >= 0 && card <= 32 && device >= 0 && device <= 32) {
sprintf(tmpname, "hw:%d,%d", card, device);
name = tmpname; /* override */
}
}
}
if ((err = open_hwdep (name)) < 0) {
if (!quiet)
fprintf (stderr, "Could not open hwdep %s: %s\n",
name, snd_strerror (err));
return -1;
}
return 0;
}
/*
* Unsubscribe client from destination port
* and close sequencer
*/
static void
finish_hwdep ()
{
snd_hwdep_close(handle);
handle = NULL;
}
/*
* Load a .SBI FM instrument patch
* sbiload [-p client:port] [-l] [-P path] [-v level] instfile drumfile
*
* -D, --device=name - An ALSA hwdep name to use
* -2 --opl2 - two operators file type (*.sb)
* -4 --opl3 - four operators file type (*.o3)
* -P, --path=path - Specify the patch path
* -v, --verbose=level - Verbose level
* -q, --quiet - Be quiet, no error/warning messages
*/
int
main (int argc, char **argv) {
char opts[NELEM (long_opts) * 2 + 1];
char *name;
char *cp;
int c;
int clear = 0;
struct option *op;
/* Build up the short option string */
cp = opts;
for (op = long_opts; op < &long_opts[NELEM (long_opts)]; op++) {
*cp++ = op->val;
if (op->has_arg)
*cp++ = ':';
}
name = NULL;
/* Deal with the options */
for (;;) {
c = getopt_long (argc, argv, opts, long_opts, NULL);
if (c == -1)
break;
switch (c) {
case 'D':
name = optarg;
break;
case 'c':
clear = 1;
break;
case '2':
file_type = FM_PATCH_OPL2;
break;
case '4':
file_type = FM_PATCH_OPL3;
break;
case 'q':
quiet = 1;
verbose = 0;
break;
case 'v':
quiet = 0;
verbose = atoi (optarg);
break;
case 'V':
show_version();
exit (1);
case 'P':
patchdir = optarg;
break;
case '?':
show_usage ();
exit (1);
}
}
if (init_hwdep (name) < 0) {
return 1;
}
clear_patches ();
if (clear)
goto done;
/* Process instrument and drum file */
if (optind < argc)
name = argv[optind++];
else
name = "std";
if (load_file (0, name) < 0) {
finish_hwdep();
return 1;
}
if (optind < argc)
name = argv[optind];
else
name = "drums";
if (load_file (128, name) < 0) {
finish_hwdep();
return 1;
}
done:
finish_hwdep();
return 0;
}
|