# Generating audio on the fly

**URL:** <https://qa.fmod.com/t/generating-audio-on-the-fly/13023>\
**Category:** FMOD Engine\
**Created:** [March 8, 2017, 3:16pm UTC](https://qa.fmod.com/t/generating-audio-on-the-fly/13023 "2017-03-08T15:16:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![misop](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@misop](https://qa.fmod.com/u/misop)\
**Post date:** [March 8, 2017, 3:16pm UTC](https://qa.fmod.com/t/generating-audio-on-the-fly/13023/1 "2017-03-08T15:16:37Z")

</div>

Hi Everyone,

I have an application in which I want to generate drawing sounds based on input from a Wacom stylus.

So far I am stuck at synchronizing the generated sound to input from the pen. I tried to use both pcmreadcallback and a custom memory buffer. In both cases I started with larger buffer size (to test if everything works) and then reduced the buffer size to minimize latency. However, when using 1000 Hz audio as input I was unable to go below a buffer size of 20 samples, i.e. ~20 ms of latency, without audible artifacts. This is a serious issue for fast strokes as they get registered only after they end. I don’t think the issue is in my pcmreadcallback implementation as one execution takes ~0.4 ns.

I wanted to ask if fmod has some low latency mode. Ideally I would like to be able to directly stream data to fmod at 1000 Hz. Then I could update this data with input from the stylus on the fly.

---

<div class="post-metadata">

**Author:** ![brett](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/brett/32/261_2.png) [@brett](https://qa.fmod.com/u/brett)\
**Post date:** [March 12, 2017, 5:52am UTC](https://qa.fmod.com/t/generating-audio-on-the-fly/13023/2 "2017-03-12T05:52:51Z")

</div>

The best latency way to get audio into FMOD is to use a custom dsp effect, you can see an example in the dsp\_custom example.

20 samples @ 48khz is .4ms , not 20ms btw.

It is better not to have extreme small buffer sizes, and just update with your own low granularity inside one of fmod’s blocks. If a block of data in fmod is 256 samples, you can still update your own parameters at 128 or 64 or 4 samples at a time if you want. This is what FMOD Studio does to get sample accurate playback.

256 samples is a good block size anyway as it is only 5ms which is pretty imperceptable.

---

<div class="post-metadata">

**Author:** ![misop](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@misop](https://qa.fmod.com/u/misop)\
**Post date:** [March 13, 2017, 8:58am UTC](https://qa.fmod.com/t/generating-audio-on-the-fly/13023/3 "2017-03-13T08:58:00Z")

</div>

> [@](#):
>
> The best latency way to get audio into FMOD is to use a custom dsp effect, you can see an example in the dsp\_custom example.
> 
> 20 samples @ 48khz is .4ms , not 20ms btw.
> 
> It is better not to have extreme small buffer sizes, and just update with your own low granularity inside one of fmod’s blocks. If a block of data in fmod is 256 samples, you can still update your own parameters at 128 or 64 or 4 samples at a time if you want. This is what FMOD Studio does to get sample accurate playback.
> 
> 256 samples is a good block size anyway as it is only 5ms which is pretty imperceptable.

Thanks!  
Custom dsp seems to be the correct way to go. Though I believe the delay in the code is caused by my settings. I use the generated audio to drive a vibration motor. That’s why I use 20 samples @ 1khz. But I guess fmod has to output to the sound card at 48khz and that is introducing the problems.
