# Streaming Procedural Audio

**URL:** <https://qa.fmod.com/t/streaming-procedural-audio/18226>\
**Category:** Unity\
**Created:** [January 17, 2022, 3:55pm UTC](https://qa.fmod.com/t/streaming-procedural-audio/18226 "2022-01-17T15:55:57Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![martindevans1](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/martindevans1/32/3052_2.png) [@martindevans1](https://qa.fmod.com/u/martindevans1)\
**Post date:** [January 17, 2022, 3:55pm UTC](https://qa.fmod.com/t/streaming-procedural-audio/18226/1 "2022-01-17T15:55:57Z")

</div>

I’m working on an integration for Dissonance Voice Chat which plays out audio through FMOD instead of the normal Unity audio system. I’m having some trouble working out the best way to play streaming audio.

My initial approach has been to create a DSP with a read callback which pumps the Dissonance audio system for however much audio it needs ([see example code here](https://gist.github.com/martindevans/183555c065d61207c9db4d9a3bd48691)). This is roughly how Dissonance noramlly works (normally it uses the [OnAudioFilterRead](https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnAudioFilterRead.html) Unity callback).

However, when the FMOD editor is attached with Live Update to the Unity editor sound played in this way does not appear anywhere in the mixer.

I tried playing the DSP out to a specific bus:

```auto
RuntimeManager.StudioSystem.getBus("bus:/Test", out var bus);
bus.getChannelGroup(out channel);
// pass `channel` in to playDSP method

```

but this doesn’t help.

I’ve also experimented with creating a `Sound` with a `PCMReadCallback` but haven’t got that working yet (constantly crashing the editor).

What’s the correct way to do this?

---

<div class="post-metadata">

**Author:** ![fendercodes](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/fendercodes/32/2971_2.png) [@fendercodes](https://qa.fmod.com/u/fendercodes)\
**Post date:** [January 22, 2022, 9:02pm UTC](https://qa.fmod.com/t/streaming-procedural-audio/18226/2 "2022-01-22T21:02:32Z")

</div>

Bump. Would also love to know how to do this.

---

<div class="post-metadata">

**Author:** ![jeff\_fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/jeff_fmod/32/1766_2.png) [@jeff\_fmod](https://qa.fmod.com/u/jeff_fmod)\
**Post date:** [January 23, 2022, 11:43pm UTC](https://qa.fmod.com/t/streaming-procedural-audio/18226/3 "2022-01-23T23:43:23Z")

</div>

After grabbing your source pcm data, you can pass it to FMOD either in a `PCMReadCallback` or by copying it into a `Sound` and playing that back as a programmer sound.  
To copy into a sound you can use [`Sound.lock`](https://fmod.com/resources/documentation-api?version=2.02&page=core-api-sound.html#sound_lock) to get a pointer to the sound’s buffer. Then you copy your pcm data into the buffer and unlock the sound with [`Sound.unlock`](https://fmod.com/resources/documentation-api?version=2.02&page=core-api-sound.html#sound_unlock). There has been some discussion about this topic recently in [another thread](https://qa.fmod.com/t/using-fmod-with-voice-chat-utilizing-onaudiofilterread/17763/5) which is worth reading, and has examples of both the `Sound.lock` approach. They ended up having issues with `OnAudioFliterRead` and this is due to an old [Unity bug](https://issuetracker.unity3d.com/issues/fmod-not-calculating-mixing-matrices-for-onaudiofilterread-script) that they have no plan of solving, but that shouldn’t be an issue in your case if you aren’t using `OnAudioFilterRead`.

---

<div class="post-metadata">

**Author:** ![martindevans1](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/martindevans1/32/3052_2.png) [@martindevans1](https://qa.fmod.com/u/martindevans1)\
**Post date:** [January 24, 2022, 4:33pm UTC](https://qa.fmod.com/t/streaming-procedural-audio/18226/4 "2022-01-24T16:33:01Z")

</div>

If I create and play a `Sound` object like this:

```auto
RuntimeManager.CoreSystem.playSound(_sound, default(ChannelGroup), false, out _channel);

```

Will that show up in the FMOD studio mixer somewhere? If not, should I direct it to a specific ChannelGroup for that, or must I use a programmer instrument to play the sound instead?

---

<div class="post-metadata">

**Author:** ![TorutheRedFox](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/torutheredfox/32/5255_2.png) [@TorutheRedFox](https://qa.fmod.com/u/TorutheRedFox)\
**Post date:** [January 24, 2022, 4:55pm UTC](https://qa.fmod.com/t/streaming-procedural-audio/18226/5 "2022-01-24T16:55:46Z")

</div>

you need to get the channel group out of the mixer group and pass that to playSound using something like this

```auto
    FMOD.Studio.Bus bus = FMODUnity.RuntimeManager.GetBus("bus:/master/music");
    bus.lockChannelGroup(); // make sure to unlock it once you're done using it
     
    // this is to make sure the bus is loaded before you get the channel group
    // otherwise an error will be thrown and FMOD will enter an unstable state
    FMODUnity.RuntimeManager.StudioSystem.flushCommands();
    bus.getChannelGroup(out channelGroup);

```

I did it for mixing music and it works well

---

<div class="post-metadata">

**Author:** ![martindevans1](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/martindevans1/32/3052_2.png) [@martindevans1](https://qa.fmod.com/u/martindevans1)\
**Post date:** [January 25, 2022, 4:01pm UTC](https://qa.fmod.com/t/streaming-procedural-audio/18226/6 "2022-01-25T16:01:37Z")

</div>

That worked! I would never have figured out locking/flushing without your hint. Thanks so much ♥

I have one final hurdle though.

Since this is a VoIP system, audio _must_ be played back at all times (to keep in sync and prevent buffers filling up etc). However at the moment if the bus is muted in the Studio mixer `pcmreadcallback` stops being called (and it’s the same if I use a `Dsp` for playback as well).

Is it possible to tell FMOD to never stop pumping a bus for audio, even when it’s muted?

---

<div class="post-metadata">

**Author:** ![jeff\_fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/jeff_fmod/32/1766_2.png) [@jeff\_fmod](https://qa.fmod.com/u/jeff_fmod)\
**Post date:** [January 26, 2022, 9:27pm UTC](https://qa.fmod.com/t/streaming-procedural-audio/18226/7 "2022-01-26T21:27:34Z")

</div>

We don’t have a way to explicitly tell FMOD to continue running the pcm read callback when muted. Rather than muting, you can try using a parameter to turn the volume down to 99.99% (-79db), which would be inaudible, but prevent the voice from going virtual and losing sync, and also allow the pcm read callback to continue running.

---

<div class="post-metadata">

**Author:** ![martindevans1](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/martindevans1/32/3052_2.png) [@martindevans1](https://qa.fmod.com/u/martindevans1)\
**Post date:** [January 27, 2022, 11:21pm UTC](https://qa.fmod.com/t/streaming-procedural-audio/18226/8 "2022-01-27T23:21:13Z")

</div>

Unfortunately that doesn’t really solve the problem - I was trying to avoid VoIP desyncing when the mixer is muted in FMOD studio (e.g. another Bus is soloed to work on a specific audio effect). It’s not a huge issue though, for now I can just tell people working in FMOD to expect desyncs (i.e. it won’t be a problem for players).

---

<div class="post-metadata">

**Author:** ![jeff\_fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/jeff_fmod/32/1766_2.png) [@jeff\_fmod](https://qa.fmod.com/u/jeff_fmod)\
**Post date:** [January 31, 2022, 11:03pm UTC](https://qa.fmod.com/t/streaming-procedural-audio/18226/9 "2022-01-31T23:03:01Z")

</div>

I see, we are planning on fixing the virtual voice sync issue in a future release, unfortunately there’s currently no workaround if you want to mute from the mixer.

---

<div class="post-metadata">

**Author:** ![martindevans1](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/martindevans1/32/3052_2.png) [@martindevans1](https://qa.fmod.com/u/martindevans1)\
**Post date:** [February 1, 2022, 5:32pm UTC](https://qa.fmod.com/t/streaming-procedural-audio/18226/10 "2022-02-01T17:32:24Z")

</div>

Unfortunately I think I’ve just bumped into the same problem from a different angle - when the voice is muted due to distance (e.g. `rolloff = Linear, Distance > maxdistance`) it seems that the bus is suspended and the same desync issue happens.

For now I can work around this (cut off VoIP before ever getting to the max distance), but do you have any idea when that fix you mentioned might be available?

---

<div class="post-metadata">

**Author:** ![jeff\_fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/jeff_fmod/32/1766_2.png) [@jeff\_fmod](https://qa.fmod.com/u/jeff_fmod)\
**Post date:** [February 2, 2022, 9:54pm UTC](https://qa.fmod.com/t/streaming-procedural-audio/18226/11 "2022-02-02T21:54:57Z")

</div>

I agree that sounds like the same problem again- there is no hard date on this fix, but it requires breaking changes so it is scheduled for a major release at this stage.
