• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.common.midi;
18 
19 import android.media.midi.MidiSender;
20 import android.util.Log;
21 
22 import java.io.IOException;
23 
24 public class MidiEventThread extends MidiEventScheduler {
25     protected static final String TAG = "MidiEventThread";
26 
27     private EventThread mEventThread;
28     MidiDispatcher mDispatcher = new MidiDispatcher();
29 
30     class EventThread extends Thread {
31         private boolean go = true;
32 
33         @Override
run()34         public void run() {
35             while (go) {
36                 try {
37                     MidiEvent event = (MidiEvent) waitNextEvent();
38                     try {
39                         Log.i(TAG, "Fire event " + event.data[0] + " at "
40                                 + event.getTimestamp());
41                         mDispatcher.send(event.data, 0,
42                                 event.count, event.getTimestamp());
43                     } catch (IOException e) {
44                         e.printStackTrace();
45                     }
46                     // Put event back in the pool for future use.
47                     addEventToPool(event);
48                 } catch (InterruptedException e) {
49                     // OK, this is how we stop the thread.
50                 }
51             }
52         }
53 
54         /**
55          * Asynchronously tell the thread to stop.
56          */
requestStop()57         public void requestStop() {
58             go = false;
59             interrupt();
60         }
61     }
62 
start()63     public void start() {
64         stop();
65         mEventThread = new EventThread();
66         mEventThread.start();
67     }
68 
69     /**
70      * Asks the thread to stop then waits for it to stop.
71      */
stop()72     public void stop() {
73         if (mEventThread != null) {
74             mEventThread.requestStop();
75             try {
76                 mEventThread.join(500);
77             } catch (InterruptedException e) {
78                 Log.e(TAG,
79                         "Interrupted while waiting for MIDI EventScheduler thread to stop.");
80             } finally {
81                 mEventThread = null;
82             }
83         }
84     }
85 
getSender()86     public MidiSender getSender() {
87         return mDispatcher.getSender();
88     }
89 
90 }
91