// // Programmer: Craig Stuart Sapp // Creation Date: Fri Dec 18 23:05:42 PST 1998 // Last Modified: Thu Dec 24 15:56:37 PST 1998 // Filename: ...linuxmidi/output/ranwalk.cpp // Syntax: C++ // $Smake: g++ -O3 -Wall -o %b %f && strip %b // #include #include #include #include #include #include "SigTimer.cpp" // global variables: const char* dev = "/dev/sequencer"; // name of sequencer file int status; // for error checking // function declarations: void randomwalk(int seqfd, int device); int main(int argc, char** argv) { int seqfd = open(dev, O_WRONLY, 0); if (seqfd < 0) { cout << "Error: cannot open " << dev << endl; exit(1); } int numMidi; // number of midi outputs status = ioctl(seqfd, SNDCTL_SEQ_NRMIDIS, &numMidi); if (status != 0) { cout << "Error: cannot access MIDI devices on soundcard" << endl; exit(1); } cout << "\nThere are: " << numMidi << " Midi output devices" << endl; int device = 0; // MIDI output device to use if (argc >= 2) { device = argv[1][0] - '0'; } if (device >= numMidi) { cout << "Error: specified device is not valid: " << device << endl; exit(1); } else { cout << "Device set to: " << device << endl; } randomwalk(seqfd, device); close(seqfd); return 0; } void randomwalk(int seqfd, int device) { unsigned char outpacket[4]; outpacket[0] = SEQ_MIDIPUTC; outpacket[2] = device; outpacket[3] = 0; cout << "What timbre do you want: "; int userinput; cin >> userinput; outpacket[1] = 0xc0; write(seqfd, outpacket, 4); outpacket[1] = userinput & 0xff; write(seqfd, outpacket, 4); SigTimer timer; cout << "Enter a tempo for quarter notes per minute: "; double tempo = 120.0; cin >> tempo; timer.setTempo(tempo); timer.reset(); timer.update(-1); int key = 60; while (1) { if (timer.expired()) { timer.update(); switch (rand() % 2) { case 0: key--; if (key < 0) key = 0; break; case 1: key++; if (key > 127) key = 127; break; } outpacket[1] = 0x90; // note-on MIDI command write(seqfd, outpacket, 4); outpacket[1] = key; // note-on key MIDI data write(seqfd, outpacket, 4); outpacket[1] = 127; // note-on velocity MIDI data write(seqfd, outpacket, 4); } } close(seqfd); }