【发布时间】:2016-12-09 16:31:09
【问题描述】:
我昨天收到了一个合成器作为礼物,并且有兴趣向它写入数据。我做了这么多工作,这是一个通过一些音符进行缩放的程序。 然后我认为让它捕捉 Ctrl+C 信号并关闭会很好。 只关闭文件描述符的问题是 MIDI 设备仍然处理它给出的最后一个音符,所以我编写了静音函数,它告诉 MIDI 设备静音。这样可行。
然后我尝试让信号处理程序在退出之前使设备静音,从那以后我一直在苦苦挣扎。信号(SIGINT,intHandler);函数不会接受额外的参数。所以我想我会很聪明,并编写一个函数mySig,它调用信号函数并获取设备文件描述符和数据指针,并且能够在退出之前进行最后一次写入。
IDK,这甚至可能有效,但 mySig 函数似乎从一开始就被调用,并且从未发生缩放。
如何在使用信号函数退出程序之前调用我的静音函数?
这是我的第一个信号处理程序,我在运行linux,程序是C语言的。
#include <sys/soundcard.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
static volatile int keepRunning = 1;
char* device = "/dev/midi1";
//function headers:
void mute(int fd, char *data);
void intHandler(int dummy);
void mySig(void (*intHandler)(int dummy), int fd, char *data);
int main(void){
unsigned int note=50;
char data[3] = {0x90, note, 33}; //device address, note, volume
int fd = open(device, O_WRONLY, 0);
if( fd < 0 ){
printf("Error: cannot open Synth %s\n", device);
exit(1);
}
signal(SIGINT, intHandler);
// mySig(intHandler,fd,data);
while(keepRunning){
for( note=30; note < 95; note++ ){
data[1]=note;//change note
write( fd, data, sizeof(data) );
usleep(100000);
if(note>=89){
note =30;
}
}
mute(fd,data); //mutes the data stream.
close(fd); // close device
return 0;
}
}
//functions:
void mute(int fd, char *data){
data[2]=0;//setVolume to 0
write(fd, data, sizeof(data));
close(fd);
}
void mySig(void (*intHandler)(int dummy), int fd, char *data){
printf("my Sig has been called\n");
mute(fd,data);
signal(SIGINT, intHandler);
}
void intHandler(int dummy) {
printf("my Sig has been called\n");
keepRunning = 1;
printf("ctrl+c was pressed, exiting\n");
usleep(10000);
exit(1);
}
【问题讨论】:
-
请推荐一个更好的标题。
-
题目是这个问题唯一清楚的部分。