// // Programmer: Craig Stuart Sapp // Creation Date: Thu Dec 24 15:38:37 PST 1998 // Last Modified: Thu Dec 24 15:45:15 PST 1998 // Filename: ...linuxmidi/output/midipc.c // Syntax: C // $Smake: gcc -O3 -Wall -o %b %f && strip %b // // Description: This program send a patch change command to an // External MIDI synthesizer. Usage: // midipc instrument-number [midi-device-number] // #include #include #include #include #include #include // global variables: int seqfd = -1; // sequencer file descriptor const char* dev = "/dev/sequencer"; // name of sequencer device int status; // for error checking // function declarations: void sendPatchChange(int device, int patchnum); int main(int argc, char** argv) { int numMidi = 0; // number of available MIDI outputs int instrument = 0; // instrument to play (default = piano) int device = 0; // for selecting which MIDI output to use seqfd = open(dev, O_WRONLY, 0); if (seqfd < 0) { printf("Error: cannot open %s\n", dev); exit(1); } if (argc >= 2) { instrument = atoi(argv[1]); } if (argc >= 3) { device = argv[2][0] - '0'; } status = ioctl(seqfd, SNDCTL_SEQ_NRMIDIS, &numMidi); if (status != 0) { printf("Error: cannot access MIDI devices on soundcard\n"); exit(1); } printf("\nThere are: %d MIDI output devices available\n", numMidi); if (device >= numMidi) { printf("You specified an invalid MIDI device\n"); exit(1); } printf("Device set to: %d\n", device); sendPatchChange(device, instrument); close(seqfd); return 0; } void sendPatchChange(int device, int patchnum) { unsigned char outpacket[8]; outpacket[0] = SEQ_MIDIPUTC; outpacket[1] = 0xc0; // patch change command (Channel 0) outpacket[2] = device; outpacket[3] = 0; outpacket[4] = SEQ_MIDIPUTC; // outpacket[5] will be the instrument number outpacket[6] = device; outpacket[7] = 0; outpacket[5] = patchnum & 0xff; write(seqfd, outpacket, 8); }