Search Unity

Set pitch of clip played through OnAudioFilterRead

Discussion in 'Scripting' started by atomtwist, Jul 13, 2014.

  1. atomtwist

    atomtwist

    Joined:
    Mar 20, 2013
    Posts:
    32
    Hey Everyone.
    I'm playing several audioClips through one AudioSource via OnAudioFilterRead -
    But that also means I loose the ability to set the pitch for each clip.

    Does anybody know of a method to set the pitch of a clip that gets played through OnAudioFilterRead??
     
  2. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    Implement a resampling algorithm!
    Linear interpolation is the cheap and dirty way to do it, and Unity's way from what my ears tell me.
    You might need to implement a kind of datasource model to handle mixing different sources into one at a variable sample rate.

    Or, just get G-Audio which already does all that for you... ;)

    Shamelessly,

    Gregzo
     
  3. atomtwist

    atomtwist

    Joined:
    Mar 20, 2013
    Posts:
    32
    Hey Gregzo.
    Thanks for your reply.

    I've actually been thinking about getting G-Audio for a few days now.

    I'm currently writing a sequencer in the editor, and on the front end I'm pretty far since I implemented a lot of musical features that I always wanted a sequencer to have. ;-)
    But under the hood I'm starting to run into limitations with what unity can do ( lots of audiosources for one track - not able to schedule pitch & volume changes for a clip precisely, etc.)

    From what I understand G-Audio mixes everything through 1 AudioSource?

    The reason I'm trying to do it myself now, (apart from wanting to learn about it) is that I want to be able to hook it up easily to the new Unity 5 Mixer.
    I was figuring: one Audiosource per Sequencer Track -> that i then can easily set that to a Mixer channel of my choice.

    How will the Unity 5 features impact G-Audio? Will you incorporate the new stuff - or keep it separate?

    I think in the in the end I will end up getting G-Audio anyway - since you made so much cool stuff to play with already - but for now I would really like to delve more into how these things are done.

    Would you mind sharing a little more detailed, what you talked about in your post?
    (linear resampling algorithm & datasource model etc.)


    Greetings,

    Atomtwist
     
  4. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    You need G-Audio like a zombie needs flesh. Get, must get, now...

    Lots to say, onwards!

    Awesome project. And yes, AudioSource/AudioClip is quite limited outside of positional audio. That's why I made G-Audio. Perhaps we could envisage a common project? We can PM if you're interested in talking about it. As it stands, implementing a sequencer in G-Audio is actually quite straightforward: you can play chunks of data, set start and end playback position, fades, and lots more.

    Well, every G-Audio Player mixes everything through one AudioSource, which ceases to act as one( it's simply an access point to the audio buffer ). I do support multiple players, which may be useful in certain cases, but in general all can be done through a unique player.


    Looking forward to 5 very much. G-Audio won't need to do as much mixing ( still a little, as AudioSources really are a pain for non-positional audio and WILL still stick around ). It'll benefit us greatly, I think.

    I wouldn't, but time is scarce! Look up linear interpolation resampling on google, you'll find it. It's a way of interpolating samples to change sample rate. It's not the most sophisticated, but is fast and ok for quick and dirty realtime use.
    About the datasource pattern: if you are simultaneously pitch shifting multiple sources of different length, it can become quite the nightmare. The datasource pattern, in that case, could be implemented in such a way that the mixer asks every source of data ( audio data containers ) to provide a certain amount of data at a certain sample rate. The datasource obliges, or signals eof or error. Keeps things nice and tidy!

    Cheers,

    Gregzo