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:

function stopListening()
{
    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>
    <meta charset="utf-8" />
    <title>Annyang Mock</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    <script>
        var AudioMock = 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 = new Date();

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

                window.setTimeout(that.end, 10000);
                that.timer = window.setInterval(that.mockPlayTime, 500);
            }
            this.mockPlayTime = function () {
                var d = new Date();
                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 = new annyangMock();



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

    </script>
    <script>

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

            function monitorSound() {
                lastSound = new Date();
                if (monitorId && monitorId > 0) return;
                monitorId = window.setInterval(trackSound, 1000);
                annyang.start();
            }

            var lastSound = new Date();
            function onSoundHeard() {
                lastSound = new Date();
                //console.log(lastSound);
            }

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

            function stopListening() {

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

            }



            function playNextAudio() {
                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);

                }

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

            }



            // Start from here

            var 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>
    <style type="text/css">
        #playlist li {
            width: 200px;
            padding: 5px;
        }

        div {
            padding: 15px;
        }

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

        #playlist li.loading {
            border: 1px solid red;
            background: #dedede;
        }
    </style>
</head>
<body>
    <div>
        <b>Annyang State:</b> <span id="AnnyangState"></span>
    </div>
    <div><b>Audio State:</b> <span id="AudioState"></span></div>
    <div id="playlist">
        <b>Playlist:</b>
    </div>


    <div id="Controls">
        <input id="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?"