electro-music.com   Dedicated to experimental electro-acoustic
and electronic music
 
    Front Page  |  Articles  |  Radio
 |  Media  |  Forum  |  Wiki  |  Links  |  Store
Forum with support of Syndicator RSS
 FAQFAQ   CalendarCalendar   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   LinksLinks
 RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in  Chat RoomChat Room 
 Forum index » DIY Hardware and Software » Arduino
Programming midi sequencer
Post new topic   Reply to topic
Page 2 of 2 [47 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Goto page: Previous 1, 2
Author Message
Blue Hell
Site Admin


Joined: Apr 03, 2004
Posts: 20473
Location: The Netherlands, Enschede
Audio files: 147
G2 patch files: 318

PostPosted: Fri Jul 12, 2013 11:37 am    Post subject: Reply with quote  Mark this post and the followings unread

OK

You probably will want to handle MIDI reception in an interrupt routine of your own. Likely you will also want to have a means to look up the time of reception of the Midi clock bytes ... this may need some tinkering.

Once you have two times from the difference of thosetimes you can calculate the needed delay for your sequencer.

To get a good reference clock you may need a timer interrupt, and the tinkering will be in what to do when you get a time overflow - or maybe you will find a trick to avoid overflows.

That should be about it, in principle.

_________________
Jan
Back to top
View user's profile Send private message Visit poster's website
capicoso



Joined: Nov 19, 2012
Posts: 126
Location: Argentina

PostPosted: Fri Jul 12, 2013 12:49 pm    Post subject: Reply with quote  Mark this post and the followings unread

Ok, thanks. I'll try everything...
Searching i found this:
http://little-scale.blogspot.com.ar/2008/05/how-to-deal-with-midi-clock-signals-in.html
I thought i could do something like, put the note events inside void sync(). And if i want to send a note every 1/4, i would put a counter inside the sync() when the counter reachs 24(1/4 right?) it sends the note, if i want to send at 1/8 note, it'd count to 12 and then send it and reset to 0. Does this make sense? The idea would be that that 24, 12, 8 and etc, are dynamic, so if step one is at 1/4 duration, it counts to 24, then if the next one is at 1/8 duration it counts to 12...
Back to top
View user's profile Send private message
Blue Hell
Site Admin


Joined: Apr 03, 2004
Posts: 20473
Location: The Netherlands, Enschede
Audio files: 147
G2 patch files: 318

PostPosted: Fri Jul 12, 2013 3:32 pm    Post subject: Reply with quote  Mark this post and the followings unread

trying would not hurt Laughing

could be a good solution even, as long as you only want a sequencer Cool

the reason i brought in some extra complexities is that in my work experience there has always been something like .. customer wish creep. ... Shocked

anyway, try it, and when you run into a wall ... there are some ways out.

_________________
Jan
Back to top
View user's profile Send private message Visit poster's website
MusicMan11712



Joined: Aug 08, 2009
Posts: 718
Location: Upstate New York USA
Audio files: 148

PostPosted: Fri Jul 12, 2013 8:48 pm    Post subject: Reply with quote  Mark this post and the followings unread

capicoso wrote:
Ok, thanks. I'll try everything...
Searching i found this:
http://little-scale.blogspot.com.ar/2008/05/how-to-deal-with-midi-clock-signals-in.html
I thought i could do something like, put the note events inside void sync(). And if i want to send a note every 1/4, i would put a counter inside the sync() when the counter reachs 24(1/4 right?) it sends the note, if i want to send at 1/8 note, it'd count to 12 and then send it and reset to 0. Does this make sense? The idea would be that that 24, 12, 8 and etc, are dynamic, so if step one is at 1/4 duration, it counts to 24, then if the next one is at 1/8 duration it counts to 12...

I haven't tried this, but couldn't you use midi library callbacks to update your sequencer clock based on the incoming midi clock data? So far as I can see, no need for you to write interrupt handlers.
Steve
Back to top
View user's profile Send private message
capicoso



Joined: Nov 19, 2012
Posts: 126
Location: Argentina

PostPosted: Sun Jul 14, 2013 6:02 pm    Post subject: Reply with quote  Mark this post and the followings unread

MusicMan11712 wrote:
capicoso wrote:
Ok, thanks. I'll try everything...
Searching i found this:
http://little-scale.blogspot.com.ar/2008/05/how-to-deal-with-midi-clock-signals-in.html
I thought i could do something like, put the note events inside void sync(). And if i want to send a note every 1/4, i would put a counter inside the sync() when the counter reachs 24(1/4 right?) it sends the note, if i want to send at 1/8 note, it'd count to 12 and then send it and reset to 0. Does this make sense? The idea would be that that 24, 12, 8 and etc, are dynamic, so if step one is at 1/4 duration, it counts to 24, then if the next one is at 1/8 duration it counts to 12...

I haven't tried this, but couldn't you use midi library callbacks to update your sequencer clock based on the incoming midi clock data? So far as I can see, no need for you to write interrupt handlers.
Steve

I would have to read about callbacks... Will check it in the reference
Back to top
View user's profile Send private message
capicoso



Joined: Nov 19, 2012
Posts: 126
Location: Argentina

PostPosted: Tue Jul 16, 2013 9:16 pm    Post subject: Reply with quote  Mark this post and the followings unread

Hey, i modified the sketch a lot, it had some redundant things and some other non-senses... Now it's much better, i just wanted to see if i could get the time accuracy better but nope. I'm using a timer on puredata via midi to compare the time, i don't know precise it's. But with a simple sketch that sends midi note, delay(250), and nothing else, it was the same... So i'm happy because i thought i was doing something very wrong.
The question, i know i won't be able to make a perfect clock, it's not my objetive, since i'll sync it to midi clock. But could a crystal oscillator make it better, than the ceramic resonator my board has?
Back to top
View user's profile Send private message
MusicMan11712



Joined: Aug 08, 2009
Posts: 718
Location: Upstate New York USA
Audio files: 148

PostPosted: Wed Jul 17, 2013 2:15 am    Post subject: Reply with quote  Mark this post and the followings unread

Have you tried using the arduino as your midi clock source? If so, how does it compare with having the arduino as the slave?
Steve
Back to top
View user's profile Send private message
capicoso



Joined: Nov 19, 2012
Posts: 126
Location: Argentina

PostPosted: Thu Jul 18, 2013 10:13 pm    Post subject: Reply with quote  Mark this post and the followings unread

MusicMan11712 wrote:
Have you tried using the arduino as your midi clock source? If so, how does it compare with having the arduino as the slave?
Steve

Nope. Just now i'm trying receiving midi clock from computer to arduino. Used little-scale code, i think callbacks were more complicated... and trying this
void Sync() {
if(counter == 24){

digitalWrite(13, LOW);

counter = 0;}
else{digitalWrite(13, HIGH);
counter++;}

// do something for every MIDI Clock pulse when the sequencer is running
}

Measuring d13 output with my oscilloscope i see periods from(at 120bpm) 499.6ms to 501ms. It's the best a computer can do i guess? As long as all the instruments are synced...
Back to top
View user's profile Send private message
MusicMan11712



Joined: Aug 08, 2009
Posts: 718
Location: Upstate New York USA
Audio files: 148

PostPosted: Fri Jul 19, 2013 1:52 pm    Post subject: Reply with quote  Mark this post and the followings unread

Could it be that the variation in your counter is due to some variation in the serial buffer? I seem to remember reading something about that and also a way to see how much is in the buffer. I will see if I can find the reference.

This is not the reference I was looking for, but it might be useful if you really want to fine tune the clock handling: http://electro-music.com/forum/post-373369.html#373369

I think there was something else that led me to this: http://arduino.cc/en/Serial/Available (more details about accessing the serial buffer).

Steve
Back to top
View user's profile Send private message
capicoso



Joined: Nov 19, 2012
Posts: 126
Location: Argentina

PostPosted: Fri Jul 19, 2013 3:15 pm    Post subject: Reply with quote  Mark this post and the followings unread

MusicMan11712 wrote:
Could it be that the variation in your counter is due to some variation in the serial buffer? I seem to remember reading something about that and also a way to see how much is in the buffer. I will see if I can find the reference.

This is not the reference I was looking for, but it might be useful if you really want to fine tune the clock handling: http://electro-music.com/forum/post-373369.html#373369

I think there was something else that led me to this: http://arduino.cc/en/Serial/Available (more details about accessing the serial buffer).

Steve

I'll check those links.
But i think the variation is because the midi clock isn't 100% precise, luckily. I searched for a external clock for midi "atomic clock" 5000usd Razz
Now i changed the led part and it was sending a note on on every 23 tick.

So i finally tested with my ears and it's all in sync, so the one to blame is the midi clock, but as long as they're all synced... The only weird thing is that the note on at speeds over 120bpm, sometimes it's a bit shorter so i have to put a bit of relase in my ADSR to hear it. In the oscilloscope when connected to the led i saw that too, sometimes the pulse is a bit shorter, this would be annoying. But in my actual sketch, the pulse will send a noteOff, so it won't bother, and the note on will last 24 ticks if its a 1/4, 12 if its 1/8, and so on.
I thought maybe that my optocoupler isn't fast enough or something? I'm using the whitey one 4n28. It was the first result that google returned when searching for midi in arduino. But when i was putting it all together i read that it was the worst option, and the 6n138 would be better...
About the interrupts, i'm not using anymore, i thought i really needed them but no... the thing was that i needed the note on to send just a pulse, and not constantly inside the loop(). But i added a counter that sums +1 when the note on is sent, and when the counter is = 1, the note off is sent. This way it's just a pulse. The code is a lot cleaner now, if you're interested i post it here
Back to top
View user's profile Send private message
smidirin



Joined: May 01, 2007
Posts: 14
Location: analogfx.studio

PostPosted: Wed Jul 24, 2013 5:09 am    Post subject: Reply with quote  Mark this post and the followings unread

Quote:
The code is a lot cleaner now, if you're interested i post it here


please do capicoso!
i am not very capable of understanding the arduino at this stage.
i'm always trying but so far not a lot of success Sad

is it usb midi you are syncing as well?
i have a nano i want to use as slave & have had problems where to modify my code..
i have a 6n138 setup too as well as inbuilt usb and wonder if i can use a midi cable to slave & still output usb midi to PC??
Back to top
View user's profile Send private message
capicoso



Joined: Nov 19, 2012
Posts: 126
Location: Argentina

PostPosted: Wed Jul 24, 2013 8:51 am    Post subject: Reply with quote  Mark this post and the followings unread

smidirin wrote:
Quote:
The code is a lot cleaner now, if you're interested i post it here


please do capicoso!
i am not very capable of understanding the arduino at this stage.
i'm always trying but so far not a lot of success Sad

is it usb midi you are syncing as well?
i have a nano i want to use as slave & have had problems where to modify my code..
i have a 6n138 setup too as well as inbuilt usb and wonder if i can use a midi cable to slave & still output usb midi to PC??

Here it is
http://sourceforge.net/projects/arduinovariseq/files/arduinovariseq.ino/download

it doesn't do anything new like the midi clock or anything, it's the same code but optimized
I'm not using usb, just standar midi din and now adding midi in with 4n28 opto. Did you try the hiduino firmware?
Right now in the code midi in is not implemented
i'm experimenting it now. My latest code(not uploaded yet) has midi clock in, and the sequence syncs to the midi clock, also i added a trasposition pot for the sequence. The schematic will be available soon
Back to top
View user's profile Send private message
smidirin



Joined: May 01, 2007
Posts: 14
Location: analogfx.studio

PostPosted: Wed Jul 24, 2013 5:34 pm    Post subject: Reply with quote  Mark this post and the followings unread

thanks, i'll keep an eye on your project!
i have looked into hiduino but never tried it. there is also gretel's MIDIsync.
Back to top
View user's profile Send private message
xarolium



Joined: Oct 25, 2011
Posts: 19
Location: France

PostPosted: Thu Jul 25, 2013 1:04 pm    Post subject: Reply with quote  Mark this post and the followings unread

Hi capicoso,

Did you solve your problem to receive Midi Clock?
try this code it work fine. I built a Midi to DIN converter with arduino and i didn't miss any 0xF8 clock message Wink I hope it'll help you

Quote:

void Midi_Synchro(){

  if(Serial.available()>0){
    byte data;
    data = Serial.read();//Store incoming byte
    switch (data){

      //MIDI START
    case 0xfa:
      play=1;
      ppqn_count=0;
      break;

      //MIDI STOP
    case 0xfc:
      play=0;
      step_count=0;
      break;

      //MIDI CLOCK
    case 0xf8:
      ppqn_count++; 
      break;
    } 
  }
  if(play){
    if (ppqn_count>=6){//24PPQN = 6pulse per step in 1/16 scale
      ppqn_count=0;
      step_count++;         
      if(step_count>=16){//max step 16
        step_count=0;       
      }
    }
  }
}

Back to top
View user's profile Send private message
MusicMan11712



Joined: Aug 08, 2009
Posts: 718
Location: Upstate New York USA
Audio files: 148

PostPosted: Fri Jul 26, 2013 11:01 am    Post subject: Reply with quote  Mark this post and the followings unread

This looks quite straightforward and compact; I will have to give it a try myself. Thanks for sharing.
Steve
Back to top
View user's profile Send private message
xarolium



Joined: Oct 25, 2011
Posts: 19
Location: France

PostPosted: Fri Jul 26, 2013 12:53 pm    Post subject: Reply with quote  Mark this post and the followings unread

Quite simple but powerful if you don't need to read other MIDI message like noteOn or NoteOFF Very Happy
Back to top
View user's profile Send private message
capicoso



Joined: Nov 19, 2012
Posts: 126
Location: Argentina

PostPosted: Fri Jul 26, 2013 2:31 pm    Post subject: Reply with quote  Mark this post and the followings unread

xarolium wrote:
Hi capicoso,

Did you solve your problem to receive Midi Clock?
try this code it work fine. I built a Midi to DIN converter with arduino and i didn't miss any 0xF8 clock message Wink I hope it'll help you

Quote:

void Midi_Synchro(){

  if(Serial.available()>0){
    byte data;
    data = Serial.read();//Store incoming byte
    switch (data){

      //MIDI START
    case 0xfa:
      play=1;
      ppqn_count=0;
      break;

      //MIDI STOP
    case 0xfc:
      play=0;
      step_count=0;
      break;

      //MIDI CLOCK
    case 0xf8:
      ppqn_count++; 
      break;
    } 
  }
  if(play){
    if (ppqn_count>=6){//24PPQN = 6pulse per step in 1/16 scale
      ppqn_count=0;
      step_count++;         
      if(step_count>=16){//max step 16
        step_count=0;       
      }
    }
  }
}


Yes, i solved it. Thanks though
The code is similar... The counter goes +1 every tick, and a if statement for the note off and on events. Length potentiometers controls the variable at which number the counter resets to 0. So if step 1 is 12, and 2nd step is 24, it'd play a 1/8 and then a 1/4 note. Right now i'm not on my computer, i'll upload this part of the code. The rest is the same
Back to top
View user's profile Send private message
capicoso



Joined: Nov 19, 2012
Posts: 126
Location: Argentina

PostPosted: Fri Jul 26, 2013 5:50 pm    Post subject: Reply with quote  Mark this post and the followings unread

here it's
Code:

void clock() {
 

       
if(counter > durnota ){
 
   //MIDI.sendNoteOff(60, 0, 1);
   eventoNoteoff();
 
 
  counter = 0;
}
  else  if(counter == 0){
    eventoNoteon();
    counter++;
   
  }
 

}

at every clock tick, the function clock is called. Durnota is the note duration, defined by each of the potentiometers(this is in the main loop). They're fixed values like 11, 22, ...
When the counter starts at 0 it sends the noteon event, and depending on the durnota value it'll last longer or shorter. Then it resets to 0 and on to next step...
Back to top
View user's profile Send private message
MusicMan11712



Joined: Aug 08, 2009
Posts: 718
Location: Upstate New York USA
Audio files: 148

PostPosted: Sat Jul 27, 2013 3:47 pm    Post subject: Reply with quote  Mark this post and the followings unread

xarolium wrote:
Quite simple but powerful if you don't need to read other MIDI message like noteOn or NoteOFF Very Happy

It seems to me that if you stored the 1st byte and tested it for being a midi timing byte, you could pass that first byte to a second midi handler that processed two-byte and three-byte midi commands.
Steve
Back to top
View user's profile Send private message
capicoso



Joined: Nov 19, 2012
Posts: 126
Location: Argentina

PostPosted: Wed Jul 31, 2013 4:42 pm    Post subject: Reply with quote  Mark this post and the followings unread

Now it has transpose !
http://www.youtube.com/watch?v=EcYOu2Nxh1Q
Careful at 0.30 my little kid touched my camera and there's a loud pop.
Back to top
View user's profile Send private message
capicoso



Joined: Nov 19, 2012
Posts: 126
Location: Argentina

PostPosted: Thu Aug 08, 2013 12:38 am    Post subject: Reply with quote  Mark this post and the followings unread

hey...
i added transpose via midi note, kinda like an arpeggiator:
https://www.youtube.com/watch?v=zX3zPnibdj4
and
it sends midi clock out now! and it seems stable, yay:
https://www.youtube.com/watch?v=aT6m8nsZZuw
Back to top
View user's profile Send private message
capicoso



Joined: Nov 19, 2012
Posts: 126
Location: Argentina

PostPosted: Thu Nov 07, 2013 8:58 pm    Post subject: Reply with quote  Mark this post and the followings unread

a few months ago I added multi-gate mode:
https://www.youtube.com/watch?v=2SOoyA7ecYQ
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic
Page 2 of 2 [47 Posts]
View unread posts
View new posts in the last week
Goto page: Previous 1, 2
Mark the topic unread :: View previous topic :: View next topic
 Forum index » DIY Hardware and Software » Arduino
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum
e-m mkii

Please support our site. If you click through and buy from
our affiliate partners, we earn a small commission.


Forum with support of Syndicator RSS
Powered by phpBB © 2001, 2005 phpBB Group
Copyright © 2003 through 2009 by electro-music.com - Conditions Of Use