<commit_before>var utils = require('radiodan-client').utils,
    logger = utils.logger(__filename);

module.exports = function(players, services, state) {
  var settings = {},
      announcing = false,
      volumeDiff = 50;

  return {
    start: startAnnouncing,
    stop:  stopAnnouncing,
    announce: announce
  };

  function announce(event) {
    return players.announcer.add({
      clear: true, playlist: [announceTrack('standby')]
    })
    .then(function(){
      return players.announcer.play();
    })
  }

  function startAnnouncing(announceSettings, avoiding) {
    var volPromise;

    logger.info('startAnnouncing');

    if(announceSettings) {
      settings = announceSettings;
    }

    settings.avoiding = (avoiding === true);

    if(announcing) {
      volPromise = utils.promise.resolve();
    } else {
      volPromise = utils.promise.all([
        players.main.volume({diff: -volumeDiff}),
        players.avoider.volume({diff: -volumeDiff})
      ]);
    }

    return volPromise.then(function(){
      return players.announcer.add({
        clear: true, playlist: [announceTrack()]
      }).delay(1000)
      .then(function(){
        return players.announcer.play();
      })
      .then(function(){
        announcing = true;

        players.announcer.once('player.state', function() {
          logger.info('Announcer spoken');
          state.emit('msg', 'announcer', announcingMessage());
          stopAnnouncing();
        });
      });
    });
  }

  function stopAnnouncing() {
    var self = this;

    logger.info('stopAnnouncing');

    if(!announcing) {
      logger.debug('Asked to stop announcing, but not currently doing so');
      return;
    }

    return utils.promise.all([
        players.main.volume({diff: volumeDiff}),
        players.avoider.volume({diff: volumeDiff})
        ]).then(function(){
          announcing = false;
          emitter.emit('msg', 'announcer', announcingMessage());
        });
  }

  function announceTrack(eventName) {
    var mp3;

    try {
      if(eventName) {
        mp3 = settings.announcer.toLowerCase() + '/status/' +
              eventName + '.mp3';
      } else {
        var dir = settings.avoiding ? 'avoiding' : 'playing';

        mp3 = settings.announcer.toLowerCase() + '/' +
              dir + '/' + services.current() + '.mp3';
        }
    } catch(err) {
      logger.warn(err);
      mp3 = null;
    }

    logger.info('track', mp3);
    return mp3;
  }

  function announcingMessage() {
    return {
      isAnnouncing: announcing
    };
  }
}
<commit_msg>Play announcer event track<commit_after>var utils = require('radiodan-client').utils,
    logger = utils.logger(__filename);

module.exports = function(players, services, state) {
  var settings = {},
      announcing = false,
      volumeDiff = 50;

  return {
    start: startAnnouncing,
    stop:  stopAnnouncing,
    announce: announce
  };

  function announce(event) {
    return players.announcer.add({
      clear: true, playlist: [announceTrack(event)]
    })
    .then(function(){
      return players.announcer.play();
    })
  }

  function startAnnouncing(announceSettings, avoiding) {
    var volPromise;

    logger.info('startAnnouncing');

    if(announceSettings) {
      settings = announceSettings;
    }

    settings.avoiding = (avoiding === true);

    if(announcing) {
      volPromise = utils.promise.resolve();
    } else {
      volPromise = utils.promise.all([
        players.main.volume({diff: -volumeDiff}),
        players.avoider.volume({diff: -volumeDiff})
      ]);
    }

    return volPromise.then(function(){
      return players.announcer.add({
        clear: true, playlist: [announceTrack()]
      }).delay(1000)
      .then(function(){
        return players.announcer.play();
      })
      .then(function(){
        announcing = true;

        players.announcer.once('player.state', function() {
          logger.info('Announcer spoken');
          state.emit('msg', 'announcer', announcingMessage());
          stopAnnouncing();
        });
      });
    });
  }

  function stopAnnouncing() {
    var self = this;

    logger.info('stopAnnouncing');

    if(!announcing) {
      logger.debug('Asked to stop announcing, but not currently doing so');
      return;
    }

    return utils.promise.all([
        players.main.volume({diff: volumeDiff}),
        players.avoider.volume({diff: volumeDiff})
        ]).then(function(){
          announcing = false;
          emitter.emit('msg', 'announcer', announcingMessage());
        });
  }

  function announceTrack(eventName) {
    var mp3;

    try {
      if(eventName) {
        mp3 = settings.announcer.toLowerCase() + '/status/' +
              eventName + '.mp3';
      } else {
        var dir = settings.avoiding ? 'avoiding' : 'playing';

        mp3 = settings.announcer.toLowerCase() + '/' +
              dir + '/' + services.current() + '.mp3';
        }
    } catch(err) {
      logger.warn(err);
      mp3 = null;
    }

    logger.info('track', mp3);
    return mp3;
  }

  function announcingMessage() {
    return {
      isAnnouncing: announcing
    };
  }
}

{'fraction_non_alphanumeric': 0.1431628087920638, 'fraction_numerical': 0.005446411204045905, 'mean_word_length': 2.34765625, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '633442', 'n_tokens_mistral': 1601, 'n_tokens_neox': 1534, 'n_words': 287}