Specifying OnAudioFilterRead()'s audiosource

I’m working on a music sequencer bundled with a synthesizer, using OnAudioFilterRead to generate notes. Currently i have a gameobject for every note with an audiosource attached, as well as the Note script which implements OnAudioFilterRead.

I’m afraid that this might cause problems such as too many gameobjects or audiosources with longer arrangements, therefore i would like to move away from this solution, preferably Note being a pure C# data class. (in that caseI’m not sure if i can implement OnAudioFilterRead without deriving Monobehaviour)

However, removing the audiosource from the Note’s gameobject causes OnAudioFilterRead to not generate any sound at all. I would like only a single, master audiosource with all Note scripts adding their values to its data stream. So, how can I specify what audiosource’s data stream OnAudioFilterRead modifies?

If you have multiple scripts on the same gameobject each with a OnAudioFilterRead method they will be called in turn. However i guess you do not add your data but you replace it, right? In that case only the last scripts action will take effect as it will overwrite everything any other “filter” has done to the stream.

Though i wouldn’t recommend to implement multiple OnAudioFilterRead methods for this. You want to have a single mixer script with the OnAudioFilterRead method which does simply mix your different sources together. Keep in mind that audio mixing (especially with many channels) is quite tricky. Accoustically you just need to add up the samples from all your sources. However depending on the amplitude of each source you can easily clip. So you either need to have enough Headroom or use some dynamic compression. Though compression usually always distorts the signals more or less heavy depending on the technique.