# Android MIDI sequencer with Processing interface

**URL:** https://discourse.processing.org/t/android-midi-sequencer-with-processing-interface/1500
**Category:** Processing for Android
**Created:** [July 4, 2018, 4:34pm UTC](https://discourse.processing.org/t/android-midi-sequencer-with-processing-interface/1500 "2018-07-04T16:34:54Z")
**Posts on this page:** 20
**Page:** 3

<div class="post-metadata">

### Author: ![chanof](https://avatars.discourse-cdn.com/v4/letter/c/3e96dc/32.png) [@chanof](https://discourse.processing.org/u/chanof)
#### Post date: [July 7, 2018, 3:39pm UTC](https://discourse.processing.org/t/android-midi-sequencer-with-processing-interface/1500/44 "2018-07-07T15:39:57Z")

</div>

haha i was thinking the same 😉

---

<div class="post-metadata">

### Author: ![sensn](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@sensn](https://discourse.processing.org/u/sensn)
#### Post date: [July 7, 2018, 4:48pm UTC](https://discourse.processing.org/t/android-midi-sequencer-with-processing-interface/1500/45 "2018-07-07T16:48:13Z")

</div>

@chanof @shedMusic

I found a workaround for this. I put all the Midi-code from setup() in an own function called midisetup(), and I call it with a delay from draw(). This does work. I guess another way would be to have a button in draw() and call midisetup() if this button is pushed.  
I think the reason for this is that the sketch starts 2 times ( one normal, then restart in orientation mode) and so setup might be called twice, and therefore the mididevice might be blocked.  
The button might be called ‘Start shedMusics-MIDI engine’ or something.😄

---

<div class="post-metadata">

### Author: ![chanof](https://avatars.discourse-cdn.com/v4/letter/c/3e96dc/32.png) [@chanof](https://discourse.processing.org/u/chanof)
#### Post date: [July 7, 2018, 4:57pm UTC](https://discourse.processing.org/t/android-midi-sequencer-with-processing-interface/1500/46 "2018-07-07T16:57:02Z")

</div>

Great solution @sensn I’m sick of thinking oblique…  
My question is, if to use less buttons i recall this function with the delay but before recall the sequencer try to send a midi note the app will crash?  
I not already implemented play stop buttons for this i ask  
May i ask if you could try, thanks!

---

<div class="post-metadata">

### Author: ![sensn](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@sensn](https://discourse.processing.org/u/sensn)
#### Post date: [July 7, 2018, 5:03pm UTC](https://discourse.processing.org/t/android-midi-sequencer-with-processing-interface/1500/47 "2018-07-07T17:03:03Z")

</div>

@chanof  
Hm, not sure about this; but in my example the pattern is empty so no event is trying to send a midi out.  
so if you have an empty pattern at the start it should work.  
I didn’t test how short the delay can be. I tried with 250 ms cause thats the tempo of my sequencer.  
Be shure to call midisetup() only once, otherwise there will be the same problem again, i think.  
I did this using a boolean set to true at start. then after 250ms check with if statement if its true, then call midisetup and set the boolean to false.  
Hope that helps. I can post my code if this doesn’t work for you.

---

<div class="post-metadata">

### Author: ![sensn](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@sensn](https://discourse.processing.org/u/sensn)
#### Post date: [July 7, 2018, 5:17pm UTC](https://discourse.processing.org/t/android-midi-sequencer-with-processing-interface/1500/48 "2018-07-07T17:17:33Z")

</div>

@chanof  
Ok, tried it. No crash. Midi works. I actually got pretty nice soundscape from my Korg M1 with hanging notes.😄

---

<div class="post-metadata">

### Author: ![chanof](https://avatars.discourse-cdn.com/v4/letter/c/3e96dc/32.png) [@chanof](https://discourse.processing.org/u/chanof)
#### Post date: [July 7, 2018, 5:24pm UTC](https://discourse.processing.org/t/android-midi-sequencer-with-processing-interface/1500/49 "2018-07-07T17:24:44Z")

</div>

I use it and work as you say 😉

```auto
if (recall == true){
midiSetup();
recall = false;
}

```

---

<div class="post-metadata">

### Author: ![shedMusic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/shedmusic/32/601_2.png) [@shedMusic](https://discourse.processing.org/u/shedMusic)
#### Post date: [July 7, 2018, 5:30pm UTC](https://discourse.processing.org/t/android-midi-sequencer-with-processing-interface/1500/50 "2018-07-07T17:30:44Z")

</div>

Hmmm … If the workaround works then good! I still want to know why it stops working on a rotate though! I will investigate further … I am guessing that if the inputPort is already open and connected to the playback device then this causes a lock up when it tries again to connect. Only a maybe, but I’ll see what I can discover.

---

<div class="post-metadata">

### Author: ![chanof](https://avatars.discourse-cdn.com/v4/letter/c/3e96dc/32.png) [@chanof](https://discourse.processing.org/u/chanof)
#### Post date: [July 7, 2018, 5:53pm UTC](https://discourse.processing.org/t/android-midi-sequencer-with-processing-interface/1500/51 "2018-07-07T17:53:01Z")

</div>

what do you think of the timestamp: play the notes 20ms in the future to avoid timing and stay below the threshold of the perceivable? I already can’t listen on a machine if it work.

---

<div class="post-metadata">

### Author: ![sensn](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@sensn](https://discourse.processing.org/u/sensn)
#### Post date: [July 7, 2018, 6:31pm UTC](https://discourse.processing.org/t/android-midi-sequencer-with-processing-interface/1500/52 "2018-07-07T18:31:00Z")

</div>

I noticed that it’s enough to call midiSetup(); from inside draw(). There doesn’t need to be a delay at all.  
I just tested it with a little control surface sketch for my synth with broken knobs…

---

<div class="post-metadata">

### Author: ![chanof](https://avatars.discourse-cdn.com/v4/letter/c/3e96dc/32.png) [@chanof](https://discourse.processing.org/u/chanof)
#### Post date: [July 7, 2018, 6:39pm UTC](https://discourse.processing.org/t/android-midi-sequencer-with-processing-interface/1500/53 "2018-07-07T18:39:55Z")

</div>

Clear! I’m curious to know how this timestamp works and whats about CC massages to control them with sensors for example… May i ask your email sensn, after some test on synth i would like to send you my sequencer version of this amazing topic powered by @shedMusic, would be nice to share each other the final results

---

<div class="post-metadata">

### Author: ![chanof](https://avatars.discourse-cdn.com/v4/letter/c/3e96dc/32.png) [@chanof](https://discourse.processing.org/u/chanof)
#### Post date: [July 8, 2018, 12:20am UTC](https://discourse.processing.org/t/android-midi-sequencer-with-processing-interface/1500/54 "2018-07-08T00:20:50Z")

</div>

`long now = System.nanoTime()`

Looks like `mills()` but with nano seconds, tomorrow i will check if we can create a better clock.

---

<div class="post-metadata">

### Author: ![chanof](https://avatars.discourse-cdn.com/v4/letter/c/3e96dc/32.png) [@chanof](https://discourse.processing.org/u/chanof)
#### Post date: [July 8, 2018, 11:06am UTC](https://discourse.processing.org/t/android-midi-sequencer-with-processing-interface/1500/55 "2018-07-08T11:06:42Z")

</div>

@shedMusic @sensn  
What you think about this internal clock?

```auto
long lasttimecheck;
long timeinterval;

void setup() {
  lasttimecheck = System.nanoTime();
  //step duration 
  timeinterval = 125000000L;
}
void draw() {
  if (System.nanoTime() > lasttimecheck + timeinterval) {
    lasttimecheck = System.nanoTime();
    println("yeah");
  }
}

```

---

<div class="post-metadata">

### Author: ![sensn](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@sensn](https://discourse.processing.org/u/sensn)
#### Post date: [July 8, 2018, 11:15am UTC](https://discourse.processing.org/t/android-midi-sequencer-with-processing-interface/1500/56 "2018-07-08T11:15:43Z")

</div>

thanks. I like simple examples. Will try it out with my sequencer.

I think we need an example for Midi receive too. Might be nice for arpeggiators.

---

<div class="post-metadata">

### Author: ![chanof](https://avatars.discourse-cdn.com/v4/letter/c/3e96dc/32.png) [@chanof](https://discourse.processing.org/u/chanof)
#### Post date: [July 8, 2018, 11:21am UTC](https://discourse.processing.org/t/android-midi-sequencer-with-processing-interface/1500/57 "2018-07-08T11:21:42Z")

</div>

Hi! Im not so interested about receive midi inside android except for midi clock would be nice, but if it’s hard the [https://github.com/googlesamples/android-MidiSynth](https://github.com/googlesamples/android-MidiSynth)  
the example above into android studio could help.

Im not sure to understand, midi receive i think to receive notes or maybe an external clock, if you want build an arpeggiator you already have what you need i presume, or why you want listen an external arpeggiator, i m curious

---

<div class="post-metadata">

### Author: ![shedMusic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/shedmusic/32/601_2.png) [@shedMusic](https://discourse.processing.org/u/shedMusic)
#### Post date: [July 8, 2018, 11:23am UTC](https://discourse.processing.org/t/android-midi-sequencer-with-processing-interface/1500/58 "2018-07-08T11:23:23Z")

</div>

I am going to be looking at this later today. It’s going to he best not to do timing checks in draw() though … A separate thread probably

---

<div class="post-metadata">

### Author: ![chanof](https://avatars.discourse-cdn.com/v4/letter/c/3e96dc/32.png) [@chanof](https://discourse.processing.org/u/chanof)
#### Post date: [July 8, 2018, 11:25am UTC](https://discourse.processing.org/t/android-midi-sequencer-with-processing-interface/1500/59 "2018-07-08T11:25:03Z")

</div>

Thanks Mark i was thinking it too, but im not sure to dispose the things correctly, do you mean like below?

```auto
long lasttimecheck;
long timeinterval;

void setup() {
  lasttimecheck = System.nanoTime();
  //step duration 
  timeinterval = 125000000L;
}

void clock(){
  if (System.nanoTime() > lasttimecheck + timeinterval) {
    lasttimecheck = System.nanoTime();
    println("yeah" + lasttimecheck);
  }

}
void draw() {
clock();
}

```

Take your time, i close with social life these days… 🙂 Im so exited about the progress

---

<div class="post-metadata">

### Author: ![sensn](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@sensn](https://discourse.processing.org/u/sensn)
#### Post date: [July 8, 2018, 11:32am UTC](https://discourse.processing.org/t/android-midi-sequencer-with-processing-interface/1500/60 "2018-07-08T11:32:14Z")

</div>

I want to play a note on the keyboard, send that to my phone, and the phone generates an arp based on that note and sends it back to the synth.

About your example: How would i change the tempo ( with a slider )?  
I don’t know what the L means and how i can create such a number?

```auto
 timeinterval = 125000000L;

```

I guess it is a notation for nanoseconds; but how would I change that with a map() from a slider?

Edit: OK, i can just give it an integer and it is converted to long automatically.  
Learning never stops.😄

---

<div class="post-metadata">

### Author: ![chanof](https://avatars.discourse-cdn.com/v4/letter/c/3e96dc/32.png) [@chanof](https://discourse.processing.org/u/chanof)
#### Post date: [July 8, 2018, 4:40pm UTC](https://discourse.processing.org/t/android-midi-sequencer-with-processing-interface/1500/61 "2018-07-08T16:40:17Z")

</div>

I’ve been testing some drum machines, nice feeling! but i wonder to know how do you deal with the note length:  
With drums could be good that the note and when one another have to play, but if i use stop button the note never end, unfortunately i can not test it again before tuesday. I wonder to know how to send note off when stop button is pressed.  
Any idea?  
Maybe send one another note with 0 velocity?  
Thanks

---

<div class="post-metadata">

### Author: ![shedMusic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/shedmusic/32/601_2.png) [@shedMusic](https://discourse.processing.org/u/shedMusic)
#### Post date: [July 8, 2018, 4:56pm UTC](https://discourse.processing.org/t/android-midi-sequencer-with-processing-interface/1500/62 "2018-07-08T16:56:56Z")

</div>

@chanof @sensn

Right ,… timing and sending notes is hopefully coming soon, but for now, a bug fix for the crash on rotate. It’s being cause by the inputPort being reopened whilst it is already open, Don’t know why exactly but that’s the cause . The fix

At top of sketch a new global variable

> ```java
> 
> MidiDevice myOpenDevice ;
> 
> ```

and then in setup() use it like this

> ```java
> 
> m.openDevice(deviceInfo[0], new MidiManager.OnDeviceOpenedListener() {
> @Override
> public void onDeviceOpened(MidiDevice device) {
> if (device == null) {
> println("could not open device");
> } else {
> myOpenDevice = device;
> //Open input port
> inputPort = myOpenDevice.openInputPort(0);
> println("device opened and ready for use");
> }//end else
> }
> } //end m.opendevice
> , new Handler(Looper.getMainLooper())
> );
> 
> ```

This is because ‘device’ is unavailable outside this code snippet so using it to open the port means it can’t be closed anywhere, but if you use your new myOpenDevice you can close it … in onPause()

> ```java
> 
> void onPause() {
> try {
> inputPort.close();
> } 
> catch(Exception e) {
> }
> }
> 
> ```

`
`

``

Much better than doing your setup in draw()!

---

<div class="post-metadata">

### Author: ![sensn](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@sensn](https://discourse.processing.org/u/sensn)
#### Post date: [July 8, 2018, 6:11pm UTC](https://discourse.processing.org/t/android-midi-sequencer-with-processing-interface/1500/63 "2018-07-08T18:11:38Z")

</div>

Great, I’ll test. If i didn’t touch the display for some time, it turned off, and I had to restart the app too. Maybe that issue is gone now too.

For timing I tried gotoloops Countdown Class, it works pretty good.  
[https://forum.processing.org/two/discussion/27733/countdown-class-library-for-java-js-python](https://forum.processing.org/two/discussion/27733/countdown-class-library-for-java-js-python)  
I implemented a tempo slider, but i didn’t sync it to any clock, so i don’t know how accurate it really is. I’m curious what shedMusic will come up with.  
I also worked on a M1 front panel sketch, which works verry well. It’s great to have that MIDI feature.

[Previous page](https://discourse.processing.org/t/android-midi-sequencer-with-processing-interface/1500.md?page=2)

[Next page](https://discourse.processing.org/t/android-midi-sequencer-with-processing-interface/1500.md?page=4)
