Пример #1
0
        public string ConvertToWav(string fileName)
        {
            FileInfo outputFormat = new FileInfo
            {
                FileType         = FileType.Wav,
                Channels         = 1,
                SampleSizeInBits = 8,
                SamplingRate     = _samplingRate,
                EncodingType     = EncodingType.UnsignedInteger
            };

            var            fileNameExt    = Path.GetExtension(fileName);
            ConvertOptions convertOptions = new ConvertOptions
            {
                InputFileInfo = new FileInfo
                {
                    FileType = Sox.GetFileTypeFromExtension(fileNameExt)
                },
                OutputFileInfo = outputFormat
            };

            using (Stream input = File.OpenRead(fileName))
            {
                string resultFileName = Path.GetTempFileName();
                _sox.Convert(input, convertOptions).WriteToFile(resultFileName);
                return(resultFileName);
            }
        }
Пример #2
0
        public Stream Convert(string fileName)
        {
            FileInfo rawFileFormat = new FileInfo
            {
                FileType         = FileType.RawUnsignedInteger8,
                Channels         = 1,
                SampleSizeInBits = 8,
                SamplingRate     = _samplingRate,
                EncodingType     = EncodingType.UnsignedInteger
            };

            var            fileNameExt    = Path.GetExtension(fileName);
            ConvertOptions convertOptions = new ConvertOptions
            {
                InputFileInfo = new FileInfo
                {
                    FileType = Sox.GetFileTypeFromExtension(fileNameExt)
                },
                OutputFileInfo = rawFileFormat
            };

            using (Stream input = File.OpenRead(fileName))
            {
                return(_sox.Convert(input, convertOptions));
            }
        }
Пример #3
0
        public void ConvertWavToRaw()
        {
            Sox sox = new Sox
            {
                SoxDirectory = Path.Combine(TestHelper.GetSolutionDirectory(), @"libs\sox-14.3.2\")
            };
            ConvertOptions options = new ConvertOptions
            {
                InputFileInfo = new FileInfo
                {
                    FileType = FileType.Wav,
                },
                OutputFileInfo = new FileInfo
                {
                    FileType = FileType.RawUnsignedInteger8,
                    Channels = 1,
                    SampleSizeInBits = 8,
                    SamplingRate = 8000,
                    EncodingType = EncodingType.UnsignedInteger
                }
            };

            int preRunTempDirFileCount = Directory.GetFiles(sox.TempDir).Length;
            using (Stream input = GetType().Assembly.GetManifestResourceStream("SoxLib.Test.Unit.doh.wav"))
            using (Stream expected = GetType().Assembly.GetManifestResourceStream("SoxLib.Test.Unit.doh-wav.u8"))
            using (Stream output = sox.Convert(input, options))
            {
                StreamTestHelper.AssertAreEqual(expected, output, 2);
            }
            Assert.AreEqual(preRunTempDirFileCount, Directory.GetFiles(sox.TempDir).Length);
        }
Пример #4
0
 public AudioFileService()
 {
     _sox = new Sox
     {
         SoxDirectory = @"..\..\libs\sox-14.3.2\" // todo: figure this out
     };
 }
Пример #5
0
        public string ConvertToWav(string sourceFileName, string destinationFileName, bool highQuality = false)
        {
            _log.Debug(string.Format("Atempting to convert '{0}' to '{1}'", sourceFileName, destinationFileName));
            FileInfo outputFormat = new FileInfo
            {
                FileType         = FileType.Wav,
                Channels         = 1,
                SampleSizeInBits = 8,
                SamplingRate     = highQuality ? SAMPLING_RATE * 2 : SAMPLING_RATE,
                EncodingType     = EncodingType.UnsignedInteger
            };

            var            fileNameExt    = Path.GetExtension(sourceFileName);
            ConvertOptions convertOptions = new ConvertOptions
            {
                InputFileInfo = new FileInfo
                {
                    FileType = Sox.GetFileTypeFromExtension(fileNameExt)
                },
                OutputFileInfo = outputFormat
            };

            using (Stream input = File.OpenRead(sourceFileName))
            {
                string resultFileName = string.IsNullOrEmpty(destinationFileName) ? Path.GetTempFileName() : destinationFileName;
                using (var tempConvertedFileStream = _sox.Convert(input, convertOptions))
                {
                    tempConvertedFileStream.WriteToFile(resultFileName);
                };
                return(resultFileName);
            }
        }
Пример #6
0
        private void btnConvert_Click(object sender, EventArgs e)
        {
            string soxPath    = tbSoxPath.Text;
            string inputFile  = tbInput.Text;
            string outputFile = tbOutput.Text;

            if (string.IsNullOrEmpty(soxPath))
            {
                MessageBox.Show("Please specify path to Sox executable file");
                return;
            }
            if (string.IsNullOrEmpty(inputFile))
            {
                MessageBox.Show("Please specify path to ");
                return;
            }
            if (string.IsNullOrEmpty(outputFile))
            {
                MessageBox.Show("Please specify path to ");
                return;
            }
            try
            {
                SoxAudioFileType OutputType = (SoxAudioFileType)(AudioFileIndex);
                Sox.Convert(soxPath, inputFile, outputFile, OutputType);
                MessageBox.Show("Complete!");
            }
            catch (SoxException ex)
            {
                MessageBox.Show(String.Format("{0}: {1}", ex.Code, ex.Message));
            }
        }
Пример #7
0
 public AudioFileService()
 {
     _sox = new Sox
     {
         SoxDirectory = SOX_DIR
     };
 }
Пример #8
0
        public void ConvertWavToMp3()
        {
            Sox sox = new Sox
            {
                SoxDirectory = Path.Combine(TestHelper.GetSolutionDirectory(), @"libs\sox-14.3.2\")
            };
            ConvertOptions options = new ConvertOptions
            {
                InputFileInfo = new FileInfo
                {
                    FileType = FileType.Mp3,
                },
                OutputFileInfo = new FileInfo
                {
                    FileType         = FileType.RawUnsignedInteger8,
                    Channels         = 1,
                    SampleSizeInBits = 8,
                    SamplingRate     = 8000,
                    EncodingType     = EncodingType.UnsignedInteger
                }
            };

            int preRunTempDirFileCount = Directory.GetFiles(sox.TempDir).Length;

            using (Stream input = GetType().Assembly.GetManifestResourceStream("SoxLib.Test.Unit.doh.mp3"))
                using (Stream expected = GetType().Assembly.GetManifestResourceStream("SoxLib.Test.Unit.doh-mp3.u8"))
                    using (Stream output = sox.Convert(input, options))
                    {
                        StreamTestHelper.AssertAreEqual(expected, output, 2);
                    }
            Assert.AreEqual(preRunTempDirFileCount, Directory.GetFiles(sox.TempDir).Length);
        }
Пример #9
0
 private void SetAudioInfo(string file)
 {
     using (Sox sox = new Sox("C:\\Program Files (x86)\\sox-14-4-1\\sox.exe"))
     {
         info = sox.GetInfo(file);
     }
 }
Пример #10
0
        public void ConvertWavToRaw()
        {
            Sox            sox     = CreateSoxInstance();
            ConvertOptions options = new ConvertOptions
            {
                InputFileInfo = new FileInfo
                {
                    FileType = FileType.Wav,
                },
                OutputFileInfo = new FileInfo
                {
                    FileType         = FileType.RawUnsignedInteger8,
                    Channels         = 1,
                    SampleSizeInBits = 8,
                    SamplingRate     = 8000,
                    EncodingType     = EncodingType.UnsignedInteger
                }
            };

            int preRunTempDirFileCount = Directory.GetFiles(sox.TempDir).Length;

            using (Stream input = GetType().Assembly.GetManifestResourceStream("SoxLib.Test.Unit.doh.wav"))
                using (Stream expected = GetType().Assembly.GetManifestResourceStream("SoxLib.Test.Unit.doh-wav.u8"))
                    using (Stream output = sox.Convert(input, options))
                    {
                        StreamTestHelper.AssertAreEqual(expected, output, 2);
                    }
            Assert.AreEqual(preRunTempDirFileCount, Directory.GetFiles(sox.TempDir).Length);
        }
        public bool ExportAudio(string changedSoundName)
        {
            try
            {
                if (soundFilePath != null && streamProcessor != null)
                {
                    //MediaType mediaType;
                    switch (nowParseSoundFormat)
                    {
                    case SupportedAudioFormat.MP3:
                        var mediaType = MediaFoundationEncoder.SelectMediaType(AudioSubtypes.MFAudioFormat_MP3, new WaveFormat(44100, 2), 256000);
                        using (var reader = new MediaFoundationReader(soundFilePath))     //这里暂时没用,是导入已存在的音频文件
                        {
                            using (var encoder = new MediaFoundationEncoder(mediaType))
                            {
                                encoder.Encode(changedSoundName, streamProcessor);    //直接用被soundtouch处理的音频wav
                            }
                        }
                        return(true);

                    case SupportedAudioFormat.OGG:
                        WaveFileWriter.CreateWaveFile(changedSoundName.Replace(".ogg", ".wav"), streamProcessor);

                        using (Sox sox = new Sox(@"SOX\sox.exe"))
                        {
                            sox.OnProgress        += sox_OnProgress;
                            sox.Output.SampleRate  = 44100;
                            sox.Output.Compression = 7;    //压缩率1-10
                            sox.Process(changedSoundName.Replace(".ogg", ".wav"), changedSoundName);
                        }
                        File.Delete(changedSoundName.Replace(".ogg", ".wav"));
                        return(true);

                    default:
                        return(false);
                    }

                    //var mediaType = MediaFoundationEncoder.SelectMediaType(AudioSubtypes.MFAudioFormat_MP3, new WaveFormat(44100, 2), 256000);
                    //using (var reader = new MediaFoundationReader(soundFilePath)) //这里暂时没用,是导入已存在的音频文件
                    //{
                    //    using (var encoder = new MediaFoundationEncoder(mediaType))
                    //    {

                    //        encoder.Encode(changedSoundName, streamProcessor);//直接用被soundtouch处理的音频wav
                    //    }
                    //}
                    //return true;
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception)
            {
                //return false;
                throw;
            }
        }
Пример #12
0
        private static (string, string) Mp3ToWave(IFormFile file, string filePath)
        {
            var sourcePath = Path.Combine(filePath, $"{Guid.NewGuid()}.mp3");
            var targetPath = Path.Combine(filePath, $"{Guid.NewGuid()}.wav");

            using (FileStream fs = System.IO.File.Create(sourcePath))
            {
                file.CopyTo(fs);
                fs.Flush();
            }

            using (Sox sox = new Sox("offlineTool\\sox-14-4-2\\sox.exe"))
            {
                sox.Process(sourcePath, targetPath);
            }

            return(sourcePath, targetPath);
        }
Пример #13
0
        public void Trim()
        {
            Sox sox = new Sox
            {
                SoxDirectory = Path.Combine(TestHelper.GetSolutionDirectory(), @"libs\sox-14.3.2\")
            };
            TrimOptions options = new TrimOptions
            {
                InputFileInfo = new FileInfo
                {
                    FileType         = FileType.RawUnsignedInteger8,
                    Channels         = 1,
                    SampleSizeInBits = 8,
                    SamplingRate     = 8000,
                    EncodingType     = EncodingType.UnsignedInteger
                },
                OutputFileInfo = new FileInfo
                {
                    FileType         = FileType.RawUnsignedInteger8,
                    Channels         = 1,
                    SampleSizeInBits = 8,
                    SamplingRate     = 8000,
                    EncodingType     = EncodingType.UnsignedInteger
                },
                StartTime = new SoxTimeSamples {
                    Value = 0
                },
                Length = new SoxTimeSamples {
                    Value = 1000
                },
            };

            int preRunTempDirFileCount = Directory.GetFiles(sox.TempDir).Length;

            using (Stream input = GetType().Assembly.GetManifestResourceStream("SoxLib.Test.Unit.doh-wav.u8"))
                using (Stream expected = GetType().Assembly.GetManifestResourceStream("SoxLib.Test.Unit.doh-wav-trim.u8"))
                    using (Stream output = sox.Trim(input, options))
                    {
                        StreamTestHelper.AssertAreEqual(expected, output, 2);
                    }
            Assert.AreEqual(preRunTempDirFileCount, Directory.GetFiles(sox.TempDir).Length);
        }