⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 amidi.c

📁 alsa-utils-1.0.14编译声卡驱动所需要的一些文件源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *  amidi.c - read from/write to RawMIDI ports * *  Copyright (c) Clemens Ladisch <clemens@ladisch.de> * * *   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., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA */#define _GNU_SOURCE#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <string.h>#include <ctype.h>#include <getopt.h>#include <errno.h>#include <signal.h>#include <sys/types.h>#include <sys/poll.h>#include <sys/stat.h>#include <unistd.h>#include <fcntl.h>#include <alsa/asoundlib.h>#include "aconfig.h"#include "version.h"static int do_device_list, do_rawmidi_list;static char *port_name = "default";static char *send_file_name;static char *receive_file_name;static char *send_hex;static char *send_data;static int send_data_length;static int receive_file;static int dump;static int timeout;static int stop;static snd_rawmidi_t *input, **inputp;static snd_rawmidi_t *output, **outputp;static void error(const char *format, ...){	va_list ap;	va_start(ap, format);	vfprintf(stderr, format, ap);	va_end(ap);	putc('\n', stderr);}static void usage(void){	printf(		"Usage: amidi options\n"		"\n"		"-h, --help             this help\n"		"-V, --version          print current version\n"		"-l, --list-devices     list all hardware ports\n"		"-L, --list-rawmidis    list all RawMIDI definitions\n"		"-p, --port=name        select port by name\n"		"-s, --send=file        send the contents of a (.syx) file\n"		"-r, --receive=file     write received data into a file\n"		"-S, --send-hex=\"...\"   send hexadecimal bytes\n"		"-d, --dump             print received data as hexadecimal bytes\n"		"-t, --timeout=seconds  exits when no data has been received\n"		"                       for the specified duration\n"		"-a, --active-sensing   don't ignore active sensing bytes\n");}static void version(void){	puts("amidi version " SND_UTIL_VERSION_STR);}static void *my_malloc(size_t size){	void *p = malloc(size);	if (!p) {		error("out of memory");		exit(EXIT_FAILURE);	}	return p;}static int is_input(snd_ctl_t *ctl, int card, int device, int sub){	snd_rawmidi_info_t *info;	int err;	snd_rawmidi_info_alloca(&info);	snd_rawmidi_info_set_device(info, device);	snd_rawmidi_info_set_subdevice(info, sub);	snd_rawmidi_info_set_stream(info, SND_RAWMIDI_STREAM_INPUT);		if ((err = snd_ctl_rawmidi_info(ctl, info)) < 0 && err != -ENXIO)		return err;	else if (err == 0)		return 1;	return 0;}static int is_output(snd_ctl_t *ctl, int card, int device, int sub){	snd_rawmidi_info_t *info;	int err;	snd_rawmidi_info_alloca(&info);	snd_rawmidi_info_set_device(info, device);	snd_rawmidi_info_set_subdevice(info, sub);	snd_rawmidi_info_set_stream(info, SND_RAWMIDI_STREAM_OUTPUT);		if ((err = snd_ctl_rawmidi_info(ctl, info)) < 0 && err != -ENXIO)		return err;	else if (err == 0)		return 1;	return 0;}static void list_device(snd_ctl_t *ctl, int card, int device){	snd_rawmidi_info_t *info;	const char *name;	const char *sub_name;	int subs, subs_in, subs_out;	int sub, in, out;	int err;	snd_rawmidi_info_alloca(&info);	snd_rawmidi_info_set_device(info, device);	snd_rawmidi_info_set_stream(info, SND_RAWMIDI_STREAM_INPUT);	snd_ctl_rawmidi_info(ctl, info);	subs_in = snd_rawmidi_info_get_subdevices_count(info);	snd_rawmidi_info_set_stream(info, SND_RAWMIDI_STREAM_OUTPUT);	snd_ctl_rawmidi_info(ctl, info);	subs_out = snd_rawmidi_info_get_subdevices_count(info);	subs = subs_in > subs_out ? subs_in : subs_out;	sub = 0;	in = out = 0;	if ((err = is_output(ctl, card, device, sub)) < 0) {		error("cannot get rawmidi information %d:%d: %s",		      card, device, snd_strerror(err));		return;	} else if (err)		out = 1;	if (err == 0) {		if ((err = is_input(ctl, card, device, sub)) < 0) {			error("cannot get rawmidi information %d:%d: %s",			      card, device, snd_strerror(err));			return;		}	} else if (err) 		in = 1;	if (err == 0)		return;	name = snd_rawmidi_info_get_name(info);	sub_name = snd_rawmidi_info_get_subdevice_name(info);	if (sub_name[0] == '\0') {		if (subs == 1) {			printf("%c%c  hw:%d,%d    %s\n", 			       in ? 'I' : ' ', out ? 'O' : ' ',			       card, device, name);		} else			printf("%c%c  hw:%d,%d    %s (%d subdevices)\n",			       in ? 'I' : ' ', out ? 'O' : ' ',			       card, device, name, subs);	} else {		sub = 0;		for (;;) {			printf("%c%c  hw:%d,%d,%d  %s\n",			       in ? 'I' : ' ', out ? 'O' : ' ',			       card, device, sub, sub_name);			if (++sub >= subs)				break;			in = is_input(ctl, card, device, sub);			out = is_output(ctl, card, device, sub);			snd_rawmidi_info_set_subdevice(info, sub);			if (out) {				snd_rawmidi_info_set_stream(info, SND_RAWMIDI_STREAM_OUTPUT);				if ((err = snd_ctl_rawmidi_info(ctl, info)) < 0) {					error("cannot get rawmidi information %d:%d:%d: %s",					      card, device, sub, snd_strerror(err));					break;				} 			} else {				snd_rawmidi_info_set_stream(info, SND_RAWMIDI_STREAM_INPUT);				if ((err = snd_ctl_rawmidi_info(ctl, info)) < 0) {					error("cannot get rawmidi information %d:%d:%d: %s",					      card, device, sub, snd_strerror(err));					break;				}			}			sub_name = snd_rawmidi_info_get_subdevice_name(info);		}	}}static void list_card_devices(int card){	snd_ctl_t *ctl;	char name[32];	int device;	int err;	sprintf(name, "hw:%d", card);	if ((err = snd_ctl_open(&ctl, name, 0)) < 0) {		error("cannot open control for card %d: %s", card, snd_strerror(err));		return;	}	device = -1;	for (;;) {		if ((err = snd_ctl_rawmidi_next_device(ctl, &device)) < 0) {			error("cannot determine device number: %s", snd_strerror(err));			break;		}		if (device < 0)			break;		list_device(ctl, card, device);	}	snd_ctl_close(ctl);}static void device_list(void){	int card, err;	card = -1;	if ((err = snd_card_next(&card)) < 0) {		error("cannot determine card number: %s", snd_strerror(err));		return;	}	if (card < 0) {		error("no sound card found");		return;	}	puts("Dir Device    Name");	do {		list_card_devices(card);		if ((err = snd_card_next(&card)) < 0) {			error("cannot determine card number: %s", snd_strerror(err));			break;		}	} while (card >= 0);}static void rawmidi_list(void){	snd_output_t *output;	snd_config_t *config;	int err;	if ((err = snd_config_update()) < 0) {		error("snd_config_update failed: %s", snd_strerror(err));		return;	}	if ((err = snd_output_stdio_attach(&output, stdout, 0)) < 0) {		error("snd_output_stdio_attach failed: %s", snd_strerror(err));		return;	}	if (snd_config_search(snd_config, "rawmidi", &config) >= 0) {		puts("RawMIDI list:");		snd_config_save(config, output);	}	snd_output_close(output);}static void load_file(void){	int fd;	off_t length;	fd = open(send_file_name, O_RDONLY);	if (fd == -1) {		error("cannot open %s - %s", send_file_name, strerror(errno));		return;	}	length = lseek(fd, 0, SEEK_END);	if (length == (off_t)-1) {		error("cannot determine length of %s: %s", send_file_name, strerror(errno));		goto _error;	}	send_data = my_malloc(length);	lseek(fd, 0, SEEK_SET);	if (read(fd, send_data, length) != length) {		error("cannot read from %s: %s", send_file_name, strerror(errno));		goto _error;	}	if (length >= 4 && !memcmp(send_data, "MThd", 4)) {		error("%s is a Standard MIDI File; use aplaymidi to send it", send_file_name);		goto _error;	}	send_data_length = length;	goto _exit;_error:	free(send_data);	send_data = NULL;_exit:	close(fd);}static int hex_value(char c){	if ('0' <= c && c <= '9')		return c - '0';	if ('A' <= c && c <= 'F')		return c - 'A' + 10;	if ('a' <= c && c <= 'f')		return c - 'a' + 10;	error("invalid character %c", c);	return -1;}static void parse_data(void){	const char *p;	int i, value;	send_data = my_malloc(strlen(send_hex)); /* guesstimate */	i = 0;	value = -1; /* value is >= 0 when the first hex digit of a byte has been read */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -