Skip to content Skip to sidebar Skip to footer

How To Make Annyang Detect No Sound For 3 Seconds Or Longer, Then Start Next Audio?

I got a person's help to build this so far. But my goal is after the first audio file, the program would still listen what the user says until the user finishes talking. And then i

Solution 1:

You have logic errors in your code. When stopping and starting timers, you need to refer to the global variables.

This code isn't going to stop your timer:

functionstopListening()
{
    var monitorId = 0;
    window.clearInterval(monitorId);
}

Your code also only initiates your play loop. It never starts the listening timer.

You logic will also play the first song twice.

To demonstrate the control flow, I have mocked the audio and annyang objects. The audio mock simulates the playing of 3 ten second audio files. When the window loads, the first audio mock will play. After which the annyang and the listening timer will start.

To mock annyang's sound detect there is a "mock sound" button. If it is clicked it will extend sound detection for another 3 seconds. Once 3 seconds goes by annyang will be paused and the next audio is played.

<!DOCTYPE html><html><head><metacharset="utf-8" /><title>Annyang Mock</title><scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><script>varAudioMock = function () {
            this.src = null;
            this.start = null;
            this.timer = 0;
            var that = this;
            this.load = function () {
                $('#AudioState').html("Loaded " + that.src);
            }
            this.play = function () {
                that.start = newDate();

                $('#AudioState').html("Playing: " + this.src);

                window.setTimeout(that.end, 10000);
                that.timer = window.setInterval(that.mockPlayTime, 500);
            }
            this.mockPlayTime = function () {
                var d = newDate();
                var elapsed = (d - that.start) / 1000;
                $('#AudioState').html("Playing: " + that.src + " " + elapsed + " secs");
            }
            this.endMockPlayTime = function () {
                window.clearInterval(that.timer);
            }
            this.end = function () {
                that.endMockPlayTime();
                $('#AudioState').html("Ended: " + that.src);
                if (that.ended) {
                    that.ended();
                }
            }
            this.ended = null;
        };

        var annyangMock = function () {
            this.callbacks = {};
            this.paused = false;
        };


        annyangMock.prototype.addCallback = function (name, callback) {
            this.callbacks[name] = callback;

        }
        annyangMock.prototype.removeCallback = function (name, callback) {
            this.callbacks[name] = null;
        }
        annyangMock.prototype.pause = function () {
            $('#AnnyangState').html("Annyang: pause()");
            this.paused = true;
        }
        annyangMock.prototype.start = function () {
            $('#AnnyangState').html("Annyang: start()");
            this.paused = false;
        }
        annyangMock.prototype.invoke = function (name) {


            if (!this.paused) {
                $('#AnnyangState').html("called(" + name + ")");
                var cb = this.callbacks[name];
                if (cb) cb();
            }
        }
        annyangMock.prototype.debug = function (flag) { };

        var annyang = newannyangMock();



        var annyangMock = function () {
            var callbacks = {};
            var _paused = false;
            var pause = function () {
                $('#AnnyangState').html("Annyang: pause()");
                _paused = true;
            }
            functionaddCallback(name, callback) {
                callbacks[name] = callback;
            }
            functionremoveCallback(name, callback) {
                callbacks[name] = null;
            }
            functioninvoke(name) {
                $('#AnnyangState').html("called(" + name + ")");
                if (name == "start") {
                    _paused = false;
                }
                else {
                    if (!_paused) {
                        var cb = callbacks[name];
                        if (cb) cb();
                    }
                }
            }
        }

    </script><script>

        audio = newAudioMock();
        var monitorId = 0;
        if (annyang) {
            annyang.addCallback('start', function () { console.log('started listening!'); });
            annyang.addCallback('soundstart', onSoundHeard);

            functionmonitorSound() {
                lastSound = newDate();
                if (monitorId && monitorId > 0) return;
                monitorId = window.setInterval(trackSound, 1000);
                annyang.start();
            }

            var lastSound = newDate();
            functiononSoundHeard() {
                lastSound = newDate();
                //console.log(lastSound);
            }

            functiontrackSound() {
                var now = newDate();
                var elapsed = now - lastSound;
                $('#AnnyangState').html("Listening: " + (elapsed / 1000) + " secs");
                if ((now - lastSound) > 3000) {
                    stopListening();
                    playNextAudio();
                    return;
                }
            }

            functionstopListening() {

                window.clearInterval(monitorId);
                monitorId = 0;
                annyang.pause();

            }



            functionplayNextAudio() {
                if (audioIndex === playList.length - 1) {
                    console.log("Played all audios");
                    return; // we have played all audio
                }
                else {
                    audio.src = dir + playList[++audioIndex] + extention;
                    load();
                    setTimeout(function () { play(); }, 1000);

                }

            }
            functionload() {
                $($('#playlist li')[audioIndex]).addClass("loading");
                audio.load();
            }
            functionplay() {
                audio.play();
                $('#playlist li').removeClass("loading")
                var li = $('#playlist li')[audioIndex];
                $(li).addClass("playing");
            }
            functionplayFirstAudio() {
                annyang.pause();
                audio.src = dir + playList[audioIndex] + extention;
                load();
                audio.ended = function () {
                    $('#playlist li').removeClass("playing");
                    lastSound = newDate(); // set timestampmonitorSound(); // poll sound detection
                }
                setTimeout(function () { play(); }, 1000);
                //console.log('First audio is playing');

            }



            // Start from herevar playList = ["1_hello", "2_how_old", "3_what_did_you_make"];
            var dir = "sound/";
            var extention = ".wav";

            var audioIndex = 0;

            annyang.debug(true);
            $(document).ready(function () {
                playFirstAudio();
                var l = $("<ol>");
                playList.forEach(function (j) {
                    l.append($("<li>").html(j));

                });
                $('#playlist').append(l);
            })
        };
    </script><styletype="text/css">#playlistli {
            width: 200px;
            padding: 5px;
        }

        div {
            padding: 15px;
        }

        #playlistli.playing {
            border: 1px solid green;
            background: #dedede;
        }

        #playlistli.loading {
            border: 1px solid red;
            background: #dedede;
        }
    </style></head><body><div><b>Annyang State:</b><spanid="AnnyangState"></span></div><div><b>Audio State:</b><spanid="AudioState"></span></div><divid="playlist"><b>Playlist:</b></div><divid="Controls"><inputid="MockSound"type="button"value="Mock Sound"onclick="annyang.invoke('soundstart');" /></div></body></html>

Post a Comment for "How To Make Annyang Detect No Sound For 3 Seconds Or Longer, Then Start Next Audio?"