Ejemplo n.º 1
0
        /// <summary>Wavs the file extract.</summary>
        /// <param name="InputFullFilename">The input full filename.</param>
        /// <param name="OutputFullFilename">The output full filename.</param>
        /// <param name="tStartTimeOffset">The t start time offset.</param>
        /// <param name="tDuration">Duration of the t.</param>
        /// <exception cref="Exception">
        /// input file '" + InputFullFilename + "' was not created successfully
        /// or
        /// Start Time is greater than the Call Duration
        /// or
        /// output file '" + OutputFullFilename + "' was not created successfully
        /// </exception>
        public void WavFileExtract(string InputFullFilename, string OutputFullFilename, TimeSpan tStartTimeOffset, TimeSpan tDuration)
        {
            DateTime BeginTimeUTC = DateTime.UtcNow;
            string   arguments    = string.Empty;

            try
            {
                // Check Input file exists
                if (System.IO.File.Exists(InputFullFilename) == false)
                {
                    throw new Exception("input file '" + InputFullFilename + "' was not created successfully");
                }

                // Delete any pre-existing output file.
                if (System.IO.File.Exists(OutputFullFilename) == true)
                {
                    System.IO.File.SetAttributes(OutputFullFilename, System.IO.FileAttributes.Normal);
                    System.IO.File.Delete(OutputFullFilename);
                }

                var oHeader = new WavHeaderClass();
                // Read the header of the input file. So we can check if it is in stereo format.
                oHeader.Load(InputFullFilename);

                // Check Call Duration
                int iCallDurationMilliseconds = oHeader.GetDurationInMilliseconds();

                // Check StartTime
                if (tStartTimeOffset.TotalMilliseconds > iCallDurationMilliseconds)
                {
                    throw new Exception("Start Time is greater than the Call Duration");
                }

                // Check Duration
                if (tDuration.TotalMilliseconds > iCallDurationMilliseconds - tStartTimeOffset.TotalMilliseconds)
                {
                    tDuration = TimeSpan.FromMilliseconds(iCallDurationMilliseconds - tStartTimeOffset.TotalMilliseconds);
                }


                // Setup arguments to create beep file
                arguments = "\"" + InputFullFilename + "\" \"" + OutputFullFilename + "\" trim " + Convert.ToDecimal(tStartTimeOffset.TotalMilliseconds / 1000).ToString("######.000") + " " + Convert.ToDecimal(tDuration.TotalMilliseconds / 1000).ToString("######.000");

                // Check if running in Docker Container
                if (InDocker)
                {
                    ShellHelper.ExecBashProcess("sox", arguments, _logger);
                }
                else
                {
                    ShellHelper.ExecWindowsProcess(CURRENTAPPLICATIONPATH + @"\Executables\sox.exe", arguments);
                }

                // Throw an exception, if no output file was created.
                if (System.IO.File.Exists(OutputFullFilename) == false)
                {
                    throw new Exception("output file '" + OutputFullFilename + "' was not created successfully");
                }

                // The audio file was converted successfully.
                _logger.LogInformation($"WavFileExtract .Converted file '" + InputFullFilename + "' to '" + OutputFullFilename + "' successfully. (" + (DateTime.UtcNow - BeginTimeUTC).TotalMilliseconds.ToString("###,##0") + " ms)");

                return;
            }
            catch (Exception Ex)
            {
                _logger.LogInformation($"WavFileExtract Exception {Ex.Message}");
                throw;
            }
        }
Ejemplo n.º 2
0
        /// <summary>Redacts the audio.</summary>
        /// <param name="InputFullFilename">The input full filename.</param>
        /// <param name="OutputFullFilename">The output full filename.</param>
        /// <param name="lStartTimeOffsetMilliseconds">The l start time offset milliseconds.</param>
        /// <param name="lRedactionLengthMilliseconds">The l redaction length milliseconds.</param>
        /// <exception cref="Exception">
        /// input file '" + InputFullFilename + "' was not created successfully
        /// or
        /// Start Time Offset is greater than the Call Duration
        /// or
        /// output file '" + OutputFullFilename + "' was not created successfully
        /// </exception>
        public void RedactWavAudio(string InputFullFilename, string OutputFullFilename, long lStartTimeOffsetMilliseconds, long lRedactionLengthMilliseconds)
        {
            DateTime BeginTimeUTC            = DateTime.UtcNow;
            string   arguments               = string.Empty;
            string   sTempStartFile          = Utilities.GetTempFilenameSafe(".wav");
            string   sTempEndFile            = Utilities.GetTempFilenameSafe(".wav");
            string   sBeepFile               = Utilities.GetTempFilenameSafe(".wav");
            string   sTempFinalLocalFileName = Utilities.GetTempFilenameSafe(".wav");

            try
            {
                // Check Input file exists
                if (System.IO.File.Exists(InputFullFilename) == false)
                {
                    throw new Exception("input file '" + InputFullFilename + "' was not created successfully");
                }

                // Delete any pre-existing output file.
                if (System.IO.File.Exists(OutputFullFilename) == true)
                {
                    System.IO.File.SetAttributes(OutputFullFilename, System.IO.FileAttributes.Normal);
                    System.IO.File.Delete(OutputFullFilename);
                }

                //Convert incoming file to sample rate
                this.WavConverter(InputFullFilename, sTempFinalLocalFileName, "wav", 8000, 16, 2);

                // Check sTempFinalLocalFileName file exists
                if (System.IO.File.Exists(sTempFinalLocalFileName) == false)
                {
                    throw new Exception("sTempFinalLocalFileName file '" + sTempFinalLocalFileName + "' was not created successfully");
                }

                var oHeader = new WavHeaderClass();
                // Read the header of the input file. So we can check if it is in stereo format.
                oHeader.Load(sTempFinalLocalFileName);

                // Check Call Duration
                int iCallDurationMilliseconds = oHeader.GetDurationInMilliseconds();

                // Add extra 10 milliseconds to redaction start offset to bring offset to the start of the next word
                lStartTimeOffsetMilliseconds += 10;

                // Check StartTimeOffset
                if (lStartTimeOffsetMilliseconds > iCallDurationMilliseconds)
                {
                    throw new Exception("Start Time Offset is greater than the Call Duration");
                }

                if (lStartTimeOffsetMilliseconds + lRedactionLengthMilliseconds > iCallDurationMilliseconds)
                {
                    lRedactionLengthMilliseconds = iCallDurationMilliseconds - lStartTimeOffsetMilliseconds;
                }

                // Setup arguments to create beep file
                arguments = " -r 8000 -c 2 -b 16 -n " + sBeepFile + " synth " + Convert.ToDecimal(lRedactionLengthMilliseconds / 1000.00).ToString("######.000") + " sine 270.0";

                // Check if running in Docker Container
                if (InDocker)
                {
                    ShellHelper.ExecBashProcess("sox", arguments, _logger);
                }
                else
                {
                    ShellHelper.ExecWindowsProcess(CURRENTAPPLICATIONPATH + @"\Executables\sox.exe", arguments);
                }

                // Throw an exception, if no output file was created.
                if (System.IO.File.Exists(sBeepFile) == false)
                {
                    throw new Exception("BeepFile file '" + sBeepFile + "' was not created successfully");
                }

                // Setup arguments to create start file
                arguments = "\"" + sTempFinalLocalFileName + "\" \"" + sTempStartFile + "\" trim 0 " + Convert.ToDecimal(lStartTimeOffsetMilliseconds / 1000.00).ToString("######.000");

                // Check if running in Docker Container
                if (InDocker)
                {
                    ShellHelper.ExecBashProcess("sox", arguments, _logger);
                }
                else
                {
                    ShellHelper.ExecWindowsProcess(CURRENTAPPLICATIONPATH + @"\Executables\sox.exe", arguments);
                }

                // Setup arguments to create end file
                arguments = "\"" + sTempFinalLocalFileName + "\" \"" + sTempEndFile + "\" trim " + Convert.ToDecimal(lStartTimeOffsetMilliseconds / (double)1000 + lRedactionLengthMilliseconds / (double)1000).ToString("######.000") + " =" + Convert.ToDecimal(iCallDurationMilliseconds / (double)1000).ToString("######.000");

                // Check if running in Docker Container
                if (InDocker)
                {
                    ShellHelper.ExecBashProcess("sox", arguments, _logger);
                }
                else
                {
                    ShellHelper.ExecWindowsProcess(CURRENTAPPLICATIONPATH + @"\Executables\sox.exe", arguments);
                }

                // Throw an exception, if no output file was created.
                if (System.IO.File.Exists(sTempEndFile) == false)
                {
                    throw new Exception("TempEndFile file '" + sTempEndFile + "' was not created successfully");
                }

                // Setup arguments to combine the 3 files
                arguments = "\"" + sTempStartFile + "\" \"" + sBeepFile + "\" \"" + sTempEndFile + "\"  \"" + OutputFullFilename + "\"";

                // Check if running in Docker Container
                if (InDocker)
                {
                    ShellHelper.ExecBashProcess("sox", arguments, _logger);
                }
                else
                {
                    ShellHelper.ExecWindowsProcess(CURRENTAPPLICATIONPATH + @"\Executables\sox.exe", arguments);
                }


                // Throw an exception, if no output file was created.
                if (System.IO.File.Exists(OutputFullFilename) == false)
                {
                    throw new Exception("output file '" + OutputFullFilename + "' was not created successfully");
                }

                // The audio file was converted successfully.
                _logger.LogInformation($"RedactAudio .Converted file '" + InputFullFilename + "' to '" + OutputFullFilename + "' successfully. (" + (DateTime.UtcNow - BeginTimeUTC).TotalMilliseconds.ToString("###,##0") + " ms)");

                return;
            }
            catch (Exception Ex)
            {
                _logger.LogInformation($"RedactAudio Exception {Ex.Message}");
                throw;
            }
            finally
            {
                // Delete any temporary file.
                if (sTempStartFile.Length > 0 && System.IO.File.Exists(sTempStartFile) == true)
                {
                    try
                    {
                        System.IO.File.Delete(sTempStartFile);
                    }
                    catch (Exception ex)
                    {
                        // Ignore any error.
                    }
                }

                if (sTempEndFile.Length > 0 && System.IO.File.Exists(sTempEndFile) == true)
                {
                    try
                    {
                        System.IO.File.Delete(sTempEndFile);
                    }
                    catch (Exception ex)
                    {
                        // Ignore any error.
                    }
                }

                if (sBeepFile.Length > 0 && System.IO.File.Exists(sBeepFile) == true)
                {
                    try
                    {
                        System.IO.File.Delete(sBeepFile);
                    }
                    catch (Exception ex)
                    {
                        // Ignore any error.
                    }
                }

                if (sTempFinalLocalFileName.Length > 0 && System.IO.File.Exists(sTempFinalLocalFileName) == true)
                {
                    try
                    {
                        System.IO.File.Delete(sTempFinalLocalFileName);
                    }
                    catch (Exception ex)
                    {
                        // Ignore any error.
                    }
                }
            }
        }