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

Can you loop a part of a wav file?

Discussion in 'Audio & Video' started by San_Holo, Dec 2, 2014.

  1. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    152
    Hi...

    Look at this Time Samples documentation for play back, now if I could loop part of a wav file I could make an entire music track and then using Time.Samples go to the next play position and then loop that part, can it be done? please advise, thanks
     
  2. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    Not straight solution out of the box, no.

    The problem is not starting the audio at a precise position in the clip, but looping at an end position earlier than the clip's end.

    4 paths lie ahead:

    1) The cunning
    - If you know your loop points in advance, instead of 1 big track import many chunks and loop these. Do NOT use Play but PlayDelayed and AudioSettings.dspTime, alternating AudioSources, to stitch clips sample accurately.

    2) The lazy
    - Use AudioClip.Create to create empty clips of the appropriate size, and AudioClip.GetData and SetData to copy chunks of data to your runtime generated clips, which will loop perfectly. Wasteful, triggers a lot of garbage collection, but a usable strategy in some cases. Note that your source clip will have to be loaded in memory, not streamed.

    3) The masochist
    - Read audio files yourself into audio data containers that you can pipe into OnAudioFilterRead().

    4) The rich
    - Get G-Audio( see my signature ). It does what solution 3 suggests out of the box.
     
    Last edited: Dec 2, 2014
    MacroPinch and San_Holo like this.
  3. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    You could actually use a script for this. It would set the playback position then go back to that position once it reaches a certain length after it.
     
    astracat111 and San_Holo like this.
  4. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    152
    I'm intending to manipulate my own music into chunks so I'll know the bpm/tempo before hand and hopefully using AudioSettings.dspTime and some cunning scripting I'll try and follow Gregzo's thinking with his option one perhaps, I'll have individual sample files/wavs each one the exact same bpm and length only just adding instruments etc as you go on... so perhaps just playing my wavs one after the other just looping the current sound.
     
  5. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    @
    This approach unfortunately won't work, it'll always be too late or too early for resetting playback position accurately.
     
  6. Tazman

    Tazman

    Joined:
    Sep 27, 2011
    Posts:
    94
    Fabric can read markers from wavfiles and set a loop region that can play indefinitely or for a number of times before it reaches the end... its doing this by creating audio sources for each section during initialization and then using the PlayScheduled and AudioSettings.dspTime to stitch them together sample accurately. Its not that wasteful because it doesn't copy the data around (only once during awake) but it does mean that you cant change the loop region at runtime and that your audio clip has to be uncompressed and loaded into memory. However, it should be relative easy to support compressed and streamed audio clips simply by creating them automatically before you build the game for the target platform.
     
  7. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    In case anyone's wondering what Fabric is, that's because it's not on the Asset Store. Tazman has his own website dedicated to it. A quite advanced product for sure. Not sure if there's a reviews section though?
     
  8. Tazman

    Tazman

    Joined:
    Sep 27, 2011
    Posts:
    94
  9. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    More information that I don't think anyone offered. When you play an mp3 for sound effects, there's an extremely long latency and slowdown before it starts playing. That is not true of wav files (start playing 120x faster). So stay far away from mp3's for sound effects at all costs.
     
  10. Koyemsi

    Koyemsi

    Joined:
    Sep 25, 2017
    Posts:
    29
    Hi, I wrote this simple code to loop on a part of the audio :

    Code (CSharp):
    1.  
    2. public AudioSource myAudio;
    3. [Range(0f, 3.5f)] // 2nd value should be the duration of the audio in seconds
    4. public float loopStart, loopEnd;
    5.  
    6. void Start(){
    7. myAudio.Play();
    8. }
    9.  
    10. void Update(){
    11. if (myAudio.isPlaying && myAudio.time > loopEnd) {
    12.       myAudio.time = loopStart;
    13.    }
    14. }
     
    mdeglobal likes this.
  11. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    That code will not work perfectly. The update code only runs once per frame. Three problems with this. Number one is that you're only going to get probably 30 to 60 frames per second when this code will run. Number two is that your frames are all not probably exactly the same length between each other all the time. Number three is the frame and your code will not happen at exactly the moment you want, hence your greater than sign. If you could do it with an equal sign and not greater than , it would theoretically work, but it's impossible if the code only runs once a frame. You could over shoot your target ending by a lot and actually it probably will loop at a different point every single time.

    I wish that code would work. It would make things so easy.
     
  12. Koyemsi

    Koyemsi

    Joined:
    Sep 25, 2017
    Posts:
    29
    Bad news :(
    It was quite to simple to work, wasn't it ?
    You're probably right, although it seems to work for me (but with a poor quality result, maybe because my audio wasn't designed for looping).
    Well, a better solution is still to be found.
     
  13. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Yeah. I had code like that before, but later realized why it didn't work.