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