<commit_before><?php
	require_once dirname(__FILE__) . '/MusicIDTrack.php';

	class MusicID
	{
		private $ExecutableFileName = 'identifystream';

		private $ClientID = null;
		private $ClientIDTag = null;

		private $LicenseFileName = null;

		public function __construct($ClientID, $ClientIDTag, $LicenseFileName)
		{
			if(empty($ClientID) || !is_string($ClientID))
				throw new Exception('ClientID must be a string');

			if(empty($ClientIDTag) || !is_string($ClientIDTag))
				throw new Exception('ClientIDTag must be a string');

			$this->ClientID = $ClientID;
			$this->ClientIDTag = $ClientIDTag;

			$this->LicenseFileName = $LicenseFileName;
		}

		public function Identify($FileName, $SamplesPerSecond = 44100, $SampleSizeInBits = 16, $NumberOfChannels = 2, &$Log = array())
		{
			$Command = sprintf('./%s %s %s %s %s %s %s \; < %s', $this->ExecutableFileName, $this->ClientID, $this->ClientIDTag, $this->LicenseFileName, (int) $SamplesPerSecond, (int) $SampleSizeInBits, (int) $NumberOfChannels, $FileName);

			$Process = proc_open($Command, array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w')), $Std);

			if(is_resource($Process))
			{
				stream_set_blocking($Std[1], 0);
				stream_set_blocking($Std[2], 0);

				$Log = array();

				while(true)
				{
					$StdOut = fgets($Std[1]);
					$StdErr = fgets($Std[2]);

					$Line = $StdOut === false ? ($StdErr === false ? null : trim($StdErr)) : trim($StdOut);

					if(!empty($Line))
					{
						$Line = $this->ParseLine($Line);

						$Log[] = $Line;

						if($Line[0] === 'error' || ($Line[0] === 'status' && (int) $Line[1][1] === 100))
						{
							$Matches = array();

							foreach($Log as $Line)
								if($Line[0] === 'match')
									$Matches[] = new MusicIDTrack($Line[1][0], $Line[1][1], $Line[1][2], $Line[1][3], $Line[1][4], $Line[1][5]);

							if(!empty($Matches))
								return $Matches;

							break;
						}
					}
				}
			}

			return false;
		}

		private function ParseLine($Line)
		{
			$Command = strpos($Line, ':');

			$Arguments = substr($Line, $Command + 1);

			$Command = substr($Line, 0, $Command);

			$Arguments = explode(';', $Arguments);
<commit_msg>Closing process on return/break<commit_after><?php
	require_once dirname(__FILE__) . '/MusicIDTrack.php';

	class MusicID
	{
		private $ExecutableFileName = 'identifystream';

		private $ClientID = null;
		private $ClientIDTag = null;

		private $LicenseFileName = null;

		public function __construct($ClientID, $ClientIDTag, $LicenseFileName)
		{
			if(empty($ClientID) || !is_string($ClientID))
				throw new Exception('ClientID must be a string');

			if(empty($ClientIDTag) || !is_string($ClientIDTag))
				throw new Exception('ClientIDTag must be a string');

			$this->ClientID = $ClientID;
			$this->ClientIDTag = $ClientIDTag;

			$this->LicenseFileName = $LicenseFileName;
		}

		public function Identify($FileName, $SamplesPerSecond = 44100, $SampleSizeInBits = 16, $NumberOfChannels = 2, &$Log = array())
		{
			$Command = sprintf('./%s %s %s %s %s %s %s \; < %s', $this->ExecutableFileName, $this->ClientID, $this->ClientIDTag, $this->LicenseFileName, (int) $SamplesPerSecond, (int) $SampleSizeInBits, (int) $NumberOfChannels, $FileName);

			$Process = proc_open($Command, array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w')), $Std);

			if(is_resource($Process))
			{
				stream_set_blocking($Std[1], 0);
				stream_set_blocking($Std[2], 0);

				$Log = array();

				while(true)
				{
					$StdOut = fgets($Std[1]);
					$StdErr = fgets($Std[2]);

					$Line = $StdOut === false ? ($StdErr === false ? null : trim($StdErr)) : trim($StdOut);

					if(!empty($Line))
					{
						$Line = $this->ParseLine($Line);

						$Log[] = $Line;

						if($Line[0] === 'error' || ($Line[0] === 'status' && (int) $Line[1][1] === 100))
						{
							proc_close($Process);

							$Matches = array();

							foreach($Log as $Line)
								if($Line[0] === 'match')
									$Matches[] = new MusicIDTrack($Line[1][0], $Line[1][1], $Line[1][2], $Line[1][3], $Line[1][4], $Line[1][5]);

							if(!empty($Matches))
								return $Matches;

							break;
						}
					}
				}
			}

			return false;
		}

		private function ParseLine($Line)
		{
			$Command = strpos($Line, ':');

			$Arguments = substr($Line, $Command + 1);

			$Command = substr($Line, 0, $Command);

			$Arguments = explode(';', $Arguments);

{'fraction_non_alphanumeric': 0.19767970882620564, 'fraction_numerical': 0.01637852593266606, 'mean_word_length': 3.6186974789915967, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, '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': '297708', 'n_tokens_mistral': 1906, 'n_tokens_neox': 1501, 'n_words': 316}