Cycling through array of audio files, need to go to next audio file

When speech is detected then the first track plays, it plays till the end and then turns the mic on to listen for something, when it hears something it plays the second track and so on until track ten.

Running this code on a chrome browser in ubuntu. Ive posted on stack overflow as well https://stackoverflow.com/questions/55453566/is-there-a-mistake-in-my-method-of-cycling-through-an-array-of-audio-files

    <script type="text/javascript" src="p5/p5.min.js"></script>
    <script type="text/javascript" src="p5/addons/p5.dom.min.js"></script>
    <script type="text/javascript" src="p5/addons/p5.sound.min.js"></script>
    <script type="text/javascript" src="p5/addons/p5.speech.js"></script>
    <script type="text/javascript">

        var myRec = new p5.SpeechRec('en-US', parseResult);
        myRec.continuous = true;
        myRec.interimResults = false;
        var songs = ['1.wav', '2.wav', '3.wav', '4.wav', '5.wav', '6.wav', '7.wav', '8.wav', '9.wav', '10.wav'];
        var songCount = songs.length;
        var currentSong = 0;
        var song;

        function preload() {
            soundFormats('wav');
            song = loadSound('audioFiles/' + songs[currentSong]);
        }

        function setup() {
            createCanvas(600, 600);
            background(255, 255, 255);
            fill(0, 0, 0, 255);
            myRec.start();
        }

        function draw() {
        }

        function parseResult() {
            var mostrecentword = myRec.resultString.split(' ').pop();
            if(mostrecentword.indexOf("")!==-1) {
                    song.playMode('untilDone');
                    song.play();
                    background(0, 255, 0);
            }
            else if(song.isPlaying())   {
                myRec.stop();
            }
            console.log(mostrecentword);
            console.log(currentSong);
            currentSong = currentSong + 1;
        }

    </script>
1 Like

So… what happens now with this code? What, specifically, do you want to happen instead?

got it to work. followed advice from the stackoverflow link ^ in my description. I wanted it to cycle through the audio tracks from a folder based on speech input. Something like if I say “play” or “next” it goes through the audio tracks.

Glad the preload / loadSound solution on StackOverflow worked for you.

1 Like