Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- val duration = 10 // duration of sound
- val sampleRate = 22050 // Hz (maximum frequency is 7902.13Hz (B8))
- val numSamples = duration * sampleRate
- val samples = DoubleArray(numSamples)
- val buffer = ShortArray(numSamples)
- var note :IntArray = IntArray(buffer.lastIndex)
- var i :Int = 0
- while (i < buffer.lastIndex){
- i++
- samples[i] = Math.sin(2.0 * Math.PI * i.toDouble() / (sampleRate / note[0])) // Sine wave
- buffer[i] = (samples[i] * java.lang.Short.MAX_VALUE).toShort() // Higher amplitude increases volume
- }
- var audioTrack = AudioTrack(
- AudioManager.STREAM_MUSIC,
- sampleRate, AudioFormat.CHANNEL_OUT_MONO,
- AudioFormat.ENCODING_PCM_16BIT, buffer.lastIndex,
- AudioTrack.MODE_STATIC)
- audioTrack.write(buffer, 0, buffer.lastIndex)
- audioTrack.play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement