Play a sound while touching?

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!

function touched(t)
    if t.state==BEGAN then  music("Game Music One:Trancey",true)
    elseif t.state==ENDED then  music.stop()  end

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?

@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:

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

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

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

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

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

@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.

That’s ok

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

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.

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

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 ?

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

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.

This works for me, run it and touch the screen

function draw()
    if playSound then sound(SOUND_PICKUP, 46506) end

function touched(t)
    if t.state~=ENDED then  
    else playSound=false

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


    if t.state == BEGAN then
        touches[] = sound("A Hero's Quest:Curse", 1, 1, 0, true)
    elseif t.state == MOVING then
     --pitch bend?

@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.

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