/// <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"); } }
public static string ConvertedFile(string filePath, Presentation pres) { AudioLib.WavFormatConverter audioConverter = new WavFormatConverter(true, true); int samplingRate = (int)pres.MediaDataManager.DefaultPCMFormat.Data.SampleRate; int channels = pres.MediaDataManager.DefaultPCMFormat.Data.NumberOfChannels; int bitDepth = pres.MediaDataManager.DefaultPCMFormat.Data.BitDepth; string directoryPath = pres.DataProviderManager.DataFileDirectoryFullPath; string convertedFile = null; try { if (Path.GetExtension(filePath).ToLower() == ".wav") { Stream wavStream = null; wavStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); uint dataLength; AudioLibPCMFormat newFilePCMInfo = AudioLibPCMFormat.RiffHeaderParse(wavStream, out dataLength); if (wavStream != null) { wavStream.Close(); } if (newFilePCMInfo.SampleRate == samplingRate && newFilePCMInfo.NumberOfChannels == channels && newFilePCMInfo.BitDepth == bitDepth) { convertedFile = filePath; } else { AudioLibPCMFormat pcmFormat = new AudioLibPCMFormat((ushort)channels, (uint)samplingRate, (ushort)bitDepth); AudioLibPCMFormat originalPCMFormat = null; convertedFile = audioConverter.ConvertSampleRate(filePath, directoryPath, pcmFormat, out originalPCMFormat); } } else if (Path.GetExtension(filePath).ToLower() == ".mp3") { AudioLibPCMFormat pcmFormat = new AudioLibPCMFormat((ushort)channels, (uint)samplingRate, (ushort)bitDepth); AudioLibPCMFormat originalPCMFormat = null; convertedFile = audioConverter.UnCompressMp3File(filePath, directoryPath, pcmFormat, out originalPCMFormat); } else if (Path.GetExtension(filePath).ToLower() == ".mp4" || Path.GetExtension(filePath).ToLower() == ".m4a") { AudioLibPCMFormat pcmFormat = new AudioLibPCMFormat((ushort)channels, (uint)samplingRate, (ushort)bitDepth); AudioLibPCMFormat originalPCMFormat = null; convertedFile = audioConverter.UnCompressMp4_AACFile(filePath, directoryPath, pcmFormat, out originalPCMFormat); } else { MessageBox.Show(string.Format(Localizer.Message("AudioFormatConverter_Error_FileExtentionNodSupported"), filePath), Localizer.Message("Caption_Error")); return(null); } // rename converted file to original file if names are different if (Path.GetFileName(filePath) != Path.GetFileName(convertedFile)) { string newConvertedFilePath = Path.Combine(Path.GetDirectoryName(convertedFile), Path.GetFileNameWithoutExtension(filePath) + ".wav"); for (int i = 0; i < 99999 && File.Exists(newConvertedFilePath); i++) { newConvertedFilePath = Path.Combine(Path.GetDirectoryName(convertedFile), i.ToString() + Path.GetFileNameWithoutExtension(filePath) + ".wav"); if (!File.Exists(newConvertedFilePath)) { MessageBox.Show(string.Format(Localizer.Message("Import_AudioFormat_RenameFile"), Path.GetFileNameWithoutExtension(filePath) + ".wav", Path.GetFileName(newConvertedFilePath)), Localizer.Message("Caption_Information"), MessageBoxButtons.OK); break; } } File.Move(convertedFile, newConvertedFilePath); convertedFile = newConvertedFilePath; } } catch (System.Exception ex) { MessageBox.Show(ex.ToString()); return(null); } return(convertedFile); }