Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

Sound looping

em2em2
in Questions Posts: 174

I know that sound can be looped, when playing it from a source. What I am trying to do is to get sound generated like this:

s = sound({
            Waveform = SOUND_SINEWAVE,
            StartFrequency = Music.frequencyToSFXR(Music.noteToFrequency("A4")),
            SustainTime = 0.06,
            Volume = 1
        })

and loop it, but it doesn't work.
My project is supposed to play sound indefinitely until somebody presses the stop button. What I cannot understand is when I try to print(s), all I get is nil, so I cannot turn on s.looping. How can I get my sound to loop?
Tagged:

Comments

  • dave1707dave1707 Mod
    Posts: 6,392

    @em2 If you read the built in documentation for sound, you can only loop sounds from an asset pack. Here's an example of looping the sound Pop. I also added code to stop and start the sound. Tap the screen to stop it, tap again to start it.

    function setup()
        s=sound("Game Sounds One:Pop 1", .5, 1, 0, true)  -- true loops the sound
    end
    
    function draw()
        background(0)
        fill(255)
        text("tap screen to start or stop the sound",WIDTH/2,HEIGHT/2)
    end
    
    function touched(t)
        if t.state==BEGAN then
            if not pause  then
                s:stop()
            else
                s=sound("Game Sounds One:Pop 1", .5, 1, 0, true)
            end
            pause=not pause
        end
    end
    
  • em2em2
    Posts: 174

    Thanks for your reply. Is there any way I can get sounds generated with a parameterTable to loop? The sounds have to play at specific frequencies.

  • dave1707dave1707 Mod
    Posts: 6,392

    The documentation says only sounds from an asset can loop. One way you can loop your sound is with a timer. If your sound last 10 seconds, then have a timer start your sound every 11 seconds.

  • dave1707dave1707 Mod
    Posts: 6,392

    @em2 Here's an example of looping sounds that you create yourself. Depending on the length of your sound, you'll have to modify the tween delay time.

    function setup()
        play()
    end
    
    function draw()
        background(40, 40, 50)   
    end
    
    function play()
        s=sound( { Waveform = SOUND_NOISE,AttackTime = 1.2,SustainTime = 1} )
        tween.delay(6,play)
    end
    
  • em2em2
    Posts: 174

    Is there any way I can get the exact length of the sound?

  • dave1707dave1707 Mod
    Posts: 6,392

    No, not that I know of. You'll have to time it yourself.

  • em2em2
    Posts: 174

    I got it timed properly and my problem has been solved. Thanks a lot. One final thing that I found strange is that sounds generated with a parameterTable do not return anything, so I cannot stop them. I'lll have to implement my own methods of stopping sound.

  • dave1707dave1707 Mod
    Posts: 6,392

    The only way I found to stop a parameterTable sound is to just let it stop on its own. So when you want it to stop, you have a variable to not call the tween timer. Here's an example.

    function setup()
        str="tap screen to start the sound"
    end
    
    function draw()
        background(40, 40, 50)   
        fill(255)
        text(str,WIDTH/2,HEIGHT/2)
    end
    
    function play()
        if playSound then
            s=sound( { Waveform = SOUND_NOISE,AttackTime = 1.2,SustainTime = 1} )
            tween.delay(6,play)
        end
    end
    
    function touched(t)
        if t.state==BEGAN then
            playSound=not playSound -- toggle playSound true/false
            if playSound then
                str="sound is playing\n\ntap to stop"
                play()
            else
                str="sound will stop when sound cycle is complete\n\ntap to start again"
            end
        end
    end
    
  • em2em2
    Posts: 174

    I pretty much did the same thing, but with a very short SustainTime and rapid loop. I also added minor AttackTime and DecayTime to make it play smoother.

Sign In or Register to comment.