val zenTone = ZenTone()ZenTone accepts 2 commonly-used arguments, each having a sensible default:
sampleRate:SampleRate=SampleRate.Hz44100channelMask: Int = AudioFormat.CHANNEL_OUT_MONO
based on your requirement, you can pass a different value when instantiating ZenTone i.e
val zenTone = ZenTone(channelMask = AudioFormat.CHANNEL_OUT_STEREO)To choose a different supported sample rate:
val zenTone = ZenTone(sampleRate = SampleRate.Hz48000)ZenTone uses AudioFormat.ENCODING_PCM_16BIT by default.
If you need to override the encoding, use the advanced factory:
val zenTone = ZenTone.advanced(
sampleRate = SampleRate.Hz44100,
encoding = AudioFormat.ENCODING_PCM_8BIT,
channelMask = AudioFormat.CHANNEL_OUT_MONO
)Currently, ZenTone supports AudioFormat.ENCODING_PCM_8BIT and AudioFormat.ENCODING_PCM_16BIT output, with mono or stereo channel masks.
zenTone.play(frequency = 400f, volume = 2)play() accepts 4 arguments:
frequency: Floatvolume: Int. It ranges from 0 to 100, where 0 is no audio and 100 is full volume.playbackCount: Int =0. Use0for unlimited playback,1to play the generated signal once,2to play it twice, and so on.waveByteArrayGenerator:WaveByteArrayGenerator = SineWaveGenerator(), hereSineWaveGenerator()is a sensible default.- Possible options are
SineWaveGenerator(),SquareWaveGenerator(),TriangleWaveGenerator(),SawtoothWaveGenerator()andPulseWaveGenerator()
- Possible options are
based on your requirement, you can pass a different value when calling play() i.e
zenTone.play(frequency = 440f,
playbackCount = 1,
volume = 10,
waveByteArrayGenerator = SquareWaveGenerator())You can also configure the pulse width explicitly:
zenTone.play(frequency = 440f,
volume = 10,
waveByteArrayGenerator = PulseWaveGenerator(dutyCycle = 0.25))zenTone.stop()To release resources held by ZenTone i.e release mic, you can call release() function.
Usually you'll need to call this in onDestroy():
override fun onDestroy() {
super.onDestroy()
zenTone.release()
}val isPlaying = zenTone.isPlayingzenTone.togglePlayback(frequency = 440f, volume = 10)togglePlayback() accepts 3 arguments:
frequency: Floatvolume: Int. It ranges from 0 to 100, where 0 is no audio and 100 is full volume.playbackCount: Int =0. Use0for unlimited playback or a positive number to stop automatically after that many signal writes.