1

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:

<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>
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
dackdel
  • 21
  • 2

2 Answers2

1

In order to play songs from the array you will need to call loadSound for each.

From the reference page:

If loadSound is called during preload(), the p5.SoundFile will be ready to play in time for setup() and draw(). If called outside of preload, the p5.SoundFile will not be ready immediately, so loadSound accepts a callback as the second parameter.

One way to make sure all songs are available is to load them all in preload() and keep the loaded songs in an array instead of just the song name.

This is a complete script that demonstrates loading sounds in preload for latter use:

    var songs = [];
    var songNames = ['ir_begin.wav', 'ir_inter.wav', 'ir_end.wav'];
    var songCount = songNames.length;
    var currentSong = 0;
    var song;

    function preload() {
      for (let i = 0; i < songNames.length; i++){
        songs.push(loadSound('audioFiles/' + songNames[i]));
      }
    }

    function setup() {
      frameRate(1);
    }

  function draw(){
      songs[currentSong].setVolume(0.9);
      songs[currentSong].play();
      currentSong++;
      if (currentSong >= songs.length){
        noLoop();
      }
  }

The downside of loading all the songs in preload is that your sketch will take the hit upfront for loading all the songs. You can spread out the delay time by only loading songs as you need them.

The code to load on demand is a little more complicated but something like this will do the job:

    var songs = [];
    var loadedSongs = [];
    var songNames = ['ir_begin.wav', 'ir_inter.wav', 'ir_end.wav'];
    var songCount = songNames.length;
    var currentSong = 0;
    var song;

    function preload() {
        songs.push(loadSound('audioFiles/' + songNames[currentSong]));
    }

    function setup() {
      frameRate(1);
    }

  function draw(){
  if (currentSong < songs.length){
      songs[currentSong].setVolume(0.9);
      songs[currentSong].play();
      currentSong++;
      } else {
        loadSong(songNames[currentSong]);
      }
      if (currentSong >= songCount){
        noLoop();
      }
  }

  function loadSong(songName){
    for (let i = 0; i < loadedSongs.length; i++){
      if (songName === loadedSongs[i]){
        return;
      }
    }
    loadSound('audioFiles/' + songName, loadSongCallBack);
    loadedSongs.push(songName);
  }

  function loadSongCallBack(song){
    songs.push(song);
  }
Charlie Wallace
  • 1,810
  • 1
  • 15
  • 17
0

Unless you call preload() again the song is still assigned to the first .wav file. So when you increment currentSong in parseResult you still need to then reassign the next song. You could probably just call preload() again or add song = loadSound('audioFiles/' + songs[currentSong]); to the end of parseResult.

Christopher Cook
  • 780
  • 8
  • 16