Penn Controller Audio Segment Playing

PennController for IBEX Forums Support Penn Controller Audio Segment Playing

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #10475
    olivia
    Participant

    Hi, I just have a question regarding whether there is a feature of if it is possible to write a function which allows the user to play a segment of an audio (marked with a starting time and an end time) instead of playing the entire audio. We do not have the project set up yet but would like to ask if this is a feasible feature to add.

    Thank you so much for your help!

    #10477
    Jeremy
    Keymaster

    Hi,

    There is no built-in command in PennController for that, but it’s easily implementable using a Function element:

    newTrial(
        newButton("Start").print().wait().remove()
        ,
        newAudio("sample", "https://freetestdata.com/wp-content/uploads/2021/09/Free_Test_Data_100KB_MP3.mp3")
        ,
        newFunction("playSegment", async ()=>{
            const a = getAudio("sample")._element.audio;
            a.pause();
            a.currentTime = 1.25; // start at 1.25s
            a.play();
            await new Promise(r=>(function checkAudio() {
                if (a.currentTime >= 2.30) { // end at 2.30s
                    a.pause();
                    r();
                }
                else setTimeout(checkAudio);
            })());
        })
        ,
        newButton("Play segment").callback( getFunction("playSegment").call() ).print()
        ,
        newButton().wait()
    )

    Jeremy

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.