Skip to content

Latest commit

 

History

History
104 lines (73 loc) · 2.79 KB

File metadata and controls

104 lines (73 loc) · 2.79 KB

Usage

Create an instance of ZenTone:

val zenTone = ZenTone()

ZenTone accepts 2 commonly-used arguments, each having a sensible default:

  1. sampleRate: SampleRate = SampleRate.Hz44100
  2. channelMask: 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.

Start playing audio with a frequency and volume:

zenTone.play(frequency = 400f, volume = 2)

play() accepts 4 arguments:

  1. frequency: Float
  2. volume: Int. It ranges from 0 to 100, where 0 is no audio and 100 is full volume.
  3. playbackCount: Int = 0. Use 0 for unlimited playback, 1 to play the generated signal once, 2 to play it twice, and so on.
  4. waveByteArrayGenerator: WaveByteArrayGenerator = SineWaveGenerator(), here SineWaveGenerator() is a sensible default.
    • Possible options are SineWaveGenerator(), SquareWaveGenerator(), TriangleWaveGenerator(), SawtoothWaveGenerator() and PulseWaveGenerator()

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))

Stop playing audio:

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()
}

Check if ZenTone is playing audio by querying the isPlaying flag:

val isPlaying = zenTone.isPlaying

Toggle playback

zenTone.togglePlayback(frequency = 440f, volume = 10)

togglePlayback() accepts 3 arguments:

  1. frequency: Float
  2. volume: Int. It ranges from 0 to 100, where 0 is no audio and 100 is full volume.
  3. playbackCount: Int = 0. Use 0 for unlimited playback or a positive number to stop automatically after that many signal writes.