안드로이드에서 Synth 화음을 쉽게 생성하는 방법은 무엇입니까? 8bit를 사용하여 게임 음악을 동적으로 생성하고 싶습니다. AudioTrack으로 시도했지만 아직 좋은 사운드는 얻지 못했습니다.안드로이드에서 Synth 화음을 쉽게 생성하는 방법은 무엇입니까?
예제가 있습니까?
내가 성공하지 않고 다음 코드를 시도 :
public class BitLoose {
private final int duration = 1; // seconds
private final int sampleRate = 4200;
private final int numSamples = duration * sampleRate;
private final double sample[] = new double[numSamples];
final AudioTrack audioTrack;
public BitLoose() {
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_8BIT, numSamples,
AudioTrack.MODE_STREAM);
audioTrack.play();
}
public void addTone(final int freqOfTone) {
// fill out the array
for (int i = 0; i < numSamples; ++i) {
sample[i] = Math.sin(2 * Math.PI * i/(sampleRate/freqOfTone));
}
// convert to 16 bit pcm sound array
// assumes the sample buffer is normalised.
final byte generatedSnd[] = new byte[numSamples];
int idx = 0;
for (final double dVal : sample) {
// scale to maximum amplitude
final short val = (short) ((((dVal * 255))) % 255);
// in 16 bit wav PCM, first byte is the low order byte
generatedSnd[idx++] = (byte) (val);
}
audioTrack.write(generatedSnd, 0, sampleRate);
}
public void stop() {
audioTrack.stop();
}