예제 #1
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Gets format values from the audio file, such as sample rate, sample size, etc.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        private void GetAudioFormatValues(string audioFilePath)
        {
            FileStream   stream = null;
            BinaryReader reader = null;

            try
            {
                stream = File.Open(audioFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                reader = new BinaryReader(stream, Encoding.ASCII);

                //Start at the beginning of fmt chunk plus advance eight
                //positions to move ahead of the chunk ID and chunk size.
                stream.Position = AudioReader.GetChunkOffset(stream, AudioReader.kidFmtChunk) + 8;

                //Read the correct values that correspond with each category
                //of the fmt chunk, then write the information to the database.
                FormatTag = reader.ReadUInt16();
                Channels  = reader.ReadUInt16();
                int samplesPerSecond = reader.ReadInt32();
                SamplesPerSecond      = samplesPerSecond;
                AverageBytesPerSecond = reader.ReadInt32();
                BlockAlignment        = reader.ReadUInt16();
                int bitsPerSample = reader.ReadUInt16();
                BitsPerSample = bitsPerSample;

                // Also set default record info
                RecordFileFormat = 1;                 // This is for wave files

                //Start at the beginning of data chunk plus advance
                //four positions to move ahead of the chunk ID.
                stream.Position = AudioReader.GetChunkOffset(stream, AudioReader.kidDataChunk) + 4;

                //Read the data chunk size and write to the database.
                int dataChunkSize = reader.ReadInt32();
                DataChunkSize = dataChunkSize;

                //Calculate number of samples
                NumberOfSamples = 8 * dataChunkSize / bitsPerSample;
            }
            catch
            {
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }

                stream = null;
                reader = null;
            }
        }
        /// ------------------------------------------------------------------------------------
        /// <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);
        }