コード例 #1
0
        /// <summary>
        /// takes wav file / mp3 file as input and converts it to wav file with audio format info supplied as parameter
        /// </summary>
        /// <param name="SourceFilePath"></param>
        /// <param name="destinationDirectory"></param>
        /// <param name="destinationFormatInfo"></param>
        /// <returns> full file path of converted file  </returns>
        private string ConvertToDefaultFormat(string SourceFilePath, string destinationDirectory, PCMFormatInfo destinationFormatInfo, bool skipACM)
        {
            if (!File.Exists(SourceFilePath))
            {
                throw new FileNotFoundException(SourceFilePath);
            }

            if (!Directory.Exists(destinationDirectory))
            {
                FileDataProvider.CreateDirectory(destinationDirectory);
            }

            AudioFileType sourceFileType = GetAudioFileType(SourceFilePath);

            switch (sourceFileType)
            {
            case AudioFileType.WavUncompressed:
            case AudioFileType.WavCompressed:
            {
                if (FirstDiscoveredPCMFormat == null)
                {
                    Stream wavStream = null;
                    try
                    {
                        wavStream = File.Open(SourceFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);

                        uint dataLength;
                        AudioLibPCMFormat pcmInfo = null;

                        pcmInfo = AudioLibPCMFormat.RiffHeaderParse(wavStream, out dataLength);

                        //FirstDiscoveredPCMFormat = new PCMFormatInfo(pcmInfo);
                        FirstDiscoveredPCMFormat = new AudioLibPCMFormat();
                        FirstDiscoveredPCMFormat.CopyFrom(pcmInfo);
                    }
                    finally
                    {
                        if (wavStream != null)
                        {
                            wavStream.Close();
                        }
                    }
                }

                WavFormatConverter formatConverter1 = new WavFormatConverter(true, skipACM);

                AddSubCancellable(formatConverter1);

                // Preserve existing WAV PCM format, the call below to ConvertSampleRate detects the equality of PCM formats and copies the audio file instead of resampling.
                AudioLibPCMFormat pcmFormat = m_autoDetectPcmFormat ? FirstDiscoveredPCMFormat :
                                              (destinationFormatInfo != null ? destinationFormatInfo.Data : new AudioLibPCMFormat());


                string result = null;
                try
                {
                    AudioLibPCMFormat originalPcmFormat;
                    result = formatConverter1.ConvertSampleRate(SourceFilePath, destinationDirectory,
                                                                pcmFormat,
                                                                out originalPcmFormat);
                    if (originalPcmFormat != null && FirstDiscoveredPCMFormat != null)
                    {
                        DebugFix.Assert(FirstDiscoveredPCMFormat.Equals(originalPcmFormat));
                    }
                }
                finally
                {
                    RemoveSubCancellable(formatConverter1);
                }

                return(result);
            }

            case AudioFileType.Mp4_AAC:
            case AudioFileType.Mp3:
            {
                WavFormatConverter formatConverter2 = new WavFormatConverter(true, skipACM);

                AddSubCancellable(formatConverter2);

                string result = null;
                try
                {
                    AudioLibPCMFormat pcmFormat = m_autoDetectPcmFormat ? FirstDiscoveredPCMFormat :         // can be null!
                                                  (destinationFormatInfo != null ? destinationFormatInfo.Data : new AudioLibPCMFormat());

                    AudioLibPCMFormat originalPcmFormat;
                    if (sourceFileType == AudioFileType.Mp3)
                    {
                        result = formatConverter2.UnCompressMp3File(SourceFilePath, destinationDirectory,
                                                                    pcmFormat,
                                                                    out originalPcmFormat);
                    }
                    else
                    {
                        DebugFix.Assert(sourceFileType == AudioFileType.Mp4_AAC);

                        result = formatConverter2.UnCompressMp4_AACFile(SourceFilePath, destinationDirectory,
                                                                        pcmFormat,
                                                                        out originalPcmFormat);
                    }

                    if (originalPcmFormat != null)
                    {
                        if (FirstDiscoveredPCMFormat == null)
                        {
                            //FirstDiscoveredPCMFormat = new PCMFormatInfo(originalPcmFormat);
                            FirstDiscoveredPCMFormat = new AudioLibPCMFormat();
                            FirstDiscoveredPCMFormat.CopyFrom(originalPcmFormat);
                        }
                    }
                }
                finally
                {
                    RemoveSubCancellable(formatConverter2);
                }

                return(result);
            }

            default:
                throw new Exception("Source file format not supported");
            }
        }