Skip to contentSkip to conversation

obtaining timestamps from sequencer events

DISCUSSION Michael Geis 2 participants 4 messages Dec 21, 2004 – Dec 22, 2004

lore
Share & tools
lore
#0 lore raw

Hello.
I wanted to add recording capability to my module to have an additional way of "importing"
musical scores. One of Dr. Mathias Nagorni's examples shows exactly how to retrieve events from an external device. I get stuck, though, when I want to retrieve timestamps from those events. Following is a minimal program that sets up a connection to an external keyboard (72:0 in my case) and polls events. The problem lies in the fact that the time of of the incoming events is always zero.

#include <alsa/asoundlib.h>
#include <iostream>
using namespace std;

 int  main() {
    snd_seq_t* seq; 
    unsigned int client_type=SND_SEQ_OPEN_INPUT;  
    snd_seq_open(&seq,"default", client_type, 0);
unsigned int capability=SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE;
    unsigned int port_type=SND_SEQ_PORT_TYPE_APPLICATION;
    int port_id=snd_seq_create_simple_port(seq,"writable port",capability,port_type);
    //get a queue
    //are those necessary/required with us being on the receiving end? 
    int queue = snd_seq_alloc_queue(seq);       
    snd_seq_start_queue(seq,queue,NULL);
    //fill in source and destination
    snd_seq_addr_t sender, dest;
    sender.client = 72;
    sender.port =  0;
    dest.client = snd_seq_client_id(seq); 
    dest.port = port_id;
    //add them to the subscription
    snd_seq_port_subscribe_t *subs;
    snd_seq_port_subscribe_alloca(&subs);
    snd_seq_port_subscribe_set_sender(subs, &sender);
    snd_seq_port_subscribe_set_dest(subs, &dest);
    //add the queue to the subscription
    snd_seq_port_subscribe_set_queue(subs, 1);
    snd_seq_port_subscribe_set_time_update(subs, 1);
    snd_seq_port_subscribe_set_time_real(subs, 0);
    
    //make the subscription
    snd_seq_subscribe_port(seq, subs);

    //open poll descriptor
    struct pollfd* pfd = (struct pollfd *)alloca(sizeof(struct pollfd));
    snd_seq_poll_descriptors(seq, pfd, 1, POLLIN);
 
    while (1) {
      int success=poll(pfd, 1, 100000);
      if (success > 0) {
        snd_seq_event_t *ev;
        do {          
   snd_seq_event_input(seq, &ev);    
   if(snd_seq_ev_is_real(ev)) cout<<"event at tick time "<<ev->time.tick<<endl;
   if(snd_seq_ev_is_tick(ev)) cout<<"event at real time"<<ev>time.time.tv_sec<<endl;   
   snd_seq_free_event(ev);
          //input pending with fetch sequencer =0;
        } while (snd_seq_event_input_pending(seq, 0) > 0);
      }  
    }
     free(&pfd);
     return 0;

}

What am I missing? Does the queue setup work differently?
A second problem, which is related to playing MIDI is the following: I would like to empty
the client pool after with snd_seq_drain_output after I have filled it to a certain fraction.
What fraction is not really important, I just dont want to hardcode any number that might actually lie below the pool capacity an a bad day.
So I gathered the ALSA function macros for obtaining pools info. When I call the function, I get 0 for all both size and space remaining of both the input and output pool. Can someone advise me on what needs to be done differently?

 int GetClientPoolSize(snd_seq_t* seq) {
    snd_seq_client_pool_t * pool;
    int success=snd_seq_client_pool_malloc(&pool);
    cout<<"did we allocate a pool.\n"<<success<<endl;
    success=snd_seq_get_client_pool(seq,pool);
    cout<<"did we find a pool.\n"<<success<<endl;
    size_t size=snd_seq_client_pool_get_output_room(pool);
    size_t in=snd_seq_client_pool_get_input_pool(pool);
    cout<<"input pool size:"<<in<<endl;
    size_t in_free=snd_seq_client_pool_get_input_free(pool);
    cout<<"input pool free:"<<in_free<<endl;
    size_t out_free=snd_seq_client_pool_get_output_free(pool);
    cout<<"output pool free:"<<out_free<<endl;
    snd_seq_client_pool_free(pool);
    return size;
} 

Merci beaucoup in advance.
Regards,
Michael

---------------------------------
Do you Yahoo!?
Dress up your holiday email, Hollywood style. Learn more.

#1 lore raw
Quoted 7 lines · Michael Geis

Hello. I wanted to add recording capability to my module to have an additional way of "importing" musical scores. One of Dr. Mathias Nagorni's examples shows exactly how to retrieve events from an external device. I get stuck, though, when I want to retrieve timestamps from those events. Following is a minimal program that sets up a connection to an external keyboard (72:0 in my case) and polls events. The problem lies in the fact that the time of of the incoming events is always zero.

You must set what kind of timestamp you need. See:

snd_seq_port_info_set_timestamping()
snd_seq_port_info_set_timestamp_real()
snd_seq_ev_timestamp_type()
snd_seq_ev_is_tick()
snd_seq_ev_is_real()

Jaroslav

-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SUSE Labs

-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now.
http://productguide.itmanagersjournal.com/

#2 lore raw

Thanks Jaroslav. I played around with those functions. I now set up the port manually.
I also check the queue time at very incoming event. Still, after setting a time stamp
for my port, all timestamps remain 0.
I gather that the queue is running properly, since omitting snd_seq_start_queue keeps all
events from arriving and polling works once snd_seq_start_queue is called.
The time functions snd_seq_port_info_set_timestamping() and snd_seq_port_info_set_timestamp_real() get arguments of 1 to switch on the time_stamping and making it real time. The events are realtime, so I am not trying to cout events of the wrong kind.
Also, there are no more functions relating to setting up a timestamp for a port than those I used, so I cant really have missed anything there. My code now look slike this:

#include <alsa/asoundlib.h>
#include <iostream>
using namespace std;

int  main() {
    snd_seq_t* seq;     
    snd_seq_open(&seq,"default",SND_SEQ_OPEN_INPUT, 0);
    snd_seq_port_info_t * port_info;
    snd_seq_port_info_malloc(&port_info);
    int port_id=0;
    snd_seq_port_info_set_client(port_info,snd_seq_client_id(seq) );
    snd_seq_port_info_set_port(port_info,port_id);
    snd_seq_port_info_set_name(port_info, "Hallo Welt");
    snd_seq_port_info_set_capability(port_info,SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE);
    snd_seq_port_info_set_type(port_info,SND_SEQ_PORT_TYPE_APPLICATION);
  
    snd_seq_port_info_set_timestamping(port_info, 1);
    snd_seq_port_info_set_timestamp_real(port_info,1);
    int queue = snd_seq_alloc_queue(seq);       
    snd_seq_port_info_set_timestamp_queue(port_info, queue);
    snd_seq_create_port(seq, port_info);
    snd_seq_port_info_free(port_info);
   
    //fill in source and destination
    snd_seq_addr_t sender, dest;
    sender.client = 72;
    sender.port =  0;
    dest.client = snd_seq_client_id(seq); 
    dest.port =port_id;
    //add them to the subscription
    snd_seq_port_subscribe_t *subs;
    snd_seq_port_subscribe_alloca(&subs);
    snd_seq_port_subscribe_set_sender(subs, &sender);
    snd_seq_port_subscribe_set_dest(subs, &dest);
    //add the queue to the subscription
    snd_seq_port_subscribe_set_queue(subs, 1);
    snd_seq_port_subscribe_set_time_update(subs, 1);
    snd_seq_port_subscribe_set_time_real(subs, 1);
    
    //make the subscription
    snd_seq_subscribe_port(seq, subs);
    snd_seq_start_queue(seq,queue,NULL);
    //get teh queue time before polling events
    snd_seq_queue_status_t * queue_status;
    snd_seq_queue_status_malloc(&queue_status);
    snd_seq_get_queue_status(seq, queue, queue_status);
    const snd_seq_real_time_t* time=snd_seq_queue_status_get_real_time(queue_status); 
    cout<<"queue time at start"<<time->tv_sec<<endl;
    //open poll descriptor
    struct pollfd* pfd = (struct pollfd *)alloca(sizeof(struct pollfd));
    snd_seq_poll_descriptors(seq, pfd, 1, POLLIN);
    while (1) {
      int success=poll(pfd, 1, 100000);
      if (success > 0) {
        snd_seq_event_t *ev;
        do {          
   snd_seq_event_input(seq, &ev);   
   snd_seq_get_queue_status(seq, queue, queue_status);
   const snd_seq_real_time_t* time=snd_seq_queue_status_get_real_time(queue_status); 
          cout<<"received event at queue time: "<<time->tv_sec<<endl;
   if(snd_seq_ev_is_real(ev)) cout<<"received event with real time stamp: "<<ev->time.tick<<endl;
   switch (ev->type) {
      case SND_SEQ_EVENT_NOTEOFF: 
    cout<<"NoteOff"<<endl;
    break;        
      case SND_SEQ_EVENT_NOTEON:
                  cout<<"NoteOn"<<endl;
                  break;        
   }
   snd_seq_free_event(ev);
          //input pending with fetch sequencer =0;
        } while (snd_seq_event_input_pending(seq, 0) > 0);
      }  
    }
     snd_seq_queue_status_free(queue_status);
     free(&pfd);
     return 0;

}

Any further ideas?
Thanks very much,

Michael

---------------------------------
Do you Yahoo!?
Send holiday email and support a worthy cause. Do good.

#3 lore raw
Quoted 3 lines · Michael Geis

Thanks Jaroslav. I played around with those functions. I now set up the port manually. I also check the queue time at very incoming event. Still, after setting a time stamp for my port, all timestamps remain 0.

You must also start the timer for given queue. See snd_seq_start_queue() and event_decoder_start_timer() function in alsa-lib/test/seq-decoder.c . I also mystified you a bit, because you need to use snd_seq_port_subscribe_set_time_update() and snd_seq_port_subscribe_set_time_real() functions rather than setting capabilities of your allocated port. I've not used the sequencer stuff for long time. The seq-decoder.c source will help you with all things.

Jaroslav

-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SUSE Labs

-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now.
http://productguide.itmanagersjournal.com/