Page 1 of 1

Read .mid file in C

Posted: Mon Jun 01, 2015 4:44 pm
by Babzouille
Hello everyone!

I'd like to be able to write a program which reads .mid files and do something at the right time for each note on and note off (for example printf the note when it has to be played or stopped). I don't need all other information like pitch, sysEx or whatever :P
I thought it would be quite easy, I didn't expect that would be so hard to work on .mid files. I've spent my whole day to understand/search for information/code on the internet but in the end I have nothing. :cry:

I've found some codes/explanations but I can't compile it. here or here or http://www.cap-lore.com/EnglishSuites/code/code.html.

Before someone suggests to google midifile.h, already done and dowloaded, I tried to modify the code without success....

I've seen other examples in C# or C++ but i'd rather write a C program or at least in C++. (I want to run it on a Raspberry Pi :wink: )

Thanks !

Re: Read .mid file in C

Posted: Mon Jun 01, 2015 6:59 pm
by j_e_f_f_g
Here's my own MidiFileIO library. It works on both Linux and Windows. It has lots of profusely commented C and C++ examples. And very detailed docs. (Read MidiFileIO.htm in your browser.)

http://wikisend.com/download/752464/midifileio.zip

Re: Read .mid file in C

Posted: Tue Jun 02, 2015 8:08 am
by Babzouille
:D

Thank you very much for your replies.

I feel so stupid, i didn't even think about a midifile library... And I've never heard of libsmf library while i was searching for examples. :P

I'll see what I can do with libsmf or MidiFileIO !

Thanks guys !

Re: Read .mid file in C

Posted: Tue Jun 02, 2015 11:42 am
by Babzouille
At first, sorry for the double post :mrgreen:
I tried to compile a simple code on Ubuntu and after some researches I manage to do so.
I installed libsmf with sudo apt-get install libsmf-dev.

Here is the code:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <smf.h>
#include <time.h>
 
/**
	get the current time in sec
**/
double get_time_s()
{
struct timeval t;
gettimeofday(&t, NULL);
return (t.tv_sec + (t.tv_usec / 1000000.0));
}


 int main(){
 
		smf_t *smf;
      smf_event_t *event;
      char * string = NULL;
      double start_time=0;
      double current_time=0;
      
      start_time=get_time_s();

      smf = smf_load("file.mid");
      if (smf == NULL) {
              printf("Whoops, something went wrong.");
              return 1;
      }
      
 
      while ((event = smf_get_next_event(smf)) != NULL) {
              if (smf_event_is_metadata(event))
              continue;
              
              //wait for the right time
              do{
              		current_time=get_time_s();
              }    
              while((current_time-start_time)<event->time_seconds);		//sleep(event->time_seconds);
                           														
                           														
              //decode then display the event
              string=smf_event_decode(event);
              printf("%s \n", string);
              free(string);
              //feed_to_midi_output(event->midi_buffer, event->midi_buffer_length);
      }

      smf_delete(smf);
      
 		return 0;
}
When I used: "gcc test.c -o" test, there was a "fatal error: glib.h: No such file or directory".
I found this commad line : "gcc `pkg-config --cflags glib-2.0` foo.c `pkg-config --libs glib-2.0`" here

Now I use "gcc `pkg-config --cflags glib-2.0 smf` test.c `pkg-config --libs glib-2.0 smf`".

Do you know if there is a simple command line? Or a makefile to write without pkg-config?

PS: It works on Ubuntu and on the RPI2 (Raspbian) :D Thank you so much !!! If you have an idea about how to compile without pkg-config I'll be glad to learn it :D

Re: Read .mid file in C

Posted: Thu Jun 04, 2015 8:11 am
by Babzouille
Ok too bad :(
Thanks !!! :)