{"size": 1020, "ext": "js", "max_stars_repo_path": "src/service/speaker.js", "max_stars_repo_name": "romaneckert/robot", "max_stars_repo_stars_event_min_datetime": "2017-04-25T01:09:54.000Z", "max_stars_repo_stars_event_max_datetime": "2017-04-25T01:09:54.000Z", "max_issues_repo_path": "src/service/speaker.js", "max_issues_repo_name": "romaneckert/robot", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/service/speaker.js", "max_forks_repo_name": "romaneckert/robot", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 17.8947368421, "max_line_length": 107, "alphanum_fraction": 0.5166666667}
const player = require('play-sound')(opts = {});

/** speaker module */
class Speaker {

    constructor() {
        super();

        this._isSpeaking = false;
        this._queue = [];

    }

    say(message) {

        let entry = {
            message: message,
            filePath: null,
        };

        this._queue.push(entry);

        this.module.marytts.textToSpeech(message, (message, filePath) => {

            entry.filePath = filePath;

            this._speak();

        })
    }

    _speak() {
        if (this._isSpeaking || 0 === this._queue.length || this._queue[0].filePath === null) return false;

        this._isSpeaking = true;

        let entry = this._queue.shift();

        this.logger.debug(entry.message);

        player.play(entry.filePath, (error) => {
            if (error) throw new Error(error);

            this._isSpeaking = false;
            this._speak();

        });
    }

    get isSpeaking() {
        return this._isSpeaking;
    }

}

module.exports = Speaker;
