- Home /
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?
Answer by Bunny83 · Mar 21, 2018 at 04:27 PM
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.
You may want to have a look at this question for some mixing advice
I do add the data, i have tested the many notes per gameobject idea before and it does work but i don't really feel like that was an improvement as it made it a lot harder to organize the notes.
The mixer idea sounds great actually, since it sounds like i could iterate through every note in the single call of OnAudioFilterRead and run pretty much the same code as before with very few changes. After recently implementing overtones i immediately realized that more overtones meant overall louder sound so i knew i'd have to tackle the mixing subject at some point. I'll look into it.
Your answer
Follow this Question
Related Questions
GetSpectrumData over entire file 1 Answer
Glitched Music 0 Answers
PlayScheduled or PlayDelayed 0 Answers
Stereo Mix as Input? 1 Answer
Can not play a disabled audio source 2 Answers