Howdy, Stranger!

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

Play a sound while touching?

in Questions Posts: 35

Just a simple question - is it possible to play a sound (possibly a generated one) while the screen is being touched (for the whole duration of the touch), and stop the sound when the touch is released? I can't seem to find a way to do this since most sound functions have a set duration.

Thanks for any responses!

Tagged:

Comments

  • IgnatzIgnatz Mod
    Posts: 5,396
    function touched(t)
        if t.state==BEGAN then  music("Game Music One:Trancey",true)
        elseif t.state==ENDED then  music.stop()  end
    end
    
  • Posts: 35

    Thanks, but I was looking for a way that let you change the pitch of the sound, and you can't do that by using music sadly... any alternatives?

  • Posts: 2,020

    @MPan1 There are in fact a whole load of sound functions which have been in Codea for years but for some reason have never been added to the documentation. You can find a discussion of them here:

    https://codea.io/talk/discussion/5310/hidden-sound-functions#latest

    There is indeed a sound:stop() command, trigger this on touch.ended

  • Posts: 2,020

    I created a ticket asking for these methods to be documented.

    https://bitbucket.org/TwoLivesLeft/core/issues/397/add-undocumented-sound-functions-to-the

  • IgnatzIgnatz Mod
    Posts: 5,396

    @MPan1 - it helps if you ask the real question up front!

  • Posts: 2,020

    @Ignatz I think it's clear that play a sound (possibly a generated one) refers to sound not music

  • IgnatzIgnatz Mod
    Posts: 5,396

    yew, but changing pitch is very different to just stopping.

  • Posts: 35

    @yojimbo2000 - Thanks for telling me about those, I'll try them.
    @Ignatz - I was sort of trying to say that with the generated sound thing before, I know it wasn't that clear though.

  • IgnatzIgnatz Mod
    Posts: 5,396

    That's ok

  • edited June 2016 Posts: 35

    Actually @jojimbo2000, I'm not sure those functions would really work for what I'm trying to do- I'll actually explain it properly this time:

    I'm trying to do a simple sound-project where I want you to be able to touch the screen to start a sound playing, and release your finger to stop that sound (I want the sound to keep playing while your finger is being held, and only stop when you release it).

    But, I also want you to be able to change the pitch and 'strength' of the sound-the more you drag your finger to the left (on the X axis), the more the sound gets pitched down, and the opposite for when you drag your finger right. This could probably be done with the pitchTo function posted before, but I don't really know what I could do to change the 'strength' of the sound (I don't just want to change the volume).

    See, I want the sound to be more muffled/filtered when your finger is at the very bottom of the screen (on the Y axis), and when it's at the top for the sound to be non-muffled and more sawtooth-sounding. I'm probably going to have to use generated sounds for this, but since they don't support looping (I think) and have a set time (you have to set how long the sound is going to be before you can play it), it doesn't really work well considering the touch can be held for any length.

    I know there's a lot of "I want" in those past paragraphs, but I'm just trying to explain what I'm trying to do, sorry if it's a pain. Thanks for anyone who can think of any hacky method I could try to get this to work

  • Posts: 2,020

    I've not done much with generated sound, that's a good question though about whether they support looping (or maybe you just have to give them really long sustain values)?

    Certainly with samples .wav or whatever, you have lots of control over volume, and so on. I've not tried pitch bending. It does support looping , though not in a very extensive way, in that you can only loop the entire sound, whereas often you want there to be an attack, followed by the looping section. My workaround for this was to split the sound into two files, one for the attack (which just played once), and one for the looped section, and just trigger both at once.

    but I don't really know what I could do to change the 'strength' of the sound (I don't just want to change the volume).

    Not sure what you mean here by strength. On keyboards, key velocity affects both the volume and the amount of attack (perhaps this is what you mean by strength?), but attack just affects the beginning of the sound, it's not something that you can change once the note is looping.

  • edited June 2016 Posts: 35

    By 'strength' I just meant that I was trying to make the sound sound muffled (like if you hum with your mouth closed) when your finger is at the bottom of the screen, but then get it to sound more like a sawtooth synth as you move your finger up the screen

    Actually, it just occurred to me that I could do this by overlapping a sawtooth sound and a hum sound... why didn't I think of that before? Nevermind about all that other stuff

  • Posts: 2,020

    Oh I think I know what you mean, like a high-pass filter (cut the high frequencies)? Kind of like the first few seconds of Around the World ?

  • Posts: 35

    Yeah, pretty much that, god knows how you could do that though

  • Posts: 35

    Sorry to keep bothering you guys, but I have a new problem- I can't get any of the functions that @yojimbo2000 linked to work. I even copied the entire example that the guy used to test the sound functions, and I couldn't even get it to PLAY the sound.

  • IgnatzIgnatz Mod
    Posts: 5,396

    This works for me, run it and touch the screen

    function draw()
        if playSound then sound(SOUND_PICKUP, 46506) end
    end
    
    function touched(t)
        if t.state~=ENDED then  
            playSound=true
        else playSound=false
        end
    end
    
  • Posts: 2,020

    You call them as a method on the variable you assigned to the sound:

    Eg

        if t.state == BEGAN then
            touches[t.id] = sound("A Hero's Quest:Curse", 1, 1, 0, true)
        elseif t.state == MOVING then
         --pitch bend?
        else
          touches[t.id]:stop()
        end
    
  • edited June 2016 Posts: 35

    @Ignatz - The problem isn't starting and stopping the sound, it's the actual pitch bending function. I know how I could DO the pitch bending by checking the touches like you did in your example, but the actual FUNCTION (pitchTo) or any others (E.G. fadeTo) aren't working for me. Try copying the exact example from this page and see if it works at all. It doesn't for me.

  • IgnatzIgnatz Mod
    Posts: 5,396

    @MPan1 - it works for me, I get a whistling sound that rises and falls

  • edited June 2016 Posts: 35

    @Ignatz - Ok, but it doesn't seem to play anything for me...

    Anyway, I'm much stupider than I thought. The problem was that I was thinking about it all wrong. I had no clue you could change the pitch and the volume of a sound simply by changing sound.pitch and sound.volume... for some dumb reason I thought that once a sound was already being played, there was no possible way to change anything about it, but I was completely wrong. Oh well, problem solved now! Thanks @Ignatz and @yojimbo2000 for the suggestions!

    By the way, to any developers of Codea:
    For me, doing sound.loop = true doesn't actually make the sound loop, however all the other variables I described before change the sound just fine. It doesn't really matter though since you can just set whether the sound loops when calling the sound function. Fantastic app by the way, I love that multitouch actually works.

  • Posts: 38

    "Thank you" to @Ignatz. I was scratching my head over a problem I was trying to solve. I have multiple buttons created using a classes and each button plays a different sound, a music file rather. I want to play the music while pressing the button and stop when released.

    Ignatz's solution at the top of this discussion worked great. Thanks again.

    Now on to the next problem.

    ...Scotty

Sign In or Register to comment.