<commit_before>package main

import (
	"fmt"
	"github.com/krig/go-sox"
	"time"
)

//go:generate stringer -type=PlayerState
type PlayerState int

const (
	// NothingPlaying is the state when the player has nothing cued up/paused/buffered
	NothingPlaying PlayerState = iota
	Resume         PlayerState = iota
	Play           PlayerState = iota
	Pause          PlayerState = iota
	Stop           PlayerState = iota
	FastForward    PlayerState = iota
	Rewind         PlayerState = iota
	ExitPlayer     PlayerState = iota
)

var (
	playerPosition int = -1
	startTime      time.Time
	lengthOfFile   uint64
	playerControl  chan PlayerState
	fileChannel    chan string
	exitChannel    chan bool
	playing        string
	playerState    = NothingPlaying
)

// StartPlayer starts the global player. The player is global since there is only one of them
// I'm not really a fan of moving it into an object as it should not be reused
func StartPlayer() {
	playerControl = make(chan PlayerState)
	fileChannel = make(chan string)
	exitChannel = make(chan bool)
	go startPlayer()
}

// DisposePlayer sends a signal to the player to destroy itself, and then waits for the player to exit
// This function will deadlock if called twice
func DisposePlayer() {
	playerControl <- ExitPlayer
	<-exitChannel
}

func TogglePlayerState() {
	if playerState == Play {
		playerControl <- Pause
	} else if playerState == Pause {
		ResumePlayer()
	}
}

func PausePlayer() {
	playerPosition += int(time.Since(startTime).Seconds())
}

func ResumePlayer() {
	if playerState == Pause {
		playerControl <- Play
	}
}

func StopPlayer() {
	//reset position
	playerPosition = -1
	playerState = NothingPlaying
	playing = ""
	lengthOfFile = 0 //set length
}

func GetLengthOfPlayingFile() uint64 {
	return lengthOfFile
}

func GetPlayerState() PlayerState {
	return playerState
}

func SetPlayerState(state PlayerState) {
	playerControl <- state
}

func SetPlaying(filename string) {
	fileChannel <- filename
}

func GetPlayerPosition() int {
	if playerPosition < 0 {
		return playerPosition
	}
	return playerPosition + int(time.Since(startTime).Seconds())
}

//this runs on its own thread to start/stop and select the media that is playing
func startPlayer() {
	var (
		chain      *sox.EffectsChain
		inputFile  *sox.Format
		outputFile *sox.Format
		status     = NothingPlaying
		stopToExit = false
	)
	if !sox.Init() {
		panic("Unable to start the player")
	}
	defer sox.Quit()
	for stopToExit != true {
		status = NothingPlaying //reset status

		select {
		case status = <-playerControl:
		case playing = <-fileChannel:
			continue
		}

		switch status {
		case NothingPlaying:
			panic("invalid state when switching status, this should never happen")
		case Play:
			playFile(chain, inputFile, outputFile)
		case Pause:
			//save time and file then cleanup
			PausePlayer()
			cleanupSoxData(chain, inputFile, outputFile)
		case Stop:
			StopPlayer()
			cleanupSoxData(chain, inputFile, outputFile)
		case FastForward:
			fastForwardPlayer()
		case Rewind:
			rewindPlayer()
		case ExitPlayer:
			cleanupSoxData(chain, inputFile, outputFile)<commit_msg>well it's playing a bunch of stuff over each other but it works kind of now<commit_after>package main

import (
	"fmt"
	"github.com/krig/go-sox"
	"time"
)

//go:generate stringer -type=PlayerState
type PlayerState int

const (
	// NothingPlaying is the state when the player has nothing cued up/paused/buffered
	NothingPlaying PlayerState = iota
	Resume         PlayerState = iota
	Play           PlayerState = iota
	Pause          PlayerState = iota
	Stop           PlayerState = iota
	FastForward    PlayerState = iota
	Rewind         PlayerState = iota
	ExitPlayer     PlayerState = iota
)

var (
	playerPosition int = -1
	startTime      time.Time
	lengthOfFile   uint64
	playerControl  chan PlayerState
	fileChannel    chan string
	exitChannel    chan bool
	playing        string
	playerState    = NothingPlaying
)

// StartPlayer starts the global player. The player is global since there is only one of them
// I'm not really a fan of moving it into an object as it should not be reused
func StartPlayer() {
	playerControl = make(chan PlayerState)
	fileChannel = make(chan string)
	exitChannel = make(chan bool)
	go startPlayer()
}

// DisposePlayer sends a signal to the player to destroy itself, and then waits for the player to exit
// This function will deadlock if called twice
func DisposePlayer() {
	playerControl <- ExitPlayer
	<-exitChannel
}

func TogglePlayerState() {
	if playerState == Play {
		playerControl <- Pause
	} else if playerState == Pause {
		playerControl <- Play
	}
}

func StopPlayer() {
	//reset position
	playerPosition = -1
	playerState = NothingPlaying
	playing = ""
	lengthOfFile = 0 //set length
}

func GetLengthOfPlayingFile() uint64 {
	return lengthOfFile
}

func GetPlayerState() PlayerState {
	return playerState
}

func SetPlayerState(state PlayerState) {
	playerControl <- state
}

func SetPlaying(filename string) {
	fileChannel <- filename
}

func GetPlayerPosition() int {
	if playerPosition < 0 {
		return playerPosition
	}
	return playerPosition + int(time.Since(startTime).Seconds())
}

//this runs on its own thread to start/stop and select the media that is playing
func startPlayer() {
	var (
		chain      *sox.EffectsChain
		inputFile  *sox.Format
		outputFile *sox.Format
		status     = NothingPlaying
		stopToExit = false
	)
	if !sox.Init() {
		panic("Unable to start the player")
	}
	defer sox.Quit()
	for stopToExit != true {
		status = NothingPlaying //reset status

		select {
		case status = <-playerControl:
		case playing = <-fileChannel:
			continue
		}

		switch status {
		case NothingPlaying:
			panic("invalid state when switching status, this should never happen")
		case Play:
			playFile(chain, inputFile, outputFile)
		case Pause:
			//save time and file then cleanup
			playerPosition += int(time.Since(startTime).Seconds())
			cleanupSoxData(chain, inputFile, outputFile)
			playerState = Pause
		case Stop:
			StopPlayer()
			cleanupSoxData(chain, inputFile, outputFile)
		case FastForward:
			fastForwardPlayer()
		case Rewind:
			rewindPlayer()
		case ExitPlayer:
			cleanupSoxData(chain, inputFile, outputFile)
{'fraction_non_alphanumeric': 0.0779890428617467, 'fraction_numerical': 0.002578150177247825, 'mean_word_length': 3.574060427413412, 'pattern_counts': {'":': 0, '<': 21, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1107011', 'n_tokens_mistral': 2016, 'n_tokens_neox': 1850, 'n_words': 629}