Blame view

orchestre/analyse_midi_keyboard.c 2.24 KB
346cc2f5   aarnaude   ajout code vivian
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  #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 24
  
  static snd_seq_t *seq;
  
  
  
  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); 
  
  }
  
  static 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");
  }
  
  static 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");
  }
  
  static 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");
  			      
  }
  
  void action(snd_seq_event_t *event){
  	
  		if (event->type == SND_SEQ_EVENT_NOTEON) 
  			{
  				printf("note :  %d , velocite : %d\n",event->data.note.note, event->data.note.velocity);
  			}
  		if (event->type == SND_SEQ_EVENT_NOTEOFF) 
  			{
  				printf("noteOFF : %2d , %d velocite : %d\n", event->data.note.channel, event->data.note.note, event->data.note.velocity);
  			}
  		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);
  			}
  
  }
  
  int main(){
   init_seq();
   struct pollfd *pfds;
   int npfds, err;
   create_port();
   connect_port();
  
  	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)
  			  {
  				action(event);
  			  }
  		} while (err > 0);
  		fflush(stdout);
     }
  
    
    
    snd_seq_close(seq);
  
  
  	recup_num();	
   
    return 0;
  }