package jp.co.sylc.android.Thereminvox;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.openintents.filemanager.FileManagerActivity;
import jp.co.sylc.android.Thereminvox.sound.midi.MIDI;
import jp.co.sylc.android.Thereminvox.sound.midi.MIDIEventListener;
import jp.co.sylc.android.Thereminvox.sound.midi.MIDIPlayer;
import jp.co.sylc.android.Thereminvox.sound.midi.MetaEvent;
import jp.co.sylc.android.Thereminvox.sound.midi.SysEXEvent;
import jp.co.sylc.android.Thereminvox.sound.midi.channelmessage.ChannelMessage;
import jp.co.sylc.android.Thereminvox.sound.midi.channelmessage.NoteOff;
import jp.co.sylc.android.Thereminvox.sound.midi.channelmessage.NoteOn;
import jp.co.sylc.android.Thereminvox.sound.wav.RectWave;
import jp.co.sylc.android.Thereminvox.sound.wav.SawWave;
import jp.co.sylc.android.Thereminvox.sound.wav.SineWave;
import jp.co.sylc.android.Thereminvox.sound.wav.Wave;
import jp.co.sylc.android.Thereminvox.sound.wav.WaveManager;
import jp.co.sylc.android.handler.GameHandler;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
/**
* :
* 1919
* Thereminvox
* Theremin
*
*
* @author yagi
*
*/
public class Thereminvox extends Activity {
private final static String TAG = Thereminvox.class.getSimpleName();
//
private final static int TONE_MENU = 0;
private final static int SETTING = 1;
private final static int HELP = 2;
private final static int OPEN = 3;
//
private final static int INTENT_SETTING = 0;
private final static int FILE_OPEN = 1;
/**
*
*/
private WaveManager manager = null;
/**
*
*/
private ThereminvoxSensor sensor = null;
/**
*
*/
private GameHandler handler = null;
private Dialog toneDialog = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//
this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
this.manager = new WaveManager();
this.manager.setGain(0.3, 0);
this.manager.start();
this.sensor = new ThereminvoxSensor(this, this.manager);
View v = new ThereminvoxView(this, this.manager, this.sensor);
this.setContentView(v);
this.handler = new ViewHandle(v, 10);
this.handler.start();
}
private Dialog makeDialog() {
final LayoutInflater mInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Wave[] waveList = new Wave[] { new SineWave(manager.getHz(), manager.getBit()), new SawWave(manager.getHz(), manager.getBit()), new RectWave(manager.getHz(), manager.getBit()) };
ArrayAdapter<Wave> adapter = new ArrayAdapter<Wave>(this, android.R.layout.simple_list_item_single_choice, waveList) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int resouceId = android.R.layout.simple_list_item_single_choice;
View view;
TextView text;
if (convertView == null) {
Log.d(TAG, "cotentView == null");
view = mInflater.inflate(resouceId, parent, false);
}
else {
view = convertView;
}
try {
text = (TextView) view;
} catch (ClassCastException e) {
Log.e("ArrayAdapter", "You must supply a resource ID for a TextView");
throw new IllegalStateException("ArrayAdapter requires the resource ID to be a TextView", e);
}
Wave i = this.getItem(position);
if (i == null) {
Log.d(TAG, "sche null!!");
}
else {
text.setTextSize(30);
text.setTextColor(Color.WHITE);
text.setText(i.getName());
if (manager.getWave(0).getName().equals(i.getName()))
((CheckedTextView) view).setChecked(true);
else
((CheckedTextView) view).setChecked(false);
}
return view;
}
};
//
ListView view = new ListView(this);
view.setAdapter(adapter);
view.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> list, View parent, int position, long id) {
Wave w = (Wave) list.getItemAtPosition(position);
manager.setWave(w, 0);
toneDialog.dismiss();
}
});
this.toneDialog = new AlertDialog.Builder(this).setView(view).create();
return this.toneDialog;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, TONE_MENU, 0, "Tone").setIcon(android.R.drawable.ic_menu_more);
menu.add(0, SETTING, 0, "Setting").setIcon(android.R.drawable.ic_menu_preferences);
menu.add(0, HELP, 0, "Help").setIcon(android.R.drawable.ic_menu_help);
menu.add(0, OPEN, 0, "Midi Open").setIcon(android.R.drawable.ic_menu_set_as);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case TONE_MENU:
makeDialog().show();
break;
case SETTING: {
Intent intent = new Intent(this, Setting.class);
startActivityForResult(intent, INTENT_SETTING);
}
break;
case HELP:{
Intent intent = new Intent(this, ThereminvoxHelp.class);
startActivity(intent);
}
break;
case OPEN: {
/*
* Intent intent = new Intent(this, FileManagerActivity.class);
* startActivityForResult(intent, FILE_OPEN);
*/
this.sensor.onPause();
this.manager.clear();
this.manager.play();
this.open();
}
break;
}
return true;
}
/**
* Intent
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
this.manager.end();
this.sensor.onStop();
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause");
this.sensor.onPause();
this.manager.pause();
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
this.sensor.onResume();
this.manager.play();
}
private void open() {
Thread t = new Thread() {
@Override
public void run() {
try {
File f = new File("/sdcard/MIDI/m160.mid");
final MIDIPlayer player = new MIDIPlayer(f);
player.setTempo(90);
player.addMIDIEventListener(new MIDIEventListener() {
private Map<Integer, Wave> map = new HashMap<Integer, Wave>();
@Override
public void onChannelMessage(ChannelMessage msg) {
if (msg instanceof NoteOn) {
NoteOn on = (NoteOn) msg;
if(!this.map.containsKey(on.getNote())){
double hz = MIDI.noteToHelz(on.getNote());
Wave w = new SineWave(WaveManager.SAMPLE_RATE, WaveManager.SAMPLE_SIZE);
w.setPitch(hz);
w.setGain(0.2);
manager.add(w);
this.map.put(on.getNote(), w);
Log.d(TAG, "ON:: note:" + on.getNote() + " hz:" + hz);
//manager.setPitch(hz, 0);
}
}
else if (msg instanceof NoteOff) {
NoteOff off = (NoteOff) msg;
Log.d(TAG, off.toString());
if(this.map.containsKey(off.getNote())){
manager.remove(this.map.get(off.getNote()));
this.map.remove(off.getNote());
}
}
}
@Override
public void onMetaEvent(MetaEvent event) {
}
@Override
public void onSysExEvent(SysEXEvent event) {
}
});
player.play();
} catch (Exception e) {
e.printStackTrace();
}
Wave w = new SineWave(WaveManager.SAMPLE_RATE, WaveManager.SAMPLE_SIZE);
w.setGain(0.5);
manager.add(w);
sensor.onResume();
}
};
t.start();
}
/**
* TODO
*
* @author yagi
*
*/
private class ViewHandle extends GameHandler {
public ViewHandle(View v, int f) {
super(v, f);
}
@Override
public boolean loop(Message msg) {
return true;
}
}
}
|