Android Open Source - BLE-MIDI-for-Android Sound Maker






From Project

Back to project page BLE-MIDI-for-Android.

License

The source code is released under:

Apache License

If you think the Android project BLE-MIDI-for-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package jp.kshoji.blemidi.sample.util;
/*from   w  ww.  j a v  a2 s.  co m*/
import java.util.Set;

/**
 * Simple sound synthesizer
 * 
 * @author K.Shoji
 */
public class SoundMaker {
  private static final SoundMaker instance;

  private static double sinTable[] = new double[1024];
  private static double sawTable[] = new double[1024];
  private static double triangleTable[] = new double[1024];
  private static double squareTable[] = new double[1024];

  private static int iter = 0;

  private static final int samplingRate = 44100 / 2;

  /**
   * Initializes wave table
   */
  static {
    for (int i = 0; i < 1024; i++) {
      sawTable[i] = (i / 512.0) - 1.0;
      if (i < 256) {
        triangleTable[i] = (double) i / 256;
      } else if (i < 768) {
        triangleTable[i] = (512.0 - i) / 256.0;
      } else {
        triangleTable[i] = (i - 768.0) / 256.0;
      }
      squareTable[i] = Math.sin(Math.PI * i / 512.0) > 0.0 ? 1.0 : -1.0;
      sinTable[i] = Math.sin(Math.PI * i / 512.0);
    }
    instance = new SoundMaker();
  }

  public static SoundMaker getInstance() {
    return instance;
  }

  public int getSamplingRate() {
    return samplingRate;
  }

  /**
   * @return -1.0 to 1.0
   */
  public double makeWaveStream(Set<Tone> tones) {
    iter++;

    double output = 0.0;
    for (Tone tone : tones) {
      switch (tone.getForm()) {
      case Tone.FORM_SIN:
        output += sinTable[(int) (1024 * tone.getFrequency() / samplingRate * iter) % 1024] * tone.getVolume();
        break;
      case Tone.FORM_SAW:
        output += sawTable[(int) (1024 * tone.getFrequency() / samplingRate * iter) % 1024] * tone.getVolume();
        break;
      case Tone.FORM_SQUARE:
        output += squareTable[(int) (1024 * tone.getFrequency() / samplingRate * iter) % 1024] * tone.getVolume();
        break;
      case Tone.FORM_TRIANGLE:
        output += triangleTable[(int) (1024 * tone.getFrequency() / samplingRate * iter) % 1024] * tone.getVolume();
        break;
      default:
        // do nothing.
        break;
      }
    }

    return output;
  }

}




Java Source Code List

jp.kshoji.blemidi.central.BleMidiCallback.java
jp.kshoji.blemidi.central.BleMidiCentralProvider.java
jp.kshoji.blemidi.central.MidiInputDevice.java
jp.kshoji.blemidi.central.MidiOutputDevice.java
jp.kshoji.blemidi.device.MidiInputDevice.java
jp.kshoji.blemidi.device.MidiOutputDevice.java
jp.kshoji.blemidi.listener.OnMidiDeviceAttachedListener.java
jp.kshoji.blemidi.listener.OnMidiDeviceDetachedListener.java
jp.kshoji.blemidi.listener.OnMidiInputEventListener.java
jp.kshoji.blemidi.listener.OnMidiScanStatusListener.java
jp.kshoji.blemidi.peripheral.BleMidiPeripheralProvider.java
jp.kshoji.blemidi.peripheral.MidiInputDevice.java
jp.kshoji.blemidi.peripheral.MidiOutputDevice.java
jp.kshoji.blemidi.sample.CentralActivity.java
jp.kshoji.blemidi.sample.PeripheralActivity.java
jp.kshoji.blemidi.sample.util.SoundMaker.java
jp.kshoji.blemidi.sample.util.Tone.java
jp.kshoji.blemidi.util.BleMidiDeviceUtils.java
jp.kshoji.blemidi.util.BleUtils.java
jp.kshoji.blemidi.util.BleUuidUtils.java
jp.kshoji.blemidi.util.Constants.java
jp.kshoji.blemidi.util.MidiParser.java
jp.kshoji.blemidi.util.ReusableByteArrayOutputStream.java