analyse_midi_keyboard.c 2.44 KB
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <getopt.h>
#include <sys/poll.h>
#include <alsa/asoundlib.h>



//#define NUM_PORT 20


static snd_seq_t *seq;
int NUM_PORT;
typedef struct {
	int type;
	int note;
	int velocity;	
} Evenement;

void recup_num(){
	FILE* fd;
	//int NUM_PORT;
	fd=fopen("num_count2.txt","r");
	fscanf(fd, "%d", &NUM_PORT);
	//printf("num = %d \n", NUM_PORT);
	fclose(fd); 

}

void init_seq(void)
{
	int err;

	/* open sequencer */
	err = snd_seq_open(&seq, "default", SND_SEQ_OPEN_DUPLEX, 0);
	if (err<0) printf("open sequencer");

	/* set our client's name */
	err = snd_seq_set_client_name(seq, "analyseurmidi");
	if (err<0) printf("set client name");
}

void create_port(void)
{
	int err;

	err = snd_seq_create_simple_port(seq, "analyseurmidi",
			SND_SEQ_PORT_CAP_WRITE |
			SND_SEQ_PORT_CAP_SUBS_WRITE,
			SND_SEQ_PORT_TYPE_MIDI_GENERIC |
			SND_SEQ_PORT_TYPE_APPLICATION);
	if (err<0)
		printf("Port non créé \n");
}

void connect_port(void)
{
	int err;
	err = snd_seq_connect_from(seq, 0, NUM_PORT, 0);
	if (err < 0)
		printf("Impossible de se connecter au port \n");

}

Evenement action(snd_seq_event_t *event){
	Evenement k;
	if (event->type == SND_SEQ_EVENT_NOTEON) 
	{
		k.type=0;
		k.note=event->data.note.note;
		k.velocity=event->data.note.velocity;
		//printf("note :  %d , velocite : %d\n",event->data.note.note, event->data.note.velocity);
		return k;
	}
	if (event->type == SND_SEQ_EVENT_NOTEOFF) 
	{
		k.type=1;
		k.note=event->data.note.note;
		k.velocity=event->data.note.velocity;
		//printf("noteOFF : %2d , %d velocite : %d\n", event->data.note.channel, event->data.note.note, event->data.note.velocity);
		return k;
	}
/*	if (event->type == SND_SEQ_EVENT_CONTROLLER)
	{
		printf("programme : %2d, %d %d\n", event->data.control.channel, event->data.control.param, event->data.control.value);
	}*/
	return k;
}

Evenement wait_event(){
	struct pollfd *pfds;
	int npfds, err;
	npfds = snd_seq_poll_descriptors_count(seq, POLLIN);
	pfds = alloca(sizeof(*pfds) * npfds);
	for (;;) {
		snd_seq_poll_descriptors(seq, pfds, npfds, POLLIN);
		if (poll(pfds, npfds, -1) < 0)
			break;
		do {
			snd_seq_event_t *event;
			err = snd_seq_event_input(seq, &event);
			if (err < 0)
				break;
			if (event)
			{
				return action(event);
			}
		} while (err > 0);
		fflush(stdout);
	}
	

}


void close_sequencer(){
	snd_seq_close(seq);
}