Exemple #1
0
int MidiInit(void *arg)
{
	int status;
	int mode = SND_RAWMIDI_SYNC;
	char *portname = arg;
	midi.midiin = NULL;
	midi.midiout = NULL;
	char resetPad[3] = {0xB0, 0x00, 0x00};
	char setDrumMapping[3] = {0xB0, 0x00, 0x02};	// configure drum rack layout

	// open in/out
	if ((status = snd_rawmidi_open(&(midi.midiin), &(midi.midiout), portname, mode)) < 0) {
		errormessage("Problem opening MIDI input: %s", snd_strerror(status));
		exit(1);
	}

	// reset launchpad (replace with generic reset)
	if ((status = snd_rawmidi_write(midi.midiout, resetPad, 3)) < 0) {
		errormessage("Problem writing to MIDI output: %s", snd_strerror(status));
		exit(1);
	}

	// reset launchpad (replace with generic reset)
	if ((status = snd_rawmidi_write(midi.midiout, setDrumMapping, 3)) < 0) {
		errormessage("Problem writing to MIDI output: %s", snd_strerror(status));
		exit(1);
	}

	return NULL;
}
Exemple #2
0
int writepattern(snd_rawmidi_t *handle_out, unsigned char *obuf)
{
	int patsize, i;

	patsize = 0;
	for (i = 0; i < 15; i++) {
		obuf[patsize++] = 0x90 + i;
		obuf[patsize++] = 0x40;
		obuf[patsize++] = 0x3f;
		obuf[patsize++] = 0xb0 + i;
		obuf[patsize++] = 0x2e;
		obuf[patsize++] = 0x7a;
		obuf[patsize++] = 0x80 + i;
		obuf[patsize++] = 0x23;
		obuf[patsize++] = 0x24;
		obuf[patsize++] = 0xf0;
		obuf[patsize++] = i;
		obuf[patsize++] = 0xf7;
	}
	i = snd_rawmidi_write(handle_out, obuf, patsize);
	if (i != patsize) {
		printf("Written only %i bytes from %i bytes\n", i, patsize);
		exit(EXIT_FAILURE);
	}
	return patsize;
}
Exemple #3
0
bool Dicer_control_process::set_button_state(const dicer_t              &dicer_index,
                                             const dicer_mode_t         &mode,
                                             const dicer_button_t       &button_index,
                                             const dicer_button_state_t &state)
{
    if (this->is_open == true)
    {
        unsigned char midi_buf[3] = {0x0, 0x0, 0x0};

        // Build the MIDI command which changes the state of a button on the Dicer.
        this->build_midi_buffer(dicer_index, mode, button_index, state, midi_buf);
    #if 0
        cout << "write"
             << "\tl/r+mode:"   << hex << "0x"<< static_cast<unsigned int>(static_cast<unsigned char>(midi_buf[0]))
             << "\tbutton:"     << hex << "0x"<< static_cast<unsigned int>(static_cast<unsigned char>(midi_buf[1]))
             << "\tlit+color::" << hex << "0x"<< static_cast<unsigned int>(static_cast<unsigned char>(midi_buf[2]))
             << endl;
    #endif
        // Send the MIDI command to the Dicer.
#ifdef WIN32
        // TODO: add Dicer support for Windows.
        return false;
#else
        int err = 0;
        if ((err = snd_rawmidi_write(this->midi_out, midi_buf, sizeof(midi_buf))) < 0)
        {
            qCWarning(DS_DICER) << "can not set button state to Dicer: " << snd_strerror(err);
            return false;
        }
#endif
    }

    return true;
}
Exemple #4
0
int mus_midi_write(int line, unsigned char *buffer, int bytes)
{
  if ((line < 0) || (line >= midis)) return(-1);
  if (midi_directions[line] == MIDI_WRITE)
    return(snd_rawmidi_write(midi_lines[line], buffer, bytes));
#ifdef NO_SNDLIB
  fprintf(stderr, "can't write to input %s\n", midi_names[line]);
#else
  return(mus_error(MUS_MIDI_WRITE_ERROR, "can't write to input %s", midi_names[line]));
#endif
  return(-1);
}
Exemple #5
0
void *midifunction(void *arg)
{
	// this is the parameter passed via last argument of pthread_create():
	struct midinodes *midi = (struct midinodes*)arg;
	char buffer[1];
	int  count = 0;
	int status;
	int byteNum = 0;
	char outBuffer[3] = {0x90, 0x00, 15};
	note currentNote;

	printf("midi thread start\n");

	while (1) {
		if ((status = snd_rawmidi_read(midi->midiin, buffer, 1)) < 0) {
			errormessage("Problem reading MIDI input: %s", snd_strerror(status));
		}
		count++;
		// determine part of message
		if(byteNum == 0)
		{
			// note
			outBuffer[1] = currentNote.key = buffer[0];
			byteNum = 1;
		}
		else
		{
			if(buffer[0] > 0)
				outBuffer[2] = currentNote.vel = NOTE_ON;
			else
				outBuffer[2] = currentNote.vel = NOTE_OFF;
			byteNum = 0;
		}
		// after velocity, write
		if(byteNum == 0)
		{
			if ((status = snd_rawmidi_write(midi->midiout, outBuffer, 3)) < 0) {
				errormessage("Problem writing to MIDI output: %s", snd_strerror(status));
				exit(1);
			}
			printf("Note: %d %d %d \n", outBuffer[0], currentNote.key, currentNote.vel);
			SynthNoteStart(currentNote);
		}
		fflush(stdout);
	}

	return NULL;
}
Exemple #6
0
static int midiwrite(unsigned char *buffer, int buflen) {
    int status;

    if(MidiOut==NULL) {
	midiexcept("midi not opened", 0);
	return 0;
    }

    status = snd_rawmidi_write(MidiOut, buffer, buflen);

    if(status<0) {
	midiexcept("error writing midi: %s", status);
	return 0;
    }
    return 1;
}
Exemple #7
0
int main(void) {


	snd_rawmidi_open(&midi_in, &midi_out, "virtual", SND_RAWMIDI_NONBLOCK);
	//snd_rawmidi_open(&midi_in, &midi_out, "virtual", 0);


	if (ftdi_init( &ftdi )) {
		fprintf(stderr,  "usb - init error !\n");
		return 1;
	}

	if (ftdi_usb_open(&ftdi, 0x0403, 0x6001)) {
		fprintf(stderr,  "usb - open error (cannot find?) !\n");
		fprintf(stderr, "ftdi_usb_open failed, error (%s)\n", ftdi_get_error_string(&ftdi));
		ftdi_deinit( &ftdi );
		return 2;
	}

	if (ftdi_usb_reset( &ftdi )) {
		fprintf(stderr,  "usb - reset error !\n");
		ftdi_usb_close( &ftdi );
		ftdi_deinit( &ftdi );
		return 3;
	}

	ftdi_disable_bitbang( &ftdi );
	ftdi_set_baudrate(&ftdi, BAUD);

	unsigned char buf[BUFFER_SIZE];
	int ret;
	while(1) {
		//FTDI2MIDI
		ret = ftdi_read_data(&ftdi, buf, BUFFER_SIZE);
		if(ret < 0) break;
		if(ret > 0) snd_rawmidi_write(midi_out, buf, BUFFER_SIZE);

		//MIDI2FTDI
		/*
		ret = snd_rawmidi_read(midi_in, buf,BUFFER_SIZE);
		if(ret < 0 && ret != -EAGAIN) break;
		if(ret > 0) ftdi_write_data(&ftdi, buf,BUFFER_SIZE);
		*/
		usleep(LATENCY);
	}
	exit(0);
}
Exemple #8
0
/* alsa_rawmidi_output:
 *   Outputs MIDI data.
 */
static void alsa_rawmidi_output(int data)
{
   int err;

   /* If there are too many errors, just give up.  Otherwise the calling thread
    * can end up consuming CPU time for no reason.  It probably means the user
    * hasn't configured ALSA properly.
    */
   if (alsa_rawmidi_errors > ALSA_RAWMIDI_MAX_ERRORS) {
      return;
   }

   err = snd_rawmidi_write(rawmidi_handle, &data, sizeof(char));
   if (err) {
      alsa_rawmidi_errors++;
      if (alsa_rawmidi_errors == ALSA_RAWMIDI_MAX_ERRORS) {
	  TRACE("al-alsamidi: too many errors, giving up\n");
      }
   }
}
Exemple #9
0
void echomidi(snd_rawmidi_t* midiin, snd_rawmidi_t* midiout) {
	unsigned char readbuffer;	// storage for input MIDI byte stream
	unsigned char buffer[1024]={0};	// storage space for incoming commands
	int dataptr=0;	// writing index in buffer
	int argsLeft=0;	// bytes left to read for a command
	int transpose=6;// transposition for note out
	int newnote;	// temp storage for output note
	int status;	// storage for error codes
	while(true) {
		if ((status=snd_rawmidi_read(midiin, &readbuffer, 1)) < 0) {
			errormessage("Problem reading MIDI input: %s", snd_strerror(status));
		}
		if (readbuffer & 0x80) {// a command byte has arrived
			argsLeft=getArgsExpected(readbuffer);
			dataptr=0;
			buffer[dataptr++]=readbuffer;
		} else {// a data byte has arrived
			if ((dataptr==0) || (argsLeft <=0)) {
				argsLeft=getArgsExpected(buffer[0]);
				dataptr=1;
			}
			buffer[dataptr++]=readbuffer;
			argsLeft--;
		}
		if ((argsLeft==0) && (((buffer[0] & 0xf0)==0x90) || ((buffer[0] & 0xf0)==0x80))) {
			newnote=buffer[1] + transpose;
			if ((newnote > 0) && (newnote < 128)) {
				buffer[1]=(unsigned char)newnote;
				if ((status=snd_rawmidi_write(midiout, buffer, 3)) < 0) {
					errormessage("Problem with MIDI out: %s", snd_strerror(status));
					exit(EXIT_FAILURE);
				} else if (((buffer[0] & 0xf0)==0x90) && ((buffer[2] & 0x0f)!=0)) {
					printf("New note: %d\n", newnote);
				}
			}
		}
		if (dataptr > 1000) {
			dataptr=0;
		}
	}
}
Exemple #10
0
int main(int argc,char** argv)
{
        int i;
        int err;
        int thru=0;
        int verbose = 0;
        char *device_in = NULL;
        char *device_out = NULL;
        char *node_in = NULL;
        char *node_out = NULL;
        
        int fd_in = -1,fd_out = -1;
        snd_rawmidi_t *handle_in = 0,*handle_out = 0;
        
        if (argc==1) {
                usage();
                exit(0);
        }
        
        for (i = 1 ; i<argc ; i++) {
                if (argv[i][0]=='-') {
                        switch (argv[i][1]) {
                                case 'h':
                                        usage();
                                        break;
                                case 'v':
                                        verbose = 1;
                                        break;
                                case 't':
                                        thru = 1;
                                        break;
                                case 'i':
                                        if (i + 1 < argc)
                                                device_in = argv[++i];
                                        break;
                                case 'I':
                                        if (i + 1 < argc)
                                                node_in = argv[++i];
                                        break;
                                case 'o':
                                        if (i + 1 < argc)
                                                device_out = argv[++i];
                                        break;
                                case 'O':
                                        if (i + 1 < argc)
                                                node_out = argv[++i];
                                        break;
                        }                       
                }
        }
        if (verbose) {
                fprintf(stderr,"Using: \n");
                fprintf(stderr,"Input: ");
                if (device_in) {
                        fprintf(stderr,"device %s\n",device_in);
                }else if (node_in){
                        fprintf(stderr,"%s\n",node_in); 
                }else{
                        fprintf(stderr,"NONE\n");
                }
                fprintf(stderr,"Output: ");
                if (device_out) {
                        fprintf(stderr,"device %s\n",device_out);
                }else if (node_out){
                        fprintf(stderr,"%s\n",node_out);                
                }else{
                        fprintf(stderr,"NONE\n");
                }
        }
        
        if (device_in) {
                err = snd_rawmidi_open(&handle_in,NULL,device_in,0);    
                if (err) {
                        fprintf(stderr,"snd_rawmidi_open %s failed: %d\n",device_in,err);
                }
        }
        if (node_in && (!node_out || strcmp(node_out,node_in))) {
                fd_in = open(node_in,O_RDONLY);
                if (fd_in<0) {
                        fprintf(stderr,"open %s for input failed\n",node_in);
                }       
        }
        signal(SIGINT,sighandler);
        if (device_out) {
                err = snd_rawmidi_open(NULL,&handle_out,device_out,0);
                if (err) {
                        fprintf(stderr,"snd_rawmidi_open %s failed: %d\n",device_out,err);
                }
        }
        if (node_out && (!node_in || strcmp(node_out,node_in))) {
                fd_out = open(node_out,O_WRONLY);               
                if (fd_out<0) {
                        fprintf(stderr,"open %s for output failed\n",node_out);
                }       
        }
        if (node_in && node_out && strcmp(node_out,node_in)==0) {
                fd_in = fd_out = open(node_out,O_RDWR);         
                if (fd_out<0) {
                        fprintf(stderr,"open %s for input and output failed\n",node_out);
                }               
        }
        if (!thru) {
                if (handle_in || fd_in!=-1) {
                        fprintf(stderr,"Read midi in\n");
                        fprintf(stderr,"Press ctrl-c to stop\n");
                }
                if (handle_in) {
                        unsigned char ch;
                        while (!stop) {
                                snd_rawmidi_read(handle_in,&ch,1);
                                if (verbose) {
                                        fprintf(stderr,"read %02x\n",ch);
                                }
                        }
                }
                if (fd_in!=-1) {
                        unsigned char ch;
                        while (!stop) {
                                read(fd_in,&ch,1);
                                if (verbose) {
                                        fprintf(stderr,"read %02x\n",ch);
                                }
                        }       
                }
                if (handle_out || fd_out!=-1) {
                        fprintf(stderr,"Writing note on / note off\n");
                }
                if (handle_out) {
                        unsigned char ch;
                        ch=0x90; snd_rawmidi_write(handle_out,&ch,1);
                        ch=60;   snd_rawmidi_write(handle_out,&ch,1);
                        ch=100;  snd_rawmidi_write(handle_out,&ch,1);
                        snd_rawmidi_drain(handle_out);
                        sleep(1);
                        ch=0x90; snd_rawmidi_write(handle_out,&ch,1);
                        ch=60;   snd_rawmidi_write(handle_out,&ch,1);
                        ch=0;    snd_rawmidi_write(handle_out,&ch,1);
                        snd_rawmidi_drain(handle_out); 
                }
                if (fd_out!=-1) {
                        unsigned char ch;
                        ch=0x90; write(fd_out,&ch,1);
                        ch=60;   write(fd_out,&ch,1);
                        ch=100;  write(fd_out,&ch,1);
                        sleep(1);
                        ch=0x90; write(fd_out,&ch,1);
                        ch=60;   write(fd_out,&ch,1);
                        ch=0;    write(fd_out,&ch,1);
                }
        } else {
                if ((handle_in || fd_in!=-1) && (handle_out || fd_out!=-1)) {
                        if (verbose) {
                                fprintf(stderr,"Testing midi thru in\n");
                        }
                        while (!stop) {
                                unsigned char ch;
                        
                                if (handle_in) {
                                        snd_rawmidi_read(handle_in,&ch,1);
                                }
                                if (fd_in!=-1) {
                                        read(fd_in,&ch,1);
                                }       
                                if (verbose) {
                                        fprintf(stderr,"thru: %02x\n",ch);
                                }
                                if (handle_out) {
                                        snd_rawmidi_write(handle_out,&ch,1);
                                        snd_rawmidi_drain(handle_out); 
                                }
                                if (fd_out!=-1) {
                                        write(fd_out,&ch,1);
                                }
                        }
                }else{
                                fprintf(stderr,"Testing midi thru needs both input and output\n");              
                                exit(-1);
                }
        }
        if (verbose) {
                fprintf(stderr,"Closing\n");
        }
        
        if (handle_in) {
                snd_rawmidi_drain(handle_in); 
                snd_rawmidi_close(handle_in);   
        }
        if (handle_out) {
                snd_rawmidi_drain(handle_out); 
                snd_rawmidi_close(handle_out);  
        }
        if (fd_in!=-1) {
                close(fd_in);
        }
        if (fd_out!=-1) {
                close(fd_out);
        }
        return 0;
}
Exemple #11
0
int main(int argc, char **argv) {
        int inputf, e, err;
	int sock;
        struct input_event buffer;
	int npfds;
	struct pollfd *pfds;
	struct sockaddr_in si_me, si_other;
	int len, slen = sizeof(si_other);
	int optval = 1;

        if(argc < 2) {
                fprintf(stderr, "%s <device>\n", argv[0]);
                exit(-1);
        }
        e = snd_rawmidi_open(&input, &output, argv[1], SND_RAWMIDI_NONBLOCK);
	if (e == -1) {
	        fprintf(stderr, "snd:rawmidi:open-%X\n", e);
		return 1;
	}

	if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
		perror("udp socket");
		return 1;
	}

	memset((void *) &si_me, 0, sizeof(si_me));
	si_me.sin_family = AF_INET;
	si_me.sin_port = htons(UDPMIDI_PORT);
	si_me.sin_addr.s_addr = htonl(INADDR_ANY);

	memset((void *) &si_other, 0, sizeof(si_me));
	si_other.sin_family = AF_INET;
	si_other.sin_port = htons(UDPMIDI_PORT);
	si_other.sin_addr.s_addr = htonl(INADDR_BROADCAST);

	if (bind(sock, (const struct sockaddr*)&si_me, sizeof(si_me)) == -1) {
		perror("udp port");
		close(sock);
		return 1;
	}

	if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &optval, sizeof (optval)) ==- 1) {
		perror("udp broadcast");
		close(sock);
		return 1;
	}

	npfds = snd_rawmidi_poll_descriptors_count(input) + 1;
	pfds = malloc((npfds + 1) * sizeof(struct pollfd));
	snd_rawmidi_poll_descriptors(input, pfds, npfds);
	pfds[npfds-1].fd = sock;
	pfds[npfds-1].events = POLLRDNORM;

	setvbuf (stdout, NULL, _IONBF, BUFSIZ);

        while(1) {
		int i,rlen, midiin;
		err = poll(pfds, npfds, 200);

		midiin = 0;
		for (i = 0; i < npfds; ++i) {
			if (pfds[i].revents & POLLIN)
				midiin = 1;
		}
		if (midiin) {
			unsigned char buf[10];
			unsigned char outbuf[12];

			printf("m");
			rlen = snd_rawmidi_read(input, buf, sizeof(buf));
			if (rlen == -EAGAIN)
				continue;
			if (rlen < 0) {
				fprintf(stderr, "Cannot read from port \"%s\": %s", argv[1], snd_strerror(err));
				break;
			}
			// UDP broadcast
			memcpy(outbuf, packet_signature, 2);
			if (rlen > 10) continue;
			memcpy(outbuf + 2, buf, rlen);
			if (sendto(sock, outbuf, 2 + rlen, 0, (struct sockaddr *)&si_other, sizeof(si_other)) == -1) {
				fprintf(stderr, "Error sending broadcast packet.\n");
			}
			memcpy(last_packet, outbuf, rlen + 2);
		}

		if (pfds[npfds-1].revents & POLLRDNORM) {
			int len,slen;
			unsigned char buffer[10];

			slen = sizeof(si_other);
			len = recvfrom(sock, buffer, 10, 0, (struct sockaddr *)&si_other, &slen);
			// Drop our own broadcast packets
			if (memcmp(last_packet, buffer, len) == 0) continue;

			printf("u");
			if (len > 2 && memcmp(buffer, packet_signature, 2) == 0) {
        			int wlen = snd_rawmidi_write(output, buffer + 2, len - 2);
				if (wlen < len - 2) {
					fprintf(stderr, "Midi out: Wrote %d of %d bytes\n", wlen, len - 2);
				}
			}
		}
        }

        return 0;
}
Exemple #12
0
static void midoalsav_write(unsigned char val)
{
    if (!handle_v)
	return;
    snd_rawmidi_write(handle_v, &val, 1);
}
Exemple #13
0
void launchpad_write(char note[3])
{
	snd_rawmidi_write(out, note, 3);
}
Exemple #14
0
static
int do_midi_output(process_midi_t *proc)
{
    int worked = 0;
    output_port_t *port = (output_port_t*) proc->port;

    if (!midi_is_ready(proc))
        return 0;

    // eat events
    while (port->next_event.time <= proc->cur_time) {
        port->todo += port->next_event.size;
        if (jack_ringbuffer_read(port->base.event_ring, (char*)&port->next_event, sizeof(port->next_event))!=sizeof(port->next_event)) {
            port->next_event.time = 0;
            port->next_event.size = 0;
            break;
        } else
            debug_log("midi_out: at %ld got %d bytes for %ld", (long)proc->cur_time, (int)port->next_event.size, (long)port->next_event.time);
    }

    if (port->todo)
        debug_log("midi_out: todo = %d at %ld", (int)port->todo, (long)proc->cur_time);

    // calc next wakeup time
    if (!port->todo && port->next_event.time && port->next_event.time < proc->next_time) {
        proc->next_time = port->next_event.time;
        debug_log("midi_out: next_time = %ld", (long)proc->next_time);
    }

    if (port->todo && port->base.is_ready) {
        // write data
        int size = port->todo;
        int res;
        jack_ringbuffer_data_t vec[2];

        jack_ringbuffer_get_read_vector(port->base.data_ring, vec);
        if (size > vec[0].len) {
            size = vec[0].len;
            assert (size > 0);
        }
        res = snd_rawmidi_write(port->base.rawmidi, vec[0].buf, size);
        if (res > 0) {
            jack_ringbuffer_read_advance(port->base.data_ring, res);
            debug_log("midi_out: written %d bytes to %s", res, port->base.name);
            port->todo -= res;
            worked = 1;
        } else if (res == -EWOULDBLOCK) {
            port->base.is_ready = 0;
            debug_log("midi_out: -EWOULDBLOCK on %s", port->base.name);
            return 1;
        } else {
            error_log("midi_out: writing to port %s failed: %s", port->base.name, snd_strerror(res));
            return 0;
        }
        snd_rawmidi_drain(port->base.rawmidi);
    }

    // update pfds for this port
    if (!midi_update_pfds(proc))
        return 0;

    if (!port->todo) {
        int i;
        if (worked)
            debug_log("midi_out: relaxing on %s", port->base.name);
        for (i=0; i<port->base.npfds; ++i)
            proc->wpfds[i].events &= ~POLLOUT;
    } else {
        int i;
        for (i=0; i<port->base.npfds; ++i)
            proc->wpfds[i].events |= POLLOUT;
    }
    return 1;
}
Exemple #15
0
int main (int argc, char *argv [])
{
    int argn = 1;
    if (argn == argc
    ||  streq (argv [argn], "-h") || streq (argv [argn], "--help")) {
        puts ("midicast [-v] [-p port] [-i interface]");
        puts ("Reads MIDI events from port and sends to Zyre MIDI group");
        puts (" -h, --help: this help");
        puts (" -v, --verbose: trace events as they happen");
        puts (" -p, --port: specify port name, e.g. '-p hw:1,0,0'");
        puts (" -i, --interface: specify WiFi interface, e.g. '-i wlan0'");
        return 0;
    }
    char *midi_port_name = "hw:2,0";
    char *wifi_interface = NULL;
    bool verbose = false;
    while (argn < argc) {
        if (streq (argv [argn], "-p") || streq (argv [argn], "--port"))
            midi_port_name = argv [++argn];
        else
        if (streq (argv [argn], "-i") || streq (argv [argn], "--interface"))
            wifi_interface = argv [++argn];
        else
        if (streq (argv [argn], "-v") || streq (argv [argn], "--verbose"))
            verbose = true;
        argn++;
    }

    snd_rawmidi_t *output;
    int rc = snd_rawmidi_open (NULL, &output, midi_port_name, SND_RAWMIDI_SYNC);
    if (rc < 0) {
        zsys_error ("cannot open port \"%s\": %s", midi_port_name, snd_strerror (rc));
        return 0;
    }
    zsys_info ("forwarding MIDI cast to %s", midi_port_name);

    zyre_t *zyre = zyre_new (NULL);
    if (wifi_interface)
        zyre_set_interface (zyre, wifi_interface);
    zyre_start (zyre);
    zyre_join (zyre, "MIDI");
    zsys_info ("this player is %s", zyre_name (zyre));

    while (!zsys_interrupted) {
        zyre_event_t *event = zyre_event_new (zyre);
        if (!event) {
            printf (" interrupted\n");
            break;
        }
        if (zyre_event_type (event) == ZYRE_EVENT_JOIN)
            zsys_info ("[%s] player joined", zyre_event_peer_name (event));
        else
        if (zyre_event_type (event) == ZYRE_EVENT_LEAVE)
            zsys_info ("[%s] player left", zyre_event_peer_name (event));
        else
        if (zyre_event_type (event) == ZYRE_EVENT_SHOUT) {
            if (streq (zyre_event_group (event), "MIDI")) {
                zframe_t *frame = zmsg_first (zyre_event_msg (event));
                //  Forward the MIDI event
                snd_rawmidi_write (output, zframe_data (frame), zframe_size (frame));

                if (verbose) {
                    printf ("%zd:", zframe_size (frame));
                    int byte_nbr;
                    for (byte_nbr = 0; byte_nbr < zframe_size (frame); byte_nbr++)
                        printf (" %02X", zframe_data (frame) [byte_nbr]);
                    printf ("\n");
                }
            }
        }
        zyre_event_destroy (&event);
    }
    snd_rawmidi_close (output);
    zyre_destroy (&zyre);
	return 0;
}
Exemple #16
0
/**
 *  \param data data to be sent
 *  \param length data length
 *
 *  Sends data to device. This function uses global output variable.
 **/
void send_data(char *data, int length)
{
    snd_rawmidi_write(output, data, length);
}
void MidiAlsaRaw::sendByte( unsigned char c )
{
	snd_rawmidi_write( m_output, &c, sizeof( c ) );
}
Exemple #18
0
int main(int argc, char *argv[])
{
  fd_set rfds; 
  int res;
  int err;
  int version = -1, ioret = -1; 
  unsigned numevs, c;
  unsigned char read_buffer[sizeof(struct input_event)*3]; /* max 3 events per read */
  unsigned char ch;
  unsigned char cCommand[3];
  unsigned char cStates[255];
  struct input_event *currev;
  char device_name[1024];

  char *device_out = NULL;
  device_out = argv[2];

  int fd_in = -1,fd_out = -1;
  snd_rawmidi_t *handle_in = 0,*handle_out = 0;

  memset(&cStates, 0, sizeof(cStates));

  /* struct sigaction sighandler;
  memset(&sighandler, 0, sizeof(sighandler));
  sighandler.sa_handler = catch_signal;
  sigaction(SIGINT, &sighandler, NULL);
  sigaction(SIGQUIT, &sighandler, NULL);

  */

  signal(SIGINT,catch_signal);


  if ( argc < 2 ) { 
    fprintf(stderr, "Device needed\n");
    return -1;
  }

  FD_ZERO(&rfds);
  fd = open(argv[1], O_RDONLY);
  if ( -1 == fd ) {
    fprintf(stderr, "unable to read from mice - please give me a keyboard.\n");
    return -1;
  }

  ioret = ioctl(fd, EVIOCGVERSION, &version);
  ioret = ioctl(fd, EVIOCGNAME(sizeof(device_name)), device_name);
  ioret = ioctl(fd, EVIOCGRAB, &grab);
  if ( -1 == ioret ) {
    perror("ioctl()");
  }
  fprintf(stdout, "ver: %d, ret = %d\n", version, ioret);
  printf("device name is: %s\n", device_name);
  printf("EVIOCGRAB is: %lu\n", EVIOCGRAB);


  if (device_out) {
	err = snd_rawmidi_open(NULL,&handle_out,device_out,0);
    if (err) {
		fprintf(stderr,"snd_rawmidi_open %s failed: %d\n",device_out,err);
    }
  }


  /*
	to read the config file:

    char *option;
    char *value;

	fscanf( "%20[^#=]s=%80[^\n]", &option, &value );


  */


  FD_SET(fd, &rfds);
  while ( 1 ) {
    res = select(fd + 1, &rfds, NULL, NULL, NULL);
    if ( -1 == res && EINTR == errno ) {
      continue;
    }
    if ( -1 == res ) {
      perror("select() failed");
      fprintf(stderr, "failed to select, fd is %d\n", fd);
      return -1;
    }
    if ( FD_ISSET(fd, &rfds) ) {
//      fprintf(stdout, "got some data\n");
      res = read(fd, read_buffer, sizeof(read_buffer));
      if ( -1 == res) {
        fprintf(stderr, "error reading data\n");
        return -1;
      }
//      fprintf(stdout, "got %d bytes\n", res);
      numevs = ( res / sizeof(struct input_event) ); /* get how many input events we got */
//      fprintf(stdout, "got %u events\n", numevs);
      for ( c = 0; c < numevs; c++ ) {
        currev = (struct input_event *)(read_buffer + (sizeof(struct input_event) * c));
//        fprintf(stdout, "event time %ld/%ld\n", currev->time.tv_sec, currev->time.tv_usec);
//        fprintf(stdout, "event type = %hd, code = %hd, value = %d\n", currev->type, currev->code, currev->value);
		if ( 1 == currev->type ) {
			if ( 1 == currev->value ) { // press
				fprintf(stdout, " %hd \n", currev->code);

				// note on
				ch=0x90; 			snd_rawmidi_write(handle_out,&ch,1);
				ch=currev->code;	snd_rawmidi_write(handle_out,&ch,1);
				ch=127;    			snd_rawmidi_write(handle_out,&ch,1);
				// controller up
				ch=0xB0; 			snd_rawmidi_write(handle_out,&ch,1);
				ch=currev->code;	snd_rawmidi_write(handle_out,&ch,1);
				ch=127;    			snd_rawmidi_write(handle_out,&ch,1);

				if ( 1 == cStates[currev->code] ) { // toggle was on already
					cStates[currev->code] = 0;
					// send a note-off
					ch=0x91;			snd_rawmidi_write(handle_out,&ch,1);
					ch=currev->code;	snd_rawmidi_write(handle_out,&ch,1);
					ch=0;    			snd_rawmidi_write(handle_out,&ch,1);				
					// and a controller down
					ch=0xB1;			snd_rawmidi_write(handle_out,&ch,1);
					ch=currev->code;	snd_rawmidi_write(handle_out,&ch,1);
					ch=0;    			snd_rawmidi_write(handle_out,&ch,1);				
				} else { // toggle was off or uninitialised
					cStates[currev->code] = 1;
					// send a note-on
					ch=0x91;			snd_rawmidi_write(handle_out,&ch,1);
					ch=currev->code;	snd_rawmidi_write(handle_out,&ch,1);
					ch=127;    			snd_rawmidi_write(handle_out,&ch,1);				
					// and controller up 
					ch=0xB1;			snd_rawmidi_write(handle_out,&ch,1);
					ch=currev->code;	snd_rawmidi_write(handle_out,&ch,1);
					ch=127;    			snd_rawmidi_write(handle_out,&ch,1);				
				}

				snd_rawmidi_drain(handle_out);

			} else if ( 2 == currev->value ) { // repeat
				fprintf(stdout, "*%hd*\n", currev->code);
			} else if ( 0 == currev->value ) { // release
				fprintf(stdout, "[%hd]\n", currev->code);


				// note off
				ch=0x90; 			snd_rawmidi_write(handle_out,&ch,1);
				ch=currev->code;   	snd_rawmidi_write(handle_out,&ch,1);
				ch=0;  				snd_rawmidi_write(handle_out,&ch,1);
				// controller down
				ch=0xB0; 			snd_rawmidi_write(handle_out,&ch,1);
				ch=currev->code;   	snd_rawmidi_write(handle_out,&ch,1);
				ch=0;  				snd_rawmidi_write(handle_out,&ch,1);

				snd_rawmidi_drain(handle_out);
				// sleep(1);

			} else { // dunno
				fprintf(stdout, "?%hd? - What is value %d?\n", currev->code, currev->value);
			}
		}
      } 
    } else {
      fprintf(stderr, "odd ... no data and we only listen in 1 fd\n");
    }
  }
  return 0;
}