{"size": 6781, "ext": "js", "max_stars_repo_path": "src/store/modules/playlistState.js", "max_stars_repo_name": "MicuEmerson/visual-piano", "max_stars_repo_stars_event_min_datetime": "2020-12-09T07:16:00.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-15T23:06:27.000Z", "max_issues_repo_path": "src/store/modules/playlistState.js", "max_issues_repo_name": "MicuEmerson/visual-piano", "max_issues_repo_issues_event_min_datetime": "2022-02-13T21:20:30.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-27T10:54:17.000Z", "max_forks_repo_path": "src/store/modules/playlistState.js", "max_forks_repo_name": "MicuEmerson/visual-piano", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 5, "max_issues_count": 3, "max_forks_count": null, "avg_line_length": 37.4640883978, "max_line_length": 150, "alphanum_fraction": 0.482967114}
import { Midi } from "@tonejs/midi"
import Timer  from "../../utils/SetTimeoutTimer"
import { canvasState, keyboardState, menuState } from "@/store/consts/states.js";
import { SET_CURRENT_SONG, 
    SET_CURRENT_SONG_DURATION,
    SET_IS_LOADING,
    ADD_NEW_RECORDED_SONG,
    ADD_TIMER,
    CLEAR_TIMERS,
    SET_NOTE_PRESSED,
    CLEAR_PRESSED_KEYS
} from "@/store/consts/mutations.js";
import { changeSong, 
    clearTimes,
    pauseTimers,
    resumeTimers,
    prepareNotes,
    prepareSong,
    stopPlaying,
    startDrawNote,
    stopDrawNote,
  } from "@/store/consts/actions.js";

const SONGS_URL = "./songs/";

export default {
    namespaced: true,

    state: {
       songs:[
           {name : "Beethoven - Fur Elise", fromPlaylist: true },
           {name : "Schubert - Six Moments Musicaux", fromPlaylist: true},
           {name : "Sinding - Rustle of Spring", fromPlaylist: true },
        ],
       currentSong: "",
       timers : [],
       currentSongDuration: 0,
       isLoading : false,
    },

    mutations: {
       [SET_CURRENT_SONG](state, val){
            state.currentSong = val;
       },
       [SET_CURRENT_SONG_DURATION](state, val){
            state.currentSongDuration = val;
       },
       [SET_IS_LOADING](state, val){
           state.isLoading = val;
       },
       [ADD_NEW_RECORDED_SONG](state, val){
           state.songs.unshift(val);
       },
       [ADD_TIMER](state, timer){
           state.timers.push(timer);
       },
       [CLEAR_TIMERS](state){
           state.timers.length = 0;
       }
    },

    actions: {
        [changeSong]({commit, dispatch}, currentSong) {
            commit(SET_CURRENT_SONG, currentSong);
            if(currentSong != ""){
                dispatch(prepareSong);
            }
        },

        [clearTimes]({state, commit}){
            state.timers.forEach(timer => timer.pause());
            commit(CLEAR_TIMERS);
        },

        [pauseTimers]({state}){
            state.timers.forEach(timer => timer.pause());
        },

        [resumeTimers]({state}){
            state.timers.forEach(timer => timer.resume());
        },

        [prepareNotes]({state, rootState, commit, dispatch}, {notes, lastSong}) {
            notes.forEach((note, i) => {
                if(lastSong && i === notes.length - 1){
                    commit(SET_CURRENT_SONG_DURATION, note.time + note.duration);
                }
                
                rootState.toneState.tone.Transport.schedule(() => {
                    commit(ADD_TIMER, new Timer(() => {
                        
                        if(state.currentSong.fromPlaylist){
                            rootState.toneState.sampler.triggerAttackRelease(note.name, note.duration, rootState.toneState.tone.now(), note.velocity);
                        } else {
                            if(rootState.menuState.sustain == false){
                                rootState.toneState.sampler.triggerAttack(note.name);
                            } else {
                                rootState.toneState.sampler.triggerAttackRelease(note.name, "2n", rootState.toneState.tone.now());
                            }
                        }

                    }, rootState.canvasState.waterfallDelay));
              
                }, note.time)
      
                let index = null;
                let forBlackNote = false;
                for (let i = 0; i < rootState.keyboardState.notes.length; i++) {
                    if(rootState.keyboardState.notes[i].note === note.name){
                      index = i;
                      break;
                    } 
                    else if(rootState.keyboardState.notes[i].blackNote && rootState.keyboardState.notes[i].blackNote.note === note.name){
                      index = i;
                      forBlackNote = true;
                      break;
                    }
                }
       
                rootState.toneState.tone.Transport.schedule(time => {
                    // rootState.toneState.tone.Draw.schedule(() => {
                        if(index != null){
                            commit(ADD_TIMER, new Timer(() => {
                                commit(keyboardState + "/" + SET_NOTE_PRESSED, {index, forBlackNote, pressed : true}, {root:true});
                            }, rootState.canvasState.waterfallDelay));
                            
                            dispatch(canvasState + "/" + startDrawNote, {noteName : note.name, forBlackNote}, {root:true});
                        }
                    // }, time)
                }, note.time)
      
                rootState.toneState.tone.Transport.schedule(time => {
                    // rootState.toneState.tone.Draw.schedule(() => {
                        if(index != null){
                            commit(ADD_TIMER, new Timer(() => {

                                commit(keyboardState + "/" + SET_NOTE_PRESSED, {index, forBlackNote, pressed : false}, {root:true});

                                if(rootState.menuState.sustain == false){
                                    rootState.toneState.sampler.triggerRelease(note.name);
                                }

                                if(lastSong && i === notes.length - 1){
                                    dispatch(stopPlaying, "");
                                }

                            }, rootState.canvasState.waterfallDelay));

                            dispatch(canvasState + "/" + stopDrawNote, {noteName : note.name, forBlackNote}, {root:true});
                        }
                    // }, time)
                }, note.time + note.duration)
      
            })
        },

        [prepareSong]({state, dispatch}){
            if(state.currentSong.fromPlaylist){
                Midi.fromUrl(SONGS_URL + state.currentSong.name + ".mid").then(midi => {
                    midi.tracks.forEach((track, i) => dispatch(prepareNotes, {notes : track.notes, lastSong : midi.tracks.length == i + 1}));
                });  
            } else {
                dispatch(prepareNotes, {notes:state.currentSong.notes, lastSong : true})
            }
        },

        [stopPlaying]({dispatch, rootState, commit}, currentSong){
            commit(SET_IS_LOADING, true);
            dispatch(clearTimes);
            commit(keyboardState + "/" + CLEAR_PRESSED_KEYS, {}, {root:true});
            dispatch(menuState + "/" + stopPlaying, {}, {root:true});
            
            setTimeout(() => {
                rootState.toneState.tone.Transport.stop();
                rootState.toneState.tone.Transport.cancel();
                dispatch(changeSong, currentSong);
                commit(SET_IS_LOADING, false);
            }, 10)
        }
    }
}