void checkOptions(Options& opts, int argc, char* argv[]) { opts.define("r|rhythm|rhythm-cycle|=s:4"); // rhythm to sample notes at opts.define("m|metric=s"); // metric cycle to sample opts.define("d|divide=b"); // determine if to split input notes opts.define("debug=b"); // determine bad input line num opts.define("author=b"); // author of program opts.define("version=b"); // compilation info opts.define("example=b"); // example usages opts.define("h|help=b"); // short description opts.process(argc, argv); // handle basic options: if (opts.getBoolean("author")) { cout << "Written by Craig Stuart Sapp, " << "[email protected], July 1998" << endl; exit(0); } else if (opts.getBoolean("version")) { cout << argv[0] << ", version: 5 December 2000" << endl; cout << "compiled: " << __DATE__ << endl; cout << MUSEINFO_VERSION << endl; exit(0); } else if (opts.getBoolean("help")) { usage(opts.getCommand()); exit(0); } else if (opts.getBoolean("example")) { example(); exit(0); } double duration = 0.0; int style = 0; int length; length = strlen(opts.getString("rhythm")); char buffer[length + 1]; strcpy(buffer, opts.getString("rhythm")); durations.reset(); styles.reset(); char *durstr; durstr = strtok(buffer, " ,:\t\n;"); while (durstr != NULL) { duration = Convert::kernToDuration(durstr); if (strchr(durstr, 'r') != 0) { style = 'r'; } else if (strchr(durstr, 'x') != 0) { style = 'x'; } else { style = 0; } durations.insert(duration); styles.insert(style); durstr = strtok(NULL, " ,:\t\n;"); } }
void start(void) { comparestate = 1; sentout.reset(); // initialize the MIDI output storage receivedin.reset(); // initialize the MIDI input storage midiinput.setPort(synth.getInputPort()); midiinput.open(); }
void initialization(void) { eventBuffer.setPollPeriod(10); notetimes.reset(); notetimes.insert(0); notetimes.insert(0); notes.reset(); notes.insert(0); notes.insert(0); noteontimes.zero(); }
void initialization(void) { memory.setSize(10000); // up to 10 seconds of squelch possible memory.reset(); mintime = 48; // minimum delta time maxtime = 180; // maximum delta time description(); }
int main ( int argc, char ** argv ) { char optChar; char * target; int optindx = 0; int interval = 0; int count = -1; int timeout = ICMP_TIMEOUT_SECS; bool debug = false; size_t size = 0; timeval tvin, tvsnt, tvo; static struct option l_opts[] = { {"count", required_argument, 0, 'c'}, {"debug", no_argument, 0, 'd'}, {"interval", required_argument, 0, 'i'}, {"help", no_argument, 0, 'h'}, {"size", required_argument, 0, 's'}, {"version", no_argument, 0, 'v'}, {0,0,0,0} }; if ( argc < 2 ) usage(); while ( (optChar = getopt_long(argc, argv, "c:di:hs:v", l_opts, &optindx)) != EOF ) { switch ( optChar ) { case 'c': count = StringUtils::FromString<int>(optarg); break; case 'd': debug = true; break; case 'i': interval = StringUtils::FromString<int>(optarg); break; case 'h': usage(); break; case 's': size = StringUtils::FromString<size_t>(optarg); break; case 'v': version(); break; default: usage(); break; } } target = strdup(argv[argc-1]); ::memset(&tvsnt, 0, sizeof(tvsnt)); ::memset(&tvin, 0, sizeof(tvin)); ::memset(&tvo, 0, sizeof(tvo)); if ( interval == 0 ) interval = 1000; std::string host = target; ::free(target); Pid = ::getpid() & 0xFFFF; ::signal(SIGPIPE, SIG_IGN); ::signal(SIGINT, &sigHandler); ipv4addr_t dstaddr = AddrInfo::GetHostAddr(host); if ( dstaddr == 0 ) { std::cout << "Invalid target host: " << host << std::endl; exit(-1); } Socket * icmps = new Socket(dstaddr, SOCKET_ICMP, SOCKTYPE_RAW, SOCKET_ICMP); icmps->init(false); dropPriv(); neticmp_h * req = NULL; icmp_ts * its = NULL; char * wptr = NULL; char * wbuff = NULL; char * data = NULL; const char * dt = NULL; bool sendReq = true; sockaddr_t csock; sockaddr_in* sa; ipv4addr_t addr; size_t sz, buflen, idsz; ssize_t wt, rd; int cnt, sent, rcvd; float mstot, avg; cnt = 1; sent = 0; rcvd = 0; mstot = 0.0; avg = 0.0; buflen = 2048; idsz = sizeof(neticmp_h) + sizeof(icmp_ts); CircularBuffer * rbuff = new CircularBuffer(buflen); wbuff = (char*) ::malloc(buflen); req = (neticmp_h*) wbuff; its = (icmp_ts*) wbuff + sizeof(neticmp_h); data = wbuff + idsz; req->type = ICMP_ECHO; req->code = 0; req->chksum = 0; req->id = Pid; req->seq = 0; if ( count > 0 ) cnt = count; if ( size > (buflen - idsz) ) size = buflen - idsz - 4; size += Serializer::PadLen(size); InitDataBlock(size); dt = RandData.substr(0, size).data(); ::memcpy(data, dt, size); its->size = size; std::cout << "Sending "; if ( count > 0 ) std::cout << "(" << count << ") " ; std::cout << "ICMP echo requests to " << IpAddr::ntop(dstaddr) << " (" << host << ")" << std::endl; std::cout << "ICMP data size is " << size << std::endl; while ( ! Alarm ) { rbuff->reset(); if ( ! getTimeOfDay(tvin) ) errorOut("error in gettime"); float lastsnt = timeDiff(tvin, tvsnt); if ( lastsnt >= interval ) sendReq = true; if ( sendReq && cnt > 0 ) { sz = idsz + size; // account for added data size tvsnt = tvin; its->secs = tvin.tv_sec; its->usecs = tvin.tv_usec; req->chksum = 0; req->seq++; req->chksum = Socket::IpChkSum((uint16_t*)req, sz); wt = icmps->write(wbuff, sz); if ( wt < 0 ) errorOut("Error in write " + icmps->getErrorString()); sent++; sendReq = false; if ( count > 0 ) cnt--; if ( debug ) std::cout << "Request <" << sent << "> sent" << std::endl; } sz = rbuff->writePtrAvailable(); wptr = rbuff->getWritePtr(&sz); if ( wptr == NULL ) errorOut("Error in writing to rbuff"); rd = icmps->readFrom(wptr, sz, csock); if ( rd < 0 ) errorOut("Error in readFrom " + icmps->getErrorString()); sa = (sockaddr_in*) &csock; addr = sa->sin_addr.s_addr; rbuff->setWritePtr(rd); if ( rd > 0 && addr == dstaddr ) { IcmpResponse response; if ( ! getTimeOfDay(tvin) ) errorOut("error in gettime"); rd = readIcmpHeader(rbuff, response); if ( rd > 0 && response.icmph.id == Pid ) { sz = rbuff->readAvailable(); rcvd++; if ( sz == sizeof(icmp_ts) ) { timeval tv; char * idf = rbuff->getReadPtr(&sz); icmp_ts * ist = (icmp_ts*) idf; tv.tv_sec = ist->secs; tv.tv_usec = ist->usecs; if ( debug ) std::cout << " Received data field in echo response" << std::endl; rbuff->setReadPtr(sz); } float ms = timeDiff(tvin, tvsnt); std::cout << (rd+sz) << " bytes from " << IpAddr::ntop(addr) << ": seq=" << response.icmph.seq << " time=" << ms << " ms" << std::endl; mstot += ms; avg = (float) mstot / rcvd; if ( debug ) std::cout << " mstot=" << mstot << " rcvd = " << rcvd << std::endl; } } if ( cnt == 0 ) { if ( rcvd == cnt ) break; if ( tvo.tv_sec == 0 ) tvo.tv_sec = tvin.tv_sec; else if ( (tvin.tv_sec - tvo.tv_sec) > timeout ) break; } ::usleep(1000); } float loss; if ( rcvd == sent ) loss = 0.0; else loss = ( 100.0 - ( ((float) rcvd / (float) sent) * 100.0) ); std::cout << std::endl << "Results:" << std::endl; std::cout << " Sent " << sent << " requests, received " << rcvd << ": Loss=" << loss << "% : Avg Time= " << avg << " ms " << std::endl; return 0; }
void processNote(MidiEvent message, int seqLength, int direction) { static Array<char> notes; static Array<char> velocities; static Array<int> durations; static Array<int> iois; static Array<int> ontimes; static CircularBuffer<int> attacktimes; static int init = 0; static TumbleParameters temparam; char vel; if (!init) { attacktimes.setSize(256); attacktimes.reset(); notes.setSize(0); velocities.setSize(0); durations.setSize(0); iois.setSize(0); ontimes.setSize(128); ontimes.zero(); init = 1; } char note; int deltatime; int ioi0; int ioix; if (message.isNoteOn()) { attacktimes.insert(message.tick); // check to see if the ioi is in the correct range if (notes.getSize() == 0) { // no notes yet, so don't know the first ioi } else { deltatime = attacktimes[0] - attacktimes[1]; iois.append(deltatime); } if (iois.getSize() > 1) { ioi0 = iois[0]; ioix = iois[iois.getSize()-1]; if ((ioix < ioi0 * tolerance) || (ioix > ioi0 / tolerance)) { goto resettrigger; } } // at this point the note can be added to the sequence if (notes.getSize() + 1 >= seqLength) { // time to trigger an algorithm if (durations.getSize() < notes.getSize()) { // if the last note has not yet been turned off, approximate dur. deltatime = iois[iois.getSize()-1]; durations.append(deltatime); } int i; for (i=0; i<seqLength; i++) { temparam.v[i] = velocities[i]; temparam.i[i] = iois[i]; temparam.d[i] = durations[i]; temparam.n[i] = notes[i] - notes[0]; } temparam.n[0] = message.getP1() - notes[0]; temparam.current = message.getP1(); temparam.pos = 1; temparam.max = seqLength; temparam.active = 1; startAlgorithm(temparam); goto resettrigger; } else { // add the note info to the algorithm pile note = message.getP1(); notes.append(note); vel = message.getP2(); velocities.append(vel); attacktimes[message.getP1()] = message.tick; } } else if (message.isNoteOff()) { if (notes.getSize() > 0) { if (notes[notes.getSize()-1] == message.getP1()) { deltatime = message.tick - ontimes[message.getP1()]; durations.append(deltatime); } else { cout << "A funny error ocurred" << endl; } } return; resettrigger: attacktimes.setSize(0); notes.setSize(0); velocities.setSize(0); durations.setSize(0); iois.setSize(0); if (message.isNoteOn()) { note = message.getP1(); notes.append(note); ontimes[message.getP1()] = message.tick; vel = message.getP2(); velocities.append(vel); } } ////////////////////////////// // // startAlgorithm -- start playing the tumble algorithm. Inserts a // FunctionEvent into the eventBuffer which plays the tumble // algorithm sequence. The algorithm will die after the notes // fall off of the 88-note keyboard. // }
void keyboardchar(int key) { if (isdigit(key)) { int mkey = 5 * 12 + key - '0'; int mvel = (int)(drand48() * 40 + 40); //turn off old note from computer's point of view computerMessage.time = t_time; computerMessage.p2() = 0; synth.insert(computerMessage); cout << "played note: " << mkey << " with velocity: " << mvel << endl; computer.play(mkey, mvel); computerMessage.time = t_time; computerMessage.p0() = 0x90; // midi note-on command, channel 1 computerMessage.p1() = mkey; computerMessage.p2() = mvel; synth.insert(computerMessage); cout << "played note: " << mkey << " with velocity: " << mvel << endl; return; } switch (key) { case ' ': // turn output notes on/off activeQ = !activeQ; if (activeQ) { cout << "Program Activated" << endl; } else { cout << "Program Deactivated" << endl; } break; case 'c': // toggle baton control display controlDisplayQ = !controlDisplayQ; if (controlDisplayQ == 0) { cout << endl; } break; case 'd': // display baton's output notes displayOutput = !displayOutput; if (displayOutput) { cout << "Baton output notes display turned ON" << endl; } else { cout << "Baton output notes display turned OFF" << endl; } break; case 'n': // display performerPCHistory pitches { cout << "Piano notes: "; for (int i=0; i<11; i++) { cout << performerPCHistory[i] << ", "; } cout << performerPCHistory[11] << endl; } break; case 'm': // display performerPC pitches { cout << "Notes on: "; for (int i=0; i<11; i++) { cout << performerPC[i] << ", "; } cout << performerPC[11] << endl; } break; case 'r': // display the register information { cout << "Register: " << performerRegion[0] << performerRegion[1] << performerRegion[2] << endl; } break; case 'v': // display the active voice count for baton { cout << endl << "Active voices = " << countActiveVoices(); cout << ": "; for (int z=0; z<MAXVOICES; z++) { if (voiceState[z]) { cout << " " << (int)voice[z].getKey(); } else { cout << " 0"; } } cout << endl; } break; case 'z': // forget the current piano's key history { for (int z=0; z<12; z++) { performerPCHistory[z] = 0; } keys.reset(); keytimes.reset(); } break; case '[': // decrease the note memory time keyDuration -= 1000; if (keyDuration < 1000) { keyDuration = 1000; } cout << "Note memory time set to : " << keyDuration/1000 << " seconds" << endl; break; case ']': // increase the note memory time keyDuration += 1000; if (keyDuration > 60000) { keyDuration = 60000; } cout << "Note memory time set to : " << keyDuration/1000 << " seconds" << endl; break; case '-': // turn off computer keyboard note // turn off old note from computer's point of view computerMessage.time = t_time; computerMessage.p2() = 0; synth.insert(computerMessage); // turn off old note from synthesizer's point of view computer.off(); break; } }
void initialization(void) { eventBuffer.setPollPeriod(10); lastnotes.reset(); lasttimes.reset(); }