在Android和IOS上控制在背景中运行的音乐合成器

时间:2014-11-08 23:07:36

标签: android ios midi osc

我想创建一个我自己的用户界面,发出类似MIDI的信号(或OSC或其他),这些信号会导致拇指带或车库频段或我在ipad(或Galaxy)上安装的其他程序播放音乐我正在创造。我怎么做?有哪些方法可供选择。我必须使用什么编程语言来创建我的“控制器”应用程序?什么API。什么是实现这一目标的最快方法。

看起来OSC就是我应该做的,至少对于iPad而言,但在我看来它只适用于网络上的机器。我希望它可以在运行的应用程序之间工作,就像我从Jordan Rudess看到的'SampleWiz控制另一个程序或https://www.youtube.com/watch?v=ZMyRS9y20mw thumbjam控制sampleTank(我现在再看一下那个视频并看到它的MIDI)

1 个答案:

答案 0 :(得分:0)

这是一个准系统Android项目,它创建一个MIDI文件,然后可以由外部MIDI合成器播放。出于测试目的,包括playNewMIDIFile()。这使用Android自己的MediaPlayer来播放新文件。

可以找到包含此摘录的原始问题here

package com.example.midi;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;

public class Main extends Activity {

  private String file = "midi.mid";
  private MediaPlayer mediaPlayer;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    createNewMIDIFile();
    playNewMIDIFile();
  }

  public void createNewMIDIFile() {
    Integer[] stream = new Integer[]{
        //
        0x4d, 0x54, 0x68, 0x64, // MThd = MIDI file designator
        0x00, 0x00, 0x00, 0x06, // Standard MIDI File (SMF)
        0x00, 0x01, 0x00, 0x02, // multiple-track format: 2 tracks
        0x00, 0x40, // 64 ticks per beat (quarter note)
        0x4D, 0x54, 0x72, 0x6B, // Header for track 1
        0x00, 0x00, 0x00, 0x0B, // 11  bytes to describe the track
        0x00, 0xFF, 0x51, 0x03, // set tempo:
        0x0F, 0x42, 0x40, //  1,000,000 microseconds / beat: 60 bpm
        0x00, 0xFF, 0x2F, 0x00, // End of track 1
        0x4D, 0x54, 0x72, 0x6B, // Header for track 2
        0x00, 0x00, 0x00, 0x0F, // 15 bytes to describe the track
        0x00, // Immediately
        0xC1, 0x01, // change instrument for track 2 to piano
        0x00, // Immediately
        0x91, 0x3C, 0x7F, // play middle C with a velocity of 127
        0x30, // 48 ticks later (dotted eighth note)
        0x81, 0x3C, 0x00, // stop playing the middle C
        0x00, 0xFF, 0x2F, 0x00 // End of track 2
    };

    int length = stream.length;
    byte[] byteStream = new byte[length];
    for (int ii = 0; ii < length; ii++) {
      byteStream[ii] = (byte) (stream[ii] % 256);
    }

    try {
      FileOutputStream outputStream = openFileOutput(file, MODE_PRIVATE);
      outputStream.write(byteStream);
      outputStream.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void play(View view) {
  /* Triggered by a button defined in activity_main.xml as 
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="play"
    android:text="Play MIDI" />
  */
    playNewMIDIFile();
  }

  public void playNewMIDIFile() {
    try {
      String filename = getFilesDir() + "/" + file;
      File midifile = new File(filename);
      FileInputStream inputStream = new FileInputStream(midifile);
      FileDescriptor fileDescriptor = inputStream.getFD();
      mediaPlayer.reset();
      mediaPlayer.setDataSource(fileDescriptor);
      inputStream.close();
      mediaPlayer.prepare();
      mediaPlayer.start();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}