Main Page | Alphabetical List | Class List | Directories | File List | Class Members | File Members

Netmusic.cpp

Go to the documentation of this file.
00001 
00029 #include <gmpxx.h>
00030 
00031 #include "Netmusic.hh"
00032 
00033 //***************************************************************
00034 void Netmusic::check_error( int value, std::string error_text)
00035 {
00036         if(value < 0 )
00037         {
00038                 std::cout << error_text << std::endl;
00039                 _exit(0);
00040         }
00041 }
00042 
00043 //***************************************************************
00044 snd_seq_t* Netmusic::sender_open_seq()
00045 {
00046         snd_seq_t *seq_handle;
00047         int portid;
00048 
00049         Netmusic::check_error(snd_seq_open(&seq_handle, "default", 
00050                 SND_SEQ_OPEN_INPUT, 0), "Error opening sequencer sender");
00051 
00052         Netmusic::check_error(snd_seq_set_client_name(seq_handle,
00053                 "Netmusic Sender"), "Error setting name sequencer sender");
00054 
00055         portid = snd_seq_create_simple_port(seq_handle, "Netmusic Sender",
00056             SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE,
00057             SND_SEQ_PORT_TYPE_APPLICATION);
00058         Netmusic::check_error(portid, "Error creating simple port sender");
00059         return(seq_handle);
00060 }
00061 
00062 //***************************************************************
00063 snd_seq_t* Netmusic::receiver_open_seq(int* portId)
00064 {
00065         snd_seq_t *seq_handle;
00066 
00067         Netmusic::check_error(snd_seq_open(&seq_handle, "default", 
00068                 SND_SEQ_OPEN_OUTPUT, 0), "Error opening sequencer handle receiver");
00069 
00070         Netmusic::check_error(snd_seq_set_client_name(seq_handle,
00071                 "Netmusic Receiver"), "Error setting name sequencer receiver");
00072         (*portId) = snd_seq_create_simple_port(seq_handle, "Netmusic Receiver",
00073             SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ,
00074             SND_SEQ_PORT_TYPE_APPLICATION);
00075         Netmusic::check_error((*portId), "Error creating simple port receiver");
00076         return(seq_handle);
00077 }
00078 
00079 //***************************************************************
00080 void Netmusic::print_read_data(snd_seq_event_t* ev)
00081 {
00082         // output read data
00083         switch(ev->type)
00084         {
00085                 case SND_SEQ_EVENT_PGMCHANGE:
00086                         std::cout << DELIMITER << std::endl;
00087                         std::cout << "SENDER: MIDI Thread read Program Change: "
00088                                   << (int)ev->data.control.value << std::endl;
00089                         break;
00090 
00091                 case SND_SEQ_EVENT_NOTEON:
00092                         std::cout << DELIMITER << std::endl;
00093                         // recognized as note on 'meaning' note OFF
00094                         if(ev->data.note.velocity == 0)
00095                                 std::cout << "SENDER: MIDI Thread read Note Off: ";
00096                         else
00097                                 std::cout << "SENDER: MIDI Thread read Note On:  ";
00098                         std::cout << (int)ev->data.note.note << std::endl;
00099                         break;
00100 
00101                 case SND_SEQ_EVENT_NOTEOFF:
00102                         std::cout << DELIMITER << std::endl;
00103                         std::cout << "SENDER: MIDI Thread read Note Off:  ";
00104                         std::cout << (int)ev->data.note.note << std::endl;
00105                         break;
00106         }
00107 }
00108 
00109 //***************************************************************
00110 void Netmusic::sender_print_welcome_msg()
00111 {
00112         std::cout << std::endl << DELIMITER << std::endl;
00113         std::cout << "Netmusic " << VERSION << " - SENDER" << std::endl;
00114         std::cout << AUTHOR << " " << EMAIL << std::endl << std::endl;
00115 }
00116 
00117 //***************************************************************
00118 void Netmusic::receiver_print_welcome_msg()
00119 {
00120         std::cout << std::endl << DELIMITER << std::endl;
00121         std::cout << "Netmusic " << VERSION << " - RECEIVER" << std::endl;
00122         std::cout << AUTHOR << " " << EMAIL << std::endl << std::endl;
00123 }
00124 
00125 //***************************************************************
00126 void Netmusic::sender_print_help_msg()
00127 {
00128         std::cout << std::endl << DELIMITER << std::endl;
00129         std::cout << "Netmusic " << VERSION << " - 'Sender-HELP'" << std::endl << std::endl;
00130         std::cout << "-midi................... MIDI Protocol instead of MINI used." << std::endl;
00131         std::cout << "-portMidi=[x]........... Used port number for sending MIDI Messages." << std::endl;
00132         std::cout << "-portMini=[x]........... Used port number for sending MINI Messages." << std::endl;
00133         std::cout << "-portSync=[x]........... Used port number for synchronizing with the receiver." << std::endl;     
00134         std::cout << "-startKey=[x]........... The lowest key you want to use (default=36)." << std::endl;
00135         std::cout << "-receiverIp=[x]......... IP Address of the receiving computer." << std::endl;     
00136         std::cout << "-threshold=[x].......... Duration of chord window in miliseconds." << std::endl;
00137         std::cout << "-help................... Display this help text." << std::endl << std::endl;
00138 }
00139 
00140 //***************************************************************
00141 void Netmusic::receiver_print_help_msg()
00142 {
00143         std::cout << std::endl << DELIMITER << std::endl;
00144         std::cout << "Netmusic " << VERSION << " - 'Receiver-HELP'" << std::endl << std::endl;
00145         std::cout << "-portSync=[x]........... Used port number for synchronization." << std::endl;
00146         std::cout << "-pgmChange=[x].......... MIDI Instrument number for playback." << std::endl;
00147         std::cout << "-velocity=[x]........... MIDI velocity for playback." << std::endl;
00148         std::cout << "-help................... Display this help text." << std::endl << std::endl;
00149 }
00150 
00151 //***************************************************************
00152 void Netmusic::sender_check_args(int argc, char** argv, 
00153         int* mini,
00154         int* midiPort,int* miniPort, int* syncPort,
00155         int* startKey, int* threshold, 
00156         char* receiverIp)
00157 {
00158         for(int i=1;i<argc;i++)
00159         {
00160                 // midi
00161                 if(strncmp(argv[i], "-midi", 5)==0)
00162                 {
00163                         *mini = 0;
00164                 }
00165                 
00166                 // midiPort
00167                 else if(strncmp(argv[i], "-portMidi=", 10)==0)
00168                 {
00169                         strtok(argv[i], "=");
00170                         *midiPort = atoi(strtok(NULL, " "));
00171                         if((*midiPort) < 0)
00172                                 Netmusic::check_error(-1, "Error setting portMidi");
00173                 }
00174                 
00175                 // miniPort
00176                 else if(strncmp(argv[i], "-portMini=", 10)==0)
00177                 {
00178                         strtok(argv[i], "=");
00179                         *miniPort = atoi(strtok(NULL, " "));
00180                         if((*miniPort) < 0)
00181                                 Netmusic::check_error(-1, "Error setting portMini");
00182                 }
00183                 
00184                 // miniPort
00185                 else if(strncmp(argv[i], "-portSync=", 10)==0)
00186                 {
00187                         strtok(argv[i], "=");
00188                         *syncPort = atoi(strtok(NULL, " "));
00189                         if((*syncPort) < 0)
00190                                 Netmusic::check_error(-1, "Error setting portSync");
00191                 }
00192 
00193                 // startKey
00194                 else if(strncmp(argv[i], "-startKey=", 10)==0)
00195                 {
00196                         strtok(argv[i], "=");
00197                         *startKey = atoi(strtok(NULL, " "));
00198                         if((*startKey)<0)
00199                                 Netmusic::check_error(-1, "Error setting startKey");
00200                 }
00201                 
00202                 // receiverIp
00203                 else if(strncmp(argv[i], "-receiverIp=", 12)==0)
00204                 {
00205                         strtok(argv[i], "=");
00206                         char* tmp = strtok(NULL, " ");
00207                         std::cout << &tmp << std::endl;
00208                         strcpy(receiverIp, tmp);
00209                 }
00210 
00211                 // threshold
00212                 else if(strncmp(argv[i], "-threshold=", 11)==0)
00213                 {
00214                         strtok(argv[i], "=");
00215                         *threshold = atoi(strtok(NULL, " "));
00216                         if((*threshold) < 0)
00217                                 Netmusic::check_error(-1, "Error setting threshold");
00218                 }
00219 
00220                 // help
00221                 else if(strncmp(argv[i], "-help", 6)==0)
00222                 {
00223                         sender_print_help_msg();
00224                         _exit(0);
00225                 }
00226                 
00227                 // unrecognized option
00228                 else
00229                 {
00230                         std::cout << "Unrecognized option: " << argv[i] << std::endl;
00231                         sender_print_help_msg();
00232                         _exit(0);
00233                 }
00234         }
00235         
00236         // check if receiverIp is set 
00237         if(receiverIp == NULL)
00238         {
00239                 std::cout << "You must set the -receiverIp." << std::endl;
00240                 _exit(0);
00241         }
00242 }
00243 
00244 //***************************************************************
00245 void Netmusic::receiver_check_args(int argc, char** argv, 
00246                 int* syncPort, 
00247                 int* pgmChange, int* velocity)
00248 {
00249         
00250         for(int i=1;i<argc;i++)
00251         {
00252                 // sync Port
00253                 if(strncmp(argv[i], "-portSync=", 10)==0)
00254                 {
00255                         strtok(argv[i], "=");
00256                         *syncPort = atoi(strtok(NULL, " "));
00257                         if((*syncPort) < 0)
00258                                 Netmusic::check_error(-1, "Error setting portSync");
00259                 }
00260                 
00261                 // help
00262                 else if(strncmp(argv[i], "-help", 6)==0)
00263                 {
00264                         receiver_print_help_msg();
00265                         _exit(0);
00266                 }               
00267                 
00268                 // pgmChange option
00269                 else if(strncmp(argv[i], "-pgmChange=", 11)==0)
00270                 {
00271                   strtok(argv[i], "=");
00272                   *pgmChange = atoi(strtok(NULL, " "));
00273                   if((*pgmChange) < 0 || (*pgmChange) > 127)
00274                     Netmusic::check_error(-1, "Error setting pgmChange.");
00275                 }
00276 
00277                 // velocity
00278                 else if(strncmp(argv[i], "-velocity=", 10)==0)
00279                 {
00280                         strtok(argv[i], "=");
00281                         *velocity = atoi(strtok(NULL, " "));
00282                         if((*velocity) < 0 || (*velocity) > 127)
00283                                 Netmusic::check_error(-1, "Error setting velocity.");
00284                 }
00285                 
00286                 // unrecognized option
00287                 else
00288                 {
00289                         std::cout << "Unrecognized option: " << argv[i] << std::endl;
00290                         receiver_print_help_msg();
00291                         _exit(0);
00292                 }
00293         }
00294 }
00295 
00296 //***************************************************************
00297 void Netmusic::sender_print_param(
00298         int mini,
00299         int midiPort,int miniPort, int syncPort,
00300         int startKey, int threshold, 
00301         char* receiverIp)
00302 {
00303         std::cout << std::endl;
00304         
00305         // mini
00306         std::cout << "mini..................... ";
00307         if(mini==1)
00308                 std::cout << "yes" << std::endl;
00309         else 
00310                 std::cout << "no" << std::endl;
00311         // socket ports
00312         std::cout << "syncPort................. " << syncPort << std::endl;
00313         if(mini==1)
00314                 std::cout << "miniPort................. " << miniPort << std::endl;
00315         else if(mini==0)
00316                 std::cout << "midiPort................. " << midiPort << std::endl;
00317         // startKey
00318         if(mini==1)
00319                 std::cout << "startKey................. " << startKey << std::endl;
00320         // receiverIp
00321         std::cout << "IP Address Receiver...... " << receiverIp << std::endl;
00322         // threshold
00323         if(mini==1)
00324                 std::cout << "Threshold................ " << threshold << std::endl
00325                           << std::endl;
00326 }
00327 
00328 //***************************************************************
00329 void Netmusic::receiver_print_param(
00330         int mini,
00331         int midiPort,int miniPort, int syncPort,
00332         int startKey, 
00333         int pgmChange, int velocity)
00334 {       
00335         std::cout << std::endl;
00336         
00337         // mini
00338         std::cout << "mini..................... ";
00339         if(mini==1)
00340                 std::cout << "yes" << std::endl;
00341         else 
00342                 std::cout << "no" << std::endl;
00343         // socket ports
00344         std::cout << "syncPort................. " << syncPort << std::endl;
00345         if(mini==1)
00346                 std::cout << "miniPort................. " << miniPort << std::endl;
00347         else if(mini==0)
00348                 std::cout << "midiPort................. " << midiPort << std::endl;
00349         // pgmChange
00350         std::cout << "pgmChange instrument..... " << pgmChange << std::endl;
00351         // velocity
00352         std::cout << "velocity................. " << velocity << std::endl;
00353         // startKey
00354         if(mini==1)
00355                 std::cout << "startKey................. " << startKey << std::endl
00356                           << std::endl;
00357 }               

Generated on Mon Jun 13 22:06:59 2005 for Netmusic by  doxygen 1.4.3