Android'de Synth Chords Sesler nasıl kolayca üretilir? 8bit kullanarak oyun Müzik'te dinamik olarak üretebilmek istiyorum. AudioTrack ile denendi, ancak henüz güzel seslerin iyi sonuçları alınamadı.Android'de Synth Chords Sesler nasıl kolayca üretilir?
Orada örnek var mı?
Ben başarılı olamadı aşağıdaki kodu denedim:
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();
}