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.midiscope; 18 19 import android.media.midi.MidiReceiver; 20 import android.util.Log; 21 22 import java.io.IOException; 23 import java.util.Locale; 24 import java.util.concurrent.TimeUnit; 25 26 /** 27 * Convert incoming MIDI messages to a string and write them to a ScopeLogger. 28 * Assume that messages have been aligned using a MidiFramer. 29 */ 30 public class LoggingReceiver extends MidiReceiver { 31 public static final String TAG = "MidiScope"; 32 private static final long NANOS_PER_SECOND = TimeUnit.SECONDS.toNanos(1); 33 private long mStartTime; 34 private ScopeLogger mLogger; 35 36 public LoggingReceiver(ScopeLogger logger) { 37 mStartTime = System.nanoTime(); 38 mLogger = logger; 39 } 40 41 /* 42 * @see android.media.midi.MidiReceiver#onReceive(byte[], int, int, long) 43 */ 44 @Override 45 public void onSend(byte[] data, int offset, int count, long timestamp) 46 throws IOException { 47 StringBuilder sb = new StringBuilder(); 48 if (timestamp == 0) { 49 sb.append("-----0----: "); 50 } else { 51 long monoTime = timestamp - mStartTime; 52 double seconds = (double) monoTime / NANOS_PER_SECOND; 53 sb.append(String.format(Locale.US, "%10.3f: ", seconds)); 54 } 55 sb.append(MidiPrinter.formatBytes(data, offset, count)); 56 sb.append(": "); 57 sb.append(MidiPrinter.formatMessage(data, offset)); 58 String text = sb.toString(); 59 mLogger.log(text); 60 Log.i(TAG, text); 61 } 62 63 }