概要
vistaでandroid studio 1.0.1やってみた。
AudioTrack使ってみた。
参考にしたページ
サンプルコード
package com.ohisamallc.ohiapp154.ohiapp154;
import java.util.Arrays;
/**
* Created by ore on 22/08/30.
*/
public class Oscillator {
private double frequency = 440;
private double f0 = 280;
private double[] buffer;
private double t = 0;
private double sampleRate;
private Vowel targetVowel = Vowel.A;
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[] f = new double[6];
Arrays.fill(f, 0);
if (targetVowel == Vowel.A)
{
f[0] = 0.19 * Math.sin(2 * Math.PI * t * f0 * 4);
f[1] = 0.09 * Math.sin(2 * Math.PI * t * f0 * 2);
f[2] = 0.08 * Math.sin(2 * Math.PI * t * f0 * 3);
f[3] = 0.08 * Math.sin(2 * Math.PI * t * f0 * 5);
f[4] = 0.07 * Math.sin(2 * Math.PI * t * f0);
f[5] = 0.07 * Math.sin(2 * Math.PI * t * f0 * 6);
}
else if (targetVowel == Vowel.I)
{
f[0] = 0.52 * Math.sin(2 * Math.PI * t * f0);
f[1] = 0.03 * Math.sin(2 * Math.PI * t * f0 * 2);
f[2] = 0.02 * Math.sin(2 * Math.PI * t * f0 * 11);
f[3] = 0.02 * Math.sin(2 * Math.PI * t * f0 * 13);
f[4] = 0.01 * Math.sin(2 * Math.PI * t * f0 * 12);
}
else if (targetVowel == Vowel.U)
{
f[0] = 0.32 * Math.sin(2 * Math.PI * t * f0);
f[1] = 0.13 * Math.sin(2 * Math.PI * t * f0 * 6);
f[2] = 0.11 * Math.sin(2 * Math.PI * t * f0 * 2);
f[3] = 0.02 * Math.sin(2 * Math.PI * t * f0 * 5);
f[4] = 0.02 * Math.sin(2 * Math.PI * t * f0 * 4);
}
else if (targetVowel == Vowel.E)
{
f[0] = 0.18 * Math.sin(2 * Math.PI * t * f0);
f[1] = 0.14 * Math.sin(2 * Math.PI * t * f0 * 2);
f[2] = 0.13 * Math.sin(2 * Math.PI * t * f0 * 3);
f[3] = 0.03 * Math.sin(2 * Math.PI * t * f0 * 11);
f[4] = 0.03 * Math.sin(2 * Math.PI * t * f0 * 4);
}
else if (targetVowel == Vowel.O)
{
f[0] = 0.24 * Math.sin(2 * Math.PI * t * f0 * 4);
f[1] = 0.14 * Math.sin(2 * Math.PI * t * f0 * 2);
f[2] = 0.11 * Math.sin(2 * Math.PI * t * f0);
f[3] = 0.10 * Math.sin(2 * Math.PI * t * f0 * 3);
}
buffer[i] = f[0] + f[1] + f[2] + f[3] + f[4] + f[5];
t += 1 / sampleRate;
}
return buffer;
}
public void reset() {
t = 0;
targetVowel = targetVowel.next();
}
enum Vowel {
A {
public Vowel next() {
return I;
}
},
I {
public Vowel next() {
return U;
}
},
U {
public Vowel next() {
return E;
}
},
E {
public Vowel next() {
return O;
}
},
O {
public Vowel next() {
return A;
}
};
abstract Vowel next();
}
}
以上。