<commit_before>import {
  IS_PLAYING,
  IS_STOPPED,
  PAUSE,
  PLAY,
  SEEK_TO_PERCENT_COMPLETE,
  SET_BAND_VALUE,
  SET_BALANCE,
  SET_MEDIA,
  SET_VOLUME,
  START_WORKING,
  STOP,
  STOP_WORKING,
  UPDATE_TIME_ELAPSED,
  SET_EQ_OFF,
  SET_EQ_ON,
  PLAY_TRACK,
  BUFFER_TRACK,
  CHANNEL_COUNT_CHANGED
} from "./actionTypes";
import { next as nextTrack } from "./actionCreators";
import { getCurrentTrackId } from "./selectors";

export default media => store => {
  const { media: { volume, balance } } = store.getState();

  // Ensure the default state is the canonical value.
  media.setVolume(volume);
  media.setBalance(balance);
  // TODO: Ensure other values like bands and preamp are in sync

  media.addEventListener("timeupdate", () => {
    store.dispatch({
      type: UPDATE_TIME_ELAPSED,
      elapsed: media.timeElapsed()
    });
  });

  media.addEventListener("ended", () => {
    store.dispatch({ type: IS_STOPPED });
    store.dispatch(nextTrack());
  });

  media.addEventListener("playing", () => {
    store.dispatch({ type: IS_PLAYING });
  });

  media.addEventListener("waiting", () => {
    store.dispatch({ type: START_WORKING });
  });

  media.addEventListener("stopWaiting", () => {
    store.dispatch({ type: STOP_WORKING });
  });

  media.addEventListener("fileLoaded", () => {
    store.dispatch({
      type: SET_MEDIA,
      kbps: "128",
      khz: Math.round(media.sampleRate() / 1000).toString(),
      channels: media.channels(),
      length: media.duration(),
      id: getCurrentTrackId(store.getState())
    });
  });

  media.addEventListener("channelupdate", () => {
    store.dispatch({
      type: CHANNEL_COUNT_CHANGED,
      channels: media.channels()
    });
  });

  return next => action => {
    // TODO: Consider doing this after the action, and using the state as the source of truth.
    switch (action.type) {
      case PLAY:
        media.play();
        break;
      case PAUSE:
        media.pause();
        break;
      case STOP:
        media.stop();
        break;
      case SET_VOLUME:
        media.setVolume(action.volume);
        break;
      case SET_BALANCE:
        media.setBalance(action.balance);
        break;
      case SEEK_TO_PERCENT_COMPLETE:
        media.seekToPercentComplete(action.percent);
        break;
      case PLAY_TRACK:
        const track = store.getState().playlist.tracks[action.id];
        if (track != null) {
          media.loadFromUrl(track.url, true);
        }
        break;
      case BUFFER_TRACK: {
        const track = store.getState().playlist.tracks[action.id];
        if (track != null) {
          media.loadFromUrl(track.url, false);
        }
        break;
      }
      case SET_BAND_VALUE:
        if (action.band === "preamp") {
          media.setPreamp(action.value);
        } else {
          media.setEqBand(action.band, action.value);
        }
        break;
      case SET_EQ_OFF:
        media.disableEq();
        break;
      case SET_EQ_ON:<commit_msg>Enclose case to avoid variable name conflict<commit_after>import {
  IS_PLAYING,
  IS_STOPPED,
  PAUSE,
  PLAY,
  SEEK_TO_PERCENT_COMPLETE,
  SET_BAND_VALUE,
  SET_BALANCE,
  SET_MEDIA,
  SET_VOLUME,
  START_WORKING,
  STOP,
  STOP_WORKING,
  UPDATE_TIME_ELAPSED,
  SET_EQ_OFF,
  SET_EQ_ON,
  PLAY_TRACK,
  BUFFER_TRACK,
  CHANNEL_COUNT_CHANGED
} from "./actionTypes";
import { next as nextTrack } from "./actionCreators";
import { getCurrentTrackId } from "./selectors";

export default media => store => {
  const { media: { volume, balance } } = store.getState();

  // Ensure the default state is the canonical value.
  media.setVolume(volume);
  media.setBalance(balance);
  // TODO: Ensure other values like bands and preamp are in sync

  media.addEventListener("timeupdate", () => {
    store.dispatch({
      type: UPDATE_TIME_ELAPSED,
      elapsed: media.timeElapsed()
    });
  });

  media.addEventListener("ended", () => {
    store.dispatch({ type: IS_STOPPED });
    store.dispatch(nextTrack());
  });

  media.addEventListener("playing", () => {
    store.dispatch({ type: IS_PLAYING });
  });

  media.addEventListener("waiting", () => {
    store.dispatch({ type: START_WORKING });
  });

  media.addEventListener("stopWaiting", () => {
    store.dispatch({ type: STOP_WORKING });
  });

  media.addEventListener("fileLoaded", () => {
    store.dispatch({
      type: SET_MEDIA,
      kbps: "128",
      khz: Math.round(media.sampleRate() / 1000).toString(),
      channels: media.channels(),
      length: media.duration(),
      id: getCurrentTrackId(store.getState())
    });
  });

  media.addEventListener("channelupdate", () => {
    store.dispatch({
      type: CHANNEL_COUNT_CHANGED,
      channels: media.channels()
    });
  });

  return next => action => {
    // TODO: Consider doing this after the action, and using the state as the source of truth.
    switch (action.type) {
      case PLAY:
        media.play();
        break;
      case PAUSE:
        media.pause();
        break;
      case STOP:
        media.stop();
        break;
      case SET_VOLUME:
        media.setVolume(action.volume);
        break;
      case SET_BALANCE:
        media.setBalance(action.balance);
        break;
      case SEEK_TO_PERCENT_COMPLETE:
        media.seekToPercentComplete(action.percent);
        break;
      case PLAY_TRACK: {
        const track = store.getState().playlist.tracks[action.id];
        if (track != null) {
          media.loadFromUrl(track.url, true);
        }
        break;
      }
      case BUFFER_TRACK: {
        const track = store.getState().playlist.tracks[action.id];
        if (track != null) {
          media.loadFromUrl(track.url, false);
        }
        break;
      }
      case SET_BAND_VALUE:
        if (action.band === "preamp") {
          media.setPreamp(action.value);
        } else {
          media.setEqBand(action.band, action.value);
        }
        break;
      case SET_EQ_OFF:
        media.disableEq();
        break;
      case SET_EQ_ON:
{'fraction_non_alphanumeric': 0.13187178209599734, 'fraction_numerical': 0.002325195150307258, 'mean_word_length': 2.523698069046226, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '473439', 'n_tokens_mistral': 1958, 'n_tokens_neox': 1885, 'n_words': 381}