<commit_before>import * as _ from 'lodash'
import {remote} from 'electron'
import {join, dirname} from 'path'
import * as Delir from 'delir-core'
import {ProjectHelper} from 'delir-core'

import * as Platform from '../utils/platform'
import dispatcher from '../utils/Flux/Dispatcher'
import {KnownPayload} from '../actions/PayloadTypes'

import EditorStateStore from '../stores/EditorStateStore'
import AppActions from '../actions/App'
import {DispatchTypes as EditorStateDispatchTypes} from '../actions/App'

let pluginRegistry: Delir.PluginRegistry|null = null
let pluginLoader: Delir.Services.PluginLoader|null = null
// let renderer: Delir.Renderer.Renderer|null = null
let pipeline: Delir.Renderer.Pipeline|null = null
let audioContext: AudioContext|null = null
let audioBuffer: AudioBuffer|null = null
let audioBufferSource: AudioBufferSourceNode|null = null

let state: {
    project: Delir.Project.Project|null,
    composition: Delir.Project.Composition|null,
} = {
    project: null,
    composition: null,
}

const handlePayload = (payload: KnownPayload) => {
    switch (payload.type) {
        case EditorStateDispatchTypes.SetActiveProject:
            // renderer.setProject(payload.entity.project)
            pipeline!.project = payload.entity.project
            state.project = payload.entity.project
            break

        case EditorStateDispatchTypes.ChangeActiveComposition: {
            if (!state.project) break
            state.composition = ProjectHelper.findCompositionById(state.project, payload.entity.compositionId)
            // renderer.stop()
            break
        }

        case EditorStateDispatchTypes.StartPreview: {
            if (!state.project || !state.composition) break
            if (!pipeline) break

            const targetComposition = ProjectHelper.findCompositionById(state.project, payload.entity.compositionId)
            if (! targetComposition) break

            // if (renderer.isPlaying) {
            //     renderer.pause()
            //     break
            // }

            audioContext && audioContext.close()
            audioContext = new AudioContext()
            audioBuffer = audioContext.createBuffer(
                targetComposition.audioChannels,
                /* length */targetComposition.samplingRate,
                /* sampleRate */targetComposition.samplingRate,
            )
            audioBufferSource = audioContext.createBufferSource()
            audioBufferSource.buffer = audioBuffer
            audioBufferSource.connect(audioContext.destination)
            audioBufferSource.start(0)
<commit_msg>Correctry dispose AudioContext in RendererService<commit_after>import * as _ from 'lodash'
import {remote} from 'electron'
import {join, dirname} from 'path'
import * as Delir from 'delir-core'
import {ProjectHelper} from 'delir-core'

import * as Platform from '../utils/platform'
import dispatcher from '../utils/Flux/Dispatcher'
import {KnownPayload} from '../actions/PayloadTypes'

import EditorStateStore from '../stores/EditorStateStore'
import AppActions from '../actions/App'
import {DispatchTypes as EditorStateDispatchTypes} from '../actions/App'

let pluginRegistry: Delir.PluginRegistry|null = null
let pluginLoader: Delir.Services.PluginLoader|null = null
// let renderer: Delir.Renderer.Renderer|null = null
let pipeline: Delir.Renderer.Pipeline|null = null
let audioContext: AudioContext|null = null
let audioBuffer: AudioBuffer|null = null
let audioBufferSource: AudioBufferSourceNode|null = null

let state: {
    project: Delir.Project.Project|null,
    composition: Delir.Project.Composition|null,
} = {
    project: null,
    composition: null,
}

const handlePayload = (payload: KnownPayload) => {
    switch (payload.type) {
        case EditorStateDispatchTypes.SetActiveProject:
            // renderer.setProject(payload.entity.project)
            pipeline!.project = payload.entity.project
            state.project = payload.entity.project
            break

        case EditorStateDispatchTypes.ChangeActiveComposition: {
            if (!state.project) break
            state.composition = ProjectHelper.findCompositionById(state.project, payload.entity.compositionId)
            // renderer.stop()
            break
        }

        case EditorStateDispatchTypes.StartPreview: {
            if (!state.project || !state.composition) break
            if (!pipeline) break

            const targetComposition = ProjectHelper.findCompositionById(state.project, payload.entity.compositionId)
            if (! targetComposition) break

            // if (renderer.isPlaying) {
            //     renderer.pause()
            //     break
            // }

            if (audioContext) {
                audioContext.close()
            }

            audioContext = new AudioContext()
            audioBuffer = audioContext.createBuffer(
                targetComposition.audioChannels,
                /* length */targetComposition.samplingRate,
                /* sampleRate */targetComposition.samplingRate,
            )
            audioBufferSource = audioContext.createBufferSource()
            audioBufferSource.buffer = audioBuffer
            audioBufferSource.connect(audioContext.destination)
            audioBufferSource.start(0)

{'fraction_non_alphanumeric': 0.09377954244658726, 'fraction_numerical': 0.0003781433163168841, 'mean_word_length': 2.7096774193548385, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2249899', 'n_tokens_mistral': 1345, 'n_tokens_neox': 1304, 'n_words': 303}