vistaでandroid studio その7

# 概要
vistaでandroid studio 1.0.1やってみた。
AudioTrack使ってみた。

# 参考にしたページ

https://qiita.com/niusounds/items/beb0140117b9d3325ce7

# サンプルコード

```
package com.ohisamallc.ohiapp154.ohiapp154;

/**
 * Created by ore on 22/08/30.
 */
public class Oscillator {
	private double frequency = 440;
	private double[] buffer;
	private double t = 0;
	private double sampleRate;
	public Oscillator(int bufferSize, int sampleRate) {
		buffer = new double[bufferSize];
		this.sampleRate = sampleRate;
	}
	public double[] nextBuffer() {
		for (int i = 0; i < buffer.length; i++)
		{
			double sin = Math.sin(2 * Math.PI * t * frequency);
			buffer[i] = sin > 0 ? 1 : -1;
			t += 1 / sampleRate;
		}
		return buffer;
	}
	public void reset() {
		t = 0;
	}
}
```

```
package com.ohisamallc.ohiapp154.ohiapp154;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.TextView;
import android.view.View;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class MainActivity extends ActionBarActivity {
	private static final int STREAM_TYPE = AudioManager.STREAM_MUSIC;
	private static final int SAMPLE_RATE = 44100;
	private static final int CHANNEL_CONFIG = AudioFormat.CHANNEL_OUT_MONO;
	private static final int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
	private static final int BUFFER_SIZE = AudioTrack.getMinBufferSize(SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT);
	private static final int MODE = AudioTrack.MODE_STREAM;
	private Oscillator osc = new Oscillator(BUFFER_SIZE, SAMPLE_RATE);
	private AudioTrack track;
	private Thread backgroundThread;
	private boolean running;
	private boolean touching;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_main, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings)
		{
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	@Override
	protected void onResume() {
		super.onResume();
		track = new AudioTrack(STREAM_TYPE, SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT, BUFFER_SIZE, MODE);
		startBackgroundThread();
	}
	@Override
	protected void onPause() {
		stopBackgroundThread();
		track.release();
		track = null;
		super.onPause();
	}
	private void startBackgroundThread() {
		running = true;
		track.play();
		osc.reset();
		backgroundThread = new Thread() {
			@Override
			public void run() {
				short[] sBuffer = new short[BUFFER_SIZE];
				while (running)
				{
					if (touching)
					{
						double[] dBuffer = osc.nextBuffer();
						for (int i = 0; i < BUFFER_SIZE; i++)
						{
							sBuffer[i] = (short) (dBuffer[i] * Short.MAX_VALUE);
						}
					}
					else
					{
						for (int i = 0; i < BUFFER_SIZE; i++)
						{
							sBuffer[i] = 0;
						}
					}
					track.write(sBuffer, 0, BUFFER_SIZE);
				}
			}
		};
		backgroundThread.start();
	}
	private void stopBackgroundThread() {
		running = false;
		track.stop();
	}
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction())
		{
		case MotionEvent.ACTION_DOWN:
			 touching = true;
		break;
		case MotionEvent.ACTION_UP:
			 touching = false;
			 osc.reset();
		break;
		}
		return super.onTouchEvent(event);
	}
}


```

以上。