/// ------------------------------------------------------------------------------------
        /// <summary>
        /// Plays back an utterance at slowed or increased speed using Speech Analyzer.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        public Process AlteredSpeedPlayback(string callingApp, string soundFile, long from,
                                            long to, int speed)
        {
            // Make sure the wave file exists. If not, don't return false since
            // returning false is reserved for the condition when SA cannot be found.
            if (!File.Exists(soundFile))
            {
                return(null);
            }

            string fileName        = soundFile;
            string replaceFileName = Path.GetFileNameWithoutExtension(fileName);

            if (replaceFileName != null)
            {
                soundFile = fileName.Replace(replaceFileName, "tempaudiofile");
                File.Copy(fileName, soundFile, true);
            }

            // Make sure SA can be found.
            string saLoc = GetSaPath();

            if (saLoc == null)
            {
                var msg = LocalizationManager.GetString("Miscellaneous.Messages.ProblemFindingSAForPlaybackMsg",
                                                        "Speech Analyzer 3.1 is required to playback utterances at speeds other than " +
                                                        "100%, but it is not installed. Please install Speech Analyzer 3.1 and try again.",
                                                        "Message displayed when SA 3.1 is not installed and the user is attempting to playback audio at a speed other than 100 percent.");

                using (var dlg = new DownloadSaDlg(msg))
                    dlg.ShowDialog();

                return(null);
            }

            // Create the contents for the SA list file.
            string saListFileContent = string.Format(kSaListFileContentFmt,
                                                     new object[] { callingApp, soundFile, speed,
                                                                    (from >= 0 ? from.ToString() : string.Empty),
                                                                    (to >= 0 && to > from ? to.ToString() : string.Empty) });

            saListFileContent = Utils.ConvertLiteralNewLines(saListFileContent);

            // Write the list file.
            m_lstFile = Path.GetTempFileName();
            File.AppendAllText(m_lstFile, saListFileContent);

            // Start SA.
            Process prs = new Process();

            prs.StartInfo.FileName  = "\"" + saLoc + "\"";
            prs.StartInfo.Arguments = "-l " + m_lstFile;
            prs.EnableRaisingEvents = true;
            prs.Exited += SA_Exited;
            prs.Start();

            return(prs);
        }
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Attempts to edit the specified record in Speech Analyzer.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        private void EditRecordInSA(WordCacheEntry wcentry, string callingApp)
        {
            // Get the audio file field.
            var field = wcentry.Project.GetAudioFileField();

            if (field == null)
            {
                ErrorReport.NotifyUserOfProblem(LocalizationManager.GetString(
                                                    "Miscellaneous.Messages.DataSourceEditing.NoAudioFileFieldMissingMsg",
                                                    "This project doesn't contain a field definition for an audio file path."));

                return;
            }

            // Get the audio file and make sure it exists.
            var audioFile = wcentry[field.Name];

            if (string.IsNullOrEmpty(audioFile) || !File.Exists(audioFile))
            {
                ErrorReport.NotifyUserOfProblem(LocalizationManager.GetString(
                                                    "Miscellaneous.Messages.DataSourceEditing.AudioFileMissingMsg",
                                                    "The audio file '{0}' is cannot be found."), audioFile);
                return;
            }

            // Make sure SA exists.
            var saPath = AudioPlayer.GetSaPath();

            if (saPath == null || !File.Exists(saPath))
            {
                var msg = LocalizationManager.GetString("Miscellaneous.Messages.DataSourceEditing.AudioEditProblemMsg",
                                                        "Speech Analyzer 3.1 is required to edit audio data sources, but it " +
                                                        "is not installed. Please install Speech Analyzer 3.1 and try again.",
                                                        "Message displayed when SA 3.1 is not installed and the user is attempting to edit an audio file.");

                using (var dlg = new DownloadSaDlg(msg))
                    dlg.ShowDialog();

                return;
            }

            var lstFile = GetSaListFile(wcentry, audioFile, callingApp);

            // Start SA.
            var prs = new Process();

            prs.StartInfo.UseShellExecute = true;
            prs.StartInfo.FileName        = "\"" + saPath + "\"";
            prs.StartInfo.Arguments       = "-l " + lstFile;
            prs.EnableRaisingEvents       = true;
            prs.Exited += SA_Exited;

            // Create a new collection to hold the new process.
            if (s_saProcesses == null)
            {
                s_saProcesses = new List <Process>();
            }

            // Save the process so PA has a record of it. (See CloseSAInstances, below)
            s_saProcesses.Add(prs);
            prs.Start();
        }
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Initializes an object to read SA data from the SA database.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        public bool Initialize(string audioFilePath, bool isForTmpOperation)
        {
            m_doc = SaAudioDocument.Load(audioFilePath, isForTmpOperation, true);

            if (m_doc != null)
            {
                ResetSegmentEnumerators();
                return(true);
            }

            try
            {
                using (var audioReader = new AudioReader())
                {
                    var result = audioReader.Initialize(audioFilePath);
                    if (result == AudioReader.InitResult.FileNotFound)
                    {
                        ShowWaveFileNotFoundMsg(audioFilePath);
                        return(false);
                    }

                    if ((result == AudioReader.InitResult.InvalidFormat))
                    {
                        var msg = LocalizationManager.GetString("Miscellaneous.Messages.DataSourceReading.InvalidWaveFileMsg",
                                                                "The file '{0}' is not a valid wave file.");

                        Utils.MsgBox(string.Format(msg, Utils.PrepFilePathForMsgBox(audioFilePath)));
                        return(false);
                    }

                    // If audio file is old SA format, then tell user to use SA 3.0.1 to convert first.
                    if (audioReader.IsOldSaFormat)
                    {
                        if (m_worker != null)
                        {
                            m_worker.ReportProgress(-1);
                        }

                        var msg = LocalizationManager.GetString(
                            "Miscellaneous.Messages.DataSourceReading.AudioFileNeedsConversionMsg",
                            "It appears the audio file '{0}' may have been created using an old version " +
                            "of Speech Analyzer. In order for {1} to read data associated with this audio " +
                            "file it must first be converted using Speech Analyzer 3.0.1.");

                        msg = string.Format(msg, audioFilePath, Application.ProductName);
                        using (var dlg = new DownloadSaDlg(msg))
                            dlg.ShowDialog();

                        return(false);
                    }

                    // Now try reading the companion transcription file again.
                    m_doc = SaAudioDocument.Load(audioFilePath, isForTmpOperation, false);
                    ResetSegmentEnumerators();
                }
            }
            catch (Exception e)
            {
                var msg = LocalizationManager.GetString(
                    "Miscellaneous.Messages.DataSourceReading.ErrorInitializingSaDocumentReaderMsg",
                    "Error initializing SA Document reader.");

                ErrorReport.ReportNonFatalExceptionWithMessage(e, msg);
                return(false);
            }

            return(true);
        }