Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. val duration = 10 // duration of sound
  2. val sampleRate = 22050 // Hz (maximum frequency is 7902.13Hz (B8))
  3. val numSamples = duration * sampleRate
  4. val samples = DoubleArray(numSamples)
  5. val buffer = ShortArray(numSamples)
  6. var note :IntArray = IntArray(buffer.lastIndex)
  7.  
  8. var i :Int = 0
  9. while (i < buffer.lastIndex){
  10. i++
  11.  
  12. samples[i] = Math.sin(2.0 * Math.PI * i.toDouble() / (sampleRate / note[0])) // Sine wave
  13. buffer[i] = (samples[i] * java.lang.Short.MAX_VALUE).toShort() // Higher amplitude increases volume
  14.  
  15. }
  16. var audioTrack = AudioTrack(
  17. AudioManager.STREAM_MUSIC,
  18. sampleRate, AudioFormat.CHANNEL_OUT_MONO,
  19. AudioFormat.ENCODING_PCM_16BIT, buffer.lastIndex,
  20. AudioTrack.MODE_STATIC)
  21.  
  22. audioTrack.write(buffer, 0, buffer.lastIndex)
  23. audioTrack.play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement