Permalink
Browse files

Auto-update

  • Loading branch information...
google-automerger committed Sep 4, 2015
0 parents commit ad9ab669bf7370795e02a88a705b6f41b041acbb
Showing with 4,241 additions and 0 deletions.
  1. +18 −0 .google/packaging.yaml
  2. +73 −0 Application/build.gradle
  3. +55 −0 Application/src/main/AndroidManifest.xml
  4. +243 −0 Application/src/main/java/com/example/android/common/midi/EventScheduler.java
  5. +102 −0 Application/src/main/java/com/example/android/common/midi/MidiConstants.java
  6. +95 −0 Application/src/main/java/com/example/android/common/midi/MidiDispatcher.java
  7. +119 −0 Application/src/main/java/com/example/android/common/midi/MidiEventScheduler.java
  8. +90 −0 Application/src/main/java/com/example/android/common/midi/MidiEventThread.java
  9. +112 −0 Application/src/main/java/com/example/android/common/midi/MidiFramer.java
  10. +93 −0 Application/src/main/java/com/example/android/common/midi/MidiInputPortSelector.java
  11. +84 −0 Application/src/main/java/com/example/android/common/midi/MidiOutputPortConnectionSelector.java
  12. +103 −0 Application/src/main/java/com/example/android/common/midi/MidiOutputPortSelector.java
  13. +200 −0 Application/src/main/java/com/example/android/common/midi/MidiPortConnector.java
  14. +183 −0 Application/src/main/java/com/example/android/common/midi/MidiPortSelector.java
  15. +125 −0 Application/src/main/java/com/example/android/common/midi/MidiPortWrapper.java
  16. +46 −0 Application/src/main/java/com/example/android/common/midi/MidiTools.java
  17. +111 −0 Application/src/main/java/com/example/android/common/midi/synth/EnvelopeADSR.java
  18. +73 −0 Application/src/main/java/com/example/android/common/midi/synth/SawOscillator.java
  19. +52 −0 Application/src/main/java/com/example/android/common/midi/synth/SawOscillatorDPW.java
  20. +65 −0 Application/src/main/java/com/example/android/common/midi/synth/SawVoice.java
  21. +95 −0 Application/src/main/java/com/example/android/common/midi/synth/SimpleAudioOutput.java
  22. +54 −0 Application/src/main/java/com/example/android/common/midi/synth/SineOscillator.java
  23. +27 −0 Application/src/main/java/com/example/android/common/midi/synth/SineVoice.java
  24. +284 −0 Application/src/main/java/com/example/android/common/midi/synth/SynthEngine.java
  25. +35 −0 Application/src/main/java/com/example/android/common/midi/synth/SynthUnit.java
  26. +85 −0 Application/src/main/java/com/example/android/common/midi/synth/SynthVoice.java
  27. +135 −0 Application/src/main/java/com/example/android/midisynth/MainActivity.java
  28. +61 −0 Application/src/main/java/com/example/android/midisynth/MidiSynthDeviceService.java
  29. BIN Application/src/main/res/drawable-hdpi/tile.9.png
  30. +53 −0 Application/src/main/res/layout/main.xml
  31. +25 −0 Application/src/main/res/menu/main.xml
  32. BIN Application/src/main/res/mipmap-hdpi/ic_launcher.png
  33. BIN Application/src/main/res/mipmap-mdpi/ic_launcher.png
  34. BIN Application/src/main/res/mipmap-xhdpi/ic_launcher.png
  35. BIN Application/src/main/res/mipmap-xxhdpi/ic_launcher.png
  36. BIN Application/src/main/res/mipmap-xxxhdpi/ic_launcher.png
  37. +24 −0 Application/src/main/res/values-sw600dp/template-dimens.xml
  38. +25 −0 Application/src/main/res/values-sw600dp/template-styles.xml
  39. +22 −0 Application/src/main/res/values-v11/template-styles.xml
  40. +21 −0 Application/src/main/res/values-v21/base-colors.xml
  41. +24 −0 Application/src/main/res/values-v21/base-template-styles.xml
  42. +30 −0 Application/src/main/res/values/base-strings.xml
  43. +26 −0 Application/src/main/res/values/colors.xml
  44. +25 −0 Application/src/main/res/values/strings.xml
  45. +25 −0 Application/src/main/res/values/styles.xml
  46. +32 −0 Application/src/main/res/values/template-dimens.xml
  47. +42 −0 Application/src/main/res/values/template-styles.xml
  48. +21 −0 Application/src/main/res/xml/synth_device_info.xml
  49. +35 −0 CONTRIBUTING.md
  50. +647 −0 LICENSE
  51. +72 −0 README.md
  52. +12 −0 build.gradle
  53. BIN gradle/wrapper/gradle-wrapper.jar
  54. +6 −0 gradle/wrapper/gradle-wrapper.properties
  55. +164 −0 gradlew
  56. +90 −0 gradlew.bat
  57. BIN screenshots/1-main.png
  58. BIN screenshots/icon-web.png
  59. +2 −0 settings.gradle
@@ -0,0 +1,18 @@
# GOOGLE SAMPLE PACKAGING DATA
#
# This file is used by Google as part of our samples packaging process.
# End users may safely ignore this file. It has no relevance to other systems.
---
status: PUBLISHED
technologies: [Android]
categories: [Media]
languages: [Java]
solutions: [Mobile]
github: android-MidiSynth
level: EXPERT
icon: screenshots/icon-web.png
apiRefs:
- android:android.media.midi.MidiManager
- android:android.media.midi.MidiReceiver
license: apache2
@@ -0,0 +1,73 @@
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
apply plugin: 'com.android.application'
repositories {
jcenter()
}
dependencies {
compile "com.android.support:support-v4:23.0.0"
compile "com.android.support:support-v13:23.0.0"
compile "com.android.support:cardview-v7:23.0.0"
}
// The sample build uses multiple directories to
// keep boilerplate and common code separate from
// the main sample code.
List<String> dirs = [
'main', // main sample code; look here for the interesting stuff.
'common', // components that are reused by multiple samples
'template'] // boilerplate code that is generated by the sample template process
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
minSdkVersion 23
targetSdkVersion 23
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
sourceSets {
main {
dirs.each { dir ->
java.srcDirs "src/${dir}/java"
res.srcDirs "src/${dir}/res"
}
}
androidTest.setRoot('tests')
androidTest.java.srcDirs = ['tests/src']
}
}
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2015 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<manifest package="com.example.android.midisynth"
xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0">
<uses-feature
android:name="android.software.midi"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/MidiSynthTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service
android:name=".MidiSynthDeviceService"
android:permission="android.permission.BIND_MIDI_DEVICE_SERVICE">
<intent-filter>
<action android:name="android.media.midi.MidiDeviceService"/>
</intent-filter>
<meta-data
android:name="android.media.midi.MidiDeviceService"
android:resource="@xml/synth_device_info"/>
</service>
</application>
</manifest>
@@ -0,0 +1,243 @@
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.common.midi;
import java.util.SortedMap;
import java.util.TreeMap;
/**
* Store SchedulableEvents in a timestamped buffer.
* Events may be written in any order.
* Events will be read in sorted order.
* Events with the same timestamp will be read in the order they were added.
*
* Only one Thread can write into the buffer.
* And only one Thread can read from the buffer.
*/
public class EventScheduler {
private static final long NANOS_PER_MILLI = 1000000;
private final Object lock = new Object();
private SortedMap<Long, FastEventQueue> mEventBuffer;
// This does not have to be guarded. It is only set by the writing thread.
// If the reader sees a null right before being set then that is OK.
private FastEventQueue mEventPool = null;
private static final int MAX_POOL_SIZE = 200;
public EventScheduler() {
mEventBuffer = new TreeMap<Long, FastEventQueue>();
}
// If we keep at least one node in the list then it can be atomic
// and non-blocking.
private class FastEventQueue {
// One thread takes from the beginning of the list.
volatile SchedulableEvent mFirst;
// A second thread returns events to the end of the list.
volatile SchedulableEvent mLast;
volatile long mEventsAdded;
volatile long mEventsRemoved;
FastEventQueue(SchedulableEvent event) {
mFirst = event;
mLast = mFirst;
mEventsAdded = 1; // Always created with one event added. Never empty.
mEventsRemoved = 0; // None removed yet.
}
int size() {
return (int)(mEventsAdded - mEventsRemoved);
}
/**
* Do not call this unless there is more than one event
* in the list.
* @return first event in the list
*/
public SchedulableEvent remove() {
// Take first event.
mEventsRemoved++;
SchedulableEvent event = mFirst;
mFirst = event.mNext;
return event;
}
/**
* @param event
*/
public void add(SchedulableEvent event) {
event.mNext = null;
mLast.mNext = event;
mLast = event;
mEventsAdded++;
}
}
/**
* Base class for events that can be stored in the EventScheduler.
*/
public static class SchedulableEvent {
private long mTimestamp;
private SchedulableEvent mNext = null;
/**
* @param timestamp
*/
public SchedulableEvent(long timestamp) {
mTimestamp = timestamp;
}
/**
* @return timestamp
*/
public long getTimestamp() {
return mTimestamp;
}
/**
* The timestamp should not be modified when the event is in the
* scheduling buffer.
*/
public void setTimestamp(long timestamp) {
mTimestamp = timestamp;
}
}
/**
* Get an event from the pool.
* Always leave at least one event in the pool.
* @return event or null
*/
public SchedulableEvent removeEventfromPool() {
SchedulableEvent event = null;
if (mEventPool != null && (mEventPool.size() > 1)) {
event = mEventPool.remove();
}
return event;
}
/**
* Return events to a pool so they can be reused.
*
* @param event
*/
public void addEventToPool(SchedulableEvent event) {
if (mEventPool == null) {
mEventPool = new FastEventQueue(event);
// If we already have enough items in the pool then just
// drop the event. This prevents unbounded memory leaks.
} else if (mEventPool.size() < MAX_POOL_SIZE) {
mEventPool.add(event);
}
}
/**
* Add an event to the scheduler. Events with the same time will be
* processed in order.
*
* @param event
*/
public void add(SchedulableEvent event) {
synchronized (lock) {
FastEventQueue list = mEventBuffer.get(event.getTimestamp());
if (list == null) {
long lowestTime = mEventBuffer.isEmpty() ? Long.MAX_VALUE
: mEventBuffer.firstKey();
list = new FastEventQueue(event);
mEventBuffer.put(event.getTimestamp(), list);
// If the event we added is earlier than the previous earliest
// event then notify any threads waiting for the next event.
if (event.getTimestamp() < lowestTime) {
lock.notify();
}
} else {
list.add(event);
}
}
}
// Caller must synchronize on lock before calling.
private SchedulableEvent removeNextEventLocked(long lowestTime) {
SchedulableEvent event;
FastEventQueue list = mEventBuffer.get(lowestTime);
// Remove list from tree if this is the last node.
if ((list.size() == 1)) {
mEventBuffer.remove(lowestTime);
}
event = list.remove();
return event;
}
/**
* Check to see if any scheduled events are ready to be processed.
*
* @param timestamp
* @return next event or null if none ready
*/
public SchedulableEvent getNextEvent(long time) {
SchedulableEvent event = null;
synchronized (lock) {
if (!mEventBuffer.isEmpty()) {
long lowestTime = mEventBuffer.firstKey();
// Is it time for this list to be processed?
if (lowestTime <= time) {
event = removeNextEventLocked(lowestTime);
}
}
}
// Log.i(TAG, "getNextEvent: event = " + event);
return event;
}
/**
* Return the next available event or wait until there is an event ready to
* be processed. This method assumes that the timestamps are in nanoseconds
* and that the current time is System.nanoTime().
*
* @return event
* @throws InterruptedException
*/
public SchedulableEvent waitNextEvent() throws InterruptedException {
SchedulableEvent event = null;
while (true) {
long millisToWait = Integer.MAX_VALUE;
synchronized (lock) {
if (!mEventBuffer.isEmpty()) {
long now = System.nanoTime();
long lowestTime = mEventBuffer.firstKey();
// Is it time for the earliest list to be processed?
if (lowestTime <= now) {
event = removeNextEventLocked(lowestTime);
break;
} else {
// Figure out how long to sleep until next event.
long nanosToWait = lowestTime - now;
// Add 1 millisecond so we don't wake up before it is
// ready.
millisToWait = 1 + (nanosToWait / NANOS_PER_MILLI);
// Clip 64-bit value to 32-bit max.
if (millisToWait > Integer.MAX_VALUE) {
millisToWait = Integer.MAX_VALUE;
}
}
}
lock.wait((int) millisToWait);
}
}
return event;
}
}
Oops, something went wrong.

0 comments on commit ad9ab66

Please sign in to comment.