Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Sound: Attack, Decay, Sustain, Release

Discussion in 'Scripting' started by Dafu, Jul 25, 2007.

  1. Dafu

    Dafu

    Joined:
    Aug 22, 2006
    Posts:
    124
    I'm trying to achieve an Attack, Decay, Sustain, Release (ADSR) kind-of effect with some of my sound clips.

    An analogy to this effect is hitting a piano key and holding it for a while. The sound you'll get will have a definite 'attack', meaning the beginning of the sound when the key is first struck. It will also have a definite 'sustain' sound that will continue for as long as the key is being held. Finally when the key is let go the clip finishes off with it's 'release' sound.

    One restriction is that I will not know the length of the 'sustain' period prior to first playing the sound. The 'sustain' period will last an arbitrary amount of time based on the action of the user at the time the sound is being played.

    One way of achieving this is to be able to loop back to an arbitrary point in the sound clip while it's playing to get the 'sustain' effect. However, from what I've seen the audio api doesn't allow for this kind of looping.

    Another way is to split up the audio clip into 3 parts, the 'attack', 'sustain', 'release' clips. Then play them seperately, looping the sustain clip as long as necessary, but I can see this failing miserably if the timing is even a little off, and that's likely based on the game's performance at the time.

    Can anyone think of another way of achieving this sound effect?
     
  2. Dafu

    Dafu

    Joined:
    Aug 22, 2006
    Posts:
    124
    I should add that I realize that ADSR is really a volume envelope, and if I wanted to achieve the 'piano key' effect I could just get an audio clip of a piano key at a fixed volume, loop it, and fluctuate the volume as it's playing.

    However, in practice what I'm trying to implement is not a pure ADSR effect. In short I'm trying to have a chalk-like sound effect where the 'attack' is the sound of the chalk hitting the blackboard, and the 'sustain' is the sound of the chalk being dragged on the blackboard.
     
  3. drJones

    drJones

    Joined:
    Oct 19, 2005
    Posts:
    1,351
    from what i know this would be a tough one. IMO the 3 separate clips would work in basic - but from my experience with sound files the timing is going to be nowhere near as precise or repetable as what you're looking for.

    maybe i'm wrong but i would imagine you'd have to write a plugin or something that actually does what a synth would do - take a raw waveform build off of that - i don't even know if that's practical or possible. i know some people on here in the past have done some more advanced sound work (not quite as specific as this but timing based stuff) hopefully they'll chime in before too long ; )
     
  4. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    I don't see why you'd need to worry about "A" or "D" for your purposes. Can't you just have the "attack" portion of the clip play upon impact, loop for the entire time of contact, and fade out quickly upon "release"?

    I haven't gotten far enough into Unity to know for sure if there is a better solution, but music/sound is my specialty, so I'd be interested to hear what others come up with for this.

    I did notice that ogg vorbis files don't loop very cleanly in Unity or outside Unity, but I haven't tried the AIFF route. You may be able to get away without crossfading between your samples, but I wouldn't count on it.
     
  5. drJones

    drJones

    Joined:
    Oct 19, 2005
    Posts:
    1,351
    i think that would create a choppy effect when looped if he's going for that typical hard chalkboard "clack" i'm imagining.

    IMO i think you should just have two sounds - one for the chalk "clack" and another loopable one for the chalk moving across the board. have them both start together - get the volumes right so you don't notice the quieter moving chalk sound over the sharp clack - and that way you shouldn't have any synching issues either.

    for some reason i didn't see this before - from your first post i was thinking you wanted to have some user-based sound interaction ; )
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I actually thought of something like this a while ago, but didn't try it until now.

    Code (csharp):
    1. var clipA : AudioClip;
    2. var clipS : AudioClip;
    3. var clipR : AudioClip;
    4.  
    5. function Start() {
    6.     audio.clip = clipA;
    7.     audio.Play();
    8.     while (audio.isPlaying) {yield;}
    9.    
    10.     audio.clip = clipS;
    11.     audio.loop = true;
    12.     audio.Play();
    13.     yield WaitForSeconds(5);
    14.     audio.loop = false;
    15.     while (audio.isPlaying) {yield;}
    16.    
    17.     audio.clip = clipR;
    18.     audio.Play();
    19. }
    This almost works. The problem seems to be that by the time audio.isPlaying returns false, it's already too late. Which is to say, the audio is off for one frame. This is just noticeable in the editor (limited to 100 fps). I built a standalone with nothing but the sound so it's probably getting 1,000 fps or so, and it's virtually unnoticeable there. Unfortunately you can't count on getting 1,000 fps. ;) What's needed is an audio.willStopPlayingNextFrame function (which isn't really possible ;) ), or else being able to directly specify loop points in the sound sample, which would be ideal.

    Still, in "real life" situations where you presumably have other sound effects going and wouldn't notice a tiny hiccup, the above technique might work well enough. You cannot, of course, specify exactly when the sustain sound stops, and have to wait for it to reach the end of the loop, but that would be true no matter what, I think. (And by the way, I haven't had any issues with Ogg files looping.)

    --Eric
     
  7. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    You say what I recommended wouldn't work, but the solution you suggest is the same as I did. Confused. :?:

    I was tired when I wrote that, so maybe I could have been more clear. Obviously you wouldn't loop the Attack sample. That would just give a stutter effect and sound broken. I would, however, add it in at random intervals over the looping portion to simulate lifting and pressing the chalk, unless the game is one in which you are actually writing/drawing. I was imagining a character on-screen doing this, but who knows what marvelous creation you have in store. :D

    And as for Eric's suggestion, I haven't tried it, but the problem you describe is why I suggested crossfading. Just fade one audio clip out for a couple frames and have the other one fade in during the same time. An exponential curve using the power of 10 should give the most realistic results.

    Related, for everyone: NEVER just turn a sound off. I don't know if Unity has some sort of "search for zero crossing" of the waveform, but from a past project I worked on, I doubt it. Every time an audio clip stopped, an undesired click was present. Granted, there was a lot of clicking going on anyway because it was a marble rolling game, and the ball hit objects all the time, so it wasn't overly obnoxious. If your waveform is at anything other than zero when it is told to stop, which is highly likely, your speakers will be forced to return to resting position in an infinitesimal time, which is of course impossible. You'll just get an annoying clicky sound instead.
     
  8. drJones

    drJones

    Joined:
    Oct 19, 2005
    Posts:
    1,351
    haha - yep i see ; )

    agreed - for me at least it always seemed random as to whether or not you'd get any clicks or artifacts - but either way good advice.
     
  9. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    The closer the waveform is to zero, the quieter the click. It seems random to a human because the waveform could be cycling at a few hundred or thousand times per second, depending on your clip. So the click will nearly always occur somewhere between the loudest and quietest it can be. Hopefully, you have some other ambience that masks these to some degree, but if you fade the audio to zero on a smooth curve even for a single millisecond, this :twisted: evil :evil: should be avoided.
     
  10. Dafu

    Dafu

    Joined:
    Aug 22, 2006
    Posts:
    124
    Thank you for your input everyone!

    I like the cross-fading approach, I think it will work just fine, I'll give it a try.